Merge pull request #3113 from mikerite/fix-3112

Fix #3112
This commit is contained in:
Manish Goregaokar 2018-09-02 13:29:06 +05:30 committed by GitHub
commit 131c8f86b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 8 deletions

View file

@ -109,7 +109,9 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
self.visit_expr(e); self.visit_expr(e);
for arm in arms { for arm in arms {
if let Some(ref guard) = arm.guard { if let Some(ref guard) = arm.guard {
self.visit_expr(guard); match guard {
Guard::If(if_expr) => self.visit_expr(if_expr),
}
} }
// make sure top level arm expressions aren't linted // make sure top level arm expressions aren't linted
self.maybe_walk_expr(&*arm.body); self.maybe_walk_expr(&*arm.body);

View file

@ -339,7 +339,9 @@ fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings:
check_pat(cx, pat, Some(&**init), pat.span, bindings); check_pat(cx, pat, Some(&**init), pat.span, bindings);
// This is ugly, but needed to get the right type // This is ugly, but needed to get the right type
if let Some(ref guard) = arm.guard { if let Some(ref guard) = arm.guard {
check_expr(cx, guard, bindings); match guard {
Guard::If(if_expr) => check_expr(cx, if_expr, bindings),
}
} }
check_expr(cx, &arm.body, bindings); check_expr(cx, &arm.body, bindings);
bindings.truncate(len); bindings.truncate(len);

View file

@ -345,9 +345,15 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
self.visit_expr(&arm.body); self.visit_expr(&arm.body);
if let Some(ref guard) = arm.guard { if let Some(ref guard) = arm.guard {
let guard_pat = self.next("guard"); let guard_pat = self.next("guard");
println!(" if let Some(ref {}) = {}[{}].guard", guard_pat, arms_pat, i); println!(" if let Some(ref {}) = {}[{}].guard;", guard_pat, arms_pat, i);
self.current = guard_pat; match guard {
self.visit_expr(guard); hir::Guard::If(ref if_expr) => {
let if_expr_pat = self.next("expr");
println!(" if let Guard::If(ref {}) = {};", if_expr_pat, guard_pat);
self.current = if_expr_pat;
self.visit_expr(if_expr);
}
}
} }
println!(" if {}[{}].pats.len() == {};", arms_pat, i, arm.pats.len()); println!(" if {}[{}].pats.len() == {};", arms_pat, i, arm.pats.len());
for (j, pat) in arm.pats.iter().enumerate() { for (j, pat) in arm.pats.iter().enumerate() {

View file

@ -113,7 +113,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
}, },
(&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => { (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| { ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| {
self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r)) self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
&& over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r)) && over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
}) })
}, },
@ -152,6 +152,12 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr) left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
} }
fn eq_guard(&mut self, left: &Guard, right: &Guard) -> bool {
match (left, right) {
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
}
}
fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool { fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
match (left, right) { match (left, right) {
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt), (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt),
@ -497,7 +503,7 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
for arm in arms { for arm in arms {
// TODO: arm.pat? // TODO: arm.pat?
if let Some(ref e) = arm.guard { if let Some(ref e) = arm.guard {
self.hash_expr(e); self.hash_guard(e);
} }
self.hash_expr(&arm.body); self.hash_expr(&arm.body);
} }
@ -637,4 +643,14 @@ impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
}, },
} }
} }
pub fn hash_guard(&mut self, g: &Guard) {
match g {
Guard::If(ref expr) => {
let c: fn(_) -> _ = Guard::If;
c.hash(&mut self.s);
self.hash_expr(expr);
}
}
}
} }

View file

@ -113,7 +113,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
} }
if let Some(ref guard) = arm.guard { if let Some(ref guard) = arm.guard {
println!("guard:"); println!("guard:");
print_expr(cx, guard, 1); print_guard(cx, guard, 1);
} }
println!("body:"); println!("body:");
print_expr(cx, &arm.body, 1); print_expr(cx, &arm.body, 1);
@ -515,3 +515,14 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) {
}, },
} }
} }
fn print_guard(cx: &LateContext<'_, '_>, guard: &hir::Guard, indent: usize) {
let ind = " ".repeat(indent);
println!("{}+", ind);
match guard {
hir::Guard::If(expr) => {
println!("{}If", ind);
print_expr(cx, expr, indent + 1);
}
}
}