diff options
| author | 许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com> | 2024-04-20 21:45:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-20 21:45:34 +0100 |
| commit | e9e936cfa8dfdd1b7320a219316b6bd6dbccf300 (patch) | |
| tree | c340f7b5b6aa2d08191d27b92eaba7d74bb3cc9a | |
| parent | 54692c3d0b873a5dc055f2f35081d0d6a2410a49 (diff) | |
| parent | be564a8add9b05624274af0d729afdb16674a97d (diff) | |
| download | rust-e9e936cfa8dfdd1b7320a219316b6bd6dbccf300.tar.gz rust-e9e936cfa8dfdd1b7320a219316b6bd6dbccf300.zip | |
Rollup merge of #123379 - wutchzone:119266, r=compiler-errors
Print note with closure signature on type mismatch Fixes #119266 r? Nilstrieb
| -rw-r--r-- | compiler/rustc_errors/src/diagnostic.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/error_reporting/mod.rs | 19 | ||||
| -rw-r--r-- | tests/ui/inference/hint-closure-signature-119266.rs | 11 | ||||
| -rw-r--r-- | tests/ui/inference/hint-closure-signature-119266.stderr | 18 |
4 files changed, 48 insertions, 2 deletions
diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 5d345e788e9..a17e0da47a5 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -730,7 +730,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> { } } #[rustc_lint_diagnostics] - fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self { + pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self { self.sub_with_highlights(Level::Note, msg, MultiSpan::new()); self } diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 29c9f08a166..fdae9d84b5f 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -61,7 +61,7 @@ use crate::traits::{ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_errors::{ codes::*, pluralize, struct_span_code_err, Applicability, Diag, DiagCtxt, DiagStyledString, - ErrorGuaranteed, IntoDiagArg, + ErrorGuaranteed, IntoDiagArg, StringPart, }; use rustc_hir as hir; use rustc_hir::def::DefKind; @@ -1917,6 +1917,23 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { ); if !is_simple_error || terr.must_include_note() { diag.note_expected_found(&expected_label, expected, &found_label, found); + + if let Some(ty::Closure(_, args)) = + exp_found.map(|expected_type_found| expected_type_found.found.kind()) + { + diag.highlighted_note(vec![ + StringPart::normal("closure has signature: `"), + StringPart::highlighted( + self.tcx + .signature_unclosure( + args.as_closure().sig(), + rustc_hir::Unsafety::Normal, + ) + .to_string(), + ), + StringPart::normal("`"), + ]); + } } } } diff --git a/tests/ui/inference/hint-closure-signature-119266.rs b/tests/ui/inference/hint-closure-signature-119266.rs new file mode 100644 index 00000000000..35be600fd6a --- /dev/null +++ b/tests/ui/inference/hint-closure-signature-119266.rs @@ -0,0 +1,11 @@ +fn main() { + let x = |a: u8, b: (usize, u32), c: fn() -> char| -> String { "I love beans.".to_string() }; + //~^ NOTE: the found closure + + let x: fn(i32) = x; + //~^ ERROR: 5:22: 5:23: mismatched types [E0308] + //~| NOTE: incorrect number of function parameters + //~| NOTE: expected due to this + //~| NOTE: expected fn pointer `fn(i32)` + //~| NOTE: closure has signature: `fn(u8, (usize, u32), fn() -> char) -> String` +} diff --git a/tests/ui/inference/hint-closure-signature-119266.stderr b/tests/ui/inference/hint-closure-signature-119266.stderr new file mode 100644 index 00000000000..f0b957906af --- /dev/null +++ b/tests/ui/inference/hint-closure-signature-119266.stderr @@ -0,0 +1,18 @@ +error[E0308]: mismatched types + --> $DIR/hint-closure-signature-119266.rs:5:22 + | +LL | let x = |a: u8, b: (usize, u32), c: fn() -> char| -> String { "I love beans.".to_string() }; + | --------------------------------------------------- the found closure +... +LL | let x: fn(i32) = x; + | ------- ^ incorrect number of function parameters + | | + | expected due to this + | + = note: expected fn pointer `fn(i32)` + found closure `{closure@$DIR/hint-closure-signature-119266.rs:2:13: 2:64}` + = note: closure has signature: `fn(u8, (usize, u32), fn() -> char) -> String` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. |
