Skip to content

Commit 2e13be1

Browse files
committed
add tcx to fn walk
1 parent 1bc1691 commit 2e13be1

File tree

28 files changed

+71
-65
lines changed

28 files changed

+71
-65
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
5151

5252
fn node_ty_contains_target(&self, hir_id: HirId) -> Option<Ty<'tcx>> {
5353
self.node_type_opt(hir_id).map(|ty| self.infcx.resolve_vars_if_possible(ty)).filter(|ty| {
54-
ty.walk().any(|inner| {
54+
ty.walk(self.infcx.tcx).any(|inner| {
5555
inner == self.target
5656
|| match (inner.unpack(), self.target.unpack()) {
5757
(GenericArgKind::Type(inner_ty), GenericArgKind::Type(target_ty)) => {

compiler/rustc_infer/src/infer/outlives/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
189189
visited: &mut SsoHashSet<GenericArg<'tcx>>,
190190
) -> VerifyBound<'tcx> {
191191
let mut bounds = parent
192-
.walk_shallow(visited)
192+
.walk_shallow(self.tcx, visited)
193193
.filter_map(|child| match child.unpack() {
194194
GenericArgKind::Type(ty) => Some(self.type_bound(ty, visited)),
195195
GenericArgKind::Lifetime(lt) => {

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ declare_lint! {
155155
declare_lint_pass!(BoxPointers => [BOX_POINTERS]);
156156

157157
impl BoxPointers {
158-
fn check_heap_type(&self, cx: &LateContext<'_>, span: Span, ty: Ty<'_>) {
159-
for leaf in ty.walk() {
158+
fn check_heap_type<'tcx>(&self, cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) {
159+
for leaf in ty.walk(cx.tcx) {
160160
if let GenericArgKind::Type(leaf_ty) = leaf.unpack() {
161161
if leaf_ty.is_box() {
162162
cx.struct_span_lint(BOX_POINTERS, span, |lint| {

compiler/rustc_middle/src/ty/outlives.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn compute_components_recursive(
194194
out: &mut SmallVec<[Component<'tcx>; 4]>,
195195
visited: &mut SsoHashSet<GenericArg<'tcx>>,
196196
) {
197-
for child in parent.walk_shallow(visited) {
197+
for child in parent.walk_shallow(tcx, visited) {
198198
match child.unpack() {
199199
GenericArgKind::Type(ty) => {
200200
compute_components(tcx, ty, out, visited);

compiler/rustc_middle/src/ty/walk.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! An iterator over the type substructure.
22
//! WARNING: this does not keep track of the region depth.
33
4-
use crate::ty;
54
use crate::ty::subst::{GenericArg, GenericArgKind};
5+
use crate::ty::{self, TyCtxt};
66
use rustc_data_structures::sso::SsoHashSet;
77
use smallvec::{self, SmallVec};
88

@@ -11,6 +11,7 @@ use smallvec::{self, SmallVec};
1111
type TypeWalkerStack<'tcx> = SmallVec<[GenericArg<'tcx>; 8]>;
1212

1313
pub struct TypeWalker<'tcx> {
14+
tcx: TyCtxt<'tcx>,
1415
stack: TypeWalkerStack<'tcx>,
1516
last_subtree: usize,
1617
pub visited: SsoHashSet<GenericArg<'tcx>>,
@@ -25,8 +26,8 @@ pub struct TypeWalker<'tcx> {
2526
/// It maintains a set of visited types and
2627
/// skips any types that are already there.
2728
impl<'tcx> TypeWalker<'tcx> {
28-
pub fn new(root: GenericArg<'tcx>) -> Self {
29-
Self { stack: smallvec![root], last_subtree: 1, visited: SsoHashSet::new() }
29+
fn new(tcx: TyCtxt<'tcx>, root: GenericArg<'tcx>) -> Self {
30+
Self { tcx, stack: smallvec![root], last_subtree: 1, visited: SsoHashSet::new() }
3031
}
3132

3233
/// Skips the subtree corresponding to the last type
@@ -55,7 +56,7 @@ impl<'tcx> Iterator for TypeWalker<'tcx> {
5556
let next = self.stack.pop()?;
5657
self.last_subtree = self.stack.len();
5758
if self.visited.insert(next) {
58-
push_inner(&mut self.stack, next);
59+
push_inner(self.tcx, &mut self.stack, next);
5960
debug!("next: stack={:?}", self.stack);
6061
return Some(next);
6162
}
@@ -74,8 +75,8 @@ impl GenericArg<'tcx> {
7475
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
7576
/// [isize] => { [isize], isize }
7677
/// ```
77-
pub fn walk(self) -> TypeWalker<'tcx> {
78-
TypeWalker::new(self)
78+
pub fn walk(self, tcx: TyCtxt<'tcx>) -> TypeWalker<'tcx> {
79+
TypeWalker::new(tcx, self)
7980
}
8081

8182
/// Iterator that walks the immediate children of `self`. Hence
@@ -87,10 +88,11 @@ impl GenericArg<'tcx> {
8788
/// and skips any types that are already there.
8889
pub fn walk_shallow(
8990
self,
91+
tcx: TyCtxt<'tcx>,
9092
visited: &mut SsoHashSet<GenericArg<'tcx>>,
9193
) -> impl Iterator<Item = GenericArg<'tcx>> {
9294
let mut stack = SmallVec::new();
93-
push_inner(&mut stack, self);
95+
push_inner(tcx, &mut stack, self);
9496
stack.retain(|a| visited.insert(*a));
9597
stack.into_iter()
9698
}
@@ -107,18 +109,22 @@ impl<'tcx> super::TyS<'tcx> {
107109
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
108110
/// [isize] => { [isize], isize }
109111
/// ```
110-
pub fn walk(&'tcx self) -> TypeWalker<'tcx> {
111-
TypeWalker::new(self.into())
112+
pub fn walk(&'tcx self, tcx: TyCtxt<'tcx>) -> TypeWalker<'tcx> {
113+
TypeWalker::new(tcx, self.into())
112114
}
113115
}
114116

115-
// We push `GenericArg`s on the stack in reverse order so as to
116-
// maintain a pre-order traversal. As of the time of this
117-
// writing, the fact that the traversal is pre-order is not
118-
// known to be significant to any code, but it seems like the
119-
// natural order one would expect (basically, the order of the
120-
// types as they are written).
121-
fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) {
117+
/// We push `GenericArg`s on the stack in reverse order so as to
118+
/// maintain a pre-order traversal. As of the time of this
119+
/// writing, the fact that the traversal is pre-order is not
120+
/// known to be significant to any code, but it seems like the
121+
/// natural order one would expect (basically, the order of the
122+
/// types as they are written).
123+
fn push_inner<'tcx>(
124+
tcx: TyCtxt<'tcx>,
125+
stack: &mut TypeWalkerStack<'tcx>,
126+
parent: GenericArg<'tcx>,
127+
) {
122128
match parent.unpack() {
123129
GenericArgKind::Type(parent_ty) => match *parent_ty.kind() {
124130
ty::Bool
@@ -196,8 +202,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
196202
| ty::ConstKind::Error(_) => {}
197203

198204
ty::ConstKind::Unevaluated(ct) => {
199-
// TODO
200-
stack.extend(ct.substs_.unwrap().iter().rev());
205+
stack.extend(ct.substs(tcx).iter().rev());
201206
}
202207
}
203208
}

compiler/rustc_mir/src/monomorphize/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
573573
let type_length = instance
574574
.substs
575575
.iter()
576-
.flat_map(|arg| arg.walk())
576+
.flat_map(|arg| arg.walk(tcx))
577577
.filter(|arg| match arg.unpack() {
578578
GenericArgKind::Type(_) | GenericArgKind::Const(_) => true,
579579
GenericArgKind::Lifetime(_) => false,

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl Validator<'mir, 'tcx> {
365365
fn check_local_or_return_ty(&mut self, ty: Ty<'tcx>, local: Local) {
366366
let kind = self.body.local_kind(local);
367367

368-
for ty in ty.walk() {
368+
for ty in ty.walk(self.tcx) {
369369
let ty = match ty.unpack() {
370370
GenericArgKind::Type(ty) => ty,
371371

compiler/rustc_mir/src/transform/function_item_references.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FunctionItemRefChecker<'a, 'tcx> {
4949
// Handle calls to `transmute`
5050
if self.tcx.is_diagnostic_item(sym::transmute, def_id) {
5151
let arg_ty = args[0].ty(self.body, self.tcx);
52-
for generic_inner_ty in arg_ty.walk() {
52+
for generic_inner_ty in arg_ty.walk(self.tcx) {
5353
if let GenericArgKind::Type(inner_ty) = generic_inner_ty.unpack() {
5454
if let Some((fn_id, fn_substs)) =
5555
FunctionItemRefChecker::is_fn_ref(inner_ty)
@@ -110,7 +110,7 @@ impl<'a, 'tcx> FunctionItemRefChecker<'a, 'tcx> {
110110
let arg_defs = self.tcx.fn_sig(def_id).skip_binder().inputs();
111111
for (arg_num, arg_def) in arg_defs.iter().enumerate() {
112112
// For all types reachable from the argument type in the fn sig
113-
for generic_inner_ty in arg_def.walk() {
113+
for generic_inner_ty in arg_def.walk(self.tcx) {
114114
if let GenericArgKind::Type(inner_ty) = generic_inner_ty.unpack() {
115115
// If the inner type matches the type bound by `Pointer`
116116
if TyS::same_type(inner_ty, bound_ty) {

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
240240
// code at the moment, because types like `for <'a> fn(&'a ())` do
241241
// not *yet* implement `PartialEq`. So for now we leave this here.
242242
has_impl
243-
|| ty.walk().any(|t| match t.unpack() {
243+
|| ty.walk(self.tcx()).any(|t| match t.unpack() {
244244
ty::subst::GenericArgKind::Lifetime(_) => false,
245245
ty::subst::GenericArgKind::Type(t) => t.is_fn_ptr(),
246246
ty::subst::GenericArgKind::Const(_) => false,

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,14 +694,15 @@ fn substs_infer_vars<'a, 'tcx>(
694694
selcx: &mut SelectionContext<'a, 'tcx>,
695695
substs: ty::Binder<'tcx, SubstsRef<'tcx>>,
696696
) -> impl Iterator<Item = TyOrConstInferVar<'tcx>> {
697+
let tcx = selcx.tcx();
697698
selcx
698699
.infcx()
699700
.resolve_vars_if_possible(substs)
700701
.skip_binder() // ok because this check doesn't care about regions
701702
.iter()
702703
.filter(|arg| arg.has_infer_types_or_consts())
703-
.flat_map(|arg| {
704-
let mut walker = arg.walk();
704+
.flat_map(move |arg| {
705+
let mut walker = arg.walk(tcx);
705706
while let Some(c) = walker.next() {
706707
if !c.has_infer_types_or_consts() {
707708
walker.visited.remove(&c);

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn predicate_references_self(
278278
(predicate, sp): (ty::Predicate<'tcx>, Span),
279279
) -> Option<Span> {
280280
let self_ty = tcx.types.self_param;
281-
let has_self_ty = |arg: &GenericArg<'_>| arg.walk().any(|arg| arg == self_ty.into());
281+
let has_self_ty = |arg: &GenericArg<'tcx>| arg.walk(tcx).any(|arg| arg == self_ty.into());
282282
match predicate.kind().skip_binder() {
283283
ty::PredicateKind::Trait(ref data, _) => {
284284
// In the case of a trait predicate, we can skip the "self" type.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
831831

832832
let mut unsizing_params = GrowableBitSet::new_empty();
833833
if tcx.features().relaxed_struct_unsize {
834-
for arg in tail_field_ty.walk() {
834+
for arg in tail_field_ty.walk(tcx) {
835835
if let Some(i) = maybe_unsizing_param_idx(arg) {
836836
unsizing_params.insert(i);
837837
}
@@ -840,7 +840,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
840840
// Ensure none of the other fields mention the parameters used
841841
// in unsizing.
842842
for field in prefix_fields {
843-
for arg in tcx.type_of(field.did).walk() {
843+
for arg in tcx.type_of(field.did).walk(tcx) {
844844
if let Some(i) = maybe_unsizing_param_idx(arg) {
845845
unsizing_params.remove(i);
846846
}
@@ -852,7 +852,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
852852
}
853853
} else {
854854
let mut found = false;
855-
for arg in tail_field_ty.walk() {
855+
for arg in tail_field_ty.walk(tcx) {
856856
if let Some(i) = maybe_unsizing_param_idx(arg) {
857857
unsizing_params.insert(i);
858858
found = true;
@@ -868,7 +868,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
868868
// by putting it in a query; it would only need the `DefId` as it
869869
// looks at declared field types, not anything substituted.
870870
for field in prefix_fields {
871-
for arg in tcx.type_of(field.did).walk() {
871+
for arg in tcx.type_of(field.did).walk(tcx) {
872872
if let Some(i) = maybe_unsizing_param_idx(arg) {
873873
if unsizing_params.contains(i) {
874874
return Err(Unimplemented);

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
418418

419419
/// Pushes all the predicates needed to validate that `ty` is WF into `out`.
420420
fn compute(&mut self, arg: GenericArg<'tcx>) {
421-
let mut walker = arg.walk();
421+
let mut walker = arg.walk(self.tcx());
422422
let param_env = self.param_env;
423423
let depth = self.recursion_depth;
424424
while let Some(arg) = walker.next() {

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ fn well_formed_types_in_env<'tcx>(
361361
// constituents are well-formed.
362362
NodeKind::InherentImpl => {
363363
let self_ty = tcx.type_of(def_id);
364-
inputs.extend(self_ty.walk());
364+
inputs.extend(self_ty.walk(tcx));
365365
}
366366

367367
// In an fn, we assume that the arguments and all their constituents are
@@ -370,7 +370,7 @@ fn well_formed_types_in_env<'tcx>(
370370
let fn_sig = tcx.fn_sig(def_id);
371371
let fn_sig = tcx.liberate_late_bound_regions(def_id, fn_sig);
372372

373-
inputs.extend(fn_sig.inputs().iter().flat_map(|ty| ty.walk()));
373+
inputs.extend(fn_sig.inputs().iter().flat_map(|ty| ty.walk(tcx)));
374374
}
375375

376376
NodeKind::Other => (),

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
394394
if self.is_object && has_default {
395395
let default_ty = tcx.at(self.span).type_of(param.def_id);
396396
let self_param = tcx.types.self_param;
397-
if default_ty.walk().any(|arg| arg == self_param.into()) {
397+
if default_ty.walk(tcx).any(|arg| arg == self_param.into()) {
398398
// There is no suitable inference default for a type parameter
399399
// that references self, in an object type.
400400
return true;
@@ -1307,7 +1307,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13071307
// A `Self` within the original bound will be substituted with a
13081308
// `trait_object_dummy_self`, so check for that.
13091309
let references_self =
1310-
pred.skip_binder().ty.walk().any(|arg| arg == dummy_self.into());
1310+
pred.skip_binder().ty.walk(tcx).any(|arg| arg == dummy_self.into());
13111311

13121312
// If the projection output contains `Self`, force the user to
13131313
// elaborate it explicitly to avoid a lot of complexity.

compiler/rustc_typeck/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ pub(super) fn check_type_params_are_used<'tcx>(
15251525
return;
15261526
}
15271527

1528-
for leaf in ty.walk() {
1528+
for leaf in ty.walk(tcx) {
15291529
if let GenericArgKind::Type(leaf_ty) = leaf.unpack() {
15301530
if let ty::Param(param) = leaf_ty.kind() {
15311531
debug!("found use of ty param {:?}", param);

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
936936
let ty = self.resolve_vars_if_possible(ty);
937937
// We walk the argument type because the argument's type could have
938938
// been `Option<T>`, but the `FulfillmentError` references `T`.
939-
if ty.walk().any(|arg| arg == predicate.self_ty().into()) {
939+
if ty.walk(self.tcx).any(|arg| arg == predicate.self_ty().into()) {
940940
Some(i)
941941
} else {
942942
None

compiler/rustc_typeck/src/outlives/implicit_infer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
114114
required_predicates: &mut RequiredPredicates<'tcx>,
115115
explicit_map: &mut ExplicitPredicatesMap<'tcx>,
116116
) {
117-
for arg in field_ty.walk() {
117+
for arg in field_ty.walk(tcx) {
118118
let ty = match arg.unpack() {
119119
GenericArgKind::Type(ty) => ty,
120120

@@ -306,7 +306,7 @@ pub fn check_explicit_predicates<'tcx>(
306306
// 'b`.
307307
if let Some(self_ty) = ignored_self_ty {
308308
if let GenericArgKind::Type(ty) = outlives_predicate.0.unpack() {
309-
if ty.walk().any(|arg| arg == self_ty.into()) {
309+
if ty.walk(tcx).any(|arg| arg == self_ty.into()) {
310310
debug!("skipping self ty = {:?}", &ty);
311311
continue;
312312
}

src/tools/clippy/clippy_lints/src/escape.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn is_non_trait_box(ty: Ty<'_>) -> bool {
5353
struct EscapeDelegate<'a, 'tcx> {
5454
cx: &'a LateContext<'tcx>,
5555
set: HirIdSet,
56-
trait_self_ty: Option<Ty<'a>>,
56+
trait_self_ty: Option<Ty<'tcx>>,
5757
too_large_for_stack: u64,
5858
}
5959

@@ -171,7 +171,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
171171
// skip if there is a `self` parameter binding to a type
172172
// that contains `Self` (i.e.: `self: Box<Self>`), see #4804
173173
if let Some(trait_self_ty) = self.trait_self_ty {
174-
if map.name(cmt.hir_id) == kw::SelfLower && contains_ty(cmt.place.ty(), trait_self_ty) {
174+
if map.name(cmt.hir_id) == kw::SelfLower && contains_ty(self.cx.tcx, cmt.place.ty(), trait_self_ty) {
175175
return;
176176
}
177177
}

src/tools/clippy/clippy_lints/src/let_underscore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
119119
if let Some(init) = local.init;
120120
then {
121121
let init_ty = cx.typeck_results().expr_ty(init);
122-
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
122+
let contains_sync_guard = init_ty.walk(cx.tcx).any(|inner| match inner.unpack() {
123123
GenericArgKind::Type(inner_ty) => {
124124
SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
125125
},

src/tools/clippy/clippy_lints/src/loops/same_item_push.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(super) fn check<'tcx>(
4949
if same_item_push_visitor.should_lint();
5050
if let Some((vec, pushed_item)) = same_item_push_visitor.vec_push;
5151
let vec_ty = cx.typeck_results().expr_ty(vec);
52-
let ty = vec_ty.walk().nth(1).unwrap().expect_ty();
52+
let ty = vec_ty.walk(cx.tcx).nth(1).unwrap().expect_ty();
5353
if cx
5454
.tcx
5555
.lang_items()

0 commit comments

Comments
 (0)