]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/intravisit.rs
Implement existential types
[rust.git] / src / librustc / hir / intravisit.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! HIR walker for walking the contents of nodes.
12 //!
13 //! **For an overview of the visitor strategy, see the docs on the
14 //! `super::itemlikevisit::ItemLikeVisitor` trait.**
15 //!
16 //! If you have decided to use this visitor, here are some general
17 //! notes on how to do it:
18 //!
19 //! Each overridden visit method has full control over what
20 //! happens with its node, it can do its own traversal of the node's children,
21 //! call `intravisit::walk_*` to apply the default traversal algorithm, or prevent
22 //! deeper traversal by doing nothing.
23 //!
24 //! When visiting the HIR, the contents of nested items are NOT visited
25 //! by default. This is different from the AST visitor, which does a deep walk.
26 //! Hence this module is called `intravisit`; see the method `visit_nested_item`
27 //! for more details.
28 //!
29 //! Note: it is an important invariant that the default visitor walks
30 //! the body of a function in "execution order" - more concretely, if
31 //! we consider the reverse post-order (RPO) of the CFG implied by the HIR,
32 //! then a pre-order traversal of the HIR is consistent with the CFG RPO
33 //! on the *initial CFG point* of each HIR node, while a post-order traversal
34 //! of the HIR is consistent with the CFG RPO on each *final CFG point* of
35 //! each CFG node.
36 //!
37 //! One thing that follows is that if HIR node A always starts/ends executing
38 //! before HIR node B, then A appears in traversal pre/postorder before B,
39 //! respectively. (This follows from RPO respecting CFG domination).
40 //!
41 //! This order consistency is required in a few places in rustc, for
42 //! example generator inference, and possibly also HIR borrowck.
43
44 use syntax::ast::{NodeId, CRATE_NODE_ID, Ident, Name, Attribute};
45 use syntax_pos::Span;
46 use hir::*;
47 use hir::def::Def;
48 use hir::map::{self, Map};
49 use super::itemlikevisit::DeepVisitor;
50
51 use std::cmp;
52 use std::u32;
53 use std::result::Result::Err;
54
55 #[derive(Copy, Clone)]
56 pub enum FnKind<'a> {
57     /// #[xxx] pub async/const/extern "Abi" fn foo()
58     ItemFn(Name, &'a Generics, FnHeader, &'a Visibility, &'a [Attribute]),
59
60     /// fn foo(&self)
61     Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),
62
63     /// |x, y| {}
64     Closure(&'a [Attribute]),
65 }
66
67 impl<'a> FnKind<'a> {
68     pub fn attrs(&self) -> &'a [Attribute] {
69         match *self {
70             FnKind::ItemFn(.., attrs) => attrs,
71             FnKind::Method(.., attrs) => attrs,
72             FnKind::Closure(attrs) => attrs,
73         }
74     }
75 }
76
77 /// Specifies what nested things a visitor wants to visit. The most
78 /// common choice is `OnlyBodies`, which will cause the visitor to
79 /// visit fn bodies for fns that it encounters, but skip over nested
80 /// item-like things.
81 ///
82 /// See the comments on `ItemLikeVisitor` for more details on the overall
83 /// visit strategy.
84 pub enum NestedVisitorMap<'this, 'tcx: 'this> {
85     /// Do not visit any nested things. When you add a new
86     /// "non-nested" thing, you will want to audit such uses to see if
87     /// they remain valid.
88     ///
89     /// Use this if you are only walking some particular kind of tree
90     /// (i.e., a type, or fn signature) and you don't want to thread a
91     /// HIR map around.
92     None,
93
94     /// Do not visit nested item-like things, but visit nested things
95     /// that are inside of an item-like.
96     ///
97     /// **This is the most common choice.** A very common pattern is
98     /// to use `visit_all_item_likes()` as an outer loop,
99     /// and to have the visitor that visits the contents of each item
100     /// using this setting.
101     OnlyBodies(&'this Map<'tcx>),
102
103     /// Visit all nested things, including item-likes.
104     ///
105     /// **This is an unusual choice.** It is used when you want to
106     /// process everything within their lexical context. Typically you
107     /// kick off the visit by doing `walk_krate()`.
108     All(&'this Map<'tcx>),
109 }
110
111 impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
112     /// Returns the map to use for an "intra item-like" thing (if any).
113     /// e.g., function body.
114     pub fn intra(self) -> Option<&'this Map<'tcx>> {
115         match self {
116             NestedVisitorMap::None => None,
117             NestedVisitorMap::OnlyBodies(map) => Some(map),
118             NestedVisitorMap::All(map) => Some(map),
119         }
120     }
121
122     /// Returns the map to use for an "item-like" thing (if any).
123     /// e.g., item, impl-item.
124     pub fn inter(self) -> Option<&'this Map<'tcx>> {
125         match self {
126             NestedVisitorMap::None => None,
127             NestedVisitorMap::OnlyBodies(_) => None,
128             NestedVisitorMap::All(map) => Some(map),
129         }
130     }
131 }
132
133 /// Each method of the Visitor trait is a hook to be potentially
134 /// overridden.  Each method's default implementation recursively visits
135 /// the substructure of the input via the corresponding `walk` method;
136 /// e.g. the `visit_mod` method by default calls `intravisit::walk_mod`.
137 ///
138 /// Note that this visitor does NOT visit nested items by default
139 /// (this is why the module is called `intravisit`, to distinguish it
140 /// from the AST's `visit` module, which acts differently). If you
141 /// simply want to visit all items in the crate in some order, you
142 /// should call `Crate::visit_all_items`. Otherwise, see the comment
143 /// on `visit_nested_item` for details on how to visit nested items.
144 ///
145 /// If you want to ensure that your code handles every variant
146 /// explicitly, you need to override each method.  (And you also need
147 /// to monitor future changes to `Visitor` in case a new method with a
148 /// new default implementation gets introduced.)
149 pub trait Visitor<'v> : Sized {
150     ///////////////////////////////////////////////////////////////////////////
151     // Nested items.
152
153     /// The default versions of the `visit_nested_XXX` routines invoke
154     /// this method to get a map to use. By selecting an enum variant,
155     /// you control which kinds of nested HIR are visited; see
156     /// `NestedVisitorMap` for details. By "nested HIR", we are
157     /// referring to bits of HIR that are not directly embedded within
158     /// one another but rather indirectly, through a table in the
159     /// crate. This is done to control dependencies during incremental
160     /// compilation: the non-inline bits of HIR can be tracked and
161     /// hashed separately.
162     ///
163     /// **If for some reason you want the nested behavior, but don't
164     /// have a `Map` at your disposal:** then you should override the
165     /// `visit_nested_XXX` methods, and override this method to
166     /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
167     /// added in the future, we will see the panic in your code and
168     /// fix it appropriately.
169     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v>;
170
171     /// Invoked when a nested item is encountered. By default does
172     /// nothing unless you override `nested_visit_map` to return
173     /// `Some(_)`, in which case it will walk the item. **You probably
174     /// don't want to override this method** -- instead, override
175     /// `nested_visit_map` or use the "shallow" or "deep" visit
176     /// patterns described on `itemlikevisit::ItemLikeVisitor`. The only
177     /// reason to override this method is if you want a nested pattern
178     /// but cannot supply a `Map`; see `nested_visit_map` for advice.
179     #[allow(unused_variables)]
180     fn visit_nested_item(&mut self, id: ItemId) {
181         let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item(id.id));
182         if let Some(item) = opt_item {
183             self.visit_item(item);
184         }
185     }
186
187     /// Like `visit_nested_item()`, but for trait items. See
188     /// `visit_nested_item()` for advice on when to override this
189     /// method.
190     #[allow(unused_variables)]
191     fn visit_nested_trait_item(&mut self, id: TraitItemId) {
192         let opt_item = self.nested_visit_map().inter().map(|map| map.trait_item(id));
193         if let Some(item) = opt_item {
194             self.visit_trait_item(item);
195         }
196     }
197
198     /// Like `visit_nested_item()`, but for impl items. See
199     /// `visit_nested_item()` for advice on when to override this
200     /// method.
201     #[allow(unused_variables)]
202     fn visit_nested_impl_item(&mut self, id: ImplItemId) {
203         let opt_item = self.nested_visit_map().inter().map(|map| map.impl_item(id));
204         if let Some(item) = opt_item {
205             self.visit_impl_item(item);
206         }
207     }
208
209     /// Invoked to visit the body of a function, method or closure. Like
210     /// visit_nested_item, does nothing by default unless you override
211     /// `nested_visit_map` to return `Some(_)`, in which case it will walk the
212     /// body.
213     fn visit_nested_body(&mut self, id: BodyId) {
214         let opt_body = self.nested_visit_map().intra().map(|map| map.body(id));
215         if let Some(body) = opt_body {
216             self.visit_body(body);
217         }
218     }
219
220     /// Visit the top-level item and (optionally) nested items / impl items. See
221     /// `visit_nested_item` for details.
222     fn visit_item(&mut self, i: &'v Item) {
223         walk_item(self, i)
224     }
225
226     fn visit_body(&mut self, b: &'v Body) {
227         walk_body(self, b);
228     }
229
230     /// When invoking `visit_all_item_likes()`, you need to supply an
231     /// item-like visitor.  This method converts a "intra-visit"
232     /// visitor into an item-like visitor that walks the entire tree.
233     /// If you use this, you probably don't want to process the
234     /// contents of nested item-like things, since the outer loop will
235     /// visit them as well.
236     fn as_deep_visitor<'s>(&'s mut self) -> DeepVisitor<'s, Self> {
237         DeepVisitor::new(self)
238     }
239
240     ///////////////////////////////////////////////////////////////////////////
241
242     fn visit_id(&mut self, _node_id: NodeId) {
243         // Nothing to do.
244     }
245     fn visit_def_mention(&mut self, _def: Def) {
246         // Nothing to do.
247     }
248     fn visit_name(&mut self, _span: Span, _name: Name) {
249         // Nothing to do.
250     }
251     fn visit_ident(&mut self, ident: Ident) {
252         walk_ident(self, ident)
253     }
254     fn visit_mod(&mut self, m: &'v Mod, _s: Span, n: NodeId) {
255         walk_mod(self, m, n)
256     }
257     fn visit_foreign_item(&mut self, i: &'v ForeignItem) {
258         walk_foreign_item(self, i)
259     }
260     fn visit_local(&mut self, l: &'v Local) {
261         walk_local(self, l)
262     }
263     fn visit_block(&mut self, b: &'v Block) {
264         walk_block(self, b)
265     }
266     fn visit_stmt(&mut self, s: &'v Stmt) {
267         walk_stmt(self, s)
268     }
269     fn visit_arm(&mut self, a: &'v Arm) {
270         walk_arm(self, a)
271     }
272     fn visit_pat(&mut self, p: &'v Pat) {
273         walk_pat(self, p)
274     }
275     fn visit_decl(&mut self, d: &'v Decl) {
276         walk_decl(self, d)
277     }
278     fn visit_anon_const(&mut self, c: &'v AnonConst) {
279         walk_anon_const(self, c)
280     }
281     fn visit_expr(&mut self, ex: &'v Expr) {
282         walk_expr(self, ex)
283     }
284     fn visit_ty(&mut self, t: &'v Ty) {
285         walk_ty(self, t)
286     }
287     fn visit_generic_param(&mut self, p: &'v GenericParam) {
288         walk_generic_param(self, p)
289     }
290     fn visit_generics(&mut self, g: &'v Generics) {
291         walk_generics(self, g)
292     }
293     fn visit_where_predicate(&mut self, predicate: &'v WherePredicate) {
294         walk_where_predicate(self, predicate)
295     }
296     fn visit_fn_decl(&mut self, fd: &'v FnDecl) {
297         walk_fn_decl(self, fd)
298     }
299     fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
300         walk_fn(self, fk, fd, b, s, id)
301     }
302     fn visit_trait_item(&mut self, ti: &'v TraitItem) {
303         walk_trait_item(self, ti)
304     }
305     fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) {
306         walk_trait_item_ref(self, ii)
307     }
308     fn visit_impl_item(&mut self, ii: &'v ImplItem) {
309         walk_impl_item(self, ii)
310     }
311     fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
312         walk_impl_item_ref(self, ii)
313     }
314     fn visit_trait_ref(&mut self, t: &'v TraitRef) {
315         walk_trait_ref(self, t)
316     }
317     fn visit_param_bound(&mut self, bounds: &'v GenericBound) {
318         walk_param_bound(self, bounds)
319     }
320     fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: TraitBoundModifier) {
321         walk_poly_trait_ref(self, t, m)
322     }
323     fn visit_variant_data(&mut self,
324                           s: &'v VariantData,
325                           _: Name,
326                           _: &'v Generics,
327                           _parent_id: NodeId,
328                           _: Span) {
329         walk_struct_def(self, s)
330     }
331     fn visit_struct_field(&mut self, s: &'v StructField) {
332         walk_struct_field(self, s)
333     }
334     fn visit_enum_def(&mut self,
335                       enum_definition: &'v EnumDef,
336                       generics: &'v Generics,
337                       item_id: NodeId,
338                       _: Span) {
339         walk_enum_def(self, enum_definition, generics, item_id)
340     }
341     fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) {
342         walk_variant(self, v, g, item_id)
343     }
344     fn visit_label(&mut self, label: &'v Label) {
345         walk_label(self, label)
346     }
347     fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg) {
348         match generic_arg {
349             GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
350             GenericArg::Type(ty) => self.visit_ty(ty),
351         }
352     }
353     fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
354         walk_lifetime(self, lifetime)
355     }
356     fn visit_qpath(&mut self, qpath: &'v QPath, id: NodeId, span: Span) {
357         walk_qpath(self, qpath, id, span)
358     }
359     fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
360         walk_path(self, path)
361     }
362     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
363         walk_path_segment(self, path_span, path_segment)
364     }
365     fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs) {
366         walk_generic_args(self, path_span, generic_args)
367     }
368     fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
369         walk_assoc_type_binding(self, type_binding)
370     }
371     fn visit_attribute(&mut self, _attr: &'v Attribute) {
372     }
373     fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
374         walk_macro_def(self, macro_def)
375     }
376     fn visit_vis(&mut self, vis: &'v Visibility) {
377         walk_vis(self, vis)
378     }
379     fn visit_associated_item_kind(&mut self, kind: &'v AssociatedItemKind) {
380         walk_associated_item_kind(self, kind);
381     }
382     fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
383         walk_defaultness(self, defaultness);
384     }
385 }
386
387 /// Walks the contents of a crate. See also `Crate::visit_all_items`.
388 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
389     visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
390     walk_list!(visitor, visit_attribute, &krate.attrs);
391     walk_list!(visitor, visit_macro_def, &krate.exported_macros);
392 }
393
394 pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
395     visitor.visit_id(macro_def.id);
396     visitor.visit_name(macro_def.span, macro_def.name);
397     walk_list!(visitor, visit_attribute, &macro_def.attrs);
398 }
399
400 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_id: NodeId) {
401     visitor.visit_id(mod_node_id);
402     for &item_id in &module.item_ids {
403         visitor.visit_nested_item(item_id);
404     }
405 }
406
407 pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
408     for argument in &body.arguments {
409         visitor.visit_id(argument.id);
410         visitor.visit_pat(&argument.pat);
411     }
412     visitor.visit_expr(&body.value);
413 }
414
415 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
416     // Intentionally visiting the expr first - the initialization expr
417     // dominates the local's definition.
418     walk_list!(visitor, visit_expr, &local.init);
419     walk_list!(visitor, visit_attribute, local.attrs.iter());
420     visitor.visit_id(local.id);
421     visitor.visit_pat(&local.pat);
422     walk_list!(visitor, visit_ty, &local.ty);
423 }
424
425 pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
426     visitor.visit_name(ident.span, ident.name);
427 }
428
429 pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
430     visitor.visit_ident(label.ident);
431 }
432
433 pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
434     visitor.visit_id(lifetime.id);
435     match lifetime.name {
436         LifetimeName::Param(ParamName::Plain(ident)) => {
437             visitor.visit_ident(ident);
438         }
439         LifetimeName::Param(ParamName::Fresh(_)) |
440         LifetimeName::Static |
441         LifetimeName::Implicit |
442         LifetimeName::Underscore => {}
443     }
444 }
445
446 pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
447                                   trait_ref: &'v PolyTraitRef,
448                                   _modifier: TraitBoundModifier)
449     where V: Visitor<'v>
450 {
451     walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
452     visitor.visit_trait_ref(&trait_ref.trait_ref);
453 }
454
455 pub fn walk_trait_ref<'v, V>(visitor: &mut V, trait_ref: &'v TraitRef)
456     where V: Visitor<'v>
457 {
458     visitor.visit_id(trait_ref.ref_id);
459     visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
460 }
461
462 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
463     visitor.visit_vis(&item.vis);
464     visitor.visit_name(item.span, item.name);
465     match item.node {
466         ItemKind::ExternCrate(orig_name) => {
467             visitor.visit_id(item.id);
468             if let Some(orig_name) = orig_name {
469                 visitor.visit_name(item.span, orig_name);
470             }
471         }
472         ItemKind::Use(ref path, _) => {
473             visitor.visit_id(item.id);
474             visitor.visit_path(path, item.id);
475         }
476         ItemKind::Static(ref typ, _, body) |
477         ItemKind::Const(ref typ, body) => {
478             visitor.visit_id(item.id);
479             visitor.visit_ty(typ);
480             visitor.visit_nested_body(body);
481         }
482         ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
483             visitor.visit_fn(FnKind::ItemFn(item.name,
484                                             generics,
485                                             header,
486                                             &item.vis,
487                                             &item.attrs),
488                              declaration,
489                              body_id,
490                              item.span,
491                              item.id)
492         }
493         ItemKind::Mod(ref module) => {
494             // visit_mod() takes care of visiting the Item's NodeId
495             visitor.visit_mod(module, item.span, item.id)
496         }
497         ItemKind::ForeignMod(ref foreign_module) => {
498             visitor.visit_id(item.id);
499             walk_list!(visitor, visit_foreign_item, &foreign_module.items);
500         }
501         ItemKind::GlobalAsm(_) => {
502             visitor.visit_id(item.id);
503         }
504         ItemKind::Ty(ref typ, ref type_parameters) => {
505             visitor.visit_id(item.id);
506             visitor.visit_ty(typ);
507             visitor.visit_generics(type_parameters)
508         }
509         ItemKind::Existential(ExistTy {ref generics, ref bounds, impl_trait_fn}) => {
510             visitor.visit_id(item.id);
511             walk_generics(visitor, generics);
512             walk_list!(visitor, visit_param_bound, bounds);
513             if let Some(impl_trait_fn) = impl_trait_fn {
514                 visitor.visit_def_mention(Def::Fn(impl_trait_fn))
515             }
516         }
517         ItemKind::Enum(ref enum_definition, ref type_parameters) => {
518             visitor.visit_generics(type_parameters);
519             // visit_enum_def() takes care of visiting the Item's NodeId
520             visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
521         }
522         ItemKind::Impl(
523             ..,
524             ref type_parameters,
525             ref opt_trait_reference,
526             ref typ,
527             ref impl_item_refs
528         ) => {
529             visitor.visit_id(item.id);
530             visitor.visit_generics(type_parameters);
531             walk_list!(visitor, visit_trait_ref, opt_trait_reference);
532             visitor.visit_ty(typ);
533             walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
534         }
535         ItemKind::Struct(ref struct_definition, ref generics) |
536         ItemKind::Union(ref struct_definition, ref generics) => {
537             visitor.visit_generics(generics);
538             visitor.visit_id(item.id);
539             visitor.visit_variant_data(struct_definition, item.name, generics, item.id, item.span);
540         }
541         ItemKind::Trait(.., ref generics, ref bounds, ref trait_item_refs) => {
542             visitor.visit_id(item.id);
543             visitor.visit_generics(generics);
544             walk_list!(visitor, visit_param_bound, bounds);
545             walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
546         }
547         ItemKind::TraitAlias(ref generics, ref bounds) => {
548             visitor.visit_id(item.id);
549             visitor.visit_generics(generics);
550             walk_list!(visitor, visit_param_bound, bounds);
551         }
552     }
553     walk_list!(visitor, visit_attribute, &item.attrs);
554 }
555
556 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
557                                          enum_definition: &'v EnumDef,
558                                          generics: &'v Generics,
559                                          item_id: NodeId) {
560     visitor.visit_id(item_id);
561     walk_list!(visitor,
562                visit_variant,
563                &enum_definition.variants,
564                generics,
565                item_id);
566 }
567
568 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
569                                         variant: &'v Variant,
570                                         generics: &'v Generics,
571                                         parent_item_id: NodeId) {
572     visitor.visit_name(variant.span, variant.node.name);
573     visitor.visit_variant_data(&variant.node.data,
574                                variant.node.name,
575                                generics,
576                                parent_item_id,
577                                variant.span);
578     walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
579     walk_list!(visitor, visit_attribute, &variant.node.attrs);
580 }
581
582 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
583     visitor.visit_id(typ.id);
584
585     match typ.node {
586         TyKind::Slice(ref ty) => {
587             visitor.visit_ty(ty)
588         }
589         TyKind::Ptr(ref mutable_type) => {
590             visitor.visit_ty(&mutable_type.ty)
591         }
592         TyKind::Rptr(ref lifetime, ref mutable_type) => {
593             visitor.visit_lifetime(lifetime);
594             visitor.visit_ty(&mutable_type.ty)
595         }
596         TyKind::Never => {},
597         TyKind::Tup(ref tuple_element_types) => {
598             walk_list!(visitor, visit_ty, tuple_element_types);
599         }
600         TyKind::BareFn(ref function_declaration) => {
601             walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
602             visitor.visit_fn_decl(&function_declaration.decl);
603         }
604         TyKind::Path(ref qpath) => {
605             visitor.visit_qpath(qpath, typ.id, typ.span);
606         }
607         TyKind::Array(ref ty, ref length) => {
608             visitor.visit_ty(ty);
609             visitor.visit_anon_const(length)
610         }
611         TyKind::TraitObject(ref bounds, ref lifetime) => {
612             for bound in bounds {
613                 visitor.visit_poly_trait_ref(bound, TraitBoundModifier::None);
614             }
615             visitor.visit_lifetime(lifetime);
616         }
617         TyKind::Typeof(ref expression) => {
618             visitor.visit_anon_const(expression)
619         }
620         TyKind::Infer | TyKind::Err => {}
621     }
622 }
623
624 pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath, id: NodeId, span: Span) {
625     match *qpath {
626         QPath::Resolved(ref maybe_qself, ref path) => {
627             if let Some(ref qself) = *maybe_qself {
628                 visitor.visit_ty(qself);
629             }
630             visitor.visit_path(path, id)
631         }
632         QPath::TypeRelative(ref qself, ref segment) => {
633             visitor.visit_ty(qself);
634             visitor.visit_path_segment(span, segment);
635         }
636     }
637 }
638
639 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
640     visitor.visit_def_mention(path.def);
641     for segment in &path.segments {
642         visitor.visit_path_segment(path.span, segment);
643     }
644 }
645
646 pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
647                                              path_span: Span,
648                                              segment: &'v PathSegment) {
649     visitor.visit_ident(segment.ident);
650     if let Some(ref args) = segment.args {
651         visitor.visit_generic_args(path_span, args);
652     }
653 }
654
655 pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
656                                              _path_span: Span,
657                                              generic_args: &'v GenericArgs) {
658     walk_list!(visitor, visit_generic_arg, &generic_args.args);
659     walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
660 }
661
662 pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
663                                                    type_binding: &'v TypeBinding) {
664     visitor.visit_id(type_binding.id);
665     visitor.visit_ident(type_binding.ident);
666     visitor.visit_ty(&type_binding.ty);
667 }
668
669 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
670     visitor.visit_id(pattern.id);
671     match pattern.node {
672         PatKind::TupleStruct(ref qpath, ref children, _) => {
673             visitor.visit_qpath(qpath, pattern.id, pattern.span);
674             walk_list!(visitor, visit_pat, children);
675         }
676         PatKind::Path(ref qpath) => {
677             visitor.visit_qpath(qpath, pattern.id, pattern.span);
678         }
679         PatKind::Struct(ref qpath, ref fields, _) => {
680             visitor.visit_qpath(qpath, pattern.id, pattern.span);
681             for field in fields {
682                 visitor.visit_id(field.node.id);
683                 visitor.visit_ident(field.node.ident);
684                 visitor.visit_pat(&field.node.pat)
685             }
686         }
687         PatKind::Tuple(ref tuple_elements, _) => {
688             walk_list!(visitor, visit_pat, tuple_elements);
689         }
690         PatKind::Box(ref subpattern) |
691         PatKind::Ref(ref subpattern, _) => {
692             visitor.visit_pat(subpattern)
693         }
694         PatKind::Binding(_, canonical_id, ident, ref optional_subpattern) => {
695             visitor.visit_def_mention(Def::Local(canonical_id));
696             visitor.visit_ident(ident);
697             walk_list!(visitor, visit_pat, optional_subpattern);
698         }
699         PatKind::Lit(ref expression) => visitor.visit_expr(expression),
700         PatKind::Range(ref lower_bound, ref upper_bound, _) => {
701             visitor.visit_expr(lower_bound);
702             visitor.visit_expr(upper_bound)
703         }
704         PatKind::Wild => (),
705         PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
706             walk_list!(visitor, visit_pat, prepatterns);
707             walk_list!(visitor, visit_pat, slice_pattern);
708             walk_list!(visitor, visit_pat, postpatterns);
709         }
710     }
711 }
712
713 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem) {
714     visitor.visit_id(foreign_item.id);
715     visitor.visit_vis(&foreign_item.vis);
716     visitor.visit_name(foreign_item.span, foreign_item.name);
717
718     match foreign_item.node {
719         ForeignItemKind::Fn(ref function_declaration, ref param_names, ref generics) => {
720             visitor.visit_generics(generics);
721             visitor.visit_fn_decl(function_declaration);
722             for &param_name in param_names {
723                 visitor.visit_ident(param_name);
724             }
725         }
726         ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
727         ForeignItemKind::Type => (),
728     }
729
730     walk_list!(visitor, visit_attribute, &foreign_item.attrs);
731 }
732
733 pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericBound) {
734     match *bound {
735         GenericBound::Trait(ref typ, modifier) => {
736             visitor.visit_poly_trait_ref(typ, modifier);
737         }
738         GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
739     }
740 }
741
742 pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
743     visitor.visit_id(param.id);
744     walk_list!(visitor, visit_attribute, &param.attrs);
745     match param.name {
746         ParamName::Plain(ident) => visitor.visit_ident(ident),
747         ParamName::Fresh(_) => {}
748     }
749     match param.kind {
750         GenericParamKind::Lifetime { .. } => {}
751         GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
752     }
753     walk_list!(visitor, visit_param_bound, &param.bounds);
754 }
755
756 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
757     walk_list!(visitor, visit_generic_param, &generics.params);
758     visitor.visit_id(generics.where_clause.id);
759     walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
760 }
761
762 pub fn walk_where_predicate<'v, V: Visitor<'v>>(
763     visitor: &mut V,
764     predicate: &'v WherePredicate)
765 {
766     match predicate {
767         &WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
768                                                             ref bounds,
769                                                             ref bound_generic_params,
770                                                             ..}) => {
771             visitor.visit_ty(bounded_ty);
772             walk_list!(visitor, visit_param_bound, bounds);
773             walk_list!(visitor, visit_generic_param, bound_generic_params);
774         }
775         &WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
776                                                               ref bounds,
777                                                               ..}) => {
778             visitor.visit_lifetime(lifetime);
779             walk_list!(visitor, visit_param_bound, bounds);
780         }
781         &WherePredicate::EqPredicate(WhereEqPredicate{id,
782                                                       ref lhs_ty,
783                                                       ref rhs_ty,
784                                                       ..}) => {
785             visitor.visit_id(id);
786             visitor.visit_ty(lhs_ty);
787             visitor.visit_ty(rhs_ty);
788         }
789     }
790 }
791
792 pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
793     if let Return(ref output_ty) = *ret_ty {
794         visitor.visit_ty(output_ty)
795     }
796 }
797
798 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
799     for ty in &function_declaration.inputs {
800         visitor.visit_ty(ty)
801     }
802     walk_fn_ret_ty(visitor, &function_declaration.output)
803 }
804
805 pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'v>) {
806     match function_kind {
807         FnKind::ItemFn(_, generics, ..) => {
808             visitor.visit_generics(generics);
809         }
810         FnKind::Method(..) |
811         FnKind::Closure(_) => {}
812     }
813 }
814
815 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
816                                    function_kind: FnKind<'v>,
817                                    function_declaration: &'v FnDecl,
818                                    body_id: BodyId,
819                                    _span: Span,
820                                    id: NodeId) {
821     visitor.visit_id(id);
822     visitor.visit_fn_decl(function_declaration);
823     walk_fn_kind(visitor, function_kind);
824     visitor.visit_nested_body(body_id)
825 }
826
827 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
828     visitor.visit_ident(trait_item.ident);
829     walk_list!(visitor, visit_attribute, &trait_item.attrs);
830     visitor.visit_generics(&trait_item.generics);
831     match trait_item.node {
832         TraitItemKind::Const(ref ty, default) => {
833             visitor.visit_id(trait_item.id);
834             visitor.visit_ty(ty);
835             walk_list!(visitor, visit_nested_body, default);
836         }
837         TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
838             visitor.visit_id(trait_item.id);
839             visitor.visit_fn_decl(&sig.decl);
840             for &param_name in param_names {
841                 visitor.visit_ident(param_name);
842             }
843         }
844         TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
845             visitor.visit_fn(FnKind::Method(trait_item.ident,
846                                             sig,
847                                             None,
848                                             &trait_item.attrs),
849                              &sig.decl,
850                              body_id,
851                              trait_item.span,
852                              trait_item.id);
853         }
854         TraitItemKind::Type(ref bounds, ref default) => {
855             visitor.visit_id(trait_item.id);
856             walk_list!(visitor, visit_param_bound, bounds);
857             walk_list!(visitor, visit_ty, default);
858         }
859     }
860 }
861
862 pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
863     // NB: Deliberately force a compilation error if/when new fields are added.
864     let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
865     visitor.visit_nested_trait_item(id);
866     visitor.visit_ident(ident);
867     visitor.visit_associated_item_kind(kind);
868     visitor.visit_defaultness(defaultness);
869 }
870
871 pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
872     // NB: Deliberately force a compilation error if/when new fields are added.
873     let ImplItem {
874         id: _,
875         hir_id: _,
876         ident,
877         ref vis,
878         ref defaultness,
879         ref attrs,
880         ref generics,
881         ref node,
882         span: _,
883     } = *impl_item;
884
885     visitor.visit_ident(ident);
886     visitor.visit_vis(vis);
887     visitor.visit_defaultness(defaultness);
888     walk_list!(visitor, visit_attribute, attrs);
889     visitor.visit_generics(generics);
890     match *node {
891         ImplItemKind::Const(ref ty, body) => {
892             visitor.visit_id(impl_item.id);
893             visitor.visit_ty(ty);
894             visitor.visit_nested_body(body);
895         }
896         ImplItemKind::Method(ref sig, body_id) => {
897             visitor.visit_fn(FnKind::Method(impl_item.ident,
898                                             sig,
899                                             Some(&impl_item.vis),
900                                             &impl_item.attrs),
901                              &sig.decl,
902                              body_id,
903                              impl_item.span,
904                              impl_item.id);
905         }
906         ImplItemKind::Type(ref ty) => {
907             visitor.visit_id(impl_item.id);
908             visitor.visit_ty(ty);
909         }
910         ImplItemKind::Existential(ref bounds) => {
911             visitor.visit_id(impl_item.id);
912             walk_list!(visitor, visit_param_bound, bounds);
913         }
914     }
915 }
916
917 pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
918     // NB: Deliberately force a compilation error if/when new fields are added.
919     let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;
920     visitor.visit_nested_impl_item(id);
921     visitor.visit_ident(ident);
922     visitor.visit_associated_item_kind(kind);
923     visitor.visit_vis(vis);
924     visitor.visit_defaultness(defaultness);
925 }
926
927
928 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
929     visitor.visit_id(struct_definition.id());
930     walk_list!(visitor, visit_struct_field, struct_definition.fields());
931 }
932
933 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
934     visitor.visit_id(struct_field.id);
935     visitor.visit_vis(&struct_field.vis);
936     visitor.visit_ident(struct_field.ident);
937     visitor.visit_ty(&struct_field.ty);
938     walk_list!(visitor, visit_attribute, &struct_field.attrs);
939 }
940
941 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
942     visitor.visit_id(block.id);
943     walk_list!(visitor, visit_stmt, &block.stmts);
944     walk_list!(visitor, visit_expr, &block.expr);
945 }
946
947 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
948     match statement.node {
949         StmtKind::Decl(ref declaration, id) => {
950             visitor.visit_id(id);
951             visitor.visit_decl(declaration)
952         }
953         StmtKind::Expr(ref expression, id) |
954         StmtKind::Semi(ref expression, id) => {
955             visitor.visit_id(id);
956             visitor.visit_expr(expression)
957         }
958     }
959 }
960
961 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
962     match declaration.node {
963         DeclKind::Local(ref local) => visitor.visit_local(local),
964         DeclKind::Item(item) => visitor.visit_nested_item(item),
965     }
966 }
967
968 pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
969     visitor.visit_id(constant.id);
970     visitor.visit_nested_body(constant.body);
971 }
972
973 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
974     visitor.visit_id(expression.id);
975     walk_list!(visitor, visit_attribute, expression.attrs.iter());
976     match expression.node {
977         ExprKind::Box(ref subexpression) => {
978             visitor.visit_expr(subexpression)
979         }
980         ExprKind::Array(ref subexpressions) => {
981             walk_list!(visitor, visit_expr, subexpressions);
982         }
983         ExprKind::Repeat(ref element, ref count) => {
984             visitor.visit_expr(element);
985             visitor.visit_anon_const(count)
986         }
987         ExprKind::Struct(ref qpath, ref fields, ref optional_base) => {
988             visitor.visit_qpath(qpath, expression.id, expression.span);
989             for field in fields {
990                 visitor.visit_id(field.id);
991                 visitor.visit_ident(field.ident);
992                 visitor.visit_expr(&field.expr)
993             }
994             walk_list!(visitor, visit_expr, optional_base);
995         }
996         ExprKind::Tup(ref subexpressions) => {
997             walk_list!(visitor, visit_expr, subexpressions);
998         }
999         ExprKind::Call(ref callee_expression, ref arguments) => {
1000             visitor.visit_expr(callee_expression);
1001             walk_list!(visitor, visit_expr, arguments);
1002         }
1003         ExprKind::MethodCall(ref segment, _, ref arguments) => {
1004             visitor.visit_path_segment(expression.span, segment);
1005             walk_list!(visitor, visit_expr, arguments);
1006         }
1007         ExprKind::Binary(_, ref left_expression, ref right_expression) => {
1008             visitor.visit_expr(left_expression);
1009             visitor.visit_expr(right_expression)
1010         }
1011         ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
1012             visitor.visit_expr(subexpression)
1013         }
1014         ExprKind::Lit(_) => {}
1015         ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
1016             visitor.visit_expr(subexpression);
1017             visitor.visit_ty(typ)
1018         }
1019         ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
1020             visitor.visit_expr(head_expression);
1021             visitor.visit_expr(if_block);
1022             walk_list!(visitor, visit_expr, optional_else);
1023         }
1024         ExprKind::While(ref subexpression, ref block, ref opt_label) => {
1025             walk_list!(visitor, visit_label, opt_label);
1026             visitor.visit_expr(subexpression);
1027             visitor.visit_block(block);
1028         }
1029         ExprKind::Loop(ref block, ref opt_label, _) => {
1030             walk_list!(visitor, visit_label, opt_label);
1031             visitor.visit_block(block);
1032         }
1033         ExprKind::Match(ref subexpression, ref arms, _) => {
1034             visitor.visit_expr(subexpression);
1035             walk_list!(visitor, visit_arm, arms);
1036         }
1037         ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => {
1038             visitor.visit_fn(FnKind::Closure(&expression.attrs),
1039                              function_declaration,
1040                              body,
1041                              expression.span,
1042                              expression.id)
1043         }
1044         ExprKind::Block(ref block, ref opt_label) => {
1045             walk_list!(visitor, visit_label, opt_label);
1046             visitor.visit_block(block);
1047         }
1048         ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1049             visitor.visit_expr(right_hand_expression);
1050             visitor.visit_expr(left_hand_expression)
1051         }
1052         ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
1053             visitor.visit_expr(right_expression);
1054             visitor.visit_expr(left_expression)
1055         }
1056         ExprKind::Field(ref subexpression, ident) => {
1057             visitor.visit_expr(subexpression);
1058             visitor.visit_ident(ident);
1059         }
1060         ExprKind::Index(ref main_expression, ref index_expression) => {
1061             visitor.visit_expr(main_expression);
1062             visitor.visit_expr(index_expression)
1063         }
1064         ExprKind::Path(ref qpath) => {
1065             visitor.visit_qpath(qpath, expression.id, expression.span);
1066         }
1067         ExprKind::Break(ref destination, ref opt_expr) => {
1068             if let Some(ref label) = destination.label {
1069                 visitor.visit_label(label);
1070                 match destination.target_id {
1071                     Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1072                     Err(_) => {},
1073                 };
1074             }
1075             walk_list!(visitor, visit_expr, opt_expr);
1076         }
1077         ExprKind::Continue(ref destination) => {
1078             if let Some(ref label) = destination.label {
1079                 visitor.visit_label(label);
1080                 match destination.target_id {
1081                     Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1082                     Err(_) => {},
1083                 };
1084             }
1085         }
1086         ExprKind::Ret(ref optional_expression) => {
1087             walk_list!(visitor, visit_expr, optional_expression);
1088         }
1089         ExprKind::InlineAsm(_, ref outputs, ref inputs) => {
1090             for output in outputs {
1091                 visitor.visit_expr(output)
1092             }
1093             for input in inputs {
1094                 visitor.visit_expr(input)
1095             }
1096         }
1097         ExprKind::Yield(ref subexpression) => {
1098             visitor.visit_expr(subexpression);
1099         }
1100     }
1101 }
1102
1103 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1104     walk_list!(visitor, visit_pat, &arm.pats);
1105     walk_list!(visitor, visit_expr, &arm.guard);
1106     visitor.visit_expr(&arm.body);
1107     walk_list!(visitor, visit_attribute, &arm.attrs);
1108 }
1109
1110 pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
1111     if let VisibilityKind::Restricted { ref path, id } = vis.node {
1112         visitor.visit_id(id);
1113         visitor.visit_path(path, id)
1114     }
1115 }
1116
1117 pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssociatedItemKind) {
1118     // No visitable content here: this fn exists so you can call it if
1119     // the right thing to do, should content be added in the future,
1120     // would be to walk it.
1121 }
1122
1123 pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) {
1124     // No visitable content here: this fn exists so you can call it if
1125     // the right thing to do, should content be added in the future,
1126     // would be to walk it.
1127 }
1128
1129 #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
1130 pub struct IdRange {
1131     pub min: NodeId,
1132     pub max: NodeId,
1133 }
1134
1135 impl IdRange {
1136     pub fn max() -> IdRange {
1137         IdRange {
1138             min: NodeId::from_u32(u32::MAX),
1139             max: NodeId::from_u32(u32::MIN),
1140         }
1141     }
1142
1143     pub fn empty(&self) -> bool {
1144         self.min >= self.max
1145     }
1146
1147     pub fn contains(&self, id: NodeId) -> bool {
1148         id >= self.min && id < self.max
1149     }
1150
1151     pub fn add(&mut self, id: NodeId) {
1152         self.min = cmp::min(self.min, id);
1153         self.max = cmp::max(self.max, NodeId::from_u32(id.as_u32() + 1));
1154     }
1155
1156 }
1157
1158
1159 pub struct IdRangeComputingVisitor<'a, 'hir: 'a> {
1160     result: IdRange,
1161     map: &'a map::Map<'hir>,
1162 }
1163
1164 impl<'a, 'hir> IdRangeComputingVisitor<'a, 'hir> {
1165     pub fn new(map: &'a map::Map<'hir>) -> IdRangeComputingVisitor<'a, 'hir> {
1166         IdRangeComputingVisitor { result: IdRange::max(), map: map }
1167     }
1168
1169     pub fn result(&self) -> IdRange {
1170         self.result
1171     }
1172 }
1173
1174 impl<'a, 'hir> Visitor<'hir> for IdRangeComputingVisitor<'a, 'hir> {
1175     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
1176         NestedVisitorMap::OnlyBodies(&self.map)
1177     }
1178
1179     fn visit_id(&mut self, id: NodeId) {
1180         self.result.add(id);
1181     }
1182 }