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);

0 commit comments

Comments
 (0)