Skip to content

Commit 81c515b

Browse files
authored
Rollup merge of rust-lang#92336 - dtolnay:printstateself, r=michaelwoerister
Remove &self from PrintState::to_string The point of `PrintState::to_string` is to create a `State` and evaluate the caller's closure on it: https://github.com/rust-lang/rust/blob/e9fbe79292783972a222afd270db3f77c0b4f3c8/compiler/rustc_ast_pretty/src/pprust/state.rs#L868-L872 Making the caller *also* construct and pass in a `State`, which is then ignored, was confusing.
2 parents 83de77d + e9fbe79 commit 81c515b

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

compiler/rustc_ast_pretty/src/pprust/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ pub fn attribute_to_string(attr: &ast::Attribute) -> String {
7373
}
7474

7575
pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
76-
State::new().to_string(f)
76+
State::to_string(f)
7777
}
7878

7979
pub fn crate_to_string_for_macros(krate: &ast::Crate) -> String {
80-
State::new().to_string(|s| {
80+
State::to_string(|s| {
8181
s.print_inner_attributes(&krate.attrs);
8282
for item in &krate.items {
8383
s.print_item(item);

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub fn literal_to_string(lit: token::Lit) -> String {
211211
}
212212

213213
fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
214-
format!("{}{}", State::new().to_string(|s| s.print_visibility(vis)), s)
214+
format!("{}{}", State::to_string(|s| s.print_visibility(vis)), s)
215215
}
216216

217217
impl std::ops::Deref for State<'_> {
@@ -793,55 +793,55 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
793793
}
794794

795795
fn ty_to_string(&self, ty: &ast::Ty) -> String {
796-
self.to_string(|s| s.print_type(ty))
796+
Self::to_string(|s| s.print_type(ty))
797797
}
798798

799799
fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
800-
self.to_string(|s| s.print_type_bounds("", bounds))
800+
Self::to_string(|s| s.print_type_bounds("", bounds))
801801
}
802802

803803
fn pat_to_string(&self, pat: &ast::Pat) -> String {
804-
self.to_string(|s| s.print_pat(pat))
804+
Self::to_string(|s| s.print_pat(pat))
805805
}
806806

807807
fn expr_to_string(&self, e: &ast::Expr) -> String {
808-
self.to_string(|s| s.print_expr(e))
808+
Self::to_string(|s| s.print_expr(e))
809809
}
810810

811811
fn tt_to_string(&self, tt: &TokenTree) -> String {
812-
self.to_string(|s| s.print_tt(tt, false))
812+
Self::to_string(|s| s.print_tt(tt, false))
813813
}
814814

815815
fn tts_to_string(&self, tokens: &TokenStream) -> String {
816-
self.to_string(|s| s.print_tts(tokens, false))
816+
Self::to_string(|s| s.print_tts(tokens, false))
817817
}
818818

819819
fn stmt_to_string(&self, stmt: &ast::Stmt) -> String {
820-
self.to_string(|s| s.print_stmt(stmt))
820+
Self::to_string(|s| s.print_stmt(stmt))
821821
}
822822

823823
fn item_to_string(&self, i: &ast::Item) -> String {
824-
self.to_string(|s| s.print_item(i))
824+
Self::to_string(|s| s.print_item(i))
825825
}
826826

827827
fn generic_params_to_string(&self, generic_params: &[ast::GenericParam]) -> String {
828-
self.to_string(|s| s.print_generic_params(generic_params))
828+
Self::to_string(|s| s.print_generic_params(generic_params))
829829
}
830830

831831
fn path_to_string(&self, p: &ast::Path) -> String {
832-
self.to_string(|s| s.print_path(p, false, 0))
832+
Self::to_string(|s| s.print_path(p, false, 0))
833833
}
834834

835835
fn path_segment_to_string(&self, p: &ast::PathSegment) -> String {
836-
self.to_string(|s| s.print_path_segment(p, false))
836+
Self::to_string(|s| s.print_path_segment(p, false))
837837
}
838838

839839
fn vis_to_string(&self, v: &ast::Visibility) -> String {
840-
self.to_string(|s| s.print_visibility(v))
840+
Self::to_string(|s| s.print_visibility(v))
841841
}
842842

843843
fn block_to_string(&self, blk: &ast::Block) -> String {
844-
self.to_string(|s| {
844+
Self::to_string(|s| {
845845
// Containing cbox, will be closed by `print_block` at `}`.
846846
s.cbox(INDENT_UNIT);
847847
// Head-ibox, will be closed by `print_block` after `{`.
@@ -851,22 +851,22 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
851851
}
852852

853853
fn meta_list_item_to_string(&self, li: &ast::NestedMetaItem) -> String {
854-
self.to_string(|s| s.print_meta_list_item(li))
854+
Self::to_string(|s| s.print_meta_list_item(li))
855855
}
856856

857857
fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String {
858-
self.to_string(|s| s.print_attr_item(ai, ai.path.span))
858+
Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
859859
}
860860

861861
fn attribute_to_string(&self, attr: &ast::Attribute) -> String {
862-
self.to_string(|s| s.print_attribute(attr))
862+
Self::to_string(|s| s.print_attribute(attr))
863863
}
864864

865865
fn param_to_string(&self, arg: &ast::Param) -> String {
866-
self.to_string(|s| s.print_param(arg, false))
866+
Self::to_string(|s| s.print_param(arg, false))
867867
}
868868

869-
fn to_string(&self, f: impl FnOnce(&mut State<'_>)) -> String {
869+
fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
870870
let mut printer = State::new();
871871
f(&mut printer);
872872
printer.s.eof()
@@ -1202,7 +1202,7 @@ impl<'a> State<'a> {
12021202
);
12031203
}
12041204
ast::ItemKind::Mod(unsafety, ref mod_kind) => {
1205-
self.head(self.to_string(|s| {
1205+
self.head(Self::to_string(|s| {
12061206
s.print_visibility(&item.vis);
12071207
s.print_unsafety(unsafety);
12081208
s.word("mod");
@@ -1228,7 +1228,7 @@ impl<'a> State<'a> {
12281228
}
12291229
}
12301230
ast::ItemKind::ForeignMod(ref nmod) => {
1231-
self.head(self.to_string(|s| {
1231+
self.head(Self::to_string(|s| {
12321232
s.print_unsafety(nmod.unsafety);
12331233
s.word("extern");
12341234
}));
@@ -1450,7 +1450,7 @@ impl<'a> State<'a> {
14501450
ast::CrateSugar::JustCrate => self.word_nbsp("crate"),
14511451
},
14521452
ast::VisibilityKind::Restricted { ref path, .. } => {
1453-
let path = self.to_string(|s| s.print_path(path, false, 0));
1453+
let path = Self::to_string(|s| s.print_path(path, false, 0));
14541454
if path == "self" || path == "super" {
14551455
self.word_nbsp(format!("pub({})", path))
14561456
} else {

0 commit comments

Comments
 (0)