Skip to content

Commit d4cdc7d

Browse files
committed
Add and fix tests for coercion between (FnDef | Closure) and (FnDef | Closure)
1 parent ad5b9a7 commit d4cdc7d

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

src/test/ui/issues/issue-24036.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn closure_from_match() {
1010
2 => |c| c - 1,
1111
_ => |c| c - 1
1212
};
13-
//~^^^ ERROR `match` arms have incompatible types
13+
//~^^^^ ERROR 9:15: 9:16: type annotations needed [E0282]
1414
}
1515

1616
fn main() { }

src/test/ui/issues/issue-24036.stderr

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,13 @@ LL | x = |c| c + 1;
1111
= note: no two closures, even if identical, have the same type
1212
= help: consider boxing your closure and/or using it as a trait object
1313

14-
error[E0308]: `match` arms have incompatible types
15-
--> $DIR/issue-24036.rs:10:14
14+
error[E0282]: type annotations needed
15+
--> $DIR/issue-24036.rs:9:15
1616
|
17-
LL | let x = match 1usize {
18-
| _____________-
19-
LL | | 1 => |c| c + 1,
20-
| | --------- this is found to be of type `[closure@$DIR/issue-24036.rs:9:14: 9:23]`
21-
LL | | 2 => |c| c - 1,
22-
| | ^^^^^^^^^ expected closure, found a different closure
23-
LL | | _ => |c| c - 1
24-
LL | | };
25-
| |_____- `match` arms have incompatible types
26-
|
27-
= note: expected type `[closure@$DIR/issue-24036.rs:9:14: 9:23]`
28-
found closure `[closure@$DIR/issue-24036.rs:10:14: 10:23]`
29-
= note: no two closures, even if identical, have the same type
30-
= help: consider boxing your closure and/or using it as a trait object
17+
LL | 1 => |c| c + 1,
18+
| ^ consider giving this closure parameter a type
3119

3220
error: aborting due to 2 previous errors
3321

34-
For more information about this error, try `rustc --explain E0308`.
22+
Some errors have detailed explanations: E0282, E0308.
23+
For more information about an error, try `rustc --explain E0282`.

src/test/ui/issues/issue-46742-1.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// check-pass
2+
fn main() {
3+
let _: i32 = (match "" {
4+
"+" => ::std::ops::Add::add,
5+
"-" => ::std::ops::Sub::sub,
6+
"<" => |a,b| (a < b) as i32,
7+
_ => unimplemented!(),
8+
})(5, 5);
9+
let _: i32 = (match "" {
10+
"-" => ::std::ops::Sub::sub,
11+
"<" => |a,b| (a < b) as i32,
12+
"+" => ::std::ops::Add::add,
13+
_ => unimplemented!(),
14+
})(5, 5);
15+
let _: i32 = (match "" {
16+
"<" => |a,b| (a < b) as i32,
17+
"+" => ::std::ops::Add::add,
18+
"-" => ::std::ops::Sub::sub,
19+
_ => unimplemented!(),
20+
})(5, 5);
21+
22+
let _: i32 = (match "" {
23+
"+" => ::std::ops::Add::add,
24+
"<" => |a,b| (a < b) as i32,
25+
_ => unimplemented!(),
26+
})(5, 5);
27+
let _: i32 = (match "" {
28+
"<" => |a,b| (a < b) as i32,
29+
"+" => ::std::ops::Add::add,
30+
_ => unimplemented!(),
31+
})(5, 5);
32+
33+
let _: i32 = (match "" {
34+
"+" => |c,d| (c > d) as i32,
35+
"<" => |a,b| (a < b) as i32,
36+
"-" => ::std::ops::Sub::sub,
37+
_ => unimplemented!(),
38+
})(5, 5);
39+
let _: i32 = (match "" {
40+
"<" => |a,b| (a < b) as i32,
41+
"-" => ::std::ops::Sub::sub,
42+
"+" => |c,d| (c > d) as i32,
43+
_ => unimplemented!(),
44+
})(5, 5);
45+
let _: i32 = (match "" {
46+
"-" => ::std::ops::Sub::sub,
47+
"+" => |c,d| (c > d) as i32,
48+
"<" => |a,b| (a < b) as i32,
49+
_ => unimplemented!(),
50+
})(5, 5);
51+
}

src/test/ui/issues/issue-46742-2.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
unsafe fn add(a: i32, b: i32) -> i32 {
2+
a + b
3+
}
4+
fn main() {
5+
let foo = match "+" {
6+
"+" => add,
7+
"-" => |a,b| (a - b) as i32,
8+
_ => unimplemented!(),
9+
};
10+
let result: i32 = foo(5, 5); //~ ERROR call to unsafe function
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
2+
--> $DIR/issue-46742-2.rs:10:23
3+
|
4+
LL | let result: i32 = foo(5, 5);
5+
| ^^^^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)