Skip to content

Commit cd87b5d

Browse files
committed
Auto merge of #43605 - RalfJung:mapdoc, r=michaelwoerister
Improve hir::map::Map::get_parent_node doc The documentation says ``` /// Similar to get_parent, returns the parent node id or id if there is no /// parent. /// This function returns the immediate parent in the AST, whereas get_parent /// returns the enclosing item. ``` One would think that one can walk up the tree by repeatedly calling `get_parent_node` until it returns the argument, and then work on the `NodeId`s that arise. However, that is not true: `get_parent_node` will return id 0 (the crate itself) for items that sit directly in the crate; calling `get` on that `NodeId` will panic. So, the fact that `get_parent_node` returns the root when passed the root is actually not really useful, because the root itself is already a somewhat degenerate node. This improves the doc so hopefully people writing code that "walks up the tree" don't run into this issue like I did...
2 parents 22f256f + e92ddbf commit cd87b5d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,9 @@ impl<'hir> Map<'hir> {
553553
}
554554

555555
/// Similar to get_parent, returns the parent node id or id if there is no
556-
/// parent.
556+
/// parent. Note that the parent may be CRATE_NODE_ID, which is not itself
557+
/// present in the map -- so passing the return value of get_parent_node to
558+
/// get may actually panic.
557559
/// This function returns the immediate parent in the AST, whereas get_parent
558560
/// returns the enclosing item. Note that this might not be the actual parent
559561
/// node in the AST - some kinds of nodes are not in the map and these will
@@ -629,7 +631,7 @@ impl<'hir> Map<'hir> {
629631
}
630632

631633
/// Retrieve the NodeId for `id`'s enclosing method, unless there's a
632-
/// `while` or `loop` before reacing it, as block tail returns are not
634+
/// `while` or `loop` before reaching it, as block tail returns are not
633635
/// available in them.
634636
///
635637
/// ```

0 commit comments

Comments
 (0)