Factor out common code in PrintState.

The AST and HIR versions of `State::print_ident` are textually
identical, but the types differ slightly. This commit factors out the
common code they both have by replacing `print_ident` with `ann_post`,
which is a smaller function that still captures the type difference.
This commit is contained in:
Nicholas Nethercote 2023-11-14 13:32:41 +11:00
parent e16b52d4f0
commit 10c8b56af1
2 changed files with 11 additions and 8 deletions

View file

@ -258,9 +258,14 @@ impl std::ops::DerefMut for State<'_> {
pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::DerefMut {
fn comments(&mut self) -> &mut Option<Comments<'a>>;
fn print_ident(&mut self, ident: Ident);
fn ann_post(&mut self, ident: Ident);
fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool);
fn print_ident(&mut self, ident: Ident) {
self.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string());
self.ann_post(ident)
}
fn strsep<T, F>(
&mut self,
sep: &'static str,
@ -837,9 +842,8 @@ impl<'a> PrintState<'a> for State<'a> {
&mut self.comments
}
fn print_ident(&mut self, ident: Ident) {
self.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string());
self.ann.post(self, AnnNode::Ident(&ident))
fn ann_post(&mut self, ident: Ident) {
self.ann.post(self, AnnNode::Ident(&ident));
}
fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) {

View file

@ -12,7 +12,7 @@ use rustc_hir::LifetimeParamKind;
use rustc_hir::{BindingAnnotation, ByRef, GenericArg, GenericParam, GenericParamKind, Node, Term};
use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::{kw, Ident, IdentPrinter, Symbol};
use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_span::{self, FileName};
use rustc_target::spec::abi::Abi;
@ -136,9 +136,8 @@ impl<'a> PrintState<'a> for State<'a> {
&mut self.comments
}
fn print_ident(&mut self, ident: Ident) {
self.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string());
self.ann.post(self, AnnNode::Name(&ident.name))
fn ann_post(&mut self, ident: Ident) {
self.ann.post(self, AnnNode::Name(&ident.name));
}
fn print_generic_args(&mut self, _: &ast::GenericArgs, _colons_before_params: bool) {