diff --git a/compiler/rustc_ast_pretty/src/pprust/mod.rs b/compiler/rustc_ast_pretty/src/pprust/mod.rs index 25b398b84924..19bd8fd11bf2 100644 --- a/compiler/rustc_ast_pretty/src/pprust/mod.rs +++ b/compiler/rustc_ast_pretty/src/pprust/mod.rs @@ -84,6 +84,10 @@ pub fn impl_restriction_to_string(r: &ast::ImplRestriction) -> String { State::new().impl_restriction_to_string(r) } +pub fn mut_restriction_to_string(r: &ast::MutRestriction) -> String { + State::new().mut_restriction_to_string(r) +} + pub fn meta_list_item_to_string(li: &ast::MetaItemInner) -> String { State::new().meta_list_item_to_string(li) } diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index f46ce8fd7686..8b2da2acaa52 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1153,6 +1153,10 @@ fn impl_restriction_to_string(&self, r: &ast::ImplRestriction) -> String { Self::to_string(|s| s.print_impl_restriction(r)) } + fn mut_restriction_to_string(&self, r: &ast::MutRestriction) -> String { + Self::to_string(|s| s.print_mut_restriction(r)) + } + fn block_to_string(&self, blk: &ast::Block) -> String { Self::to_string(|s| { let (cb, ib) = s.head(""); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index b3a8f5d8cac3..820c49cd0612 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -511,6 +511,20 @@ pub(crate) fn print_impl_restriction(&mut self, impl_restriction: &ast::ImplRest } } + pub(crate) fn print_mut_restriction(&mut self, mut_restriction: &ast::MutRestriction) { + match &mut_restriction.kind { + ast::RestrictionKind::Restricted { path, shorthand, .. } => { + let path = Self::to_string(|s| s.print_path(path, false, 0)); + if *shorthand { + self.word_nbsp(format!("mut({path})")) + } else { + self.word_nbsp(format!("mut(in {path})")) + } + } + ast::RestrictionKind::Unrestricted => {} + } + } + fn print_defaultness(&mut self, defaultness: ast::Defaultness) { if let ast::Defaultness::Default(_) = defaultness { self.word_nbsp("default"); @@ -537,6 +551,7 @@ fn print_struct( s.maybe_print_comment(field.span.lo()); s.print_outer_attributes(&field.attrs); s.print_visibility(&field.vis); + s.print_mut_restriction(&field.mut_restriction); s.print_type(&field.ty) }); self.pclose(); @@ -562,6 +577,7 @@ fn print_struct( self.maybe_print_comment(field.span.lo()); self.print_outer_attributes(&field.attrs); self.print_visibility(&field.vis); + self.print_mut_restriction(&field.mut_restriction); self.print_ident(field.ident.unwrap()); self.word_nbsp(":"); self.print_type(&field.ty);