about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2025-02-22 14:50:00 +0100
committerUrgau <urgau@numericable.fr>2025-02-22 17:12:19 +0100
commitd0e7bfd2056cffc7ea0e5f7ed577e987a627ba04 (patch)
tree29a4c3a913d06ead7b13ee91a479777cebef032d
parent46154c9b09b81ab02137f2ad1450d2d6277789ce (diff)
downloadrust-d0e7bfd2056cffc7ea0e5f7ed577e987a627ba04.tar.gz
rust-d0e7bfd2056cffc7ea0e5f7ed577e987a627ba04.zip
Render implicit self with their shorthand syntax in HIR pretty printing
-rw-r--r--compiler/rustc_hir_pretty/src/lib.rs55
-rw-r--r--tests/ui/unpretty/debug-fmt-hir.stdout2
-rw-r--r--tests/ui/unpretty/self-hir.rs14
-rw-r--r--tests/ui/unpretty/self-hir.stdout18
4 files changed, 75 insertions, 14 deletions
diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs
index 566502a4896..56e268aea54 100644
--- a/compiler/rustc_hir_pretty/src/lib.rs
+++ b/compiler/rustc_hir_pretty/src/lib.rs
@@ -18,7 +18,8 @@ use rustc_ast_pretty::pprust::state::MacHeader;
 use rustc_ast_pretty::pprust::{Comments, PrintState};
 use rustc_hir::{
     BindingMode, ByRef, ConstArgKind, GenericArg, GenericBound, GenericParam, GenericParamKind,
-    HirId, LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term, TyPatKind,
+    HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term,
+    TyPatKind,
 };
 use rustc_span::source_map::SourceMap;
 use rustc_span::{FileName, Ident, Span, Symbol, kw};
@@ -2086,6 +2087,28 @@ impl<'a> State<'a> {
         self.print_pat(arg.pat);
     }
 
+    fn print_implicit_self(&mut self, implicit_self_kind: &hir::ImplicitSelfKind) {
+        match implicit_self_kind {
+            ImplicitSelfKind::Imm => {
+                self.word("self");
+            }
+            ImplicitSelfKind::Mut => {
+                self.print_mutability(hir::Mutability::Mut, false);
+                self.word("self");
+            }
+            ImplicitSelfKind::RefImm => {
+                self.word("&");
+                self.word("self");
+            }
+            ImplicitSelfKind::RefMut => {
+                self.word("&");
+                self.print_mutability(hir::Mutability::Mut, false);
+                self.word("self");
+            }
+            ImplicitSelfKind::None => unreachable!(),
+        }
+    }
+
     fn print_arm(&mut self, arm: &hir::Arm<'_>) {
         // I have no idea why this check is necessary, but here it
         // is :(
@@ -2151,27 +2174,33 @@ impl<'a> State<'a> {
         // Make sure we aren't supplied *both* `arg_names` and `body_id`.
         assert!(arg_names.is_empty() || body_id.is_none());
         let mut i = 0;
-        let mut print_arg = |s: &mut Self| {
-            if let Some(arg_name) = arg_names.get(i) {
-                s.word(arg_name.to_string());
-                s.word(":");
-                s.space();
-            } else if let Some(body_id) = body_id {
-                s.ann.nested(s, Nested::BodyParamPat(body_id, i));
-                s.word(":");
-                s.space();
+        let mut print_arg = |s: &mut Self, ty: Option<&hir::Ty<'_>>| {
+            if i == 0 && decl.implicit_self.has_implicit_self() {
+                s.print_implicit_self(&decl.implicit_self);
+            } else {
+                if let Some(arg_name) = arg_names.get(i) {
+                    s.word(arg_name.to_string());
+                    s.word(":");
+                    s.space();
+                } else if let Some(body_id) = body_id {
+                    s.ann.nested(s, Nested::BodyParamPat(body_id, i));
+                    s.word(":");
+                    s.space();
+                }
+                if let Some(ty) = ty {
+                    s.print_type(ty);
+                }
             }
             i += 1;
         };
         self.commasep(Inconsistent, decl.inputs, |s, ty| {
             s.ibox(INDENT_UNIT);
-            print_arg(s);
-            s.print_type(ty);
+            print_arg(s, Some(ty));
             s.end();
         });
         if decl.c_variadic {
             self.word(", ");
-            print_arg(self);
+            print_arg(self, None);
             self.word("...");
         }
         self.pclose();
diff --git a/tests/ui/unpretty/debug-fmt-hir.stdout b/tests/ui/unpretty/debug-fmt-hir.stdout
index bd7a7c4463a..2c9c96de9d1 100644
--- a/tests/ui/unpretty/debug-fmt-hir.stdout
+++ b/tests/ui/unpretty/debug-fmt-hir.stdout
@@ -13,7 +13,7 @@ struct Bar {
 }
 
 impl fmt::Debug for Bar {
-    fn fmt(self: &'_ Self, f: &'_ mut fmt::Formatter<'_>)
+    fn fmt(&self, f: &'_ mut fmt::Formatter<'_>)
         ->
             fmt::Result {
         debug_struct_field2_finish(f, "Bar", "a", &self.a, "b", &&self.b)
diff --git a/tests/ui/unpretty/self-hir.rs b/tests/ui/unpretty/self-hir.rs
new file mode 100644
index 00000000000..448d828d444
--- /dev/null
+++ b/tests/ui/unpretty/self-hir.rs
@@ -0,0 +1,14 @@
+//@ compile-flags: -Zunpretty=hir
+//@ check-pass
+
+pub struct Bar {
+    a: String,
+    b: u8,
+}
+
+impl Bar {
+    fn imm_self(self) {}
+    fn mut_self(mut self) {}
+    fn refimm_self(&self) {}
+    fn refmut_self(&mut self) {}
+}
diff --git a/tests/ui/unpretty/self-hir.stdout b/tests/ui/unpretty/self-hir.stdout
new file mode 100644
index 00000000000..4da080dc611
--- /dev/null
+++ b/tests/ui/unpretty/self-hir.stdout
@@ -0,0 +1,18 @@
+#[prelude_import]
+use ::std::prelude::rust_2015::*;
+#[macro_use]
+extern crate std;
+//@ compile-flags: -Zunpretty=hir
+//@ check-pass
+
+struct Bar {
+    a: String,
+    b: u8,
+}
+
+impl Bar {
+    fn imm_self(self) { }
+    fn mut_self(mut self) { }
+    fn refimm_self(&self) { }
+    fn refmut_self(&mut self) { }
+}