Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ae80c76

Browse files
committed
Add an always-ambiguous predicate to make sure that we don't accidentlally allow trait resolution to prove false things during coherence
1 parent 94fe30f commit ae80c76

File tree

35 files changed

+102
-17
lines changed

35 files changed

+102
-17
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRe
22
use rustc_infer::infer::NllRegionVariableOrigin;
33
use rustc_infer::traits::PredicateObligations;
44
use rustc_middle::mir::ConstraintCategory;
5-
use rustc_middle::ty::error::TypeError;
65
use rustc_middle::ty::relate::TypeRelation;
76
use rustc_middle::ty::{self, Const, Ty};
87
use rustc_span::Span;
@@ -156,10 +155,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
156155
true
157156
}
158157

159-
fn register_opaque_type_obligations(
160-
&mut self,
161-
obligations: PredicateObligations<'tcx>,
162-
) -> Result<(), TypeError<'tcx>> {
158+
fn register_opaque_type_obligations(&mut self, obligations: PredicateObligations<'tcx>) {
163159
self.type_checker
164160
.fully_perform_op(
165161
self.locations,
@@ -172,6 +168,5 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
172168
},
173169
)
174170
.unwrap();
175-
Ok(())
176171
}
177172
}

compiler/rustc_hir_analysis/src/check/dropck.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ impl<'tcx> TypeRelation<'tcx> for SimpleEqRelation<'tcx> {
260260
true
261261
}
262262

263+
fn mark_ambiguous(&mut self) {
264+
bug!()
265+
}
266+
263267
fn relate_with_variance<T: Relate<'tcx>>(
264268
&mut self,
265269
_: ty::Variance,

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ fn trait_predicate_kind<'tcx>(
517517
| ty::PredicateKind::ClosureKind(..)
518518
| ty::PredicateKind::ConstEvaluatable(..)
519519
| ty::PredicateKind::ConstEquate(..)
520+
| ty::PredicateKind::Ambiguous
520521
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
521522
}
522523
}

compiler/rustc_hir_analysis/src/outlives/explicit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> {
5959
| ty::PredicateKind::Coerce(..)
6060
| ty::PredicateKind::ConstEvaluatable(..)
6161
| ty::PredicateKind::ConstEquate(..)
62+
| ty::PredicateKind::Ambiguous
6263
| ty::PredicateKind::TypeWellFormedFromEnv(..) => (),
6364
}
6465
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
702702
// code is looking for a self type of an unresolved
703703
// inference variable.
704704
| ty::PredicateKind::ClosureKind(..)
705+
| ty::PredicateKind::Ambiguous
705706
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
706707
},
707708
)

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
803803
| ty::PredicateKind::TypeOutlives(..)
804804
| ty::PredicateKind::ConstEvaluatable(..)
805805
| ty::PredicateKind::ConstEquate(..)
806+
| ty::PredicateKind::Ambiguous
806807
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
807808
}
808809
});

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_index::vec::Idx;
2323
use rustc_index::vec::IndexVec;
2424
use rustc_middle::arena::ArenaAllocatable;
2525
use rustc_middle::mir::ConstraintCategory;
26-
use rustc_middle::ty::error::TypeError;
2726
use rustc_middle::ty::fold::TypeFoldable;
2827
use rustc_middle::ty::relate::TypeRelation;
2928
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
@@ -741,11 +740,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
741740
true
742741
}
743742

744-
fn register_opaque_type_obligations(
745-
&mut self,
746-
obligations: PredicateObligations<'tcx>,
747-
) -> Result<(), TypeError<'tcx>> {
743+
fn register_opaque_type_obligations(&mut self, obligations: PredicateObligations<'tcx>) {
748744
self.obligations.extend(obligations);
749-
Ok(())
750745
}
751746
}

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,15 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
450450
ty::Binder::dummy(predicate),
451451
));
452452
}
453+
454+
pub fn mark_ambiguous(&mut self) {
455+
self.obligations.push(Obligation::new(
456+
self.tcx(),
457+
self.trace.cause.clone(),
458+
self.param_env,
459+
ty::Binder::dummy(ty::PredicateKind::Ambiguous),
460+
));
461+
}
453462
}
454463

455464
struct Generalizer<'cx, 'tcx> {
@@ -538,6 +547,11 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
538547
true
539548
}
540549

550+
fn mark_ambiguous(&mut self) {
551+
// The generalizer always compares types against themselves,
552+
// and thus doesn't really take part in coherence.
553+
}
554+
541555
fn binders<T>(
542556
&mut self,
543557
a: ty::Binder<'tcx, T>,
@@ -820,6 +834,10 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
820834
true
821835
}
822836

837+
fn mark_ambiguous(&mut self) {
838+
bug!()
839+
}
840+
823841
fn relate_with_variance<T: Relate<'tcx>>(
824842
&mut self,
825843
_variance: ty::Variance,

compiler/rustc_infer/src/infer/equate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
4444
self.a_is_expected
4545
}
4646

47+
fn mark_ambiguous(&mut self) {
48+
self.fields.mark_ambiguous();
49+
}
50+
4751
fn relate_item_substs(
4852
&mut self,
4953
_item_def_id: DefId,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,6 +2954,10 @@ impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
29542954
true
29552955
}
29562956

2957+
fn mark_ambiguous(&mut self) {
2958+
bug!()
2959+
}
2960+
29572961
fn relate_with_variance<T: relate::Relate<'tcx>>(
29582962
&mut self,
29592963
_variance: ty::Variance,

0 commit comments

Comments
 (0)