Skip to content

Commit fcbb2e8

Browse files
authored
Rollup merge of #98607 - compiler-errors:tuple-wrap-suggestion, r=oli-obk
Clean up arg mismatch diagnostic, generalize tuple wrap suggestion This is based on top of #97542, so just look at the last commit which contains the relevant changes. 1. Remove `final_arg_types` which was one of the last places we were using raw (`usize`) indices instead of typed indices in the arg mismatch suggestion code. 2. Improve the tuple wrap suggestion, now we suggest things like `call(a, b, c, d)` -> `call(a, (b, c), d)` 😺 3. Folded in fix #98645
2 parents b2836bd + 23f3b0d commit fcbb2e8

File tree

11 files changed

+275
-196
lines changed

11 files changed

+275
-196
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ pub fn same_type_modulo_infer<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
342342
)
343343
| (&ty::Infer(ty::InferTy::TyVar(_)), _)
344344
| (_, &ty::Infer(ty::InferTy::TyVar(_))) => true,
345-
(&ty::Ref(reg_a, ty_a, mut_a), &ty::Ref(reg_b, ty_b, mut_b)) => {
346-
reg_a == reg_b && mut_a == mut_b && same_type_modulo_infer(*ty_a, *ty_b)
345+
(&ty::Ref(_, ty_a, mut_a), &ty::Ref(_, ty_b, mut_b)) => {
346+
mut_a == mut_b && same_type_modulo_infer(*ty_a, *ty_b)
347347
}
348348
_ => a == b,
349349
}

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

+169-177
Large diffs are not rendered by default.

src/test/ui/proc-macro/signature.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ LL | / pub unsafe extern "C" fn foo(a: i32, b: u32) -> u32 {
55
LL | |
66
LL | | loop {}
77
LL | | }
8-
| | ^
9-
| | |
10-
| |_call the function in a closure: `|| unsafe { /* code */ }`
11-
| required by a bound introduced by this call
8+
| |_^ call the function in a closure: `|| unsafe { /* code */ }`
129
|
1310
= help: the trait `Fn<(proc_macro::TokenStream,)>` is not implemented for `unsafe extern "C" fn(i32, u32) -> u32 {foo}`
1411
= note: unsafe function cannot be called generically without an unsafe block

