Skip to content

Commit e6db769

Browse files
Don't ICE on Fn trait error for foreign fn
1 parent 09b647e commit e6db769

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27432743
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref sig, _), .. })
27442744
| Node::TraitItem(&hir::TraitItem {
27452745
kind: hir::TraitItemKind::Fn(ref sig, _), ..
2746+
})
2747+
| Node::ForeignItem(&hir::ForeignItem {
2748+
kind: hir::ForeignItemKind::Fn(ref sig, _, _),
2749+
..
27462750
}) => (
27472751
sig.span,
27482752
None,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Make sure we don't ICE when a foreign fn doesn't implement `Fn` due to arg mismatch.
2+
3+
unsafe extern "Rust" {
4+
pub safe fn foo();
5+
pub safe fn bar(x: u32);
6+
}
7+
8+
fn test(_: impl Fn(i32)) {}
9+
10+
fn main() {
11+
test(foo); //~ ERROR function is expected to take 1 argument, but it takes 0 arguments
12+
test(bar); //~ ERROR type mismatch in function arguments
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
2+
--> $DIR/foreign-safe-fn-arg-mismatch.rs:11:10
3+
|
4+
LL | pub safe fn foo();
5+
| ------------------ takes 0 arguments
6+
...
7+
LL | test(foo);
8+
| ---- ^^^ expected function that takes 1 argument
9+
| |
10+
| required by a bound introduced by this call
11+
|
12+
note: required by a bound in `test`
13+
--> $DIR/foreign-safe-fn-arg-mismatch.rs:8:17
14+
|
15+
LL | fn test(_: impl Fn(i32)) {}
16+
| ^^^^^^^ required by this bound in `test`
17+
18+
error[E0631]: type mismatch in function arguments
19+
--> $DIR/foreign-safe-fn-arg-mismatch.rs:12:10
20+
|
21+
LL | pub safe fn bar(x: u32);
22+
| ------------------------ found signature defined here
23+
...
24+
LL | test(bar);
25+
| ---- ^^^ expected due to this
26+
| |
27+
| required by a bound introduced by this call
28+
|
29+
= note: expected function signature `fn(i32) -> _`
30+
found function signature `fn(u32) -> _`
31+
note: required by a bound in `test`
32+
--> $DIR/foreign-safe-fn-arg-mismatch.rs:8:17
33+
|
34+
LL | fn test(_: impl Fn(i32)) {}
35+
| ^^^^^^^ required by this bound in `test`
36+
help: consider wrapping the function in a closure
37+
|
38+
LL | test(|arg0: i32| bar(/* u32 */));
39+
| +++++++++++ +++++++++++
40+
41+
error: aborting due to 2 previous errors
42+
43+
Some errors have detailed explanations: E0593, E0631.
44+
For more information about an error, try `rustc --explain E0593`.

0 commit comments

Comments
 (0)