Render implicit self with their shorthand syntax in HIR pretty printing
This commit is contained in:
parent
46154c9b09
commit
d0e7bfd205
4 changed files with 75 additions and 14 deletions
|
@ -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();
|
||||
|
|
|
@ -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)
|
||||
|
|
14
tests/ui/unpretty/self-hir.rs
Normal file
14
tests/ui/unpretty/self-hir.rs
Normal file
|
@ -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) {}
|
||||
}
|
18
tests/ui/unpretty/self-hir.stdout
Normal file
18
tests/ui/unpretty/self-hir.stdout
Normal file
|
@ -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) { }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue