Skip to content

Commit 1776c24

Browse files
committed
Auto merge of rust-lang#129051 - Bryanskiy:deep-reject-ctxt-refactor, r=<try>
Make `DeepRejectCtxt` symmetric with respect to `TreatParams` The PR was split from rust-lang#128776 r? `@lcnr`
2 parents 8fbdc04 + 35ca8f8 commit 1776c24

File tree

15 files changed

+218
-206
lines changed

15 files changed

+218
-206
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

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::lang_items::LangItem;
2121
use rustc_hir::{self as hir, ExprKind, HirId, Node, PathSegment, QPath};
2222
use rustc_infer::infer::{self, RegionVariableOrigin};
2323
use rustc_middle::bug;
24-
use rustc_middle::ty::fast_reject::{simplify_type, DeepRejectCtxt, TreatParams};
24+
use rustc_middle::ty::fast_reject::{new_reject_ctxt, simplify_type, DeepRejectCtxt, TreatParams};
2525
use rustc_middle::ty::print::{
2626
with_crate_prefix, with_forced_trimmed_paths, PrintTraitRefExt as _,
2727
};
@@ -2234,7 +2234,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22342234
let target_ty = self
22352235
.autoderef(sugg_span, rcvr_ty)
22362236
.find(|(rcvr_ty, _)| {
2237-
DeepRejectCtxt::new(self.tcx, TreatParams::ForLookup)
2237+
new_reject_ctxt!(self.tcx, AsRigid, InstantiateWithInfer)
22382238
.types_may_unify(*rcvr_ty, impl_ty)
22392239
})
22402240
.map_or(impl_ty, |(ty, _)| ty)
@@ -2497,7 +2497,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24972497
.into_iter()
24982498
.any(|info| self.associated_value(info.def_id, item_name).is_some());
24992499
let found_assoc = |ty: Ty<'tcx>| {
2500-
simplify_type(tcx, ty, TreatParams::AsCandidateKey)
2500+
simplify_type(tcx, ty, TreatParams::InstantiateWithInfer)
25012501
.and_then(|simp| {
25022502
tcx.incoherent_impls(simp)
25032503
.into_iter()
@@ -3927,7 +3927,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39273927
// cases where a positive bound implies a negative impl.
39283928
(candidates, Vec::new())
39293929
} else if let Some(simp_rcvr_ty) =
3930-
simplify_type(self.tcx, rcvr_ty, TreatParams::ForLookup)
3930+
simplify_type(self.tcx, rcvr_ty, TreatParams::AsRigid)
39313931
{
39323932
let mut potential_candidates = Vec::new();
39333933
let mut explicitly_negative = Vec::new();
@@ -3945,7 +3945,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39453945
.any(|header| {
39463946
let imp = header.trait_ref.instantiate_identity();
39473947
let imp_simp =
3948-
simplify_type(self.tcx, imp.self_ty(), TreatParams::ForLookup);
3948+
simplify_type(self.tcx, imp.self_ty(), TreatParams::AsRigid);
39493949
imp_simp.is_some_and(|s| s == simp_rcvr_ty)
39503950
})
39513951
{

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
@@ -428,7 +428,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
428428
let simp = ty::fast_reject::simplify_type(
429429
tcx,
430430
self_ty,
431-
ty::fast_reject::TreatParams::ForLookup,
431+
ty::fast_reject::TreatParams::AsRigid,
432432
)
433433
.unwrap();
434434
consider_impls_for_simplified_type(simp);
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_hir::def_id::DefId;
22
pub use rustc_type_ir::fast_reject::*;
3+
pub use rustc_type_ir::new_reject_ctxt;
34

4-
use super::TyCtxt;
5-
6-
pub type DeepRejectCtxt<'tcx> = rustc_type_ir::fast_reject::DeepRejectCtxt<TyCtxt<'tcx>>;
5+
pub type DeepRejectCtxt<I, const LHS: bool, const RHS: bool> =
6+
rustc_type_ir::fast_reject::DeepRejectCtxt<I, LHS, RHS>;
77

88
pub type SimplifiedType = rustc_type_ir::fast_reject::SimplifiedType<DefId>;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod weak_types;
66
use rustc_type_ir::fast_reject::{DeepRejectCtxt, TreatParams};
77
use rustc_type_ir::inherent::*;
88
use rustc_type_ir::lang_items::TraitSolverLangItem;
9-
use rustc_type_ir::{self as ty, Interner, NormalizesTo, Upcast as _};
9+
use rustc_type_ir::{self as ty, new_reject_ctxt, Interner, NormalizesTo, Upcast as _};
1010
use tracing::instrument;
1111

1212
use crate::delegate::SolverDelegate;
@@ -144,7 +144,7 @@ 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(
147+
if !new_reject_ctxt!(ecx.cx(), AsRigid, InstantiateWithInfer).args_may_unify(
148148
goal.predicate.alias.trait_ref(cx).args,
149149
impl_trait_ref.skip_binder().args,
150150
) {

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use rustc_type_ir::fast_reject::{DeepRejectCtxt, TreatParams};
66
use rustc_type_ir::inherent::*;
77
use rustc_type_ir::lang_items::TraitSolverLangItem;
88
use rustc_type_ir::visit::TypeVisitableExt as _;
9-
use rustc_type_ir::{self as ty, elaborate, Interner, TraitPredicate, Upcast as _};
9+
use rustc_type_ir::{
10+
self as ty, elaborate, new_reject_ctxt, Interner, TraitPredicate, Upcast as _,
11+
};
1012
use tracing::{instrument, trace};
1113

1214
use crate::delegate::SolverDelegate;
@@ -47,7 +49,7 @@ where
4749
let cx = ecx.cx();
4850

4951
let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
50-
if !DeepRejectCtxt::new(ecx.cx(), TreatParams::ForLookup)
52+
if !new_reject_ctxt!(ecx.cx(), AsRigid, InstantiateWithInfer)
5153
.args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args)
5254
{
5355
return Err(NoSolution);

compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir as hir;
44
use rustc_hir::def::DefKind;
55
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
66
use rustc_middle::ty::error::{ExpectedFound, TypeError};
7-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
7+
use rustc_middle::ty::fast_reject::{new_reject_ctxt, DeepRejectCtxt, TreatParams};
88
use rustc_middle::ty::print::{FmtPrinter, Printer};
99
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty};
1010
use rustc_span::def_id::DefId;
@@ -316,7 +316,7 @@ impl<T> Trait<T> for X {
316316
{
317317
let mut has_matching_impl = false;
318318
tcx.for_each_relevant_impl(def_id, values.found, |did| {
319-
if DeepRejectCtxt::new(tcx, TreatParams::ForLookup)
319+
if new_reject_ctxt!(tcx, AsRigid, InstantiateWithInfer)
320320
.types_may_unify(values.found, tcx.type_of(did).skip_binder())
321321
{
322322
has_matching_impl = true;
@@ -337,7 +337,7 @@ impl<T> Trait<T> for X {
337337
{
338338
let mut has_matching_impl = false;
339339
tcx.for_each_relevant_impl(def_id, values.expected, |did| {
340-
if DeepRejectCtxt::new(tcx, TreatParams::ForLookup)
340+
if new_reject_ctxt!(tcx, AsRigid, InstantiateWithInfer)
341341
.types_may_unify(values.expected, tcx.type_of(did).skip_binder())
342342
{
343343
has_matching_impl = true;
@@ -357,7 +357,7 @@ impl<T> Trait<T> for X {
357357
{
358358
let mut has_matching_impl = false;
359359
tcx.for_each_relevant_impl(def_id, values.found, |did| {
360-
if DeepRejectCtxt::new(tcx, TreatParams::ForLookup)
360+
if new_reject_ctxt!(tcx, AsRigid, InstantiateWithInfer)
361361
.types_may_unify(values.found, tcx.type_of(did).skip_binder())
362362
{
363363
has_matching_impl = true;

compiler/rustc_trait_selection/src/traits/coherence.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::bug;
1515
use rustc_middle::traits::query::NoSolution;
1616
use rustc_middle::traits::solve::{CandidateSource, Certainty, Goal};
1717
use rustc_middle::traits::specialization_graph::OverlapMode;
18-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
18+
use rustc_middle::ty::fast_reject::{new_reject_ctxt, DeepRejectCtxt, TreatParams};
1919
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
2020
use rustc_middle::ty::{self, Ty, TyCtxt};
2121
pub use rustc_next_trait_solver::coherence::*;
@@ -94,7 +94,7 @@ 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 = new_reject_ctxt!(tcx, InstantiateWithInfer, InstantiateWithInfer);
9898
let impl1_ref = tcx.impl_trait_ref(impl1_def_id);
9999
let impl2_ref = tcx.impl_trait_ref(impl2_def_id);
100100
let may_overlap = match (impl1_ref, impl2_ref) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use hir::LangItem;
1313
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
1414
use rustc_hir as hir;
1515
use rustc_infer::traits::{Obligation, ObligationCause, PolyTraitObligation, SelectionError};
16-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
16+
use rustc_middle::ty::fast_reject::{new_reject_ctxt, DeepRejectCtxt, TreatParams};
1717
use rustc_middle::ty::{self, ToPolyTraitRef, Ty, TypeVisitableExt};
1818
use rustc_middle::{bug, span_bug};
1919

@@ -580,7 +580,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
580580
return;
581581
}
582582

583-
let drcx = DeepRejectCtxt::new(self.tcx(), TreatParams::ForLookup);
583+
let drcx = new_reject_ctxt!(self.tcx(), AsRigid, InstantiateWithInfer);
584584
let obligation_args = obligation.predicate.skip_binder().trait_ref.args;
585585
self.tcx().for_each_relevant_impl(
586586
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 {

0 commit comments

Comments
 (0)