Skip to content

Commit 4bfb045

Browse files
committed
Give match arms an HirId and a Span
1 parent 615c23f commit 4bfb045

File tree

12 files changed

+53
-10
lines changed

12 files changed

+53
-10
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
11021102
}
11031103

11041104
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
1105+
visitor.visit_id(arm.hir_id);
11051106
walk_list!(visitor, visit_pat, &arm.pats);
11061107
if let Some(ref g) = arm.guard {
11071108
match g {

src/librustc/hir/lowering.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,14 +1313,18 @@ impl<'a> LoweringContext<'a> {
13131313
}
13141314

13151315
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
1316+
let LoweredNodeId { node_id: _, hir_id } = self.next_id();
1317+
13161318
hir::Arm {
1319+
hir_id,
13171320
attrs: self.lower_attrs(&arm.attrs),
13181321
pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
13191322
guard: match arm.guard {
13201323
Some(Guard::If(ref x)) => Some(hir::Guard::If(P(self.lower_expr(x)))),
13211324
_ => None,
13221325
},
13231326
body: P(self.lower_expr(&arm.body)),
1327+
span: arm.span,
13241328
}
13251329
}
13261330

@@ -5023,10 +5027,14 @@ impl<'a> LoweringContext<'a> {
50235027
// Helper methods for building HIR.
50245028

50255029
fn arm(&mut self, pats: hir::HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
5030+
let LoweredNodeId { node_id: _, hir_id } = self.next_id();
5031+
50265032
hir::Arm {
5033+
hir_id,
50275034
attrs: hir_vec![],
50285035
pats,
50295036
guard: None,
5037+
span: expr.span,
50305038
body: expr,
50315039
}
50325040
}

src/librustc/hir/map/collector.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,16 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
430430
});
431431
}
432432

433+
fn visit_arm(&mut self, arm: &'hir Arm) {
434+
let node = Node::Arm(arm);
435+
436+
self.insert(arm.span, arm.hir_id, node);
437+
438+
self.with_parent(arm.hir_id, |this| {
439+
intravisit::walk_arm(this, arm);
440+
});
441+
}
442+
433443
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
434444
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
435445

