Skip to content

Commit 91d9131

Browse files
committed
Deduplicate fn trait compatibility checks
1 parent fb9e171 commit 91d9131

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_macros::HashStable;
2323
use rustc_span::symbol::{kw, sym, Symbol};
2424
use rustc_span::Span;
2525
use rustc_target::abi::VariantIdx;
26-
use rustc_target::spec::abi;
26+
use rustc_target::spec::abi::{self, Abi};
2727
use std::borrow::Cow;
2828
use std::cmp::Ordering;
2929
use std::fmt;
@@ -1403,6 +1403,18 @@ impl<'tcx> PolyFnSig<'tcx> {
14031403
pub fn abi(&self) -> abi::Abi {
14041404
self.skip_binder().abi
14051405
}
1406+
1407+
pub fn is_fn_trait_compatible(&self) -> bool {
1408+
matches!(
1409+
self.skip_binder(),
1410+
ty::FnSig {
1411+
unsafety: rustc_hir::Unsafety::Normal,
1412+
abi: Abi::Rust,
1413+
c_variadic: false,
1414+
..
1415+
}
1416+
)
1417+
}
14061418
}
14071419

14081420
pub type CanonicalPolyFnSig<'tcx> = Canonical<'tcx, Binder<'tcx, FnSig<'tcx>>>;

compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc_data_structures::fx::FxHashMap;
22
use rustc_hir::{def_id::DefId, Movability, Mutability};
33
use rustc_infer::traits::query::NoSolution;
44
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable};
5-
use rustc_target::spec::abi::Abi;
65

76
use crate::solve::EvalCtxt;
87

@@ -197,13 +196,7 @@ pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
197196
)),
198197
// keep this in sync with assemble_fn_pointer_candidates until the old solver is removed.
199198
ty::FnPtr(sig) => {
200-
if let ty::FnSig {
201-
unsafety: rustc_hir::Unsafety::Normal,
202-
abi: Abi::Rust,
203-
c_variadic: false,
204-
..
205-
} = sig.skip_binder()
206-
{
199+
if sig.is_fn_trait_compatible() {
207200
Ok(Some(sig.map_bound(|sig| (tcx.mk_tup(sig.inputs()), sig.output()))))
208201
} else {
209202
Err(NoSolution)

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_infer::traits::ObligationCause;
1111
use rustc_infer::traits::{Obligation, SelectionError, TraitObligation};
1212
use rustc_middle::ty::fast_reject::TreatProjections;
1313
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
14-
use rustc_target::spec::abi::Abi;
1514

1615
use crate::traits;
1716
use crate::traits::query::evaluate_obligation::InferCtxtExt;
@@ -302,31 +301,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
302301
candidates.ambiguous = true; // Could wind up being a fn() type.
303302
}
304303
// Provide an impl, but only for suitable `fn` pointers.
305-
ty::FnPtr(_) => {
306-
if let ty::FnSig {
307-
unsafety: hir::Unsafety::Normal,
308-
abi: Abi::Rust,
309-
c_variadic: false,
310-
..
311-
} = self_ty.fn_sig(self.tcx()).skip_binder()
312-
{
304+
ty::FnPtr(sig) => {
305+
if sig.is_fn_trait_compatible() {
313306
candidates.vec.push(FnPointerCandidate { is_const: false });
314307
}
315308
}
316309
// Provide an impl for suitable functions, rejecting `#[target_feature]` functions (RFC 2396).
317310
ty::FnDef(def_id, _) => {
318-
if let ty::FnSig {
319-
unsafety: hir::Unsafety::Normal,
320-
abi: Abi::Rust,
321-
c_variadic: false,
322-
..
323-
} = self_ty.fn_sig(self.tcx()).skip_binder()
311+
if self.tcx().fn_sig(def_id).skip_binder().is_fn_trait_compatible()
312+
&& self.tcx().codegen_fn_attrs(def_id).target_features.is_empty()
324313
{
325-
if self.tcx().codegen_fn_attrs(def_id).target_features.is_empty() {
326-
candidates
327-
.vec
328-
.push(FnPointerCandidate { is_const: self.tcx().is_const_fn(def_id) });
329-
}
314+
candidates
315+
.vec
316+
.push(FnPointerCandidate { is_const: self.tcx().is_const_fn(def_id) });
330317
}
331318
}
332319
_ => {}

0 commit comments

Comments
 (0)