diff options
Diffstat (limited to 'tests/ui/type/type-check')
17 files changed, 292 insertions, 16 deletions
diff --git a/tests/ui/type/type-check/assignment-in-if.stderr b/tests/ui/type/type-check/assignment-in-if.stderr index 9f4558ada..de133e559 100644 --- a/tests/ui/type/type-check/assignment-in-if.stderr +++ b/tests/ui/type/type-check/assignment-in-if.stderr @@ -67,6 +67,9 @@ LL | x == 5 error[E0308]: mismatched types --> $DIR/assignment-in-if.rs:44:18 | +LL | if y = (Foo { foo: x }) { + | - here the type of `x` is inferred to be `usize` +... LL | if x == x && x = x && x == x { | ------ ^ expected `bool`, found `usize` | | @@ -75,6 +78,9 @@ LL | if x == x && x = x && x == x { error[E0308]: mismatched types --> $DIR/assignment-in-if.rs:44:22 | +LL | if y = (Foo { foo: x }) { + | - here the type of `x` is inferred to be `usize` +... LL | if x == x && x = x && x == x { | ^ expected `bool`, found `usize` @@ -92,6 +98,9 @@ LL | if x == x && x == x && x == x { error[E0308]: mismatched types --> $DIR/assignment-in-if.rs:51:28 | +LL | if y = (Foo { foo: x }) { + | - here the type of `x` is inferred to be `usize` +... LL | if x == x && x == x && x = x { | ---------------- ^ expected `bool`, found `usize` | | diff --git a/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr b/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr index b63d2a3b6..09c4b2053 100644 --- a/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr +++ b/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr @@ -1,12 +1,12 @@ -error[E0282]: type annotations needed for `Vec<T>` +error[E0282]: type annotations needed for `Vec<_>` --> $DIR/cannot_infer_local_or_vec.rs:2:9 | LL | let x = vec![]; | ^ | -help: consider giving `x` an explicit type, where the type for type parameter `T` is specified +help: consider giving `x` an explicit type, where the placeholders `_` are specified | -LL | let x: Vec<T> = vec![]; +LL | let x: Vec<_> = vec![]; | ++++++++ error: aborting due to previous error diff --git a/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr b/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr index e544b3695..1fa253052 100644 --- a/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr +++ b/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr @@ -1,12 +1,12 @@ -error[E0282]: type annotations needed for `(Vec<T>,)` +error[E0282]: type annotations needed for `(Vec<_>,)` --> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:9 | LL | let (x, ) = (vec![], ); | ^^^^^ ---------- type must be known at this point | -help: consider giving this pattern a type, where the type for type parameter `T` is specified +help: consider giving this pattern a type, where the placeholders `_` are specified | -LL | let (x, ): (Vec<T>,) = (vec![], ); +LL | let (x, ): (Vec<_>,) = (vec![], ); | +++++++++++ error: aborting due to previous error diff --git a/tests/ui/type/type-check/coerce-result-return-value-2.rs b/tests/ui/type/type-check/coerce-result-return-value-2.rs new file mode 100644 index 000000000..23bafa6c5 --- /dev/null +++ b/tests/ui/type/type-check/coerce-result-return-value-2.rs @@ -0,0 +1,24 @@ +struct A; +struct B; +impl From<A> for B { + fn from(_: A) -> Self { B } +} +fn foo4(x: Result<(), A>) -> Result<(), B> { + match true { + true => x, //~ ERROR mismatched types + false => x, + } +} +fn foo5(x: Result<(), A>) -> Result<(), B> { + match true { + true => return x, //~ ERROR mismatched types + false => return x, + } +} +fn main() { + let _ = foo4(Ok(())); + let _ = foo5(Ok(())); + let _: Result<(), B> = { //~ ERROR mismatched types + Err(A); + }; +} diff --git a/tests/ui/type/type-check/coerce-result-return-value-2.stderr b/tests/ui/type/type-check/coerce-result-return-value-2.stderr new file mode 100644 index 000000000..b2c409e07 --- /dev/null +++ b/tests/ui/type/type-check/coerce-result-return-value-2.stderr @@ -0,0 +1,47 @@ +error[E0308]: mismatched types + --> $DIR/coerce-result-return-value-2.rs:8:17 + | +LL | fn foo4(x: Result<(), A>) -> Result<(), B> { + | ------------- expected `Result<(), B>` because of return type +LL | match true { +LL | true => x, + | ^ expected `Result<(), B>`, found `Result<(), A>` + | + = note: expected enum `Result<_, B>` + found enum `Result<_, A>` +help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` + | +LL | true => Ok(x?), + | +++ ++ + +error[E0308]: mismatched types + --> $DIR/coerce-result-return-value-2.rs:14:24 + | +LL | fn foo5(x: Result<(), A>) -> Result<(), B> { + | ------------- expected `Result<(), B>` because of return type +LL | match true { +LL | true => return x, + | ^ expected `Result<(), B>`, found `Result<(), A>` + | + = note: expected enum `Result<_, B>` + found enum `Result<_, A>` +help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` + | +LL | true => return Ok(x?), + | +++ ++ + +error[E0308]: mismatched types + --> $DIR/coerce-result-return-value-2.rs:21:28 + | +LL | let _: Result<(), B> = { + | ____________________________^ +LL | | Err(A); +LL | | }; + | |_____^ expected `Result<(), B>`, found `()` + | + = note: expected enum `Result<(), B>` + found unit type `()` + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/type/type-check/coerce-result-return-value.fixed b/tests/ui/type/type-check/coerce-result-return-value.fixed new file mode 100644 index 000000000..8a0540707 --- /dev/null +++ b/tests/ui/type/type-check/coerce-result-return-value.fixed @@ -0,0 +1,24 @@ +// run-rustfix +struct A; +struct B; +impl From<A> for B { + fn from(_: A) -> Self { B } +} +fn foo1(x: Result<(), A>) -> Result<(), B> { + Ok(x?) //~ ERROR mismatched types +} +fn foo2(x: Result<(), A>) -> Result<(), B> { + return Ok(x?); //~ ERROR mismatched types +} +fn foo3(x: Result<(), A>) -> Result<(), B> { + if true { + Ok(x?) //~ ERROR mismatched types + } else { + Ok(x?) //~ ERROR mismatched types + } +} +fn main() { + let _ = foo1(Ok(())); + let _ = foo2(Ok(())); + let _ = foo3(Ok(())); +} diff --git a/tests/ui/type/type-check/coerce-result-return-value.rs b/tests/ui/type/type-check/coerce-result-return-value.rs new file mode 100644 index 000000000..442203add --- /dev/null +++ b/tests/ui/type/type-check/coerce-result-return-value.rs @@ -0,0 +1,24 @@ +// run-rustfix +struct A; +struct B; +impl From<A> for B { + fn from(_: A) -> Self { B } +} +fn foo1(x: Result<(), A>) -> Result<(), B> { + x //~ ERROR mismatched types +} +fn foo2(x: Result<(), A>) -> Result<(), B> { + return x; //~ ERROR mismatched types +} +fn foo3(x: Result<(), A>) -> Result<(), B> { + if true { + x //~ ERROR mismatched types + } else { + x //~ ERROR mismatched types + } +} +fn main() { + let _ = foo1(Ok(())); + let _ = foo2(Ok(())); + let _ = foo3(Ok(())); +} diff --git a/tests/ui/type/type-check/coerce-result-return-value.stderr b/tests/ui/type/type-check/coerce-result-return-value.stderr new file mode 100644 index 000000000..adec2f612 --- /dev/null +++ b/tests/ui/type/type-check/coerce-result-return-value.stderr @@ -0,0 +1,65 @@ +error[E0308]: mismatched types + --> $DIR/coerce-result-return-value.rs:8:5 + | +LL | fn foo1(x: Result<(), A>) -> Result<(), B> { + | ------------- expected `Result<(), B>` because of return type +LL | x + | ^ expected `Result<(), B>`, found `Result<(), A>` + | + = note: expected enum `Result<_, B>` + found enum `Result<_, A>` +help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` + | +LL | Ok(x?) + | +++ ++ + +error[E0308]: mismatched types + --> $DIR/coerce-result-return-value.rs:11:12 + | +LL | fn foo2(x: Result<(), A>) -> Result<(), B> { + | ------------- expected `Result<(), B>` because of return type +LL | return x; + | ^ expected `Result<(), B>`, found `Result<(), A>` + | + = note: expected enum `Result<_, B>` + found enum `Result<_, A>` +help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` + | +LL | return Ok(x?); + | +++ ++ + +error[E0308]: mismatched types + --> $DIR/coerce-result-return-value.rs:15:9 + | +LL | fn foo3(x: Result<(), A>) -> Result<(), B> { + | ------------- expected `Result<(), B>` because of return type +LL | if true { +LL | x + | ^ expected `Result<(), B>`, found `Result<(), A>` + | + = note: expected enum `Result<_, B>` + found enum `Result<_, A>` +help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` + | +LL | Ok(x?) + | +++ ++ + +error[E0308]: mismatched types + --> $DIR/coerce-result-return-value.rs:17:9 + | +LL | fn foo3(x: Result<(), A>) -> Result<(), B> { + | ------------- expected `Result<(), B>` because of return type +... +LL | x + | ^ expected `Result<(), B>`, found `Result<(), A>` + | + = note: expected enum `Result<_, B>` + found enum `Result<_, A>` +help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` + | +LL | Ok(x?) + | +++ ++ + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/type/type-check/point-at-inference-2.stderr b/tests/ui/type/type-check/point-at-inference-2.stderr index 1368aba0d..1d2777ad6 100644 --- a/tests/ui/type/type-check/point-at-inference-2.stderr +++ b/tests/ui/type/type-check/point-at-inference-2.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/point-at-inference-2.rs:5:9 | LL | bar(v); - | --- ^ expected `i32`, found `&{integer}` + | --- ^ expected `Vec<i32>`, found `Vec<&{integer}>` | | | arguments to this function are incorrect | @@ -17,8 +17,11 @@ LL | fn bar(_: Vec<i32>) {} error[E0308]: mismatched types --> $DIR/point-at-inference-2.rs:9:9 | +LL | baz(&v); + | - here the type of `v` is inferred to be `Vec<&i32>` +LL | baz(&v); LL | bar(v); - | --- ^ expected `i32`, found `&i32` + | --- ^ expected `Vec<i32>`, found `Vec<&i32>` | | | arguments to this function are incorrect | @@ -33,8 +36,10 @@ LL | fn bar(_: Vec<i32>) {} error[E0308]: mismatched types --> $DIR/point-at-inference-2.rs:12:9 | +LL | baz(&v); + | - here the type of `v` is inferred to be `Vec<&i32>` LL | bar(v); - | --- ^ expected `i32`, found `&i32` + | --- ^ expected `Vec<i32>`, found `Vec<&i32>` | | | arguments to this function are incorrect | diff --git a/tests/ui/type/type-check/point-at-inference-3.fixed b/tests/ui/type/type-check/point-at-inference-3.fixed index 44c057c0d..edd4adf8b 100644 --- a/tests/ui/type/type-check/point-at-inference-3.fixed +++ b/tests/ui/type/type-check/point-at-inference-3.fixed @@ -2,10 +2,11 @@ fn main() { let mut v = Vec::new(); v.push(0i32); + //~^ NOTE this is of type `i32`, which causes `v` to be inferred as `Vec<i32>` v.push(0); v.push(1i32); //~ ERROR mismatched types //~^ NOTE expected `i32`, found `u32` //~| NOTE arguments to this method are incorrect - //~| NOTE associated function defined here + //~| NOTE method defined here //~| HELP change the type of the numeric literal from `u32` to `i32` } diff --git a/tests/ui/type/type-check/point-at-inference-3.rs b/tests/ui/type/type-check/point-at-inference-3.rs index e7ae54384..49d7b5007 100644 --- a/tests/ui/type/type-check/point-at-inference-3.rs +++ b/tests/ui/type/type-check/point-at-inference-3.rs @@ -2,10 +2,11 @@ fn main() { let mut v = Vec::new(); v.push(0i32); + //~^ NOTE this is of type `i32`, which causes `v` to be inferred as `Vec<i32>` v.push(0); v.push(1u32); //~ ERROR mismatched types //~^ NOTE expected `i32`, found `u32` //~| NOTE arguments to this method are incorrect - //~| NOTE associated function defined here + //~| NOTE method defined here //~| HELP change the type of the numeric literal from `u32` to `i32` } diff --git a/tests/ui/type/type-check/point-at-inference-3.stderr b/tests/ui/type/type-check/point-at-inference-3.stderr index d7936e39c..2c4907ed2 100644 --- a/tests/ui/type/type-check/point-at-inference-3.stderr +++ b/tests/ui/type/type-check/point-at-inference-3.stderr @@ -1,12 +1,15 @@ error[E0308]: mismatched types - --> $DIR/point-at-inference-3.rs:6:12 + --> $DIR/point-at-inference-3.rs:7:12 | +LL | v.push(0i32); + | ---- this is of type `i32`, which causes `v` to be inferred as `Vec<i32>` +... LL | v.push(1u32); | ---- ^^^^ expected `i32`, found `u32` | | | arguments to this method are incorrect | -note: associated function defined here +note: method defined here --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL help: change the type of the numeric literal from `u32` to `i32` | diff --git a/tests/ui/type/type-check/point-at-inference-4.rs b/tests/ui/type/type-check/point-at-inference-4.rs new file mode 100644 index 000000000..aea9b2c6c --- /dev/null +++ b/tests/ui/type/type-check/point-at-inference-4.rs @@ -0,0 +1,21 @@ +struct S<A, B>(Option<(A, B)>); + +impl<A, B> S<A, B> { + fn infer(&self, a: A, b: B) {} + //~^ NOTE method defined here + //~| NOTE + //~| NOTE +} + +fn main() { + let s = S(None); + s.infer(0i32); + //~^ ERROR this method takes 2 arguments but 1 argument was supplied + //~| NOTE an argument is missing + //~| HELP provide the argument + let t: S<u32, _> = s; + //~^ ERROR mismatched types + //~| NOTE expected `S<u32, _>`, found `S<i32, _>` + //~| NOTE expected due to this + //~| NOTE expected struct `S<u32, _>` +} diff --git a/tests/ui/type/type-check/point-at-inference-4.stderr b/tests/ui/type/type-check/point-at-inference-4.stderr new file mode 100644 index 000000000..28833d2ed --- /dev/null +++ b/tests/ui/type/type-check/point-at-inference-4.stderr @@ -0,0 +1,31 @@ +error[E0061]: this method takes 2 arguments but 1 argument was supplied + --> $DIR/point-at-inference-4.rs:12:7 + | +LL | s.infer(0i32); + | ^^^^^------ an argument is missing + | +note: method defined here + --> $DIR/point-at-inference-4.rs:4:8 + | +LL | fn infer(&self, a: A, b: B) {} + | ^^^^^ ---- ---- +help: provide the argument + | +LL | s.infer(0i32, /* b */); + | ~~~~~~~~~~~~~~~ + +error[E0308]: mismatched types + --> $DIR/point-at-inference-4.rs:16:24 + | +LL | let t: S<u32, _> = s; + | --------- ^ expected `S<u32, _>`, found `S<i32, _>` + | | + | expected due to this + | + = note: expected struct `S<u32, _>` + found struct `S<i32, _>` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0061, E0308. +For more information about an error, try `rustc --explain E0061`. diff --git a/tests/ui/type/type-check/point-at-inference.fixed b/tests/ui/type/type-check/point-at-inference.fixed new file mode 100644 index 000000000..f41fbe59f --- /dev/null +++ b/tests/ui/type/type-check/point-at-inference.fixed @@ -0,0 +1,13 @@ +// run-rustfix +fn bar(_: Vec<i32>) {} +fn baz(_: &impl std::any::Any) {} +fn main() { + let v = vec![1, 2, 3, 4, 5]; + let mut foo = vec![]; + baz(&foo); + for i in &v { + foo.push(*i); + } + baz(&foo); + bar(foo); //~ ERROR E0308 +} diff --git a/tests/ui/type/type-check/point-at-inference.rs b/tests/ui/type/type-check/point-at-inference.rs index 5c46dd4ed..6419e42e7 100644 --- a/tests/ui/type/type-check/point-at-inference.rs +++ b/tests/ui/type/type-check/point-at-inference.rs @@ -1,3 +1,4 @@ +// run-rustfix fn bar(_: Vec<i32>) {} fn baz(_: &impl std::any::Any) {} fn main() { diff --git a/tests/ui/type/type-check/point-at-inference.stderr b/tests/ui/type/type-check/point-at-inference.stderr index 2e17e5c5f..a76b4f90c 100644 --- a/tests/ui/type/type-check/point-at-inference.stderr +++ b/tests/ui/type/type-check/point-at-inference.stderr @@ -1,18 +1,25 @@ error[E0308]: mismatched types - --> $DIR/point-at-inference.rs:11:9 + --> $DIR/point-at-inference.rs:12:9 | +LL | foo.push(i); + | - this is of type `&{integer}`, which causes `foo` to be inferred as `Vec<&{integer}>` +... LL | bar(foo); - | --- ^^^ expected `i32`, found `&{integer}` + | --- ^^^ expected `Vec<i32>`, found `Vec<&{integer}>` | | | arguments to this function are incorrect | = note: expected struct `Vec<i32>` found struct `Vec<&{integer}>` note: function defined here - --> $DIR/point-at-inference.rs:1:4 + --> $DIR/point-at-inference.rs:2:4 | LL | fn bar(_: Vec<i32>) {} | ^^^ ----------- +help: consider dereferencing the borrow + | +LL | foo.push(*i); + | + error: aborting due to previous error |