From 10c8b56af1d0157708c0a81e9d05e9402bc4fef2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 14 Nov 2023 13:32:41 +1100 Subject: [PATCH] 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. --- compiler/rustc_ast_pretty/src/pprust/state.rs | 12 ++++++++---- compiler/rustc_hir_pretty/src/lib.rs | 7 +++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 9d94eb699fe..f29e8b54681 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -258,9 +258,14 @@ impl std::ops::DerefMut for State<'_> { pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefMut { fn comments(&mut self) -> &mut Option>; - 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( &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) { diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 5996d60b525..a1a218a421b 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -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) {