src/test/ui/suggestions/args-instead-of-tuple.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: tuple variant defined here
99
|
1010
LL | Ok(#[stable(feature = "rust1", since = "1.0.0")] T),
1111
| ^^
12-
help: use parentheses to construct a tuple
12+
help: wrap these arguments in parentheses to construct a tuple
1313
|
1414
LL | let _: Result<(i32, i8), ()> = Ok((1, 2));
1515
| + +
@@ -25,7 +25,7 @@ note: tuple variant defined here
2525
|
2626
LL | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
2727
| ^^^^
28-
help: use parentheses to construct a tuple
28+
help: wrap these arguments in parentheses to construct a tuple
2929
|
3030
LL | let _: Option<(i32, i8, &'static str)> = Some((1, 2, "hi"));
3131
| + +
@@ -97,7 +97,7 @@ note: function defined here
9797
|
9898
LL | fn two_ints(_: (i32, i32)) {
9999
| ^^^^^^^^ -------------
100-
help: use parentheses to construct a tuple
100+
help: wrap these arguments in parentheses to construct a tuple
101101
|
102102
LL | two_ints((1, 2));
103103
| + +
@@ -113,7 +113,7 @@ note: function defined here
113113
|
114114
LL | fn with_generic<T: Copy + Send>((a, b): (i32, T)) {
115115
| ^^^^^^^^^^^^ ----------------
116-
help: use parentheses to construct a tuple
116+
help: wrap these arguments in parentheses to construct a tuple
117117
|
118118
LL | with_generic((3, 4));
119119
| + +
@@ -129,7 +129,7 @@ note: function defined here
129129
|
130130
LL | fn with_generic<T: Copy + Send>((a, b): (i32, T)) {
131131
| ^^^^^^^^^^^^ ----------------
132-
help: use parentheses to construct a tuple
132+
help: wrap these arguments in parentheses to construct a tuple
133133
|
134134
LL | with_generic((a, b));
135135
| + +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn foo(s: &str, a: (i32, i32), s2: &str) {}
2+
3+
fn bar(s: &str, a: (&str,), s2: &str) {}
4+
5+
fn main() {
6+
foo("hi", 1, 2, "hi");
7+
//~^ ERROR this function takes 3 arguments but 4 arguments were supplied
8+
bar("hi", "hi", "hi");
9+
//~^ ERROR mismatched types
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0061]: this function takes 3 arguments but 4 arguments were supplied
2+
--> $DIR/add-tuple-within-arguments.rs:6:5
3+
|
4+
LL | foo("hi", 1, 2, "hi");
5+
| ^^^
6+
|
7+
note: function defined here
8+
--> $DIR/add-tuple-within-arguments.rs:1:4
9+
|
10+
LL | fn foo(s: &str, a: (i32, i32), s2: &str) {}
11+
| ^^^ ------- ------------- --------
12+
help: wrap these arguments in parentheses to construct a tuple
13+
|
14+
LL | foo("hi", (1, 2), "hi");
15+
| + +
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/add-tuple-within-arguments.rs:8:15
19+
|
20+
LL | bar("hi", "hi", "hi");
21+
| --- ^^^^ expected tuple, found `&str`
22+
| |
23+
| arguments to this function are incorrect
24+
|
25+
= note: expected tuple `(&str,)`
26+
found reference `&'static str`
27+
note: function defined here
28+
--> $DIR/add-tuple-within-arguments.rs:3:4
29+
|
30+
LL | fn bar(s: &str, a: (&str,), s2: &str) {}
31+
| ^^^ ------- ---------- --------
32+
help: use a trailing comma to create a tuple with one element
33+
|
34+
LL | bar("hi", ("hi",), "hi");
35+
| + ++
36+
37+
error: aborting due to 2 previous errors
38+
39+
Some errors have detailed explanations: E0061, E0308.
40+
For more information about an error, try `rustc --explain E0061`.

src/test/ui/tuple/wrong_argument_ice-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: function defined here
99
|
1010
LL | fn test(t: (i32, i32)) {}
1111
| ^^^^ -------------
12-
help: use parentheses to construct a tuple
12+
help: wrap these arguments in parentheses to construct a tuple
1313
|
1414
LL | test((x.qux(), x.qux()));
1515
| + +

src/test/ui/tuple/wrong_argument_ice.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: associated function defined here
99
|
1010
LL | pub fn push_back(&mut self, value: T) {
1111
| ^^^^^^^^^
12-
help: use parentheses to construct a tuple
12+
help: wrap these arguments in parentheses to construct a tuple
1313
|
1414
LL | self.acc.push_back((self.current_provides, self.current_requires));
1515
| + +

src/test/ui/unsized/unsized-fn-param.stderr

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t
22
--> $DIR/unsized-fn-param.rs:11:11
33
|
44
LL | foo11("bar", &"baz");
5-
| ^^^^^ doesn't have a size known at compile-time
5+
| ----- ^^^^^ doesn't have a size known at compile-time
6+
| |
7+
| required by a bound introduced by this call
68
|
79
= help: the trait `Sized` is not implemented for `str`
810
= note: required for the cast from `str` to the object type `dyn AsRef<Path>`
@@ -15,7 +17,9 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t
1517
--> $DIR/unsized-fn-param.rs:13:19
1618
|
1719
LL | foo12(&"bar", "baz");
18-
| ^^^^^ doesn't have a size known at compile-time
20+
| ----- ^^^^^ doesn't have a size known at compile-time
21+
| |
22+
| required by a bound introduced by this call
1923
|
2024
= help: the trait `Sized` is not implemented for `str`
2125
= note: required for the cast from `str` to the object type `dyn AsRef<Path>`
@@ -28,7 +32,9 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t
2832
--> $DIR/unsized-fn-param.rs:16:11
2933
|
3034
LL | foo21("bar", &"baz");
31-
| ^^^^^ doesn't have a size known at compile-time
35+
| ----- ^^^^^ doesn't have a size known at compile-time
36+
| |
37+
| required by a bound introduced by this call
3238
|
3339
= help: the trait `Sized` is not implemented for `str`
3440
= note: required for the cast from `str` to the object type `dyn AsRef<str>`
@@ -41,7 +47,9 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t
4147
--> $DIR/unsized-fn-param.rs:18:19
4248
|
4349
LL | foo22(&"bar", "baz");
44-
| ^^^^^ doesn't have a size known at compile-time
50+
| ----- ^^^^^ doesn't have a size known at compile-time
51+
| |
52+
| required by a bound introduced by this call
4553
|
4654
= help: the trait `Sized` is not implemented for `str`
4755
= note: required for the cast from `str` to the object type `dyn AsRef<str>`

src/test/ui/unsized/unsized3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn f9<X: ?Sized>(x1: Box<S<X>>) {
4444
fn f10<X: ?Sized>(x1: Box<S<X>>) {
4545
f5(&(32, *x1));
4646
//~^ ERROR the size for values of type
47+
//~| ERROR the size for values of type
4748
}
4849

4950
pub fn main() {}

src/test/ui/unsized/unsized3.stderr

+33-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ LL - fn f9<X: ?Sized>(x1: Box<S<X>>) {
100100
LL + fn f9<X>(x1: Box<S<X>>) {
101101
|
102102

103+
error[E0277]: the size for values of type `X` cannot be known at compilation time
104+
--> $DIR/unsized3.rs:45:9
105+
|
106+
LL | fn f10<X: ?Sized>(x1: Box<S<X>>) {
107+
| - this type parameter needs to be `std::marker::Sized`
108+
LL | f5(&(32, *x1));
109+
| -- ^^^^^^^^^ doesn't have a size known at compile-time
110+
| |
111+
| required by a bound introduced by this call
112+
|
113+
note: required because it appears within the type `S<X>`
114+
--> $DIR/unsized3.rs:28:8
115+
|
116+
LL | struct S<X: ?Sized> {
117+
| ^
118+
= note: required because it appears within the type `({integer}, S<X>)`
119+
= note: tuples must have a statically known size to be initialized
120+
help: consider removing the `?Sized` bound to make the type parameter `Sized`
121+
|
122+
LL - fn f10<X: ?Sized>(x1: Box<S<X>>) {
123+
LL + fn f10<X>(x1: Box<S<X>>) {
124+
|
125+
103126
error[E0277]: the size for values of type `X` cannot be known at compilation time
104127
--> $DIR/unsized3.rs:45:8
105128
|
@@ -116,13 +139,21 @@ note: required because it appears within the type `S<X>`
116139
LL | struct S<X: ?Sized> {
117140
| ^
118141
= note: required because it appears within the type `({integer}, S<X>)`
119-
= note: tuples must have a statically known size to be initialized
142+
note: required by a bound in `f5`
143+
--> $DIR/unsized3.rs:24:7
144+
|
145+
LL | fn f5<Y>(x: &Y) {}
146+
| ^ required by this bound in `f5`
120147
help: consider removing the `?Sized` bound to make the type parameter `Sized`
121148
|
122149
LL - fn f10<X: ?Sized>(x1: Box<S<X>>) {
123150
LL + fn f10<X>(x1: Box<S<X>>) {
124151
|
152+
help: consider relaxing the implicit `Sized` restriction
153+
|
154+
LL | fn f5<Y: ?Sized>(x: &Y) {}
155+
| ++++++++
125156

126-
error: aborting due to 5 previous errors
157+
error: aborting due to 6 previous errors
127158

128159
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)