Skip to content

Commit 4e8e894

Browse files
committed
require a tcx for TypeVisitor
1 parent 78c87fc commit 4e8e894

File tree

23 files changed

+175
-54
lines changed

23 files changed

+175
-54
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15251525
}
15261526

15271527
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
1528+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
1529+
self.tcx
1530+
}
1531+
15281532
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
15291533
if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
15301534
let span = self.tcx.def_span(def_id);

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorRepor
88
use rustc_hir::def_id::DefId;
99
use rustc_hir::intravisit::{walk_ty, ErasedMap, NestedVisitorMap, Visitor};
1010
use rustc_hir::{self as hir, GenericBound, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind};
11-
use rustc_middle::ty::{self, AssocItemContainer, RegionKind, Ty, TypeFoldable, TypeVisitor};
11+
use rustc_middle::ty::{
12+
self, AssocItemContainer, RegionKind, Ty, TyCtxt, TypeFoldable, TypeVisitor,
13+
};
1214
use rustc_span::symbol::Ident;
1315
use rustc_span::{MultiSpan, Span};
1416

@@ -470,8 +472,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
470472
/// Collect all the trait objects in a type that could have received an implicit `'static` lifetime.
471473
struct TraitObjectVisitor(Vec<DefId>);
472474

473-
impl TypeVisitor<'_> for TraitObjectVisitor {
474-
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<Self::BreakTy> {
475+
impl<'tcx> TypeVisitor<'tcx> for TraitObjectVisitor {
476+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
477+
bug!("tcx_for_anon_const_substs called for TraitObjectVisitor");
478+
}
479+
480+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
475481
match t.kind() {
476482
ty::Dynamic(preds, RegionKind::ReStatic) => {
477483
if let Some(def_id) = preds.principal_def_id() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ where
187187
};
188188

189189
value.skip_binder().visit_with(&mut ScopeInstantiator {
190+
tcx: self.infcx.tcx,
190191
next_region: &mut next_region,
191192
target_index: ty::INNERMOST,
192193
bound_region_scope: &mut scope,
@@ -735,13 +736,18 @@ where
735736
/// `for<..`>. For each of those, it creates an entry in
736737
/// `bound_region_scope`.
737738
struct ScopeInstantiator<'me, 'tcx> {
739+
tcx: TyCtxt<'tcx>,
738740
next_region: &'me mut dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
739741
// The debruijn index of the scope we are instantiating.
740742
target_index: ty::DebruijnIndex,
741743
bound_region_scope: &'me mut BoundRegionScope<'tcx>,
742744
}
743745

744746
impl<'me, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'me, 'tcx> {
747+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
748+
self.tcx
749+
}
750+
745751
fn visit_binder<T: TypeFoldable<'tcx>>(
746752
&mut self,
747753
t: &ty::Binder<T>,

compiler/rustc_infer/src/infer/resolve.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
121121

122122
impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
123123
type BreakTy = (Ty<'tcx>, Option<Span>);
124+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
125+
bug!("tcx_for_anon_const_substs called for UnresolvedTypeFinder");
126+
}
127+
124128
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
125129
let t = self.infcx.shallow_resolve(t);
126130
if t.has_infer_types() {

compiler/rustc_lint/src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11741174

11751175
impl<'a, 'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueTypes<'a, 'tcx> {
11761176
type BreakTy = Ty<'tcx>;
1177+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1178+
self.cx.tcx
1179+
}
11771180

11781181
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
11791182
match ty.kind() {

compiler/rustc_middle/src/ty/fold.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ pub trait TypeFolder<'tcx>: Sized {
183183

184184
pub trait TypeVisitor<'tcx>: Sized {
185185
type BreakTy = !;
186+
/// Supplies the `tcx` for an unevaluated anonymous constant in case its default substs
187+
/// are not yet supplied.
188+
///
189+
/// Visitors which do not look into these substs may leave this unimplemented, so be
190+
/// careful when calling this method elsewhere.
191+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx>;
186192

187193
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> ControlFlow<Self::BreakTy> {
188194
t.super_visit_with(self)
@@ -292,7 +298,8 @@ impl<'tcx> TyCtxt<'tcx> {
292298
value: &impl TypeFoldable<'tcx>,
293299
callback: impl FnMut(ty::Region<'tcx>) -> bool,
294300
) -> bool {
295-
struct RegionVisitor<F> {
301+
struct RegionVisitor<'tcx, F> {
302+
tcx: TyCtxt<'tcx>,
296303
/// The index of a binder *just outside* the things we have
297304
/// traversed. If we encounter a bound region bound by this
298305
/// binder or one outer to it, it appears free. Example:
@@ -314,12 +321,16 @@ impl<'tcx> TyCtxt<'tcx> {
314321
callback: F,
315322
}
316323

317-
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
324+
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<'tcx, F>
318325
where
319326
F: FnMut(ty::Region<'tcx>) -> bool,
320327
{
321328
type BreakTy = ();
322329

330+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
331+
self.tcx
332+
}
333+
323334
fn visit_binder<T: TypeFoldable<'tcx>>(
324335
&mut self,
325336
t: &Binder<T>,
@@ -355,7 +366,9 @@ impl<'tcx> TyCtxt<'tcx> {
355366
}
356367
}
357368

358-
value.visit_with(&mut RegionVisitor { outer_index: ty::INNERMOST, callback }).is_break()
369+
value
370+
.visit_with(&mut RegionVisitor { tcx: self, outer_index: ty::INNERMOST, callback })
371+
.is_break()
359372
}
360373
}
361374

@@ -655,7 +668,7 @@ impl<'tcx> TyCtxt<'tcx> {
655668
where
656669
T: TypeFoldable<'tcx>,
657670
{
658-
let mut collector = LateBoundRegionsCollector::new(just_constraint);
671+
let mut collector = LateBoundRegionsCollector::new(self, just_constraint);
659672
let result = value.as_ref().skip_binder().visit_with(&mut collector);
660673
assert!(result.is_continue()); // should never have stopped early
661674
collector.regions
@@ -830,6 +843,10 @@ struct HasEscapingVarsVisitor {
830843
impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
831844
type BreakTy = FoundEscapingVars;
832845

846+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
847+
bug!("tcx_for_anon_const_substs called for HasEscpaingVarsVisitor");
848+
}
849+
833850
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> ControlFlow<Self::BreakTy> {
834851
self.outer_index.shift_in(1);
835852
let result = t.super_visit_with(self);
@@ -897,6 +914,9 @@ struct HasTypeFlagsVisitor {
897914

898915
impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
899916
type BreakTy = FoundFlags;
917+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
918+
bug!("tcx_for_anon_const_substs called for HasTypeFlagsVisitor");
919+
}
900920

901921
#[inline]
902922
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<Self::BreakTy> {
@@ -951,7 +971,8 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
951971

952972
/// Collects all the late-bound regions at the innermost binding level
953973
/// into a hash set.
954-
struct LateBoundRegionsCollector {
974+
struct LateBoundRegionsCollector<'tcx> {
975+
tcx: TyCtxt<'tcx>,
955976
current_index: ty::DebruijnIndex,
956977
regions: FxHashSet<ty::BoundRegionKind>,
957978

@@ -965,17 +986,22 @@ struct LateBoundRegionsCollector {
965986
just_constrained: bool,
966987
}
967988

968-
impl LateBoundRegionsCollector {
969-
fn new(just_constrained: bool) -> Self {
989+
impl LateBoundRegionsCollector<'tcx> {
990+
fn new(tcx: TyCtxt<'tcx>, just_constrained: bool) -> Self {
970991
LateBoundRegionsCollector {
992+
tcx,
971993
current_index: ty::INNERMOST,
972994
regions: Default::default(),
973995
just_constrained,
974996
}
975997
}
976998
}
977999

978-
impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
1000+
impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector<'tcx> {
1001+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1002+
self.tcx
1003+
}
1004+
9791005
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> ControlFlow<Self::BreakTy> {
9801006
self.current_index.shift_in(1);
9811007
let result = t.super_visit_with(self);

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,18 +1840,22 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
18401840
where
18411841
T: TypeFoldable<'tcx>,
18421842
{
1843-
struct LateBoundRegionNameCollector<'a>(&'a mut FxHashSet<Symbol>);
1844-
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'_> {
1843+
struct LateBoundRegionNameCollector<'a, 'tcx>(TyCtxt<'tcx>, &'a mut FxHashSet<Symbol>);
1844+
impl<'a, 'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'a, 'tcx> {
1845+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1846+
self.0
1847+
}
1848+
18451849
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
18461850
if let ty::ReLateBound(_, ty::BoundRegion { kind: ty::BrNamed(_, name) }) = *r {
1847-
self.0.insert(name);
1851+
self.1.insert(name);
18481852
}
18491853
r.super_visit_with(self)
18501854
}
18511855
}
18521856

18531857
self.used_region_names.clear();
1854-
let mut collector = LateBoundRegionNameCollector(&mut self.used_region_names);
1858+
let mut collector = LateBoundRegionNameCollector(self.tcx, &mut self.used_region_names);
18551859
value.visit_with(&mut collector);
18561860
self.region_index = 0;
18571861
}

compiler/rustc_mir/src/interpret/util.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ where
2121
impl<'tcx> TypeVisitor<'tcx> for UsedParamsNeedSubstVisitor<'tcx> {
2222
type BreakTy = FoundParam;
2323

24+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
25+
self.tcx
26+
}
27+
2428
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
2529
if !c.needs_subst() {
2630
return ControlFlow::CONTINUE;

compiler/rustc_mir/src/monomorphize/polymorphize.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn mark_used_by_predicates<'tcx>(
178178
// Consider all generic params in a predicate as used if any other parameter in the
179179
// predicate is used.
180180
let any_param_used = {
181-
let mut vis = HasUsedGenericParams { unused_parameters };
181+
let mut vis = HasUsedGenericParams { tcx, unused_parameters };
182182
predicate.visit_with(&mut vis).is_break()
183183
};
184184

@@ -287,6 +287,9 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
287287
}
288288

289289
impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
290+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
291+
self.tcx
292+
}
290293
#[instrument(skip(self))]
291294
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<Self::BreakTy> {
292295
if !c.has_param_types_or_consts() {
@@ -350,13 +353,18 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
350353
}
351354

352355
/// Visitor used to check if a generic parameter is used.
353-
struct HasUsedGenericParams<'a> {
356+
struct HasUsedGenericParams<'a, 'tcx> {
357+
tcx: TyCtxt<'tcx>,
354358
unused_parameters: &'a FiniteBitSet<u32>,
355359
}
356360

357-
impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a> {
361+
impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a, 'tcx> {
358362
type BreakTy = ();
359363

364+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
365+
self.tcx
366+
}
367+
360368
#[instrument(skip(self))]
361369
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<Self::BreakTy> {
362370
if !c.has_param_types_or_consts() {

compiler/rustc_mir/src/util/pretty.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,10 @@ pub fn write_allocations<'tcx>(
661661
}
662662
struct CollectAllocIds(BTreeSet<AllocId>);
663663
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
664+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
665+
bug!("tcx_for_anon_const_substs called for CollectAllocIds")
666+
}
667+
664668
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
665669
if let ty::ConstKind::Value(val) = c.val {
666670
self.0.extend(alloc_ids_from_const(val));

compiler/rustc_privacy/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ where
178178
{
179179
type BreakTy = V::BreakTy;
180180

181+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
182+
self.def_id_visitor.tcx()
183+
}
184+
181185
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<V::BreakTy> {
182186
let tcx = self.def_id_visitor.tcx();
183187
// InternalSubsts are not visited here because they are visited below in `super_visit_with`.

compiler/rustc_trait_selection/src/opaque_types.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
442442

443443
for required_region in required_region_bounds {
444444
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
445+
tcx,
445446
op: |r| self.sub_regions(infer::CallReturn(span), required_region, r),
446447
});
447448
}
@@ -510,6 +511,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
510511
}
511512
}
512513
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
514+
tcx,
513515
op: |r| self.sub_regions(infer::CallReturn(span), least_region, r),
514516
});
515517
}
@@ -544,6 +546,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
544546
);
545547

546548
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
549+
tcx: self.tcx,
547550
op: |r| {
548551
self.member_constraint(
549552
opaque_type_def_id,
@@ -684,14 +687,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
684687
//
685688
// We ignore any type parameters because impl trait values are assumed to
686689
// capture all the in-scope type parameters.
687-
struct ConstrainOpaqueTypeRegionVisitor<OP> {
690+
struct ConstrainOpaqueTypeRegionVisitor<'tcx, OP> {
691+
tcx: TyCtxt<'tcx>,
688692
op: OP,
689693
}
690694

691-
impl<'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<OP>
695+
impl<'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<'tcx, OP>
692696
where
693697
OP: FnMut(ty::Region<'tcx>),
694698
{
699+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
700+
self.tcx
701+
}
702+
695703
fn visit_binder<T: TypeFoldable<'tcx>>(
696704
&mut self,
697705
t: &ty::Binder<T>,

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,9 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeFoldable<'tcx>>(
762762

763763
impl<'tcx> TypeVisitor<'tcx> for IllegalSelfTypeVisitor<'tcx> {
764764
type BreakTy = ();
765+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
766+
self.tcx
767+
}
765768

766769
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
767770
match t.kind() {

compiler/rustc_trait_selection/src/traits/structural_match.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ impl Search<'a, 'tcx> {
131131

132132
impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
133133
type BreakTy = NonStructuralMatchTy<'tcx>;
134+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
135+
self.tcx()
136+
}
134137

135138
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
136139
debug!("Search visiting ty: {:?}", ty);

0 commit comments

Comments
 (0)