Skip to content

Commit 4495ce7

Browse files
authored
Rollup merge of rust-lang#86493 - Smittyvb:ctor-typeck-error, r=davidtwco
Say "this enum variant takes"/"this struct takes" instead of "this function takes" This makes error messages for functions with incorrect argument counts adapt if they refer to a struct or enum variant: ``` error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied --> $DIR/struct-enum-wrong-args.rs:7:13 | LL | let _ = Ok(); | ^^-- supplied 0 arguments | | | expected 1 argument error[E0061]: this struct takes 1 argument but 0 arguments were supplied --> $DIR/struct-enum-wrong-args.rs:8:13 | LL | let _ = Wrapper(); | ^^^^^^^-- supplied 0 arguments | | | expected 1 argument ``` Fixes rust-lang#86481.
2 parents fdb1daa + b8a7bfb commit 4495ce7

File tree

5 files changed

+107
-6
lines changed

5 files changed

+107
-6
lines changed

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::check::{
1111
use rustc_ast as ast;
1212
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId};
1313
use rustc_hir as hir;
14-
use rustc_hir::def::{DefKind, Res};
14+
use rustc_hir::def::{CtorOf, DefKind, Res};
1515
use rustc_hir::def_id::DefId;
1616
use rustc_hir::{ExprKind, Node, QPath};
1717
use rustc_middle::ty::adjustment::AllowTwoPhase;
@@ -120,8 +120,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
120120
error_code: &str,
121121
c_variadic: bool,
122122
sugg_unit: bool| {
123-
let (span, start_span, args) = match &expr.kind {
124-
hir::ExprKind::Call(hir::Expr { span, .. }, args) => (*span, *span, &args[..]),
123+
let (span, start_span, args, ctor_of) = match &expr.kind {
124+
hir::ExprKind::Call(
125+
hir::Expr {
126+
span,
127+
kind:
128+
hir::ExprKind::Path(hir::QPath::Resolved(
129+
_,
130+
hir::Path { res: Res::Def(DefKind::Ctor(of, _), _), .. },
131+
)),
132+
..
133+
},
134+
args,
135+
) => (*span, *span, &args[..], Some(of)),
136+
hir::ExprKind::Call(hir::Expr { span, .. }, args) => {
137+
(*span, *span, &args[..], None)
138+
}
125139
hir::ExprKind::MethodCall(path_segment, span, args, _) => (
126140
*span,
127141
// `sp` doesn't point at the whole `foo.bar()`, only at `bar`.
@@ -137,6 +151,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
137151
})
138152
.unwrap_or(*span),
139153
&args[1..], // Skip the receiver.
154+
None, // methods are never ctors
140155
),
141156
k => span_bug!(sp, "checking argument types on a non-call: `{:?}`", k),
142157
};
@@ -157,7 +172,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
157172
let mut err = tcx.sess.struct_span_err_with_code(
158173
span,
159174
&format!(
160-
"this function takes {}{} but {} {} supplied",
175+
"this {} takes {}{} but {} {} supplied",
176+
match ctor_of {
177+
Some(CtorOf::Struct) => "struct",
178+
Some(CtorOf::Variant) => "enum variant",
179+
None => "function",
180+
},
161181
if c_variadic { "at least " } else { "" },
162182
potentially_plural_count(expected_count, "argument"),
163183
potentially_plural_count(arg_count, "argument"),

src/test/ui/span/missing-unit-argument.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ impl S {
88
}
99

1010
fn main() {
11-
let _: Result<(), String> = Ok(); //~ ERROR this function takes
11+
let _: Result<(), String> = Ok(); //~ ERROR this enum variant takes
1212
foo(); //~ ERROR this function takes
1313
foo(()); //~ ERROR this function takes
1414
bar(); //~ ERROR this function takes

src/test/ui/span/missing-unit-argument.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0061]: this function takes 1 argument but 0 arguments were supplied
1+
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied
22
--> $DIR/missing-unit-argument.rs:11:33
33
|
44
LL | let _: Result<(), String> = Ok();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Regression test of #86481.
2+
struct Wrapper(i32);
3+
struct DoubleWrapper(i32, i32);
4+
5+
fn main() {
6+
let _ = Some(3, 2); //~ ERROR this enum variant takes
7+
let _ = Ok(3, 6, 2); //~ ERROR this enum variant takes
8+
let _ = Ok(); //~ ERROR this enum variant takes
9+
let _ = Wrapper(); //~ ERROR this struct takes
10+
let _ = Wrapper(5, 2); //~ ERROR this struct takes
11+
let _ = DoubleWrapper(); //~ ERROR this struct takes
12+
let _ = DoubleWrapper(5); //~ ERROR this struct takes
13+
let _ = DoubleWrapper(5, 2, 7); //~ ERROR this struct takes
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied
2+
--> $DIR/struct-enum-wrong-args.rs:6:13
3+
|
4+
LL | let _ = Some(3, 2);
5+
| ^^^^ - - supplied 2 arguments
6+
| |
7+
| expected 1 argument
8+
9+
error[E0061]: this enum variant takes 1 argument but 3 arguments were supplied
10+
--> $DIR/struct-enum-wrong-args.rs:7:13
11+
|
12+
LL | let _ = Ok(3, 6, 2);
13+
| ^^ - - - supplied 3 arguments
14+
| |
15+
| expected 1 argument
16+
17+
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied
18+
--> $DIR/struct-enum-wrong-args.rs:8:13
19+
|
20+
LL | let _ = Ok();
21+
| ^^-- supplied 0 arguments
22+
| |
23+
| expected 1 argument
24+
25+
error[E0061]: this struct takes 1 argument but 0 arguments were supplied
26+
--> $DIR/struct-enum-wrong-args.rs:9:13
27+
|
28+
LL | let _ = Wrapper();
29+
| ^^^^^^^-- supplied 0 arguments
30+
| |
31+
| expected 1 argument
32+
33+
error[E0061]: this struct takes 1 argument but 2 arguments were supplied
34+
--> $DIR/struct-enum-wrong-args.rs:10:13
35+
|
36+
LL | let _ = Wrapper(5, 2);
37+
| ^^^^^^^ - - supplied 2 arguments
38+
| |
39+
| expected 1 argument
40+
41+
error[E0061]: this struct takes 2 arguments but 0 arguments were supplied
42+
--> $DIR/struct-enum-wrong-args.rs:11:13
43+
|
44+
LL | let _ = DoubleWrapper();
45+
| ^^^^^^^^^^^^^-- supplied 0 arguments
46+
| |
47+
| expected 2 arguments
48+
49+
error[E0061]: this struct takes 2 arguments but 1 argument was supplied
50+
--> $DIR/struct-enum-wrong-args.rs:12:13
51+
|
52+
LL | let _ = DoubleWrapper(5);
53+
| ^^^^^^^^^^^^^ - supplied 1 argument
54+
| |
55+
| expected 2 arguments
56+
57+
error[E0061]: this struct takes 2 arguments but 3 arguments were supplied
58+
--> $DIR/struct-enum-wrong-args.rs:13:13
59+
|
60+
LL | let _ = DoubleWrapper(5, 2, 7);
61+
| ^^^^^^^^^^^^^ - - - supplied 3 arguments
62+
| |
63+
| expected 2 arguments
64+
65+
error: aborting due to 8 previous errors
66+
67+
For more information about this error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)