From bd1732240b586733d8d9fb47cf9ef4cfc299c22e Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Fri, 10 Mar 2023 21:29:45 +1300 Subject: [PATCH] error-msg: impl better suggestion for `E0532` --- .../rustc_resolve/src/late/diagnostics.rs | 48 ++++++++++++------- tests/ui/issues/issue-19086.stderr | 2 +- tests/ui/pattern/issue-106862.fixed | 44 +++++++++++++++++ tests/ui/pattern/issue-106862.rs | 44 +++++++++++++++++ tests/ui/pattern/issue-106862.stderr | 48 +++++++++++++++++++ 5 files changed, 169 insertions(+), 17 deletions(-) create mode 100644 tests/ui/pattern/issue-106862.fixed create mode 100644 tests/ui/pattern/issue-106862.rs create mode 100644 tests/ui/pattern/issue-106862.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 805c2ff280d8..df7681dc4267 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1289,25 +1289,41 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => { let span = find_span(&source, err); err.span_label(self.r.def_span(def_id), &format!("`{path_str}` defined here")); - let (tail, descr, applicability) = match source { - PathSource::Pat | PathSource::TupleStruct(..) => { - ("", "pattern", Applicability::MachineApplicable) - } - _ => (": val", "literal", Applicability::HasPlaceholders), - }; + let (tail, descr, applicability, old_fields) = match source { + PathSource::Pat => ("", "pattern", Applicability::MachineApplicable, None), + PathSource::TupleStruct(_, args) => ( + "", + "pattern", + Applicability::MachineApplicable, + Some( + args.iter() + .map(|a| self.r.tcx.sess.source_map().span_to_snippet(*a).ok()) + .collect::>>(), + ), + ), + _ => (": val", "literal", Applicability::HasPlaceholders, None), + }; let field_ids = self.r.field_def_ids(def_id); let (fields, applicability) = match field_ids { - Some(field_ids) => ( - field_ids - .iter() - .map(|&field_id| { - format!("{}{tail}", self.r.tcx.item_name(field_id)) - }) - .collect::>() - .join(", "), - applicability, - ), + Some(field_ids) => { + let fields = field_ids.iter().map(|&id| self.r.tcx.item_name(id)); + + let fields = if let Some(old_fields) = old_fields { + fields + .enumerate() + .map(|(idx, new)| (new, old_fields.get(idx))) + .map(|(new, old)| { + let new = new.to_ident_string(); + if let Some(Some(old)) = old && new != *old { format!("{}: {}", new, old) } else { new } + }) + .collect::>() + } else { + fields.map(|f| format!("{f}{tail}")).collect::>() + }; + + (fields.join(", "), applicability) + } None => ("/* fields */".to_string(), Applicability::HasPlaceholders), }; let pad = match field_ids { diff --git a/tests/ui/issues/issue-19086.stderr b/tests/ui/issues/issue-19086.stderr index a3c06a725111..90d0bb406554 100644 --- a/tests/ui/issues/issue-19086.stderr +++ b/tests/ui/issues/issue-19086.stderr @@ -5,7 +5,7 @@ LL | FooB { x: i32, y: i32 } | ----------------------- `FooB` defined here ... LL | FooB(a, b) => println!("{} {}", a, b), - | ^^^^^^^^^^ help: use struct pattern syntax instead: `FooB { x, y }` + | ^^^^^^^^^^ help: use struct pattern syntax instead: `FooB { x: a, y: b }` error: aborting due to previous error diff --git a/tests/ui/pattern/issue-106862.fixed b/tests/ui/pattern/issue-106862.fixed new file mode 100644 index 000000000000..9b27a61ffd06 --- /dev/null +++ b/tests/ui/pattern/issue-106862.fixed @@ -0,0 +1,44 @@ +// run-rustfix + +#![allow(unused)] + +use Foo::{FooB, FooA}; + +enum Foo { + FooA { opt_x: Option, y: i32 }, + FooB { x: i32, y: i32 } +} + +fn main() { + let f = FooB { x: 3, y: 4 }; + + match f { + FooB { x: a, y: b } => println!("{} {}", a, b), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } + + match f { + FooB { x, y } => println!("{} {}", x, y), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } + + match f { + FooA { opt_x: Some(x), y } => println!("{} {}", x, y), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooA` + _ => (), + } + + match f { + FooB { x: a, y: _ } => println!("{}", a), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } + + match f { + FooB { x, y } => (), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } +} diff --git a/tests/ui/pattern/issue-106862.rs b/tests/ui/pattern/issue-106862.rs new file mode 100644 index 000000000000..590430a78431 --- /dev/null +++ b/tests/ui/pattern/issue-106862.rs @@ -0,0 +1,44 @@ +// run-rustfix + +#![allow(unused)] + +use Foo::{FooB, FooA}; + +enum Foo { + FooA { opt_x: Option, y: i32 }, + FooB { x: i32, y: i32 } +} + +fn main() { + let f = FooB { x: 3, y: 4 }; + + match f { + FooB(a, b) => println!("{} {}", a, b), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } + + match f { + FooB(x, y) => println!("{} {}", x, y), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } + + match f { + FooA(Some(x), y) => println!("{} {}", x, y), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooA` + _ => (), + } + + match f { + FooB(a, _, _) => println!("{}", a), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } + + match f { + FooB() => (), + //~^ ERROR expected tuple struct or tuple variant, found variant `FooB` + _ => (), + } +} diff --git a/tests/ui/pattern/issue-106862.stderr b/tests/ui/pattern/issue-106862.stderr new file mode 100644 index 000000000000..27f8ac97284e --- /dev/null +++ b/tests/ui/pattern/issue-106862.stderr @@ -0,0 +1,48 @@ +error[E0532]: expected tuple struct or tuple variant, found variant `FooB` + --> $DIR/issue-106862.rs:16:9 + | +LL | FooB { x: i32, y: i32 } + | ----------------------- `FooB` defined here +... +LL | FooB(a, b) => println!("{} {}", a, b), + | ^^^^^^^^^^ help: use struct pattern syntax instead: `FooB { x: a, y: b }` + +error[E0532]: expected tuple struct or tuple variant, found variant `FooB` + --> $DIR/issue-106862.rs:22:9 + | +LL | FooB { x: i32, y: i32 } + | ----------------------- `FooB` defined here +... +LL | FooB(x, y) => println!("{} {}", x, y), + | ^^^^^^^^^^ help: use struct pattern syntax instead: `FooB { x, y }` + +error[E0532]: expected tuple struct or tuple variant, found variant `FooA` + --> $DIR/issue-106862.rs:28:9 + | +LL | FooA { opt_x: Option, y: i32 }, + | ----------------------------------- `FooA` defined here +... +LL | FooA(Some(x), y) => println!("{} {}", x, y), + | ^^^^^^^^^^^^^^^^ help: use struct pattern syntax instead: `FooA { opt_x: Some(x), y }` + +error[E0532]: expected tuple struct or tuple variant, found variant `FooB` + --> $DIR/issue-106862.rs:34:9 + | +LL | FooB { x: i32, y: i32 } + | ----------------------- `FooB` defined here +... +LL | FooB(a, _, _) => println!("{}", a), + | ^^^^^^^^^^^^^ help: use struct pattern syntax instead: `FooB { x: a, y: _ }` + +error[E0532]: expected tuple struct or tuple variant, found variant `FooB` + --> $DIR/issue-106862.rs:40:9 + | +LL | FooB { x: i32, y: i32 } + | ----------------------- `FooB` defined here +... +LL | FooB() => (), + | ^^^^^^ help: use struct pattern syntax instead: `FooB { x, y }` + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0532`.