From 685f66b6b1be089e3a365a43e5a4b4ac8dd78ec7 Mon Sep 17 00:00:00 2001 From: Badel2 <2badel2@gmail.com> Date: Sat, 30 Apr 2022 22:22:20 +0200 Subject: [PATCH] Use source callsite in check_argument_types suggestion This makes the "remove extra arguement" suggestion valid when the function argument is a macro --- .../rustc_typeck/src/check/fn_ctxt/checks.rs | 2 +- .../ui/typeck/remove-extra-argument.fixed | 9 +++++++++ src/test/ui/typeck/remove-extra-argument.rs | 9 +++++++++ .../ui/typeck/remove-extra-argument.stderr | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/typeck/remove-extra-argument.fixed create mode 100644 src/test/ui/typeck/remove-extra-argument.rs create mode 100644 src/test/ui/typeck/remove-extra-argument.stderr diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs index 616aa11f00a6b..7e225bf43b900 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs @@ -980,7 +980,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); for (idx, arg) in matched_inputs.iter().enumerate() { let suggestion_text = if let Some(arg) = arg { - let arg_span = provided_args[*arg].span; + let arg_span = provided_args[*arg].span.source_callsite(); let arg_text = source_map.span_to_snippet(arg_span).unwrap(); arg_text } else { diff --git a/src/test/ui/typeck/remove-extra-argument.fixed b/src/test/ui/typeck/remove-extra-argument.fixed new file mode 100644 index 0000000000000..a9338c76cdc88 --- /dev/null +++ b/src/test/ui/typeck/remove-extra-argument.fixed @@ -0,0 +1,9 @@ +// run-rustfix +// Check that the HELP suggestion is `l(vec![])` instead of `l($crate::vec::Vec::new())` +fn l(_a: Vec) {} + +fn main() { + l(vec![]) + //~^ ERROR this function takes 1 argument but 2 arguments were supplied + //~| HELP remove the extra argument +} diff --git a/src/test/ui/typeck/remove-extra-argument.rs b/src/test/ui/typeck/remove-extra-argument.rs new file mode 100644 index 0000000000000..659cb8b267fd5 --- /dev/null +++ b/src/test/ui/typeck/remove-extra-argument.rs @@ -0,0 +1,9 @@ +// run-rustfix +// Check that the HELP suggestion is `l(vec![])` instead of `l($crate::vec::Vec::new())` +fn l(_a: Vec) {} + +fn main() { + l(vec![], vec![]) + //~^ ERROR this function takes 1 argument but 2 arguments were supplied + //~| HELP remove the extra argument +} diff --git a/src/test/ui/typeck/remove-extra-argument.stderr b/src/test/ui/typeck/remove-extra-argument.stderr new file mode 100644 index 0000000000000..815297765c18c --- /dev/null +++ b/src/test/ui/typeck/remove-extra-argument.stderr @@ -0,0 +1,19 @@ +error[E0061]: this function takes 1 argument but 2 arguments were supplied + --> $DIR/remove-extra-argument.rs:6:5 + | +LL | l(vec![], vec![]) + | ^ ------ argument unexpected + | +note: function defined here + --> $DIR/remove-extra-argument.rs:3:4 + | +LL | fn l(_a: Vec) {} + | ^ ----------- +help: remove the extra argument + | +LL | l(vec![]) + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0061`.