diff options
Diffstat (limited to 'tests/ui/fn')
-rw-r--r-- | tests/ui/fn/fn-compare-mismatch.stderr | 2 | ||||
-rw-r--r-- | tests/ui/fn/fn-item-type.rs | 38 | ||||
-rw-r--r-- | tests/ui/fn/fn-item-type.stderr | 53 | ||||
-rw-r--r-- | tests/ui/fn/fn-pointer-mismatch.rs | 56 | ||||
-rw-r--r-- | tests/ui/fn/fn-pointer-mismatch.stderr | 91 | ||||
-rw-r--r-- | tests/ui/fn/fn-trait-formatting.stderr | 6 | ||||
-rw-r--r-- | tests/ui/fn/implied-bounds-unnorm-associated-type-4.stderr | 2 | ||||
-rw-r--r-- | tests/ui/fn/implied-bounds-unnorm-associated-type.stderr | 2 | ||||
-rw-r--r-- | tests/ui/fn/issue-3044.stderr | 2 | ||||
-rw-r--r-- | tests/ui/fn/signature-error-reporting-under-verbose.rs | 3 | ||||
-rw-r--r-- | tests/ui/fn/signature-error-reporting-under-verbose.stderr | 3 |
11 files changed, 203 insertions, 55 deletions
diff --git a/tests/ui/fn/fn-compare-mismatch.stderr b/tests/ui/fn/fn-compare-mismatch.stderr index df838cb11..b4e71e75f 100644 --- a/tests/ui/fn/fn-compare-mismatch.stderr +++ b/tests/ui/fn/fn-compare-mismatch.stderr @@ -19,6 +19,8 @@ LL | let x = f == g; | = note: expected fn item `fn() {f}` found fn item `fn() {g}` + = note: different fn items have unique types, even if their signatures are the same + = help: consider casting both fn items to fn pointers using `as fn()` error: aborting due to 2 previous errors diff --git a/tests/ui/fn/fn-item-type.rs b/tests/ui/fn/fn-item-type.rs index 1831e6cbf..c094a34b2 100644 --- a/tests/ui/fn/fn-item-type.rs +++ b/tests/ui/fn/fn-item-type.rs @@ -1,13 +1,22 @@ // Test that the types of distinct fn items are not compatible by // default. See also `run-pass/fn-item-type-*.rs`. -fn foo<T>(x: isize) -> isize { x * 2 } -fn bar<T>(x: isize) -> isize { x * 4 } +fn foo<T>(x: isize) -> isize { + x * 2 +} +fn bar<T>(x: isize) -> isize { + x * 4 +} -fn eq<T>(x: T, y: T) { } +fn eq<T>(x: T, y: T) {} -trait Foo { fn foo() { /* this is a default fn */ } } -impl<T> Foo for T { /* `foo` is still default here */ } +trait Foo { + fn foo() { /* this is a default fn */ + } +} +impl<T> Foo for T { + /* `foo` is still default here */ +} fn main() { eq(foo::<u8>, bar::<u8>); @@ -15,39 +24,28 @@ fn main() { //~| expected fn item `fn(_) -> _ {foo::<u8>}` //~| found fn item `fn(_) -> _ {bar::<u8>}` //~| expected fn item, found a different fn item - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| different fn items have unique types, even if their signatures are the same eq(foo::<u8>, foo::<i8>); //~^ ERROR mismatched types //~| expected `u8`, found `i8` - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| different fn items have unique types, even if their signatures are the same eq(bar::<String>, bar::<Vec<u8>>); //~^ ERROR mismatched types //~| found fn item `fn(_) -> _ {bar::<Vec<u8>>}` - //~| expected struct `String`, found struct `Vec` - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| expected `String`, found `Vec<u8>` // Make sure we distinguish between trait methods correctly. eq(<u8 as Foo>::foo, <u16 as Foo>::foo); //~^ ERROR mismatched types //~| expected `u8`, found `u16` - //~| different `fn` items always have unique types, even if their signatures are the same - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer + //~| different fn items have unique types, even if their signatures are the same eq(foo::<u8>, bar::<u8> as fn(isize) -> isize); //~^ ERROR mismatched types //~| found fn pointer `fn(_) -> _` //~| expected fn item, found fn pointer - //~| change the expected type to be function pointer - //~| if the expected type is due to type inference, cast the expected `fn` to a function pointer eq(foo::<u8> as fn(isize) -> isize, bar::<u8>); // ok! } diff --git a/tests/ui/fn/fn-item-type.stderr b/tests/ui/fn/fn-item-type.stderr index f03a47d5c..da90b8b81 100644 --- a/tests/ui/fn/fn-item-type.stderr +++ b/tests/ui/fn/fn-item-type.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:13:19 + --> $DIR/fn-item-type.rs:22:19 | LL | eq(foo::<u8>, bar::<u8>); | -- ^^^^^^^^^ expected fn item, found a different fn item @@ -8,17 +8,16 @@ LL | eq(foo::<u8>, bar::<u8>); | = note: expected fn item `fn(_) -> _ {foo::<u8>}` found fn item `fn(_) -> _ {bar::<u8>}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo::<u8> as fn(isize) -> isize` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq<T>(x: T, y: T) { } +LL | fn eq<T>(x: T, y: T) {} | ^^ ---- + = help: consider casting both fn items to fn pointers using `as fn(isize) -> isize` error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:22:19 + --> $DIR/fn-item-type.rs:29:19 | LL | eq(foo::<u8>, foo::<i8>); | -- ^^^^^^^^^ expected `u8`, found `i8` @@ -27,36 +26,34 @@ LL | eq(foo::<u8>, foo::<i8>); | = note: expected fn item `fn(_) -> _ {foo::<u8>}` found fn item `fn(_) -> _ {foo::<i8>}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo::<u8> as fn(isize) -> isize` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq<T>(x: T, y: T) { } +LL | fn eq<T>(x: T, y: T) {} | ^^ ---- + = help: consider casting both fn items to fn pointers using `as fn(isize) -> isize` error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:29:23 + --> $DIR/fn-item-type.rs:34:23 | LL | eq(bar::<String>, bar::<Vec<u8>>); - | -- ^^^^^^^^^^^^^^ expected struct `String`, found struct `Vec` + | -- ^^^^^^^^^^^^^^ expected `String`, found `Vec<u8>` | | | arguments to this function are incorrect | = note: expected fn item `fn(_) -> _ {bar::<String>}` found fn item `fn(_) -> _ {bar::<Vec<u8>>}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `bar::<String> as fn(isize) -> isize` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq<T>(x: T, y: T) { } +LL | fn eq<T>(x: T, y: T) {} | ^^ ---- + = help: consider casting both fn items to fn pointers using `as fn(isize) -> isize` error[E0308]: mismatched types - --> $DIR/fn-item-type.rs:38:26 + --> $DIR/fn-item-type.rs:40:26 | LL | eq(<u8 as Foo>::foo, <u16 as Foo>::foo); | -- ^^^^^^^^^^^^^^^^^ expected `u8`, found `u16` @@ -65,14 +62,13 @@ LL | eq(<u8 as Foo>::foo, <u16 as Foo>::foo); | = note: expected fn item `fn() {<u8 as Foo>::foo}` found fn item `fn() {<u16 as Foo>::foo}` - = note: different `fn` items always have unique types, even if their signatures are the same - = help: change the expected type to be function pointer `fn()` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `<u8 as Foo>::foo as fn()` + = note: different fn items have unique types, even if their signatures are the same note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq<T>(x: T, y: T) { } +LL | fn eq<T>(x: T, y: T) {} | ^^ ---- + = help: consider casting both fn items to fn pointers using `as fn()` error[E0308]: mismatched types --> $DIR/fn-item-type.rs:45:19 @@ -84,12 +80,11 @@ LL | eq(foo::<u8>, bar::<u8> as fn(isize) -> isize); | = note: expected fn item `fn(_) -> _ {foo::<u8>}` found fn pointer `fn(_) -> _` - = help: change the expected type to be function pointer `fn(isize) -> isize` - = help: if the expected type is due to type inference, cast the expected `fn` to a function pointer: `foo::<u8> as fn(isize) -> isize` + = help: consider casting the fn item to a fn pointer: `foo::<u8> as fn(isize) -> isize` note: function defined here - --> $DIR/fn-item-type.rs:7:4 + --> $DIR/fn-item-type.rs:11:4 | -LL | fn eq<T>(x: T, y: T) { } +LL | fn eq<T>(x: T, y: T) {} | ^^ ---- error: aborting due to 5 previous errors diff --git a/tests/ui/fn/fn-pointer-mismatch.rs b/tests/ui/fn/fn-pointer-mismatch.rs new file mode 100644 index 000000000..0597478cb --- /dev/null +++ b/tests/ui/fn/fn-pointer-mismatch.rs @@ -0,0 +1,56 @@ +fn foo(x: u32) -> u32 { + x * 2 +} + +fn bar(x: u32) -> u32 { + x * 3 +} + +// original example from Issue #102608 +fn foobar(n: u32) -> u32 { + let g = if n % 2 == 0 { &foo } else { &bar }; + //~^ ERROR `if` and `else` have incompatible types + //~| different fn items have unique types, even if their signatures are the same + g(n) +} + +fn main() { + assert_eq!(foobar(7), 21); + assert_eq!(foobar(8), 16); + + // general mismatch of fn item types + let mut a = foo; + a = bar; + //~^ ERROR mismatched types + //~| expected fn item `fn(_) -> _ {foo}` + //~| found fn item `fn(_) -> _ {bar}` + //~| different fn items have unique types, even if their signatures are the same + + // display note even when boxed + let mut b = Box::new(foo); + b = Box::new(bar); + //~^ ERROR mismatched types + //~| different fn items have unique types, even if their signatures are the same + + // suggest removing reference + let c: fn(u32) -> u32 = &foo; + //~^ ERROR mismatched types + //~| expected fn pointer `fn(u32) -> u32` + //~| found reference `&fn(u32) -> u32 {foo}` + + // suggest using reference + let d: &fn(u32) -> u32 = foo; + //~^ ERROR mismatched types + //~| expected reference `&fn(u32) -> u32` + //~| found fn item `fn(u32) -> u32 {foo}` + + // suggest casting with reference + let e: &fn(u32) -> u32 = &foo; + //~^ ERROR mismatched types + //~| expected reference `&fn(u32) -> u32` + //~| found reference `&fn(u32) -> u32 {foo}` + + // OK + let mut z: fn(u32) -> u32 = foo as fn(u32) -> u32; + z = bar; +} diff --git a/tests/ui/fn/fn-pointer-mismatch.stderr b/tests/ui/fn/fn-pointer-mismatch.stderr new file mode 100644 index 000000000..a674babcb --- /dev/null +++ b/tests/ui/fn/fn-pointer-mismatch.stderr @@ -0,0 +1,91 @@ +error[E0308]: `if` and `else` have incompatible types + --> $DIR/fn-pointer-mismatch.rs:11:43 + | +LL | let g = if n % 2 == 0 { &foo } else { &bar }; + | ---- ^^^^ expected `&fn(u32) -> u32 {foo}`, found `&fn(u32) -> u32 {bar}` + | | + | expected because of this + | + = note: expected reference `&fn(u32) -> u32 {foo}` + found reference `&fn(u32) -> u32 {bar}` + = note: different fn items have unique types, even if their signatures are the same + = help: consider casting both fn items to fn pointers using `as fn(u32) -> u32` + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:23:9 + | +LL | let mut a = foo; + | --- expected due to this value +LL | a = bar; + | ^^^ expected fn item, found a different fn item + | + = note: expected fn item `fn(_) -> _ {foo}` + found fn item `fn(_) -> _ {bar}` + = note: different fn items have unique types, even if their signatures are the same + = help: consider casting both fn items to fn pointers using `as fn(u32) -> u32` + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:31:18 + | +LL | b = Box::new(bar); + | -------- ^^^ expected fn item, found a different fn item + | | + | arguments to this function are incorrect + | + = note: expected fn item `fn(_) -> _ {foo}` + found fn item `fn(_) -> _ {bar}` + = note: different fn items have unique types, even if their signatures are the same +note: associated function defined here + --> $SRC_DIR/alloc/src/boxed.rs:LL:COL + = help: consider casting both fn items to fn pointers using `as fn(u32) -> u32` + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:36:29 + | +LL | let c: fn(u32) -> u32 = &foo; + | -------------- ^^^^ expected fn pointer, found `&fn(u32) -> u32 {foo}` + | | + | expected due to this + | + = note: expected fn pointer `fn(u32) -> u32` + found reference `&fn(u32) -> u32 {foo}` +help: consider removing the reference + | +LL | let c: fn(u32) -> u32 = foo; + | ~~~ + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:42:30 + | +LL | let d: &fn(u32) -> u32 = foo; + | --------------- ^^^ expected `&fn(u32) -> u32`, found fn item + | | + | expected due to this + | + = note: expected reference `&fn(u32) -> u32` + found fn item `fn(u32) -> u32 {foo}` +help: consider using a reference + | +LL | let d: &fn(u32) -> u32 = &foo; + | ~~~~ + +error[E0308]: mismatched types + --> $DIR/fn-pointer-mismatch.rs:48:30 + | +LL | let e: &fn(u32) -> u32 = &foo; + | --------------- ^^^^ expected `&fn(u32) -> u32`, found `&fn(u32) -> u32 {foo}` + | | + | expected due to this + | + = note: expected reference `&fn(u32) -> u32` + found reference `&fn(u32) -> u32 {foo}` + = note: fn items are distinct from fn pointers + = note: when the arguments and return types match, functions can be coerced to function pointers +help: consider casting to a fn pointer + | +LL | let e: &fn(u32) -> u32 = &(foo as fn(u32) -> u32); + | ~~~~~~~~~~~~~~~~~~~~~~~~ + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/fn/fn-trait-formatting.stderr b/tests/ui/fn/fn-trait-formatting.stderr index 2a674d3c1..45d543bda 100644 --- a/tests/ui/fn/fn-trait-formatting.stderr +++ b/tests/ui/fn/fn-trait-formatting.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/fn-trait-formatting.rs:6:17 | LL | let _: () = Box::new(|_: isize| {}) as Box<dyn FnOnce(isize)>; - | -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `Box` + | -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Box<dyn FnOnce(isize)>` | | | expected due to this | @@ -17,7 +17,7 @@ error[E0308]: mismatched types --> $DIR/fn-trait-formatting.rs:10:17 | LL | let _: () = Box::new(|_: isize, isize| {}) as Box<dyn Fn(isize, isize)>; - | -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `Box` + | -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Box<dyn Fn(isize, isize)>` | | | expected due to this | @@ -32,7 +32,7 @@ error[E0308]: mismatched types --> $DIR/fn-trait-formatting.rs:14:17 | LL | let _: () = Box::new(|| -> isize { unimplemented!() }) as Box<dyn FnMut() -> isize>; - | -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `Box` + | -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Box<dyn FnMut() -> isize>` | | | expected due to this | diff --git a/tests/ui/fn/implied-bounds-unnorm-associated-type-4.stderr b/tests/ui/fn/implied-bounds-unnorm-associated-type-4.stderr index fcbaa91d1..4df639232 100644 --- a/tests/ui/fn/implied-bounds-unnorm-associated-type-4.stderr +++ b/tests/ui/fn/implied-bounds-unnorm-associated-type-4.stderr @@ -1,6 +1,8 @@ error[E0505]: cannot move out of `x` because it is borrowed --> $DIR/implied-bounds-unnorm-associated-type-4.rs:21:10 | +LL | let x = String::from("Hello World!"); + | - binding `x` declared here LL | let y = f(&x, ()); | -- borrow of `x` occurs here LL | drop(x); diff --git a/tests/ui/fn/implied-bounds-unnorm-associated-type.stderr b/tests/ui/fn/implied-bounds-unnorm-associated-type.stderr index e35f46e44..d417f2883 100644 --- a/tests/ui/fn/implied-bounds-unnorm-associated-type.stderr +++ b/tests/ui/fn/implied-bounds-unnorm-associated-type.stderr @@ -1,6 +1,8 @@ error[E0505]: cannot move out of `x` because it is borrowed --> $DIR/implied-bounds-unnorm-associated-type.rs:20:10 | +LL | let x = String::from("Hello World!"); + | - binding `x` declared here LL | let y = f(&x, ()); | -- borrow of `x` occurs here LL | drop(x); diff --git a/tests/ui/fn/issue-3044.stderr b/tests/ui/fn/issue-3044.stderr index 2690ad711..219029e2a 100644 --- a/tests/ui/fn/issue-3044.stderr +++ b/tests/ui/fn/issue-3044.stderr @@ -7,7 +7,7 @@ LL | | LL | | }); | |______- an argument is missing | -note: associated function defined here +note: method defined here --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL help: provide the argument | diff --git a/tests/ui/fn/signature-error-reporting-under-verbose.rs b/tests/ui/fn/signature-error-reporting-under-verbose.rs index d7a8c95e8..12ff113c9 100644 --- a/tests/ui/fn/signature-error-reporting-under-verbose.rs +++ b/tests/ui/fn/signature-error-reporting-under-verbose.rs @@ -9,7 +9,8 @@ fn needs_ptr(_: fn(i32, u32)) {} fn main() { needs_ptr(foo); //~^ ERROR mismatched types - //~| NOTE expected `u32`, found `i32` + //~| NOTE expected fn pointer, found fn item //~| NOTE expected fn pointer `fn(i32, u32)` //~| NOTE arguments to this function are incorrect + //~| NOTE when the arguments and return types match, functions can be coerced to function pointers } diff --git a/tests/ui/fn/signature-error-reporting-under-verbose.stderr b/tests/ui/fn/signature-error-reporting-under-verbose.stderr index 6260fc8dc..f4498db72 100644 --- a/tests/ui/fn/signature-error-reporting-under-verbose.stderr +++ b/tests/ui/fn/signature-error-reporting-under-verbose.stderr @@ -2,12 +2,13 @@ error[E0308]: mismatched types --> $DIR/signature-error-reporting-under-verbose.rs:10:15 | LL | needs_ptr(foo); - | --------- ^^^ expected `u32`, found `i32` + | --------- ^^^ expected fn pointer, found fn item | | | arguments to this function are incorrect | = note: expected fn pointer `fn(i32, u32)` found fn item `fn(i32, i32) {foo}` + = note: when the arguments and return types match, functions can be coerced to function pointers note: function defined here --> $DIR/signature-error-reporting-under-verbose.rs:5:4 | |