Skip to content

Commit a18f043

Browse files
committed
Add regression test
1 parent ad07aa1 commit a18f043

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//! Test that coercing between function items of different functions works,
2+
//! as long as their signatures match. The resulting value is a function pointer.
3+
4+
#![feature(type_alias_impl_trait)]
5+
6+
fn foo<T>(t: T) -> T {
7+
t
8+
}
9+
10+
fn bar<T>(t: T) -> T {
11+
t
12+
}
13+
14+
type F = impl Sized;
15+
16+
fn f(a: F) {
17+
let mut x = bar::<F>;
18+
x = foo::<()>; //~ ERROR: mismatched types
19+
x(a);
20+
x(());
21+
}
22+
23+
type I = impl Sized;
24+
25+
fn i(a: I) {
26+
let mut x = bar::<()>;
27+
x = foo::<I>; //~ ERROR: mismatched types
28+
x(a);
29+
x(());
30+
}
31+
32+
type J = impl Sized;
33+
34+
fn j(a: J) {
35+
let x = match true {
36+
true => bar::<J>,
37+
false => foo::<()>, //~ ERROR: incompatible types
38+
};
39+
x(a);
40+
x(());
41+
}
42+
43+
fn k() -> impl Sized {
44+
fn bind<T, F: FnOnce(T) -> T>(_: T, f: F) -> F {
45+
f
46+
}
47+
let x = match true {
48+
true => {
49+
let f = foo;
50+
bind(k(), f)
51+
}
52+
false => bar::<()>, //~ ERROR: incompatible types
53+
};
54+
todo!()
55+
}
56+
57+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:18:9
3+
|
4+
LL | type F = impl Sized;
5+
| ---------- the expected opaque type
6+
...
7+
LL | let mut x = bar::<F>;
8+
| -------- expected due to this value
9+
LL | x = foo::<()>;
10+
| ^^^^^^^^^ expected fn item, found a different fn item
11+
|
12+
= note: expected fn item `fn(F) -> F {bar::<F>}`
13+
found fn item `fn(()) {foo::<()>}`
14+
15+
error[E0308]: mismatched types
16+
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:27:9
17+
|
18+
LL | fn foo<T>(t: T) -> T {
19+
| -------------------- function `foo` defined here
20+
...
21+
LL | type I = impl Sized;
22+
| ---------- the found opaque type
23+
...
24+
LL | let mut x = bar::<()>;
25+
| --------- expected due to this value
26+
LL | x = foo::<I>;
27+
| ^^^^^^^^ expected fn item, found a different fn item
28+
|
29+
= note: expected fn item `fn(()) {bar::<()>}`
30+
found fn item `fn(I) -> I {foo::<I>}`
31+
help: use parentheses to call this function
32+
|
33+
LL | x = foo::<I>(/* I */);
34+
| +++++++++
35+
36+
error[E0308]: `match` arms have incompatible types
37+
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:37:18
38+
|
39+
LL | type J = impl Sized;
40+
| ---------- the expected opaque type
41+
...
42+
LL | let x = match true {
43+
| _____________-
44+
LL | | true => bar::<J>,
45+
| | -------- this is found to be of type `fn(J) -> J {bar::<J>}`
46+
LL | | false => foo::<()>,
47+
| | ^^^^^^^^^ expected opaque type, found `()`
48+
LL | | };
49+
| |_____- `match` arms have incompatible types
50+
|
51+
= note: expected fn item `fn(J) -> J {bar::<J>}`
52+
found fn item `fn(()) {foo::<()>}`
53+
54+
error[E0308]: `match` arms have incompatible types
55+
--> $DIR/fn_def_opaque_coercion_to_fn_ptr.rs:52:18
56+
|
57+
LL | fn k() -> impl Sized {
58+
| ---------- the expected opaque type
59+
...
60+
LL | let x = match true {
61+
| _____________-
62+
LL | | true => {
63+
LL | | let f = foo;
64+
LL | | bind(k(), f)
65+
| | ------------ this is found to be of type `fn(impl Sized) -> impl Sized {foo::<impl Sized>}`
66+
LL | | }
67+
LL | | false => bar::<()>,
68+
| | ^^^^^^^^^ expected opaque type, found `()`
69+
LL | | };
70+
| |_____- `match` arms have incompatible types
71+
|
72+
= note: expected fn item `fn(impl Sized) -> impl Sized {foo::<impl Sized>}`
73+
found fn item `fn(()) {bar::<()>}`
74+
75+
error: aborting due to 4 previous errors
76+
77+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)