src/librustc/hir/map/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ impl<'hir> Map<'hir> {
373373
Node::Pat(_) |
374374
Node::Binding(_) |
375375
Node::Local(_) |
376+
Node::Arm(_) |
376377
Node::Lifetime(_) |
377378
Node::Visibility(_) |
378379
Node::Block(_) |
@@ -1000,6 +1001,7 @@ impl<'hir> Map<'hir> {
10001001
Some(Node::Field(ref f)) => Some(&f.attrs[..]),
10011002
Some(Node::Expr(ref e)) => Some(&*e.attrs),
10021003
Some(Node::Stmt(ref s)) => Some(s.node.attrs()),
1004+
Some(Node::Arm(ref a)) => Some(&*a.attrs),
10031005
Some(Node::GenericParam(param)) => Some(&param.attrs[..]),
10041006
// Unit/tuple structs/variants take the attributes straight from
10051007
// the struct/variant definition.
@@ -1073,6 +1075,7 @@ impl<'hir> Map<'hir> {
10731075
Some(Node::TraitRef(tr)) => tr.path.span,
10741076
Some(Node::Binding(pat)) => pat.span,
10751077
Some(Node::Pat(pat)) => pat.span,
1078+
Some(Node::Arm(arm)) => arm.span,
10761079
Some(Node::Block(block)) => block.span,
10771080
Some(Node::Ctor(..)) => match self.find_by_hir_id(
10781081
self.get_parent_node_by_hir_id(hir_id))
@@ -1288,6 +1291,7 @@ impl<'a> print::State<'a> {
12881291
Node::TraitRef(a) => self.print_trait_ref(&a),
12891292
Node::Binding(a) |
12901293
Node::Pat(a) => self.print_pat(&a),
1294+
Node::Arm(a) => self.print_arm(&a),
12911295
Node::Block(a) => {
12921296
use syntax::print::pprust::PrintState;
12931297

@@ -1417,6 +1421,9 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
14171421
Some(Node::Pat(_)) => {
14181422
format!("pat {}{}", map.hir_to_pretty_string(id), id_str)
14191423
}
1424+
Some(Node::Arm(_)) => {
1425+
format!("arm {}{}", map.hir_to_pretty_string(id), id_str)
1426+
}
14201427
Some(Node::Block(_)) => {
14211428
format!("block {}{}", map.hir_to_pretty_string(id), id_str)
14221429
}

src/librustc/hir/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,9 @@ pub struct Local {
12281228
/// `<pats> (if <guard>) => <body>`.
12291229
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
12301230
pub struct Arm {
1231+
#[stable_hasher(ignore)]
1232+
pub hir_id: HirId,
1233+
pub span: Span,
12311234
pub attrs: HirVec<Attribute>,
12321235
/// Multiple patterns can be combined with `|`
12331236
pub pats: HirVec<P<Pat>>,
@@ -2656,6 +2659,7 @@ pub enum Node<'hir> {
26562659
TraitRef(&'hir TraitRef),
26572660
Binding(&'hir Pat),
26582661
Pat(&'hir Pat),
2662+
Arm(&'hir Arm),
26592663
Block(&'hir Block),
26602664
Local(&'hir Local),
26612665
MacroDef(&'hir MacroDef),

src/librustc/hir/print.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,7 @@ impl<'a> State<'a> {
18621862
self.ann.post(self, AnnNode::Pat(pat))
18631863
}
18641864

1865-
fn print_arm(&mut self, arm: &hir::Arm) -> io::Result<()> {
1865+
pub fn print_arm(&mut self, arm: &hir::Arm) -> io::Result<()> {
18661866
// I have no idea why this check is necessary, but here it
18671867
// is :(
18681868
if arm.attrs.is_empty() {

src/librustc_passes/hir_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
149149
}
150150

151151
fn visit_arm(&mut self, a: &'v hir::Arm) {
152-
self.record("Arm", Id::None, a);
152+
self.record("Arm", Id::Node(a.hir_id), a);
153153
hir_visit::walk_arm(self, a)
154154
}
155155

src/librustc_typeck/check/_match.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,17 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
781781
fn maybe_get_coercion_reason(&self, hir_id: hir::HirId, span: Span) -> Option<(Span, String)> {
782782
use hir::Node::{Block, Item, Local};
783783

784-
let node = self.tcx.hir().get_by_hir_id(self.tcx.hir().get_parent_node_by_hir_id(
785-
self.tcx.hir().get_parent_node_by_hir_id(hir_id),
786-
));
784+
let hir = self.tcx.hir();
785+
let arm_id = hir.get_parent_node_by_hir_id(hir_id);
786+
let match_id = hir.get_parent_node_by_hir_id(arm_id);
787+
let containing_id = hir.get_parent_node_by_hir_id(match_id);
788+
789+
let node = hir.get_by_hir_id(containing_id);
787790
if let Block(block) = node {
788791
// check that the body's parent is an fn
789-
let parent = self.tcx.hir().get_by_hir_id(
790-
self.tcx.hir().get_parent_node_by_hir_id(
791-
self.tcx.hir().get_parent_node_by_hir_id(block.hir_id),
792+
let parent = hir.get_by_hir_id(
793+
hir.get_parent_node_by_hir_id(
794+
hir.get_parent_node_by_hir_id(block.hir_id),
792795
),
793796
);
794797
if let (Some(expr), Item(hir::Item {

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ pub struct Arm {
908908
pub pats: Vec<P<Pat>>,
909909
pub guard: Option<Guard>,
910910
pub body: P<Expr>,
911+
pub span: Span,
911912
}
912913

913914
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]

src/libsyntax/ext/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,12 +890,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
890890
self.pat_tuple_struct(span, path, vec![pat])
891891
}
892892

893-
fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
893+
fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
894894
ast::Arm {
895895
attrs: vec![],
896896
pats,
897897
guard: None,
898898
body: expr,
899+
span,
899900
}
900901
}
901902

src/libsyntax/mut_visit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,15 @@ pub fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {
392392
vis.visit_span(span);
393393
}
394394

395-
pub fn noop_visit_arm<T: MutVisitor>(Arm { attrs, pats, guard, body }: &mut Arm, vis: &mut T) {
395+
pub fn noop_visit_arm<T: MutVisitor>(
396+
Arm { attrs, pats, guard, body, span }: &mut Arm,
397+
vis: &mut T,
398+
) {
396399
visit_attrs(attrs, vis);
397400
visit_vec(pats, |pat| vis.visit_pat(pat));
398401
visit_opt(guard, |guard| vis.visit_guard(guard));
399402
vis.visit_expr(body);
403+
vis.visit_span(span);
400404
}
401405

402406
pub fn noop_visit_guard<T: MutVisitor>(g: &mut Guard, vis: &mut T) {

src/libsyntax/parse/parser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,7 @@ impl<'a> Parser<'a> {
39463946

39473947
crate fn parse_arm(&mut self) -> PResult<'a, Arm> {
39483948
let attrs = self.parse_outer_attributes()?;
3949+
let lo = self.span;
39493950
let pats = self.parse_pats()?;
39503951
let guard = if self.eat_keyword(keywords::If) {
39513952
Some(Guard::If(self.parse_expr()?))
@@ -3965,6 +3966,8 @@ impl<'a> Parser<'a> {
39653966
let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
39663967
&& self.token != token::CloseDelim(token::Brace);
39673968

3969+
let hi = self.span;
3970+
39683971
if require_comma {
39693972
let cm = self.sess.source_map();
39703973
self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)])
@@ -4008,6 +4011,7 @@ impl<'a> Parser<'a> {
40084011
pats,
40094012
guard,
40104013
body: expr,
4014+
span: lo.to(hi),
40114015
})
40124016
}
40134017

0 commit comments

Comments
 (0)