Also check function items' signatures for Fn* trait compatibility

This commit is contained in:
Oli Scherer 2023-03-21 11:44:36 +00:00
parent 91d913168c
commit a00413f680
3 changed files with 81 additions and 8 deletions

View file

@ -189,11 +189,20 @@ pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
goal_kind: ty::ClosureKind, goal_kind: ty::ClosureKind,
) -> Result<Option<ty::Binder<'tcx, (Ty<'tcx>, Ty<'tcx>)>>, NoSolution> { ) -> Result<Option<ty::Binder<'tcx, (Ty<'tcx>, Ty<'tcx>)>>, NoSolution> {
match *self_ty.kind() { match *self_ty.kind() {
ty::FnDef(def_id, substs) => Ok(Some( // keep this in sync with assemble_fn_pointer_candidates until the old solver is removed.
tcx.fn_sig(def_id) ty::FnDef(def_id, substs) => {
.subst(tcx, substs) let sig = tcx.fn_sig(def_id);
if sig.skip_binder().is_fn_trait_compatible()
&& tcx.codegen_fn_attrs(def_id).target_features.is_empty()
{
Ok(Some(
sig.subst(tcx, substs)
.map_bound(|sig| (tcx.mk_tup(sig.inputs()), sig.output())), .map_bound(|sig| (tcx.mk_tup(sig.inputs()), sig.output())),
)), ))
} else {
Err(NoSolution)
}
}
// keep this in sync with assemble_fn_pointer_candidates until the old solver is removed. // keep this in sync with assemble_fn_pointer_candidates until the old solver is removed.
ty::FnPtr(sig) => { ty::FnPtr(sig) => {
if sig.is_fn_trait_compatible() { if sig.is_fn_trait_compatible() {

View file

@ -21,8 +21,12 @@ fn main() {
//~^ ERROR: expected a `Fn<()>` closure, found `unsafe fn() -> i32` //~^ ERROR: expected a `Fn<()>` closure, found `unsafe fn() -> i32`
//~| ERROR: type mismatch resolving `<unsafe fn() -> i32 as FnOnce<()>>::Output == i32` //~| ERROR: type mismatch resolving `<unsafe fn() -> i32 as FnOnce<()>>::Output == i32`
require_fn(g); require_fn(g);
//~^ ERROR: expected a `Fn<()>` closure, found `extern "C" fn() -> i32 {g}`
//~| ERROR: type mismatch resolving `<extern "C" fn() -> i32 {g} as FnOnce<()>>::Output == i32`
require_fn(g as extern "C" fn() -> i32); require_fn(g as extern "C" fn() -> i32);
//~^ ERROR: expected a `Fn<()>` closure, found `extern "C" fn() -> i32` //~^ ERROR: expected a `Fn<()>` closure, found `extern "C" fn() -> i32`
//~| ERROR: type mismatch resolving `<extern "C" fn() -> i32 as FnOnce<()>>::Output == i32` //~| ERROR: type mismatch resolving `<extern "C" fn() -> i32 as FnOnce<()>>::Output == i32`
require_fn(h); require_fn(h);
//~^ ERROR: expected a `Fn<()>` closure, found `unsafe fn() -> i32 {h}`
//~| ERROR: type mismatch resolving `<unsafe fn() -> i32 {h} as FnOnce<()>>::Output == i32`
} }

View file

@ -28,8 +28,38 @@ note: required by a bound in `require_fn`
LL | fn require_fn(_: impl Fn() -> i32) {} LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn` | ^^^ required by this bound in `require_fn`
error[E0277]: expected a `Fn<()>` closure, found `extern "C" fn() -> i32 {g}`
--> $DIR/fn-trait.rs:23:16
|
LL | require_fn(g);
| ---------- ^ expected an `Fn<()>` closure, found `extern "C" fn() -> i32 {g}`
| |
| required by a bound introduced by this call
|
= help: the trait `Fn<()>` is not implemented for fn item `extern "C" fn() -> i32 {g}`
= note: wrap the `extern "C" fn() -> i32 {g}` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:23
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^^^^^^^^^ required by this bound in `require_fn`
error[E0271]: type mismatch resolving `<extern "C" fn() -> i32 {g} as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:23:16
|
LL | require_fn(g);
| ---------- ^ types differ
| |
| required by a bound introduced by this call
|
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:31
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn`
error[E0277]: expected a `Fn<()>` closure, found `extern "C" fn() -> i32` error[E0277]: expected a `Fn<()>` closure, found `extern "C" fn() -> i32`
--> $DIR/fn-trait.rs:24:16 --> $DIR/fn-trait.rs:26:16
| |
LL | require_fn(g as extern "C" fn() -> i32); LL | require_fn(g as extern "C" fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn<()>` closure, found `extern "C" fn() -> i32` | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn<()>` closure, found `extern "C" fn() -> i32`
@ -45,7 +75,7 @@ LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^^^^^^^^^ required by this bound in `require_fn` | ^^^^^^^^^^^ required by this bound in `require_fn`
error[E0271]: type mismatch resolving `<extern "C" fn() -> i32 as FnOnce<()>>::Output == i32` error[E0271]: type mismatch resolving `<extern "C" fn() -> i32 as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:24:16 --> $DIR/fn-trait.rs:26:16
| |
LL | require_fn(g as extern "C" fn() -> i32); LL | require_fn(g as extern "C" fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ
@ -58,7 +88,37 @@ note: required by a bound in `require_fn`
LL | fn require_fn(_: impl Fn() -> i32) {} LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn` | ^^^ required by this bound in `require_fn`
error: aborting due to 4 previous errors error[E0277]: expected a `Fn<()>` closure, found `unsafe fn() -> i32 {h}`
--> $DIR/fn-trait.rs:29:16
|
LL | require_fn(h);
| ---------- ^ call the function in a closure: `|| unsafe { /* code */ }`
| |
| required by a bound introduced by this call
|
= help: the trait `Fn<()>` is not implemented for fn item `unsafe fn() -> i32 {h}`
= note: wrap the `unsafe fn() -> i32 {h}` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:23
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^^^^^^^^^ required by this bound in `require_fn`
error[E0271]: type mismatch resolving `<unsafe fn() -> i32 {h} as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:29:16
|
LL | require_fn(h);
| ---------- ^ types differ
| |
| required by a bound introduced by this call
|
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:31
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn`
error: aborting due to 8 previous errors
Some errors have detailed explanations: E0271, E0277. Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`. For more information about an error, try `rustc --explain E0271`.