Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5520ecc

Browse files
committedAug 7, 2024
Use DeepRejectCtxt to quickly reject ParamEnv candidates
1 parent 595316b commit 5520ecc

File tree

13 files changed

+171
-195
lines changed

13 files changed

+171
-195
lines changed
 

‎compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ impl<'tcx> InherentCollect<'tcx> {
9090
}
9191
}
9292

93-
if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) {
93+
if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer)
94+
{
9495
self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id);
9596
} else {
9697
bug!("unexpected self type: {:?}", self_ty);
@@ -129,7 +130,7 @@ impl<'tcx> InherentCollect<'tcx> {
129130
}
130131
}
131132

132-
if let Some(simp) = simplify_type(self.tcx, ty, TreatParams::AsCandidateKey) {
133+
if let Some(simp) = simplify_type(self.tcx, ty, TreatParams::InstantiateWithInfer) {
133134
self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id);
134135
} else {
135136
bug!("unexpected primitive type: {:?}", ty);

‎compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
714714
}
715715

716716
fn assemble_inherent_candidates_for_incoherent_ty(&mut self, self_ty: Ty<'tcx>) {
717-
let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) else {
717+
let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) else {
718718
bug!("unexpected incoherent type: {:?}", self_ty)
719719
};
720720
for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter().flatten() {

‎compiler/rustc_hir_typeck/src/method/suggest.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -2235,8 +2235,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22352235
let target_ty = self
22362236
.autoderef(sugg_span, rcvr_ty)
22372237
.find(|(rcvr_ty, _)| {
2238-
DeepRejectCtxt::new(self.tcx, TreatParams::ForLookup)
2239-
.types_may_unify(*rcvr_ty, impl_ty)
2238+
DeepRejectCtxt::new(
2239+
self.tcx,
2240+
TreatParams::AsRigid,
2241+
TreatParams::InstantiateWithInfer,
2242+
)
2243+
.types_may_unify(*rcvr_ty, impl_ty)
22402244
})
22412245
.map_or(impl_ty, |(ty, _)| ty)
22422246
.peel_refs();
@@ -2498,7 +2502,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24982502
.into_iter()
24992503
.any(|info| self.associated_value(info.def_id, item_name).is_some());
25002504
let found_assoc = |ty: Ty<'tcx>| {
2501-
simplify_type(tcx, ty, TreatParams::AsCandidateKey)
2505+
simplify_type(tcx, ty, TreatParams::InstantiateWithInfer)
25022506
.and_then(|simp| {
25032507
tcx.incoherent_impls(simp)
25042508
.into_iter()
@@ -3928,7 +3932,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39283932
// cases where a positive bound implies a negative impl.
39293933
(candidates, Vec::new())
39303934
} else if let Some(simp_rcvr_ty) =
3931-
simplify_type(self.tcx, rcvr_ty, TreatParams::ForLookup)
3935+
simplify_type(self.tcx, rcvr_ty, TreatParams::AsRigid)
39323936
{
39333937
let mut potential_candidates = Vec::new();
39343938
let mut explicitly_negative = Vec::new();
@@ -3946,7 +3950,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39463950
.any(|header| {
39473951
let imp = header.trait_ref.instantiate_identity();
39483952
let imp_simp =
3949-
simplify_type(self.tcx, imp.self_ty(), TreatParams::ForLookup);
3953+
simplify_type(self.tcx, imp.self_ty(), TreatParams::AsRigid);
39503954
imp_simp.is_some_and(|s| s == simp_rcvr_ty)
39513955
})
39523956
{

‎compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20172017
let simplified_self_ty = fast_reject::simplify_type(
20182018
self.tcx,
20192019
trait_ref.self_ty(),
2020-
TreatParams::AsCandidateKey,
2020+
TreatParams::InstantiateWithInfer,
20212021
);
20222022
trait_impls
20232023
.entry(trait_ref.def_id)

‎compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
437437
let simp = ty::fast_reject::simplify_type(
438438
tcx,
439439
self_ty,
440-
ty::fast_reject::TreatParams::ForLookup,
440+
ty::fast_reject::TreatParams::AsRigid,
441441
)
442442
.unwrap();
443443
consider_impls_for_simplified_type(simp);

‎compiler/rustc_middle/src/ty/trait_def.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ impl<'tcx> TyCtxt<'tcx> {
168168
// whose outer level is not a parameter or projection. Especially for things like
169169
// `T: Clone` this is incredibly useful as we would otherwise look at all the impls
170170
// of `Clone` for `Option<T>`, `Vec<T>`, `ConcreteType` and so on.
171-
// Note that we're using `TreatParams::ForLookup` to query `non_blanket_impls` while using
172-
// `TreatParams::AsCandidateKey` while actually adding them.
173-
if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::ForLookup) {
171+
// Note that we're using `TreatParams::AsRigid` to query `non_blanket_impls` while using
172+
// `TreatParams::InstantiateWithInfer` while actually adding them.
173+
if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsRigid) {
174174
if let Some(impls) = impls.non_blanket_impls.get(&simp) {
175175
for &impl_def_id in impls {
176176
f(impl_def_id);
@@ -190,7 +190,9 @@ impl<'tcx> TyCtxt<'tcx> {
190190
self_ty: Ty<'tcx>,
191191
) -> impl Iterator<Item = DefId> + 'tcx {
192192
let impls = self.trait_impls_of(trait_def_id);
193-
if let Some(simp) = fast_reject::simplify_type(self, self_ty, TreatParams::AsCandidateKey) {
193+
if let Some(simp) =
194+
fast_reject::simplify_type(self, self_ty, TreatParams::InstantiateWithInfer)
195+
{
194196
if let Some(impls) = impls.non_blanket_impls.get(&simp) {
195197
return impls.iter().copied();
196198
}
@@ -239,7 +241,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
239241
let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity();
240242

241243
if let Some(simplified_self_ty) =
242-
fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::AsCandidateKey)
244+
fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::InstantiateWithInfer)
243245
{
244246
impls.non_blanket_impls.entry(simplified_self_ty).or_default().push(impl_def_id);
245247
} else {

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@ where
144144

145145
let goal_trait_ref = goal.predicate.alias.trait_ref(cx);
146146
let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
147-
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::ForLookup).args_may_unify(
148-
goal.predicate.alias.trait_ref(cx).args,
149-
impl_trait_ref.skip_binder().args,
150-
) {
147+
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::AsRigid, TreatParams::InstantiateWithInfer)
148+
.args_may_unify(
149+
goal.predicate.alias.trait_ref(cx).args,
150+
impl_trait_ref.skip_binder().args,
151+
)
152+
{
151153
return Err(NoSolution);
152154
}
153155

‎compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ where
4747
let cx = ecx.cx();
4848

4949
let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
50-
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::ForLookup)
50+
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::AsRigid, TreatParams::InstantiateWithInfer)
5151
.args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args)
5252
{
5353
return Err(NoSolution);
@@ -111,6 +111,15 @@ where
111111
if trait_clause.def_id() == goal.predicate.def_id()
112112
&& trait_clause.polarity() == goal.predicate.polarity
113113
{
114+
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::AsRigid, TreatParams::AsRigid)
115+
.args_may_unify(
116+
goal.predicate.trait_ref.args,
117+
trait_clause.skip_binder().trait_ref.args,
118+
)
119+
{
120+
return Err(NoSolution);
121+
}
122+
114123
ecx.probe_trait_candidate(source).enter(|ecx| {
115124
let assumption_trait_pred = ecx.instantiate_binder_with_infer(trait_clause);
116125
ecx.eq(

‎compiler/rustc_trait_selection/src/traits/coherence.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ pub fn overlapping_impls(
9494
// Before doing expensive operations like entering an inference context, do
9595
// a quick check via fast_reject to tell if the impl headers could possibly
9696
// unify.
97-
let drcx = DeepRejectCtxt::new(tcx, TreatParams::AsCandidateKey);
97+
let drcx = DeepRejectCtxt::new(
98+
tcx,
99+
TreatParams::InstantiateWithInfer,
100+
TreatParams::InstantiateWithInfer,
101+
);
98102
let impl1_ref = tcx.impl_trait_ref(impl1_def_id);
99103
let impl2_ref = tcx.impl_trait_ref(impl2_def_id);
100104
let may_overlap = match (impl1_ref, impl2_ref) {

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
247247
.filter(|p| p.def_id() == stack.obligation.predicate.def_id())
248248
.filter(|p| p.polarity() == stack.obligation.predicate.polarity());
249249

250+
let drcx = DeepRejectCtxt::new(self.tcx(), TreatParams::AsRigid, TreatParams::AsRigid);
251+
let obligation_args = stack.obligation.predicate.skip_binder().trait_ref.args;
250252
// Keep only those bounds which may apply, and propagate overflow if it occurs.
251253
for bound in bounds {
254+
let bound_trait_ref = bound.map_bound(|t| t.trait_ref);
255+
if !drcx.args_may_unify(obligation_args, bound_trait_ref.skip_binder().args) {
256+
continue;
257+
}
258+
252259
// FIXME(oli-obk): it is suspicious that we are dropping the constness and
253260
// polarity here.
254-
let wc = self.where_clause_may_apply(stack, bound.map_bound(|t| t.trait_ref))?;
261+
let wc = self.where_clause_may_apply(stack, bound_trait_ref)?;
255262
if wc.may_apply() {
256263
candidates.vec.push(ParamCandidate(bound));
257264
}
@@ -568,7 +575,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
568575
return;
569576
}
570577

571-
let drcx = DeepRejectCtxt::new(self.tcx(), TreatParams::ForLookup);
578+
let drcx = DeepRejectCtxt::new(
579+
self.tcx(),
580+
TreatParams::AsRigid,
581+
TreatParams::InstantiateWithInfer,
582+
);
572583
let obligation_args = obligation.predicate.skip_binder().trait_ref.args;
573584
self.tcx().for_each_relevant_impl(
574585
obligation.predicate.def_id(),

‎compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'tcx> Children {
4040
fn insert_blindly(&mut self, tcx: TyCtxt<'tcx>, impl_def_id: DefId) {
4141
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder();
4242
if let Some(st) =
43-
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey)
43+
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::InstantiateWithInfer)
4444
{
4545
debug!("insert_blindly: impl_def_id={:?} st={:?}", impl_def_id, st);
4646
self.non_blanket_impls.entry(st).or_default().push(impl_def_id)
@@ -57,7 +57,7 @@ impl<'tcx> Children {
5757
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().skip_binder();
5858
let vec: &mut Vec<DefId>;
5959
if let Some(st) =
60-
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey)
60+
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::InstantiateWithInfer)
6161
{
6262
debug!("remove_existing: impl_def_id={:?} st={:?}", impl_def_id, st);
6363
vec = self.non_blanket_impls.get_mut(&st).unwrap();
@@ -278,7 +278,7 @@ impl<'tcx> Graph {
278278
let mut parent = trait_def_id;
279279
let mut last_lint = None;
280280
let simplified =
281-
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::AsCandidateKey);
281+
fast_reject::simplify_type(tcx, trait_ref.self_ty(), TreatParams::InstantiateWithInfer);
282282

283283
// Descend the specialization tree, where `parent` is the current parent node.
284284
loop {
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.