Skip to content

Commit 3372f27

Browse files
committed
require a tcx for TypeVisitor
1 parent 83f0822 commit 3372f27

File tree

24 files changed

+182
-50
lines changed

24 files changed

+182
-50
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
@@ -1517,6 +1517,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15171517
}
15181518

15191519
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
1520+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
1521+
self.tcx
1522+
}
1523+
15201524
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
15211525
if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
15221526
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
@@ -201,6 +201,7 @@ where
201201
};
202202

203203
value.skip_binder().visit_with(&mut ScopeInstantiator {
204+
tcx: self.infcx.tcx,
204205
next_region: &mut next_region,
205206
target_index: ty::INNERMOST,
206207
bound_region_scope: &mut scope,
@@ -756,13 +757,18 @@ where
756757
/// `for<..`>. For each of those, it creates an entry in
757758
/// `bound_region_scope`.
758759
struct ScopeInstantiator<'me, 'tcx> {
760+
tcx: TyCtxt<'tcx>,
759761
next_region: &'me mut dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
760762
// The debruijn index of the scope we are instantiating.
761763
target_index: ty::DebruijnIndex,
762764
bound_region_scope: &'me mut BoundRegionScope<'tcx>,
763765
}
764766

765767
impl<'me, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'me, 'tcx> {
768+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
769+
self.tcx
770+
}
771+
766772
fn visit_binder<T: TypeFoldable<'tcx>>(
767773
&mut self,
768774
t: &ty::Binder<'tcx, T>,

compiler/rustc_infer/src/infer/resolve.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
126126

127127
impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
128128
type BreakTy = (Ty<'tcx>, Option<Span>);
129+
130+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
131+
self.infcx.tcx
132+
}
133+
129134
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
130135
let t = self.infcx.shallow_resolve(t);
131136
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
@@ -1160,6 +1160,9 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11601160

11611161
impl<'a, 'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueTypes<'a, 'tcx> {
11621162
type BreakTy = Ty<'tcx>;
1163+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1164+
self.cx.tcx
1165+
}
11631166

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

compiler/rustc_middle/src/ty/fold.rs

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

190190
pub trait TypeVisitor<'tcx>: Sized {
191191
type BreakTy = !;
192+
/// Supplies the `tcx` for an unevaluated anonymous constant in case its default substs
193+
/// are not yet supplied.
194+
///
195+
/// Visitors which do not look into these substs may leave this unimplemented, so be
196+
/// careful when calling this method elsewhere.
197+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx>;
192198

193199
fn visit_binder<T: TypeFoldable<'tcx>>(
194200
&mut self,
@@ -301,7 +307,8 @@ impl<'tcx> TyCtxt<'tcx> {
301307
value: &impl TypeFoldable<'tcx>,
302308
callback: impl FnMut(ty::Region<'tcx>) -> bool,
303309
) -> bool {
304-
struct RegionVisitor<F> {
310+
struct RegionVisitor<'tcx, F> {
311+
tcx: TyCtxt<'tcx>,
305312
/// The index of a binder *just outside* the things we have
306313
/// traversed. If we encounter a bound region bound by this
307314
/// binder or one outer to it, it appears free. Example:
@@ -323,12 +330,16 @@ impl<'tcx> TyCtxt<'tcx> {
323330
callback: F,
324331
}
325332

326-
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
333+
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<'tcx, F>
327334
where
328335
F: FnMut(ty::Region<'tcx>) -> bool,
329336
{
330337
type BreakTy = ();
331338

339+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
340+
self.tcx
341+
}
342+
332343
fn visit_binder<T: TypeFoldable<'tcx>>(
333344
&mut self,
334345
t: &Binder<'tcx, T>,
@@ -364,7 +375,9 @@ impl<'tcx> TyCtxt<'tcx> {
364375
}
365376
}
366377

367-
value.visit_with(&mut RegionVisitor { outer_index: ty::INNERMOST, callback }).is_break()
378+
value
379+
.visit_with(&mut RegionVisitor { tcx: self, outer_index: ty::INNERMOST, callback })
380+
.is_break()
368381
}
369382
}
370383

@@ -708,7 +721,7 @@ impl<'tcx> TyCtxt<'tcx> {
708721
where
709722
T: TypeFoldable<'tcx>,
710723
{
711-
let mut collector = LateBoundRegionsCollector::new(just_constraint);
724+
let mut collector = LateBoundRegionsCollector::new(self, just_constraint);
712725
let result = value.as_ref().skip_binder().visit_with(&mut collector);
713726
assert!(result.is_continue()); // should never have stopped early
714727
collector.regions
@@ -775,6 +788,10 @@ impl<'tcx> ValidateBoundVars<'tcx> {
775788
impl<'tcx> TypeVisitor<'tcx> for ValidateBoundVars<'tcx> {
776789
type BreakTy = ();
777790

791+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
792+
bug!("default anon const substs can't contain bound vars");
793+
}
794+
778795
fn visit_binder<T: TypeFoldable<'tcx>>(
779796
&mut self,
780797
t: &Binder<'tcx, T>,
@@ -989,6 +1006,10 @@ struct HasEscapingVarsVisitor {
9891006
impl<'tcx> TypeVisitor<'tcx> for HasEscapingVarsVisitor {
9901007
type BreakTy = FoundEscapingVars;
9911008

1009+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1010+
bug!("tcx_for_anon_const_substs called for HasEscpaingVarsVisitor");
1011+
}
1012+
9921013
fn visit_binder<T: TypeFoldable<'tcx>>(
9931014
&mut self,
9941015
t: &Binder<'tcx, T>,
@@ -1059,6 +1080,9 @@ struct HasTypeFlagsVisitor {
10591080

10601081
impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
10611082
type BreakTy = FoundFlags;
1083+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1084+
bug!("tcx_for_anon_const_substs called for HasTypeFlagsVisitor");
1085+
}
10621086

10631087
#[inline]
10641088
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<Self::BreakTy> {
@@ -1113,7 +1137,8 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
11131137

11141138
/// Collects all the late-bound regions at the innermost binding level
11151139
/// into a hash set.
1116-
struct LateBoundRegionsCollector {
1140+
struct LateBoundRegionsCollector<'tcx> {
1141+
tcx: TyCtxt<'tcx>,
11171142
current_index: ty::DebruijnIndex,
11181143
regions: FxHashSet<ty::BoundRegionKind>,
11191144

@@ -1127,17 +1152,22 @@ struct LateBoundRegionsCollector {
11271152
just_constrained: bool,
11281153
}
11291154

1130-
impl LateBoundRegionsCollector {
1131-
fn new(just_constrained: bool) -> Self {
1155+
impl LateBoundRegionsCollector<'tcx> {
1156+
fn new(tcx: TyCtxt<'tcx>, just_constrained: bool) -> Self {
11321157
LateBoundRegionsCollector {
1158+
tcx,
11331159
current_index: ty::INNERMOST,
11341160
regions: Default::default(),
11351161
just_constrained,
11361162
}
11371163
}
11381164
}
11391165

1140-
impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
1166+
impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector<'tcx> {
1167+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1168+
self.tcx
1169+
}
1170+
11411171
fn visit_binder<T: TypeFoldable<'tcx>>(
11421172
&mut self,
11431173
t: &Binder<'tcx, T>,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,13 +1949,18 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
19491949
debug!("prepare_late_bound_region_info(value: {:?})", value);
19501950

19511951
struct LateBoundRegionNameCollector<'a, 'tcx> {
1952+
tcx: TyCtxt<'tcx>,
19521953
used_region_names: &'a mut FxHashSet<Symbol>,
19531954
type_collector: SsoHashSet<Ty<'tcx>>,
19541955
}
19551956

19561957
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'_, 'tcx> {
19571958
type BreakTy = ();
19581959

1960+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
1961+
self.tcx
1962+
}
1963+
19591964
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
19601965
debug!("LateBoundRegionNameCollector::visit_region(r: {:?}, address: {:p})", r, &r);
19611966
if let ty::ReLateBound(_, ty::BoundRegion { kind: ty::BrNamed(_, name), .. }) = *r {
@@ -1979,6 +1984,7 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
19791984

19801985
self.used_region_names.clear();
19811986
let mut collector = LateBoundRegionNameCollector {
1987+
tcx: self.tcx,
19821988
used_region_names: &mut self.used_region_names,
19831989
type_collector: SsoHashSet::new(),
19841990
};

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
@@ -682,6 +682,10 @@ pub fn write_allocations<'tcx>(
682682
}
683683
struct CollectAllocIds(BTreeSet<AllocId>);
684684
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
685+
fn tcx_for_anon_const_substs(&self) -> TyCtxt<'tcx> {
686+
bug!("tcx_for_anon_const_substs called for CollectAllocIds")
687+
}
688+
685689
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
686690
if let ty::ConstKind::Value(val) = c.val {
687691
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
@@ -424,6 +424,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
424424

425425
for required_region in required_region_bounds {
426426
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
427+
tcx,
427428
op: |r| self.sub_regions(infer::CallReturn(span), required_region, r),
428429
});
429430
}
@@ -499,6 +500,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
499500
}
500501
}
501502
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
503+
tcx,
502504
op: |r| self.sub_regions(infer::CallReturn(span), least_region, r),
503505
});
504506
}
@@ -533,6 +535,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
533535
);
534536

