diff options
| author | bors <bors@rust-lang.org> | 2017-08-02 13:40:35 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-08-02 13:40:35 +0000 |
| commit | cd87b5d5162c7696498a5c1e17e1f7af68202eca (patch) | |
| tree | f7a93522c3616f676d093d6e1ad50627db1de832 | |
| parent | 22f256f69e61a0f2ceb7eb842589597f6fe4ce37 (diff) | |
| parent | e92ddbf5f7b9b1c6d61f3b6138035dfd5eee2c68 (diff) | |
| download | rust-cd87b5d5162c7696498a5c1e17e1f7af68202eca.tar.gz rust-cd87b5d5162c7696498a5c1e17e1f7af68202eca.zip | |
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...
| -rw-r--r-- | src/librustc/hir/map/mod.rs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 3fdd9c34f46..45b1d6c1841 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -553,7 +553,9 @@ impl<'hir> Map<'hir> { } /// Similar to get_parent, returns the parent node id or id if there is no - /// parent. + /// parent. Note that the parent may be CRATE_NODE_ID, which is not itself + /// present in the map -- so passing the return value of get_parent_node to + /// get may actually panic. /// This function returns the immediate parent in the AST, whereas get_parent /// returns the enclosing item. Note that this might not be the actual parent /// node in the AST - some kinds of nodes are not in the map and these will @@ -629,7 +631,7 @@ impl<'hir> Map<'hir> { } /// Retrieve the NodeId for `id`'s enclosing method, unless there's a - /// `while` or `loop` before reacing it, as block tail returns are not + /// `while` or `loop` before reaching it, as block tail returns are not /// available in them. /// /// ``` |
