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

Better method call error messages #71827

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
22 changes: 22 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,28 @@ impl Diagnostic {
self
}

/// Show a suggestion that has multiple parts to it, always as it's own subwindow.
/// In other words, multiple changes need to be applied as part of this suggestion.
pub fn multipart_suggestion_verbose(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split this into a separate PR?

&mut self,
msg: &str,
suggestion: Vec<(Span, String)>,
applicability: Applicability,
) -> &mut Self {
self.suggestions.push(CodeSuggestion {
substitutions: vec![Substitution {
parts: suggestion
.into_iter()
.map(|(span, snippet)| SubstitutionPart { snippet, span })
.collect(),
}],
msg: msg.to_owned(),
style: SuggestionStyle::ShowAlways,
applicability,
});
self
}

/// Show multiple suggestions that have multiple parts.
/// See also [`Diagnostic::multipart_suggestion()`].
pub fn multipart_suggestions(
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ impl<'a> DiagnosticBuilder<'a> {
self
}

/// See [`Diagnostic::multipart_suggestion_verbose()`].
pub fn multipart_suggestion_verbose(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too?

&mut self,
msg: &str,
suggestion: Vec<(Span, String)>,
applicability: Applicability,
) -> &mut Self {
if !self.0.allow_suggestions {
return self;
}
self.0.diagnostic.multipart_suggestion_verbose(msg, suggestion, applicability);
self
}

/// See [`Diagnostic::multipart_suggestions()`].
pub fn multipart_suggestions(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty
}

fn check_expr_kind(
pub(super) fn check_expr_kind(
&self,
expr: &'tcx hir::Expr<'tcx>,
expected: Expectation<'tcx>,
Expand Down
960 changes: 720 additions & 240 deletions compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions src/test/ui/arg-count-mismatch.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/test/ui/arg-count-mismatch.stderr

This file was deleted.

5 changes: 0 additions & 5 deletions src/test/ui/arg-type-mismatch.rs

This file was deleted.

9 changes: 0 additions & 9 deletions src/test/ui/arg-type-mismatch.stderr

This file was deleted.

25 changes: 25 additions & 0 deletions src/test/ui/argument-suggestions/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Some basic "obvious" cases for the heuristic error messages added for #65853
// One for each of the detected cases

enum E { X, Y }
enum F { X2, Y2 }
struct G {}
struct H {}
struct X {}
struct Y {}
struct Z {}


fn invalid(_i: u32) {}
fn extra() {}
fn missing(_i: u32) {}
fn swapped(_i: u32, _s: &str) {}
fn permuted(_x: X, _y: Y, _z: Z) {}

fn main() {
invalid(1.0); //~ ERROR arguments to this function are incorrect
extra(""); //~ ERROR arguments to this function are incorrect
missing(); //~ ERROR arguments to this function are incorrect
swapped("", 1); //~ ERROR arguments to this function are incorrect
permuted(Y {}, Z {}, X {}); //~ ERROR arguments to this function are incorrect
}
94 changes: 94 additions & 0 deletions src/test/ui/argument-suggestions/basic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
error[E0308]: arguments to this function are incorrect
--> $DIR/basic.rs:20:5
|
LL | invalid(1.0);
| ^^^^^^^^---^
| |
| expected `u32`, found `{float}`
|
note: function defined here
--> $DIR/basic.rs:13:4
|
LL | fn invalid(_i: u32) {}
| ^^^^^^^ -------
help: provide an argument of the correct type
|
LL | invalid({u32});
| ^^^^^^^^^^^^^^

error[E0308]: arguments to this function are incorrect
--> $DIR/basic.rs:21:5
|
LL | extra("");
| ^^^^^^--^
| |
| argument unexpected
|
note: function defined here
--> $DIR/basic.rs:14:4
|
LL | fn extra() {}
| ^^^^^
help: remove the extra argument
|
LL | extra();
| ^^^^^^^

error[E0308]: arguments to this function are incorrect
--> $DIR/basic.rs:22:5
|
LL | missing();
| ^^^^^^^^^ an argument of type u32 is missing
|
note: function defined here
--> $DIR/basic.rs:15:4
|
LL | fn missing(_i: u32) {}
| ^^^^^^^ -------
help: provide the argument
|
LL | missing({u32});
| ^^^^^^^^^^^^^^

error[E0308]: arguments to this function are incorrect
--> $DIR/basic.rs:23:5
|
LL | swapped("", 1);
| ^^^^^^^^--^^-^
| | |
| | expected `&str`,found `{integer}`
| expected `u32`,found `&'static str`
|
note: function defined here
--> $DIR/basic.rs:16:4
|
LL | fn swapped(_i: u32, _s: &str) {}
| ^^^^^^^ ------- --------
help: swap these arguments
|
LL | swapped(1, "");
| ^^^^^^^^^^^^^^

error[E0308]: arguments to this function are incorrect
--> $DIR/basic.rs:24:5
|
LL | permuted(Y {}, Z {}, X {});
| ^^^^^^^^^----^^----^^----^
| | | |
| | | expected `Z`,found `X`
| | expected `Y`,found `Z`
| expected `X`,found `Y`
|
note: function defined here
--> $DIR/basic.rs:17:4
|
LL | fn permuted(_x: X, _y: Y, _z: Z) {}
| ^^^^^^^^ ----- ----- -----
help: reorder these arguments
|
LL | permuted(X {}, Y {}, Z {});
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0308`.
16 changes: 16 additions & 0 deletions src/test/ui/argument-suggestions/complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// A complex case with mixed suggestions from #65853

enum E { X, Y }
enum F { X2, Y2 }
struct G {}
struct H {}
struct X {}
struct Y {}
struct Z {}

fn complex(_i: u32, _s: &str, _e: E, _f: F, _g: G, _x: X, _y: Y, _z: Z ) {}

fn main() {
complex(1.0, H {}, &"", G{}, F::X2, Z {}, X {}, Y {});
//~^ ERROR arguments to this function are incorrect
}
19 changes: 19 additions & 0 deletions src/test/ui/argument-suggestions/complex.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0308]: arguments to this function are incorrect
--> $DIR/complex.rs:14:3
|
LL | complex(1.0, H {}, &"", G{}, F::X2, Z {}, X {}, Y {});
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: function defined here
--> $DIR/complex.rs:11:4
|
LL | fn complex(_i: u32, _s: &str, _e: E, _f: F, _g: G, _x: X, _y: Y, _z: Z ) {}
| ^^^^^^^ ------- -------- ----- ----- ----- ----- ----- ------
help: did you mean
|
LL | complex({u32}, &"", {E}, F::X2, G{}, X {}, Y {}, Z {});
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
35 changes: 35 additions & 0 deletions src/test/ui/argument-suggestions/extra_arguments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fn empty() {}
fn one_arg(_a: i32) {}
fn two_arg_same(_a: i32, _b: i32) {}
fn two_arg_diff(_a: i32, _b: &str) {}

fn main() {
empty(""); //~ ERROR arguments to this function are incorrect

one_arg(1, 1); //~ ERROR arguments to this function are incorrect
one_arg(1, ""); //~ ERROR arguments to this function are incorrect
one_arg(1, "", 1.0); //~ ERROR arguments to this function are incorrect

two_arg_same(1, 1, 1); //~ ERROR arguments to this function are incorrect
two_arg_same(1, 1, 1.0); //~ ERROR arguments to this function are incorrect

two_arg_diff(1, 1, ""); //~ ERROR arguments to this function are incorrect
two_arg_diff(1, "", ""); //~ ERROR arguments to this function are incorrect
two_arg_diff(1, 1, "", ""); //~ ERROR arguments to this function are incorrect
two_arg_diff(1, "", 1, ""); //~ ERROR arguments to this function are incorrect

// Check with weird spacing and newlines
two_arg_same(1, 1, ""); //~ ERROR arguments to this function are incorrect
two_arg_diff(1, 1, ""); //~ ERROR arguments to this function are incorrect
two_arg_same( //~ ERROR arguments to this function are incorrect
1,
1,
""
);

two_arg_diff( //~ ERROR arguments to this function are incorrect
1,
1,
""
);
}
Loading