Skip to content

Commit 66d7cfd

Browse files
Remove FnPtr hack from trait_ref_is_knowable
1 parent b706b9d commit 66d7cfd

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,25 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
238238
ecx: &mut EvalCtxt<'_, 'tcx>,
239239
goal: Goal<'tcx, Self>,
240240
) -> QueryResult<'tcx> {
241-
if goal.predicate.polarity != ty::ImplPolarity::Positive {
242-
return Err(NoSolution);
243-
}
244-
245-
if let ty::FnPtr(..) = goal.predicate.self_ty().kind() {
246-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
247-
} else {
248-
Err(NoSolution)
241+
let self_ty = goal.predicate.self_ty();
242+
match goal.predicate.polarity {
243+
ty::ImplPolarity::Positive => {
244+
if self_ty.is_fn_ptr() {
245+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
246+
} else {
247+
Err(NoSolution)
248+
}
249+
}
250+
ty::ImplPolarity::Negative => {
251+
// If a type is rigid and not a fn ptr, then we know for certain
252+
// that it does *not* implement `FnPtr`.
253+
if !self_ty.is_fn_ptr() && self_ty.is_known_rigid() {
254+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
255+
} else {
256+
Err(NoSolution)
257+
}
258+
}
259+
ty::ImplPolarity::Reservation => bug!(),
249260
}
250261
}
251262

compiler/rustc_trait_selection/src/traits/coherence.rs

-7
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,6 @@ pub fn trait_ref_is_knowable<'tcx, E: Debug>(
514514
trait_ref: ty::TraitRef<'tcx>,
515515
mut lazily_normalize_ty: impl FnMut(Ty<'tcx>) -> Result<Ty<'tcx>, E>,
516516
) -> Result<Result<(), Conflict>, E> {
517-
if Some(trait_ref.def_id) == tcx.lang_items().fn_ptr_trait() {
518-
// The only types implementing `FnPtr` are function pointers,
519-
// so if there's no impl of `FnPtr` in the current crate,
520-
// then such an impl will never be added in the future.
521-
return Ok(Ok(()));
522-
}
523-
524517
if orphan_check_trait_ref(trait_ref, InCrate::Remote, &mut lazily_normalize_ty)?.is_ok() {
525518
// A downstream or cousin crate is allowed to implement some
526519
// substitution of this trait-ref.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
5252

5353
let mut candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
5454

55-
// The only way to prove a NotImplemented(T: Foo) predicate is via a negative impl.
56-
// There are no compiler built-in rules for this.
55+
// Negative trait predicates have different rules than positive trait predicates.
5756
if obligation.polarity() == ty::ImplPolarity::Negative {
5857
self.assemble_candidates_for_trait_alias(obligation, &mut candidates);
5958
self.assemble_candidates_from_impls(obligation, &mut candidates);
@@ -1064,6 +1063,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10641063
candidates: &mut SelectionCandidateSet<'tcx>,
10651064
) {
10661065
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
1066+
10671067
match self_ty.skip_binder().kind() {
10681068
ty::FnPtr(_) => candidates.vec.push(BuiltinCandidate { has_nested: false }),
10691069
ty::Bool

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
#![feature(try_blocks)]
254254
#![feature(unboxed_closures)]
255255
#![feature(unsized_fn_params)]
256+
#![feature(with_negative_coherence)]
256257
// tidy-alphabetical-end
257258
//
258259
// Target features:

0 commit comments

Comments
 (0)