535537
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
538+
tcx: self.tcx,
536539
op: |r| {
537540
self.member_constraint(
538541
opaque_type_key.def_id,
@@ -618,14 +621,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
618621
//
619622
// We ignore any type parameters because impl trait values are assumed to
620623
// capture all the in-scope type parameters.
621-
struct ConstrainOpaqueTypeRegionVisitor<OP> {
624+
struct ConstrainOpaqueTypeRegionVisitor<'tcx, OP> {
625+
tcx: TyCtxt<'tcx>,
622626
op: OP,
623627
}
624628

625-
impl<'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<OP>
629+
impl<'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<'tcx, OP>
626630
where
627631
OP: FnMut(ty::Region<'tcx>),
628632
{
633+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
634+
self.tcx
635+
}
636+
629637
fn visit_binder<T: TypeFoldable<'tcx>>(
630638
&mut self,
631639
t: &ty::Binder<'tcx, T>,

compiler/rustc_trait_selection/src/traits/object_safety.rs

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

770770
impl<'tcx> TypeVisitor<'tcx> for IllegalSelfTypeVisitor<'tcx> {
771771
type BreakTy = ();
772+
fn tcx_for_anon_const_substs<'a>(&'a self) -> TyCtxt<'tcx> {
773+
self.tcx
774+
}
772775

773776
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
774777
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)