提交 4bfb0453 编写于 作者: M Matthew Jasper

Give match arms an HirId and a Span

上级 615c23f6
......@@ -1102,6 +1102,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
}
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
visitor.visit_id(arm.hir_id);
walk_list!(visitor, visit_pat, &arm.pats);
if let Some(ref g) = arm.guard {
match g {
......
......@@ -1313,7 +1313,10 @@ fn lower_token(&mut self, token: Token, span: Span) -> TokenStream {
}
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
let LoweredNodeId { node_id: _, hir_id } = self.next_id();
hir::Arm {
hir_id,
attrs: self.lower_attrs(&arm.attrs),
pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
guard: match arm.guard {
......@@ -1321,6 +1324,7 @@ fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
_ => None,
},
body: P(self.lower_expr(&arm.body)),
span: arm.span,
}
}
......@@ -5023,10 +5027,14 @@ fn lower_trait_bound_modifier(&mut self, f: TraitBoundModifier) -> hir::TraitBou
// Helper methods for building HIR.
fn arm(&mut self, pats: hir::HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
let LoweredNodeId { node_id: _, hir_id } = self.next_id();
hir::Arm {
hir_id,
attrs: hir_vec![],
pats,
guard: None,
span: expr.span,
body: expr,
}
}
......
......@@ -430,6 +430,16 @@ fn visit_pat(&mut self, pat: &'hir Pat) {
});
}
fn visit_arm(&mut self, arm: &'hir Arm) {
let node = Node::Arm(arm);
self.insert(arm.span, arm.hir_id, node);
self.with_parent(arm.hir_id, |this| {
intravisit::walk_arm(this, arm);
});
}
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
......
......@@ -373,6 +373,7 @@ fn def_kind(&self, node_id: NodeId) -> Option<DefKind> {
Node::Pat(_) |
Node::Binding(_) |
Node::Local(_) |
Node::Arm(_) |
Node::Lifetime(_) |
Node::Visibility(_) |
Node::Block(_) |
......@@ -1000,6 +1001,7 @@ pub fn attrs_by_hir_id(&self, id: HirId) -> &'hir [ast::Attribute] {
Some(Node::Field(ref f)) => Some(&f.attrs[..]),
Some(Node::Expr(ref e)) => Some(&*e.attrs),
Some(Node::Stmt(ref s)) => Some(s.node.attrs()),
Some(Node::Arm(ref a)) => Some(&*a.attrs),
Some(Node::GenericParam(param)) => Some(&param.attrs[..]),
// Unit/tuple structs/variants take the attributes straight from
// the struct/variant definition.
......@@ -1073,6 +1075,7 @@ pub fn span_by_hir_id(&self, hir_id: HirId) -> Span {
Some(Node::TraitRef(tr)) => tr.path.span,
Some(Node::Binding(pat)) => pat.span,
Some(Node::Pat(pat)) => pat.span,
Some(Node::Arm(arm)) => arm.span,
Some(Node::Block(block)) => block.span,
Some(Node::Ctor(..)) => match self.find_by_hir_id(
self.get_parent_node_by_hir_id(hir_id))
......@@ -1288,6 +1291,7 @@ pub fn print_node(&mut self, node: Node<'_>) -> io::Result<()> {
Node::TraitRef(a) => self.print_trait_ref(&a),
Node::Binding(a) |
Node::Pat(a) => self.print_pat(&a),
Node::Arm(a) => self.print_arm(&a),
Node::Block(a) => {
use syntax::print::pprust::PrintState;
......@@ -1417,6 +1421,9 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
Some(Node::Pat(_)) => {
format!("pat {}{}", map.hir_to_pretty_string(id), id_str)
}
Some(Node::Arm(_)) => {
format!("arm {}{}", map.hir_to_pretty_string(id), id_str)
}
Some(Node::Block(_)) => {
format!("block {}{}", map.hir_to_pretty_string(id), id_str)
}
......
......@@ -1228,6 +1228,9 @@ pub struct Local {
/// `<pats> (if <guard>) => <body>`.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
pub struct Arm {
#[stable_hasher(ignore)]
pub hir_id: HirId,
pub span: Span,
pub attrs: HirVec<Attribute>,
/// Multiple patterns can be combined with `|`
pub pats: HirVec<P<Pat>>,
......@@ -2656,6 +2659,7 @@ pub enum Node<'hir> {
TraitRef(&'hir TraitRef),
Binding(&'hir Pat),
Pat(&'hir Pat),
Arm(&'hir Arm),
Block(&'hir Block),
Local(&'hir Local),
MacroDef(&'hir MacroDef),
......
......@@ -1862,7 +1862,7 @@ pub fn print_pat(&mut self, pat: &hir::Pat) -> io::Result<()> {
self.ann.post(self, AnnNode::Pat(pat))
}
fn print_arm(&mut self, arm: &hir::Arm) -> io::Result<()> {
pub fn print_arm(&mut self, arm: &hir::Arm) -> io::Result<()> {
// I have no idea why this check is necessary, but here it
// is :(
if arm.attrs.is_empty() {
......
......@@ -149,7 +149,7 @@ fn visit_stmt(&mut self, s: &'v hir::Stmt) {
}
fn visit_arm(&mut self, a: &'v hir::Arm) {
self.record("Arm", Id::None, a);
self.record("Arm", Id::Node(a.hir_id), a);
hir_visit::walk_arm(self, a)
}
......
......@@ -781,14 +781,17 @@ fn if_fallback_coercion(
fn maybe_get_coercion_reason(&self, hir_id: hir::HirId, span: Span) -> Option<(Span, String)> {
use hir::Node::{Block, Item, Local};
let node = self.tcx.hir().get_by_hir_id(self.tcx.hir().get_parent_node_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(hir_id),
));
let hir = self.tcx.hir();
let arm_id = hir.get_parent_node_by_hir_id(hir_id);
let match_id = hir.get_parent_node_by_hir_id(arm_id);
let containing_id = hir.get_parent_node_by_hir_id(match_id);
let node = hir.get_by_hir_id(containing_id);
if let Block(block) = node {
// check that the body's parent is an fn
let parent = self.tcx.hir().get_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(block.hir_id),
let parent = hir.get_by_hir_id(
hir.get_parent_node_by_hir_id(
hir.get_parent_node_by_hir_id(block.hir_id),
),
);
if let (Some(expr), Item(hir::Item {
......
......@@ -908,6 +908,7 @@ pub struct Arm {
pub pats: Vec<P<Pat>>,
pub guard: Option<Guard>,
pub body: P<Expr>,
pub span: Span,
}
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
......
......@@ -890,12 +890,13 @@ fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
self.pat_tuple_struct(span, path, vec![pat])
}
fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
ast::Arm {
attrs: vec![],
pats,
guard: None,
body: expr,
span,
}
}
......
......@@ -392,11 +392,15 @@ pub fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) {
vis.visit_span(span);
}
pub fn noop_visit_arm<T: MutVisitor>(Arm { attrs, pats, guard, body }: &mut Arm, vis: &mut T) {
pub fn noop_visit_arm<T: MutVisitor>(
Arm { attrs, pats, guard, body, span }: &mut Arm,
vis: &mut T,
) {
visit_attrs(attrs, vis);
visit_vec(pats, |pat| vis.visit_pat(pat));
visit_opt(guard, |guard| vis.visit_guard(guard));
vis.visit_expr(body);
vis.visit_span(span);
}
pub fn noop_visit_guard<T: MutVisitor>(g: &mut Guard, vis: &mut T) {
......
......@@ -3946,6 +3946,7 @@ fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<E
crate fn parse_arm(&mut self) -> PResult<'a, Arm> {
let attrs = self.parse_outer_attributes()?;
let lo = self.span;
let pats = self.parse_pats()?;
let guard = if self.eat_keyword(keywords::If) {
Some(Guard::If(self.parse_expr()?))
......@@ -3965,6 +3966,8 @@ fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<E
let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
&& self.token != token::CloseDelim(token::Brace);
let hi = self.span;
if require_comma {
let cm = self.sess.source_map();
self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)])
......@@ -4008,6 +4011,7 @@ fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<E
pats,
guard,
body: expr,
span: lo.to(hi),
})
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册