Don't ICE on Fn trait error for foreign fn

This commit is contained in:
Michael Goulet 2024-08-07 13:08:24 -04:00
parent 833af65f38
commit d850f85055
3 changed files with 61 additions and 0 deletions

View File

@ -2729,6 +2729,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref sig, _), .. })
| Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Fn(ref sig, _), ..
})
| Node::ForeignItem(&hir::ForeignItem {
kind: hir::ForeignItemKind::Fn(ref sig, _, _),
..
}) => (
sig.span,
None,

View File

@ -0,0 +1,13 @@
// Make sure we don't ICE when a foreign fn doesn't implement `Fn` due to arg mismatch.
unsafe extern "Rust" {
pub safe fn foo();
pub safe fn bar(x: u32);
}
fn test(_: impl Fn(i32)) {}
fn main() {
test(foo); //~ ERROR function is expected to take 1 argument, but it takes 0 arguments
test(bar); //~ ERROR type mismatch in function arguments
}

View File

@ -0,0 +1,44 @@
error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
--> $DIR/foreign-safe-fn-arg-mismatch.rs:11:10
|
LL | pub safe fn foo();
| ------------------ takes 0 arguments
...
LL | test(foo);
| ---- ^^^ expected function that takes 1 argument
| |
| required by a bound introduced by this call
|
note: required by a bound in `test`
--> $DIR/foreign-safe-fn-arg-mismatch.rs:8:17
|
LL | fn test(_: impl Fn(i32)) {}
| ^^^^^^^ required by this bound in `test`
error[E0631]: type mismatch in function arguments
--> $DIR/foreign-safe-fn-arg-mismatch.rs:12:10
|
LL | pub safe fn bar(x: u32);
| ------------------------ found signature defined here
...
LL | test(bar);
| ---- ^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected function signature `fn(i32) -> _`
found function signature `fn(u32) -> _`
note: required by a bound in `test`
--> $DIR/foreign-safe-fn-arg-mismatch.rs:8:17
|
LL | fn test(_: impl Fn(i32)) {}
| ^^^^^^^ required by this bound in `test`
help: consider wrapping the function in a closure
|
LL | test(|arg0: i32| bar(/* u32 */));
| +++++++++++ +++++++++++
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0593, E0631.
For more information about an error, try `rustc --explain E0593`.