Rollup merge of #109441 - oli-obk:fn_trait_new_solver, r=compiler-errors

Only implement Fn* traits for extern "Rust" safe function pointers and items

Since calling the function via an `Fn` trait will assume `extern "Rust"` ABI and not do any safety checks, only safe `extern "Rust"` function can implement the `Fn` traits. This syncs the logic between the old solver and the new solver.

r? `@compiler-errors`
This commit is contained in:
Matthias Krüger 2023-03-21 19:00:14 +01:00 committed by GitHub
commit fef1fc4349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 189 additions and 28 deletions

View File

@ -23,7 +23,7 @@ use rustc_macros::HashStable;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use rustc_target::abi::VariantIdx;
use rustc_target::spec::abi;
use rustc_target::spec::abi::{self, Abi};
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt;
@ -1403,6 +1403,18 @@ impl<'tcx> PolyFnSig<'tcx> {
pub fn abi(&self) -> abi::Abi {
self.skip_binder().abi
}
pub fn is_fn_trait_compatible(&self) -> bool {
matches!(
self.skip_binder(),
ty::FnSig {
unsafety: rustc_hir::Unsafety::Normal,
abi: Abi::Rust,
c_variadic: false,
..
}
)
}
}
pub type CanonicalPolyFnSig<'tcx> = Canonical<'tcx, Binder<'tcx, FnSig<'tcx>>>;

View File

@ -189,12 +189,28 @@ pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
goal_kind: ty::ClosureKind,
) -> Result<Option<ty::Binder<'tcx, (Ty<'tcx>, Ty<'tcx>)>>, NoSolution> {
match *self_ty.kind() {
ty::FnDef(def_id, substs) => Ok(Some(
tcx.fn_sig(def_id)
.subst(tcx, substs)
.map_bound(|sig| (tcx.mk_tup(sig.inputs()), sig.output())),
)),
ty::FnPtr(sig) => Ok(Some(sig.map_bound(|sig| (tcx.mk_tup(sig.inputs()), sig.output())))),
// keep this in sync with assemble_fn_pointer_candidates until the old solver is removed.
ty::FnDef(def_id, 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())),
))
} else {
Err(NoSolution)
}
}
// keep this in sync with assemble_fn_pointer_candidates until the old solver is removed.
ty::FnPtr(sig) => {
if sig.is_fn_trait_compatible() {
Ok(Some(sig.map_bound(|sig| (tcx.mk_tup(sig.inputs()), sig.output()))))
} else {
Err(NoSolution)
}
}
ty::Closure(_, substs) => {
let closure_substs = substs.as_closure();
match closure_substs.kind_ty().to_opt_closure_kind() {

View File

@ -11,7 +11,6 @@ use rustc_infer::traits::ObligationCause;
use rustc_infer::traits::{Obligation, SelectionError, TraitObligation};
use rustc_middle::ty::fast_reject::TreatProjections;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
use rustc_target::spec::abi::Abi;
use crate::traits;
use crate::traits::query::evaluate_obligation::InferCtxtExt;
@ -291,6 +290,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
return;
}
// Keep this funtion in sync with extract_tupled_inputs_and_output_from_callable
// until the old solver (and thus this function) is removed.
// Okay to skip binder because what we are inspecting doesn't involve bound regions.
let self_ty = obligation.self_ty().skip_binder();
match *self_ty.kind() {
@ -299,31 +301,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
candidates.ambiguous = true; // Could wind up being a fn() type.
}
// Provide an impl, but only for suitable `fn` pointers.
ty::FnPtr(_) => {
if let ty::FnSig {
unsafety: hir::Unsafety::Normal,
abi: Abi::Rust,
c_variadic: false,
..
} = self_ty.fn_sig(self.tcx()).skip_binder()
{
ty::FnPtr(sig) => {
if sig.is_fn_trait_compatible() {
candidates.vec.push(FnPointerCandidate { is_const: false });
}
}
// Provide an impl for suitable functions, rejecting `#[target_feature]` functions (RFC 2396).
ty::FnDef(def_id, _) => {
if let ty::FnSig {
unsafety: hir::Unsafety::Normal,
abi: Abi::Rust,
c_variadic: false,
..
} = self_ty.fn_sig(self.tcx()).skip_binder()
if self.tcx().fn_sig(def_id).skip_binder().is_fn_trait_compatible()
&& self.tcx().codegen_fn_attrs(def_id).target_features.is_empty()
{
if self.tcx().codegen_fn_attrs(def_id).target_features.is_empty() {
candidates
.vec
.push(FnPointerCandidate { is_const: self.tcx().is_const_fn(def_id) });
}
candidates
.vec
.push(FnPointerCandidate { is_const: self.tcx().is_const_fn(def_id) });
}
}
_ => {}

View File

@ -1,5 +1,4 @@
// compile-flags: -Ztrait-solver=next
// check-pass
fn require_fn(_: impl Fn() -> i32) {}
@ -7,7 +6,27 @@ fn f() -> i32 {
1i32
}
extern "C" fn g() -> i32 {
2i32
}
unsafe fn h() -> i32 {
2i32
}
fn main() {
require_fn(f);
require_fn(f as fn() -> i32);
require_fn(f as unsafe fn() -> i32);
//~^ ERROR: expected a `Fn<()>` closure, found `unsafe fn() -> i32`
//~| ERROR: type mismatch resolving `<unsafe fn() -> i32 as FnOnce<()>>::Output == i32`
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);
//~^ ERROR: expected a `Fn<()>` closure, found `extern "C" fn() -> i32`
//~| ERROR: type mismatch resolving `<extern "C" fn() -> i32 as FnOnce<()>>::Output == i32`
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

@ -0,0 +1,124 @@
error[E0277]: expected a `Fn<()>` closure, found `unsafe fn() -> i32`
--> $DIR/fn-trait.rs:20:16
|
LL | require_fn(f as unsafe fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
| |
| required by a bound introduced by this call
|
= help: the trait `Fn<()>` is not implemented for `unsafe fn() -> i32`
= note: wrap the `unsafe fn() -> i32` 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 as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:20:16
|
LL | require_fn(f as unsafe fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^ 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 {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`
--> $DIR/fn-trait.rs:26:16
|
LL | require_fn(g as extern "C" fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn<()>` closure, found `extern "C" fn() -> i32`
| |
| required by a bound introduced by this call
|
= help: the trait `Fn<()>` is not implemented for `extern "C" fn() -> i32`
= note: wrap the `extern "C" fn() -> i32` 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 as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:26:16
|
LL | require_fn(g as extern "C" fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 `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.
For more information about an error, try `rustc --explain E0271`.