1
Fork 0

or-patterns: adjust save_analysis wrt. process_var_decl{_multi}.

This commit is contained in:
Mazdak Farrokhzad 2019-09-02 02:00:44 +02:00
parent a867c5f9cc
commit be95dee5a1
2 changed files with 15 additions and 83 deletions

View file

@ -897,32 +897,23 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
} }
} }
fn process_var_decl_multi(&mut self, pats: &'l [P<ast::Pat>]) { fn process_var_decl(&mut self, pat: &'l ast::Pat) {
// The pattern could declare multiple new vars,
// we must walk the pattern and collect them all.
let mut collector = PathCollector::new(); let mut collector = PathCollector::new();
for pattern in pats { collector.visit_pat(&pat);
// collect paths from the arm's patterns self.visit_pat(&pat);
collector.visit_pat(&pattern);
self.visit_pat(&pattern);
}
// process collected paths // Process collected paths.
for (id, ident, immut) in collector.collected_idents { for (id, ident, _) in collector.collected_idents {
match self.save_ctxt.get_path_res(id) { match self.save_ctxt.get_path_res(id) {
Res::Local(hir_id) => { Res::Local(hir_id) => {
let mut value = if immut == ast::Mutability::Immutable {
self.span.snippet(ident.span)
} else {
"<mutable>".to_owned()
};
let id = self.tcx.hir().hir_to_node_id(hir_id); let id = self.tcx.hir().hir_to_node_id(hir_id);
let typ = self.save_ctxt let typ = self.save_ctxt.tables.node_type_opt(hir_id)
.tables
.node_type_opt(hir_id)
.map(|t| t.to_string()) .map(|t| t.to_string())
.unwrap_or_default(); .unwrap_or_default();
value.push_str(": ");
value.push_str(&typ);
// Rust uses the id of the pattern for var lookups, so we'll use it too.
if !self.span.filter_generated(ident.span) { if !self.span.filter_generated(ident.span) {
let qualname = format!("{}${}", ident.to_string(), id); let qualname = format!("{}${}", ident.to_string(), id);
let id = id_from_node_id(id, &self.save_ctxt); let id = id_from_node_id(id, &self.save_ctxt);
@ -972,61 +963,6 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
} }
} }
fn process_var_decl(&mut self, p: &'l ast::Pat, value: String) {
// The local could declare multiple new vars, we must walk the
// pattern and collect them all.
let mut collector = PathCollector::new();
collector.visit_pat(&p);
self.visit_pat(&p);
for (id, ident, immut) in collector.collected_idents {
let mut value = match immut {
ast::Mutability::Immutable => value.to_string(),
_ => String::new(),
};
let hir_id = self.tcx.hir().node_to_hir_id(id);
let typ = match self.save_ctxt.tables.node_type_opt(hir_id) {
Some(typ) => {
let typ = typ.to_string();
if !value.is_empty() {
value.push_str(": ");
}
value.push_str(&typ);
typ
}
None => String::new(),
};
// Rust uses the id of the pattern for var lookups, so we'll use it too.
if !self.span.filter_generated(ident.span) {
let qualname = format!("{}${}", ident.to_string(), id);
let id = id_from_node_id(id, &self.save_ctxt);
let span = self.span_from_span(ident.span);
self.dumper.dump_def(
&Access {
public: false,
reachable: false,
},
Def {
kind: DefKind::Local,
id,
span,
name: ident.to_string(),
qualname,
value: typ,
parent: None,
children: vec![],
decl_id: None,
docs: String::new(),
sig: None,
attributes: vec![],
},
);
}
}
}
/// Extracts macro use and definition information from the AST node defined /// Extracts macro use and definition information from the AST node defined
/// by the given NodeId, using the expansion information from the node's /// by the given NodeId, using the expansion information from the node's
/// span. /// span.
@ -1565,14 +1501,13 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
}); });
} }
ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) => { ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) => {
let value = self.span.snippet(subexpression.span); self.process_var_decl(pattern);
self.process_var_decl(pattern, value);
debug!("for loop, walk sub-expr: {:?}", subexpression.node); debug!("for loop, walk sub-expr: {:?}", subexpression.node);
self.visit_expr(subexpression); self.visit_expr(subexpression);
visit::walk_block(self, block); visit::walk_block(self, block);
} }
ast::ExprKind::Let(ref pats, ref scrutinee) => { ast::ExprKind::Let(ref pat, ref scrutinee) => {
self.process_var_decl_multi(pats); self.process_var_decl(pat);
self.visit_expr(scrutinee); self.visit_expr(scrutinee);
} }
ast::ExprKind::Repeat(ref element, ref count) => { ast::ExprKind::Repeat(ref element, ref count) => {
@ -1599,7 +1534,7 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
} }
fn visit_arm(&mut self, arm: &'l ast::Arm) { fn visit_arm(&mut self, arm: &'l ast::Arm) {
self.process_var_decl_multi(&arm.pats); self.process_var_decl(&arm.pat);
if let Some(expr) = &arm.guard { if let Some(expr) = &arm.guard {
self.visit_expr(expr); self.visit_expr(expr);
} }
@ -1617,11 +1552,7 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
fn visit_local(&mut self, l: &'l ast::Local) { fn visit_local(&mut self, l: &'l ast::Local) {
self.process_macro_use(l.span); self.process_macro_use(l.span);
let value = l.init self.process_var_decl(&l.pat);
.as_ref()
.map(|i| self.span.snippet(i.span))
.unwrap_or_default();
self.process_var_decl(&l.pat, value);
// Just walk the initialiser and type (don't want to walk the pattern again). // Just walk the initialiser and type (don't want to walk the pattern again).
walk_list!(self, visit_ty, &l.ty); walk_list!(self, visit_ty, &l.ty);

View file

@ -1,5 +1,6 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(nll)] #![feature(nll)]
#![feature(inner_deref)]
#![recursion_limit="256"] #![recursion_limit="256"]