From f46c4129e0310b4fb6a75dbe28c2a86e00c9be27 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 6 Jul 2024 16:41:16 +0800 Subject: [PATCH] show unit output when there is only output diff in diagnostics --- .../src/infer/error_reporting/mod.rs | 11 ++++++-- tests/ui/compare-method/bad-self-type.stderr | 2 +- tests/ui/error-codes/E0308.stderr | 2 +- .../in-assoc-type-unconstrained.stderr | 2 +- .../start_lang_item_args.missing_ret.stderr | 2 +- tests/ui/method-output-diff-issue-127263.rs | 8 ++++++ .../ui/method-output-diff-issue-127263.stderr | 25 +++++++++++++++++++ 7 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 tests/ui/method-output-diff-issue-127263.rs create mode 100644 tests/ui/method-output-diff-issue-127263.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index a8fd3ca8c59..1ee535b68b0 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1168,14 +1168,21 @@ fn cmp_fn_sig( let output1 = sig1.output(); let output2 = sig2.output(); let (x1, x2) = self.cmp(output1, output2); - if !output1.is_unit() { + let only_output_diff = !lifetime_diff + && sig1.c_variadic == sig2.c_variadic + && sig1.safety == sig2.safety + && sig1.abi == sig2.abi + && sig1.inputs() == sig2.inputs() + && x1 != x2; + if !output1.is_unit() || only_output_diff { values.0.push_normal(" -> "); (values.0).0.extend(x1.0); } - if !output2.is_unit() { + if !output2.is_unit() || only_output_diff { values.1.push_normal(" -> "); (values.1).0.extend(x2.0); } + values } diff --git a/tests/ui/compare-method/bad-self-type.stderr b/tests/ui/compare-method/bad-self-type.stderr index 29ebbc5dffb..a3a31f43447 100644 --- a/tests/ui/compare-method/bad-self-type.stderr +++ b/tests/ui/compare-method/bad-self-type.stderr @@ -41,7 +41,7 @@ note: type in trait LL | fn bar(self) -> Option<()>; | ^^^^^^^^^^ = note: expected signature `fn(MyFuture) -> Option<()>` - found signature `fn(MyFuture)` + found signature `fn(MyFuture) -> ()` help: change the output type to match the trait | LL | fn bar(self) -> Option<()> {} diff --git a/tests/ui/error-codes/E0308.stderr b/tests/ui/error-codes/E0308.stderr index bc6c5a632a1..709b3119276 100644 --- a/tests/ui/error-codes/E0308.stderr +++ b/tests/ui/error-codes/E0308.stderr @@ -5,7 +5,7 @@ LL | fn size_of(); | ^ expected `usize`, found `()` | = note: expected signature `extern "rust-intrinsic" fn() -> usize` - found signature `extern "rust-intrinsic" fn()` + found signature `extern "rust-intrinsic" fn() -> ()` error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/in-assoc-type-unconstrained.stderr b/tests/ui/impl-trait/in-assoc-type-unconstrained.stderr index e32c59a75c6..75cbe43eeb4 100644 --- a/tests/ui/impl-trait/in-assoc-type-unconstrained.stderr +++ b/tests/ui/impl-trait/in-assoc-type-unconstrained.stderr @@ -35,7 +35,7 @@ note: type in trait LL | fn method() -> Self::Ty; | ^^^^^^^^ = note: expected signature `fn() -> <() as compare_method::Trait>::Ty` - found signature `fn()` + found signature `fn() -> ()` note: this item must have the opaque type in its signature in order to be able to register hidden types --> $DIR/in-assoc-type-unconstrained.rs:22:12 | diff --git a/tests/ui/lang-items/start_lang_item_args.missing_ret.stderr b/tests/ui/lang-items/start_lang_item_args.missing_ret.stderr index aa1b1b73bae..2672efe51c9 100644 --- a/tests/ui/lang-items/start_lang_item_args.missing_ret.stderr +++ b/tests/ui/lang-items/start_lang_item_args.missing_ret.stderr @@ -5,7 +5,7 @@ LL | fn start(_main: fn() -> T, _argc: isize, _argv: *const *const u8, _sigpi | ^ expected `isize`, found `()` | = note: expected signature `fn(fn() -> _, _, _, _) -> isize` - found signature `fn(fn() -> _, _, _, _)` + found signature `fn(fn() -> _, _, _, _) -> ()` error: aborting due to 1 previous error diff --git a/tests/ui/method-output-diff-issue-127263.rs b/tests/ui/method-output-diff-issue-127263.rs new file mode 100644 index 00000000000..85a903e2453 --- /dev/null +++ b/tests/ui/method-output-diff-issue-127263.rs @@ -0,0 +1,8 @@ +fn bar() {} +fn foo(x: i32) -> u32 { + 0 +} +fn main() { + let b: fn() -> u32 = bar; //~ ERROR mismatched types [E0308] + let f: fn(i32) = foo; //~ ERROR mismatched types [E0308] +} diff --git a/tests/ui/method-output-diff-issue-127263.stderr b/tests/ui/method-output-diff-issue-127263.stderr new file mode 100644 index 00000000000..35b86114f16 --- /dev/null +++ b/tests/ui/method-output-diff-issue-127263.stderr @@ -0,0 +1,25 @@ +error[E0308]: mismatched types + --> $DIR/method-output-diff-issue-127263.rs:6:26 + | +LL | let b: fn() -> u32 = bar; + | ----------- ^^^ expected fn pointer, found fn item + | | + | expected due to this + | + = note: expected fn pointer `fn() -> u32` + found fn item `fn() -> () {bar}` + +error[E0308]: mismatched types + --> $DIR/method-output-diff-issue-127263.rs:7:22 + | +LL | let f: fn(i32) = foo; + | ------- ^^^ expected fn pointer, found fn item + | | + | expected due to this + | + = note: expected fn pointer `fn(_) -> ()` + found fn item `fn(_) -> u32 {foo}` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.