Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More distinctive pretty-printing of function item types #99927

Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 59 additions & 33 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&self,
sig1: &ty::PolyFnSig<'tcx>,
sig2: &ty::PolyFnSig<'tcx>,
) -> (DiagnosticStyledString, DiagnosticStyledString) {
values: &mut (DiagnosticStyledString, DiagnosticStyledString),
highlight_empty_parens: bool,
) {
let get_lifetimes = |sig| {
use rustc_hir::def::Namespace;
let (_, sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
Expand All @@ -974,11 +976,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let (lt1, sig1) = get_lifetimes(sig1);
let (lt2, sig2) = get_lifetimes(sig2);

// illustrative example:
// unsafe extern "C" for<'a> fn(&'a T) -> &'a T
let mut values = (
DiagnosticStyledString::normal("".to_string()),
DiagnosticStyledString::normal("".to_string()),
);

// unsafe extern "C" for<'a> fn(&'a T) -> &'a T
// ^^^^^^
Expand All @@ -1002,14 +1001,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

// unsafe extern "C" for<'a> fn(&'a T) -> &'a T
// ^^^
values.0.push_normal("fn(");
values.1.push_normal("fn(");
let len1 = sig1.inputs().len();
let len2 = sig2.inputs().len();
let same_len = len1 == len2;
// when comparing between fn item and fn pointer types, make sure to
// always pull attention to the mismatching function signatures, too,
// by highlighting *something* (to compensate the stylistic difference
// `fn(...) -> ...` vs `[fn item {path::to::foo}: fn(...) -> ...]`)
let highlight_parens = highlight_empty_parens && !same_len && (len1 == 0 || len2 == 0);
values.0.push_normal("fn");
values.1.push_normal("fn");
values.0.push("(", highlight_parens);
values.1.push("(", highlight_parens);

// unsafe extern "C" for<'a> fn(&'a T) -> &'a T
// ^^^^^
let len1 = sig1.inputs().len();
let len2 = sig2.inputs().len();
if len1 == len2 {
if same_len {
for (i, (l, r)) in iter::zip(sig1.inputs(), sig2.inputs()).enumerate() {
let (x1, x2) = self.cmp(*l, *r);
(values.0).0.extend(x1.0);
Expand All @@ -1033,36 +1040,37 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

if sig1.c_variadic {
if len1 > 0 {
values.0.push_normal(", ");
values.0.push(", ", !same_len);
}
values.0.push("...", !sig2.c_variadic);
}
if sig2.c_variadic {
if len2 > 0 {
values.1.push_normal(", ");
values.1.push(", ", !same_len);
}
values.1.push("...", !sig1.c_variadic);
}

// unsafe extern "C" for<'a> fn(&'a T) -> &'a T
// ^
values.0.push_normal(")");
values.1.push_normal(")");
values.0.push(")", highlight_parens);
values.1.push(")", highlight_parens);

// unsafe extern "C" for<'a> fn(&'a T) -> &'a T
// ^^^^^^^^
let output1 = sig1.output();
let output2 = sig2.output();
let (x1, x2) = self.cmp(output1, output2);
if !output1.is_unit() {
values.0.push_normal(" -> ");
let non_unit1 = !output1.is_unit();
let non_unit2 = !output2.is_unit();
if non_unit1 {
values.0.push(" -> ", non_unit1 != non_unit2);
(values.0).0.extend(x1.0);
}
if !output2.is_unit() {
values.1.push_normal(" -> ");
if non_unit2 {
values.1.push(" -> ", non_unit1 != non_unit2);
(values.1).0.extend(x2.0);
}
values
}

/// Compares two given types, eliding parts that are the same between them and highlighting
Expand Down Expand Up @@ -1363,36 +1371,54 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
(ty::FnDef(did1, substs1), ty::FnDef(did2, substs2)) => {
let sig1 = self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1);
let sig2 = self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2);
let mut values = self.cmp_fn_sig(&sig1, &sig2);
let path1 = format!(" {{{}}}", self.tcx.def_path_str_with_substs(*did1, substs1));
let path2 = format!(" {{{}}}", self.tcx.def_path_str_with_substs(*did2, substs2));
let mut values = (
DiagnosticStyledString::normal("[fn item {".to_string()),
DiagnosticStyledString::normal("[fn item {".to_string()),
);
let path1 = self.tcx.def_path_str_with_substs(*did1, substs1);
let path2 = self.tcx.def_path_str_with_substs(*did2, substs2);
let same_path = path1 == path2;
values.0.push(path1, !same_path);
values.1.push(path2, !same_path);
values.0.push_normal("}: ");
values.1.push_normal("}: ");
self.cmp_fn_sig(&sig1, &sig2, &mut values, false);
values.0.push_normal("]");
values.1.push_normal("]");
values
}

(ty::FnDef(did1, substs1), ty::FnPtr(sig2)) => {
let sig1 = self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1);
let mut values = self.cmp_fn_sig(&sig1, sig2);
values.0.push_highlighted(format!(
" {{{}}}",
self.tcx.def_path_str_with_substs(*did1, substs1)
));
let mut values = (
DiagnosticStyledString::normal("[fn item {".to_string()),
DiagnosticStyledString::new(),
);
values.0.push_highlighted(self.tcx.def_path_str_with_substs(*did1, substs1));
values.0.push_normal("}: ");
self.cmp_fn_sig(&sig1, sig2, &mut values, true);
values.0.push_normal("]");
values
}

(ty::FnPtr(sig1), ty::FnDef(did2, substs2)) => {
let sig2 = self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2);
let mut values = self.cmp_fn_sig(sig1, &sig2);
values.1.push_normal(format!(
" {{{}}}",
self.tcx.def_path_str_with_substs(*did2, substs2)
));
let mut values = (
DiagnosticStyledString::new(),
DiagnosticStyledString::normal("[fn item {".to_string()),
);
values.1.push_highlighted(self.tcx.def_path_str_with_substs(*did2, substs2));
values.1.push_normal("}: ");
self.cmp_fn_sig(sig1, &sig2, &mut values, true);
values.1.push_normal("]");
values
}

(ty::FnPtr(sig1), ty::FnPtr(sig2)) => self.cmp_fn_sig(sig1, sig2),
(ty::FnPtr(sig1), ty::FnPtr(sig2)) => {
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
self.cmp_fn_sig(sig1, sig2, &mut values, false);
values
}

_ => {
if t1 == t2 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ pub trait PrettyPrinter<'tcx>:
}
ty::FnDef(def_id, substs) => {
let sig = self.tcx().bound_fn_sig(def_id).subst(self.tcx(), substs);
p!(print(sig), " {{", print_value_path(def_id, substs), "}}");
p!("[fn item {{", print_value_path(def_id, substs), "}}: ", print(sig), "]");
}
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
ty::Infer(infer_ty) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ fn take_fn_pointer<T1, T2>(f: fn(T1, T2), x: T1, y: T2) {
#[start]
fn start(_: isize, _: *const *const u8) -> isize {

//~ MONO_ITEM fn take_fn_once::<u32, &str, fn(u32, &str) {function::<u32, &str>}>
//~ MONO_ITEM fn take_fn_once::<u32, &str, [fn item {function::<u32, &str>}: fn(u32, &str)]>
//~ MONO_ITEM fn function::<u32, &str>
//~ MONO_ITEM fn <fn(u32, &str) {function::<u32, &str>} as std::ops::FnOnce<(u32, &str)>>::call_once - shim(fn(u32, &str) {function::<u32, &str>})
//~ MONO_ITEM fn <[fn item {function::<u32, &str>}: fn(u32, &str)] as std::ops::FnOnce<(u32, &str)>>::call_once - shim([fn item {function::<u32, &str>}: fn(u32, &str)])
take_fn_once(function, 0u32, "abc");

//~ MONO_ITEM fn take_fn_once::<char, f64, fn(char, f64) {function::<char, f64>}>
//~ MONO_ITEM fn take_fn_once::<char, f64, [fn item {function::<char, f64>}: fn(char, f64)]>
//~ MONO_ITEM fn function::<char, f64>
//~ MONO_ITEM fn <fn(char, f64) {function::<char, f64>} as std::ops::FnOnce<(char, f64)>>::call_once - shim(fn(char, f64) {function::<char, f64>})
//~ MONO_ITEM fn <[fn item {function::<char, f64>}: fn(char, f64)] as std::ops::FnOnce<(char, f64)>>::call_once - shim([fn item {function::<char, f64>}: fn(char, f64)])
take_fn_once(function, 'c', 0f64);

//~ MONO_ITEM fn take_fn_pointer::<i32, ()>
Expand Down
24 changes: 12 additions & 12 deletions src/test/codegen-units/item-collection/trait-method-as-argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ fn take_foo_mut<T, F: FnMut(T) -> T>(mut f: F, arg: T) -> T {
//~ MONO_ITEM fn start
#[start]
fn start(_: isize, _: *const *const u8) -> isize {
//~ MONO_ITEM fn take_foo_once::<u32, fn(u32) -> u32 {<u32 as Trait>::foo}>
//~ MONO_ITEM fn take_foo_once::<u32, [fn item {<u32 as Trait>::foo}: fn(u32) -> u32]>
//~ MONO_ITEM fn <u32 as Trait>::foo
//~ MONO_ITEM fn <fn(u32) -> u32 {<u32 as Trait>::foo} as std::ops::FnOnce<(u32,)>>::call_once - shim(fn(u32) -> u32 {<u32 as Trait>::foo})
//~ MONO_ITEM fn <[fn item {<u32 as Trait>::foo}: fn(u32) -> u32] as std::ops::FnOnce<(u32,)>>::call_once - shim([fn item {<u32 as Trait>::foo}: fn(u32) -> u32])
take_foo_once(Trait::foo, 0u32);

//~ MONO_ITEM fn take_foo_once::<char, fn(char) -> char {<char as Trait>::foo}>
//~ MONO_ITEM fn take_foo_once::<char, [fn item {<char as Trait>::foo}: fn(char) -> char]>
//~ MONO_ITEM fn <char as Trait>::foo
//~ MONO_ITEM fn <fn(char) -> char {<char as Trait>::foo} as std::ops::FnOnce<(char,)>>::call_once - shim(fn(char) -> char {<char as Trait>::foo})
//~ MONO_ITEM fn <[fn item {<char as Trait>::foo}: fn(char) -> char] as std::ops::FnOnce<(char,)>>::call_once - shim([fn item {<char as Trait>::foo}: fn(char) -> char])
take_foo_once(Trait::foo, 'c');

//~ MONO_ITEM fn take_foo::<u32, fn(u32) -> u32 {<u32 as Trait>::foo}>
//~ MONO_ITEM fn <fn(u32) -> u32 {<u32 as Trait>::foo} as std::ops::Fn<(u32,)>>::call - shim(fn(u32) -> u32 {<u32 as Trait>::foo})
//~ MONO_ITEM fn take_foo::<u32, [fn item {<u32 as Trait>::foo}: fn(u32) -> u32]>
//~ MONO_ITEM fn <[fn item {<u32 as Trait>::foo}: fn(u32) -> u32] as std::ops::Fn<(u32,)>>::call - shim([fn item {<u32 as Trait>::foo}: fn(u32) -> u32])
take_foo(Trait::foo, 0u32);

//~ MONO_ITEM fn take_foo::<char, fn(char) -> char {<char as Trait>::foo}>
//~ MONO_ITEM fn <fn(char) -> char {<char as Trait>::foo} as std::ops::Fn<(char,)>>::call - shim(fn(char) -> char {<char as Trait>::foo})
//~ MONO_ITEM fn take_foo::<char, [fn item {<char as Trait>::foo}: fn(char) -> char]>
//~ MONO_ITEM fn <[fn item {<char as Trait>::foo}: fn(char) -> char] as std::ops::Fn<(char,)>>::call - shim([fn item {<char as Trait>::foo}: fn(char) -> char])
take_foo(Trait::foo, 'c');

//~ MONO_ITEM fn take_foo_mut::<u32, fn(u32) -> u32 {<u32 as Trait>::foo}>
//~ MONO_ITEM fn <fn(u32) -> u32 {<u32 as Trait>::foo} as std::ops::FnMut<(u32,)>>::call_mut - shim(fn(u32) -> u32 {<u32 as Trait>::foo})
//~ MONO_ITEM fn take_foo_mut::<u32, [fn item {<u32 as Trait>::foo}: fn(u32) -> u32]>
//~ MONO_ITEM fn <[fn item {<u32 as Trait>::foo}: fn(u32) -> u32] as std::ops::FnMut<(u32,)>>::call_mut - shim([fn item {<u32 as Trait>::foo}: fn(u32) -> u32])
take_foo_mut(Trait::foo, 0u32);

//~ MONO_ITEM fn take_foo_mut::<char, fn(char) -> char {<char as Trait>::foo}>
//~ MONO_ITEM fn <fn(char) -> char {<char as Trait>::foo} as std::ops::FnMut<(char,)>>::call_mut - shim(fn(char) -> char {<char as Trait>::foo})
//~ MONO_ITEM fn take_foo_mut::<char, [fn item {<char as Trait>::foo}: fn(char) -> char]>
//~ MONO_ITEM fn <[fn item {<char as Trait>::foo}: fn(char) -> char] as std::ops::FnMut<(char,)>>::call_mut - shim([fn item {<char as Trait>::foo}: fn(char) -> char])
take_foo_mut(Trait::foo, 'c');

0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> () {
_5 = foo(move _6) -> bb1; // scope 4 at $DIR/array-index-is-temporary.rs:+4:21: +4:27
// mir::Constant
// + span: $DIR/array-index-is-temporary.rs:16:21: 16:24
// + literal: Const { ty: unsafe fn(*mut usize) -> u32 {foo}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {foo}: unsafe fn(*mut usize) -> u32], val: Value(<ZST>) }
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> () {
_5 = foo(move _6) -> bb1; // scope 4 at $DIR/array-index-is-temporary.rs:+4:21: +4:27
// mir::Constant
// + span: $DIR/array-index-is-temporary.rs:16:21: 16:24
// + literal: Const { ty: unsafe fn(*mut usize) -> u32 {foo}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {foo}: unsafe fn(*mut usize) -> u32], val: Value(<ZST>) }
}

bb1: {
Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/box_expr.main.ElaborateDrops.before.mir
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() -> () {
_4 = alloc::alloc::exchange_malloc(move _2, move _3) -> bb1; // scope 2 at $DIR/box_expr.rs:+1:13: +1:25
// mir::Constant
// + span: $DIR/box_expr.rs:7:13: 7:25
// + literal: Const { ty: unsafe fn(usize, usize) -> *mut u8 {alloc::alloc::exchange_malloc}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {alloc::alloc::exchange_malloc}: unsafe fn(usize, usize) -> *mut u8], val: Value(<ZST>) }
}

bb1: {
Expand All @@ -31,7 +31,7 @@ fn main() -> () {
(*_5) = S::new() -> [return: bb2, unwind: bb8]; // scope 0 at $DIR/box_expr.rs:+1:17: +1:25
// mir::Constant
// + span: $DIR/box_expr.rs:7:17: 7:23
// + literal: Const { ty: fn() -> S {S::new}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {S::new}: fn() -> S], val: Value(<ZST>) }
}

bb2: {
Expand All @@ -47,7 +47,7 @@ fn main() -> () {
_6 = std::mem::drop::<Box<S>>(move _7) -> [return: bb4, unwind: bb6]; // scope 1 at $DIR/box_expr.rs:+2:5: +2:12
// mir::Constant
// + span: $DIR/box_expr.rs:8:5: 8:9
// + literal: Const { ty: fn(Box<S>) {std::mem::drop::<Box<S>>}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {std::mem::drop::<Box<S>>}: fn(Box<S>)], val: Value(<ZST>) }
}

bb4: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
_2 = <T as Clone>::clone(move _3) -> bb1; // scope 0 at $DIR/combine_clone_of_primitives.rs:+2:5: +2:9
// mir::Constant
// + span: $DIR/combine_clone_of_primitives.rs:8:5: 8:9
// + literal: Const { ty: for<'r> fn(&'r T) -> T {<T as Clone>::clone}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {<T as Clone>::clone}: for<'r> fn(&'r T) -> T], val: Value(<ZST>) }
}

bb1: {
Expand All @@ -37,7 +37,7 @@
- _5 = <u64 as Clone>::clone(move _6) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/combine_clone_of_primitives.rs:+3:5: +3:11
- // mir::Constant
- // + span: $DIR/combine_clone_of_primitives.rs:9:5: 9:11
- // + literal: Const { ty: for<'r> fn(&'r u64) -> u64 {<u64 as Clone>::clone}, val: Value(<ZST>) }
- // + literal: Const { ty: [fn item {<u64 as Clone>::clone}: for<'r> fn(&'r u64) -> u64], val: Value(<ZST>) }
+ _6 = _7; // scope 0 at $DIR/combine_clone_of_primitives.rs:+3:5: +3:11
+ _5 = (*_6); // scope 0 at $DIR/combine_clone_of_primitives.rs:+3:5: +3:11
+ goto -> bb2; // scope 0 at $DIR/combine_clone_of_primitives.rs:+3:5: +3:11
Expand All @@ -53,7 +53,7 @@
- _8 = <[f32; 3] as Clone>::clone(move _9) -> [return: bb3, unwind: bb4]; // scope 0 at $DIR/combine_clone_of_primitives.rs:+4:5: +4:16
- // mir::Constant
- // + span: $DIR/combine_clone_of_primitives.rs:10:5: 10:16
- // + literal: Const { ty: for<'r> fn(&'r [f32; 3]) -> [f32; 3] {<[f32; 3] as Clone>::clone}, val: Value(<ZST>) }
- // + literal: Const { ty: [fn item {<[f32; 3] as Clone>::clone}: for<'r> fn(&'r [f32; 3]) -> [f32; 3]], val: Value(<ZST>) }
+ _9 = _10; // scope 0 at $DIR/combine_clone_of_primitives.rs:+4:5: +4:16
+ _8 = (*_9); // scope 0 at $DIR/combine_clone_of_primitives.rs:+4:5: +4:16
+ goto -> bb3; // scope 0 at $DIR/combine_clone_of_primitives.rs:+4:5: +4:16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
_0 = core::slice::<impl [&i32]>::as_ptr(move _1) -> [return: bb1, unwind: bb2]; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:31: +0:44
// mir::Constant
// + span: $DIR/const-promotion-extern-static.rs:9:36: 9:42
// + literal: Const { ty: for<'r> fn(&'r [&i32]) -> *const &i32 {core::slice::<impl [&i32]>::as_ptr}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {core::slice::<impl [&i32]>::as_ptr}: for<'r> fn(&'r [&i32]) -> *const &i32], val: Value(<ZST>) }
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
_0 = core::slice::<impl [&i32]>::as_ptr(move _1) -> [return: bb1, unwind: bb2]; // scope 0 at $DIR/const-promotion-extern-static.rs:+0:31: +0:55
// mir::Constant
// + span: $DIR/const-promotion-extern-static.rs:13:47: 13:53
// + literal: Const { ty: for<'r> fn(&'r [&i32]) -> *const &i32 {core::slice::<impl [&i32]>::as_ptr}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {core::slice::<impl [&i32]>::as_ptr}: for<'r> fn(&'r [&i32]) -> *const &i32], val: Value(<ZST>) }
}

bb1: {
Expand Down
2 changes: 1 addition & 1 deletion src/test/mir-opt/const_prop/boxes.main.ConstProp.diff
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
+ _6 = alloc::alloc::exchange_malloc(const 4_usize, const 4_usize) -> bb1; // scope 2 at $DIR/boxes.rs:+1:14: +1:22
// mir::Constant
// + span: $DIR/boxes.rs:12:14: 12:22
// + literal: Const { ty: unsafe fn(usize, usize) -> *mut u8 {alloc::alloc::exchange_malloc}, val: Value(<ZST>) }
// + literal: Const { ty: [fn item {alloc::alloc::exchange_malloc}: unsafe fn(usize, usize) -> *mut u8], val: Value(<ZST>) }
}

bb1: {
Expand Down
Loading