Skip to content

Commit a7b00f5

Browse files
committed
let_chains: Handle disallowing of let chains in places lowering won't support.
1 parent fcffac5 commit a7b00f5

File tree

2 files changed

+67
-58
lines changed

2 files changed

+67
-58
lines changed

src/librustc_passes/ast_validation.rs

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ use syntax::attr;
1717
use syntax::feature_gate::is_builtin_attr;
1818
use syntax::source_map::Spanned;
1919
use syntax::symbol::{kw, sym};
20-
use syntax::ptr::P;
2120
use syntax::visit::{self, Visitor};
2221
use syntax::{span_err, struct_span_err, walk_list};
2322
use syntax_ext::proc_macro_decls::is_proc_macro_attr;
2423
use syntax_pos::{Span, MultiSpan};
2524
use errors::{Applicability, FatalError};
26-
use log::debug;
2725

2826
#[derive(Copy, Clone, Debug)]
2927
struct OuterImplTrait {
@@ -76,20 +74,34 @@ struct AstValidator<'a> {
7674
/// these booleans.
7775
warning_period_57979_didnt_record_next_impl_trait: bool,
7876
warning_period_57979_impl_trait_in_proj: bool,
77+
78+
/// Used to ban `let` expressions in inappropriate places.
79+
is_let_allowed: bool,
80+
}
81+
82+
/// With the `new` value in `store`,
83+
/// runs and returns the `scoped` computation,
84+
/// resetting the old value of `store` after,
85+
/// and returning the result of `scoped`.
86+
fn with<C, T, S>(
87+
this: &mut C,
88+
new: S,
89+
store: impl Fn(&mut C) -> &mut S,
90+
scoped: impl FnOnce(&mut C) -> T
91+
) -> T {
92+
let old = mem::replace(store(this), new);
93+
let ret = scoped(this);
94+
*store(this) = old;
95+
ret
7996
}
8097

8198
impl<'a> AstValidator<'a> {
8299
fn with_impl_trait_in_proj_warning<T>(&mut self, v: bool, f: impl FnOnce(&mut Self) -> T) -> T {
83-
let old = mem::replace(&mut self.warning_period_57979_impl_trait_in_proj, v);
84-
let ret = f(self);
85-
self.warning_period_57979_impl_trait_in_proj = old;
86-
ret
100+
with(self, v, |this| &mut this.warning_period_57979_impl_trait_in_proj, f)
87101
}
88102

89103
fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
90-
let old = mem::replace(&mut self.is_impl_trait_banned, true);
91-
f(self);
92-
self.is_impl_trait_banned = old;
104+
with(self, true, |this| &mut this.is_impl_trait_banned, f)
93105
}
94106

95107
fn with_banned_assoc_ty_bound(&mut self, f: impl FnOnce(&mut Self)) {
@@ -99,9 +111,11 @@ impl<'a> AstValidator<'a> {
99111
}
100112

101113
fn with_impl_trait(&mut self, outer: Option<OuterImplTrait>, f: impl FnOnce(&mut Self)) {
102-
let old = mem::replace(&mut self.outer_impl_trait, outer);
103-
f(self);
104-
self.outer_impl_trait = old;
114+
with(self, outer, |this| &mut this.outer_impl_trait, f)
115+
}
116+
117+
fn with_let_allowed(&mut self, v: bool, f: impl FnOnce(&mut Self)) {
118+
with(self, v, |this| &mut this.is_let_allowed, f)
105119
}
106120

107121
fn visit_assoc_ty_constraint_from_generic_args(&mut self, constraint: &'a AssocTyConstraint) {
@@ -319,52 +333,36 @@ impl<'a> AstValidator<'a> {
319333
}
320334
}
321335

322-
/// With eRFC 2497, we need to check whether an expression is ambiguous and warn or error
323-
/// depending on the edition, this function handles that.
324-
fn while_if_let_ambiguity(&self, expr: &P<Expr>) {
325-
if let Some((span, op_kind)) = self.while_if_let_expr_ambiguity(&expr) {
326-
let mut err = self.err_handler().struct_span_err(
327-
span, &format!("ambiguous use of `{}`", op_kind.to_string())
328-
);
329-
330-
err.note(
331-
"this will be a error until the `let_chains` feature is stabilized"
332-
);
333-
err.note(
334-
"see rust-lang/rust#53668 for more information"
335-
);
336-
337-
if let Ok(snippet) = self.session.source_map().span_to_snippet(span) {
338-
err.span_suggestion(
339-
span, "consider adding parentheses", format!("({})", snippet),
340-
Applicability::MachineApplicable,
341-
);
342-
}
343-
344-
err.emit();
345-
}
346-
}
347-
348-
/// With eRFC 2497 adding if-let chains, there is a requirement that the parsing of
349-
/// `&&` and `||` in a if-let statement be unambiguous. This function returns a span and
350-
/// a `BinOpKind` (either `&&` or `||` depending on what was ambiguous) if it is determined
351-
/// that the current expression parsed is ambiguous and will break in future.
352-
fn while_if_let_expr_ambiguity(&self, expr: &P<Expr>) -> Option<(Span, BinOpKind)> {
353-
debug!("while_if_let_expr_ambiguity: expr.node: {:?}", expr.node);
336+
/// Visits the `expr` and adjusts whether `let $pat = $expr` is allowed in decendants.
337+
/// Returns whether we walked into `expr` or not.
338+
/// If we did, walking should not happen again.
339+
fn visit_expr_with_let_maybe_allowed(&mut self, expr: &'a Expr) -> bool {
354340
match &expr.node {
355-
ExprKind::Binary(op, _, _) if op.node == BinOpKind::And || op.node == BinOpKind::Or => {
356-
Some((expr.span, op.node))
357-
},
358-
ExprKind::Range(ref lhs, ref rhs, _) => {
359-
let lhs_ambiguous = lhs.as_ref()
360-
.and_then(|lhs| self.while_if_let_expr_ambiguity(lhs));
361-
let rhs_ambiguous = rhs.as_ref()
362-
.and_then(|rhs| self.while_if_let_expr_ambiguity(rhs));
363-
364-
lhs_ambiguous.or(rhs_ambiguous)
341+
// Assuming the context permits, `($expr)` does not impose additional constraints.
342+
ExprKind::Paren(_) => visit::walk_expr(self, expr),
343+
// Assuming the context permits,
344+
// l && r` allows decendants in `l` and `r` to be `let` expressions.
345+
ExprKind::Binary(op, ..) if op.node == BinOpKind::And => visit::walk_expr(self, expr),
346+
// However, we do allow it in the condition of the `if` expression.
347+
// We do not allow `let` in `then` and `opt_else` directly.
348+
ExprKind::If(ref cond, ref then, ref opt_else) => {
349+
self.with_let_allowed(false, |this| {
350+
this.visit_block(then);
351+
walk_list!(this, visit_expr, opt_else);
352+
});
353+
self.with_let_allowed(true, |this| this.visit_expr(cond));
365354
}
366-
_ => None,
355+
// The same logic applies to `While`.
356+
ExprKind::While(ref cond, ref then, ref opt_label) => {
357+
walk_list!(self, visit_label, opt_label);
358+
self.with_let_allowed(false, |this| this.visit_block(then));
359+
self.with_let_allowed(true, |this| this.visit_expr(cond));
360+
}
361+
// Don't walk into `expr` and defer further checks to the caller.
362+
_ => return false,
367363
}
364+
365+
true
368366
}
369367

370368
fn check_fn_decl(&self, fn_decl: &FnDecl) {
@@ -494,18 +492,27 @@ fn validate_generics_order<'a>(
494492
impl<'a> Visitor<'a> for AstValidator<'a> {
495493
fn visit_expr(&mut self, expr: &'a Expr) {
496494
match expr.node {
495+
ExprKind::Let(_, _) if !self.is_let_allowed => {
496+
self.err_handler()
497+
.struct_span_err(expr.span, "`let` expressions are not supported here")
498+
.note("only supported directly in conditions of `if`- and `while`-expressions")
499+
.note("as well as when nested within `&&` and parenthesis in those conditions")
500+
.emit();
501+
}
502+
_ if self.visit_expr_with_let_maybe_allowed(&expr) => {
503+
// Prevent `walk_expr` to happen since we've already done that.
504+
return;
505+
}
497506
ExprKind::Closure(_, _, _, ref fn_decl, _, _) => {
498507
self.check_fn_decl(fn_decl);
499508
}
500-
ExprKind::IfLet(_, ref expr, _, _) | ExprKind::WhileLet(_, ref expr, _, _) =>
501-
self.while_if_let_ambiguity(&expr),
502509
ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => {
503510
span_err!(self.session, expr.span, E0472, "asm! is unsupported on this target");
504511
}
505512
_ => {}
506513
}
507514

508-
visit::walk_expr(self, expr)
515+
self.with_let_allowed(false, |this| visit::walk_expr(this, expr));
509516
}
510517

511518
fn visit_ty(&mut self, ty: &'a Ty) {
@@ -917,6 +924,7 @@ pub fn check_crate(session: &Session, krate: &Crate) -> (bool, bool) {
917924
is_assoc_ty_bound_banned: false,
918925
warning_period_57979_didnt_record_next_impl_trait: false,
919926
warning_period_57979_impl_trait_in_proj: false,
927+
is_let_allowed: false,
920928
};
921929
visit::walk_crate(&mut validator, krate);
922930

src/librustc_passes/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#![feature(in_band_lifetimes)]
1010
#![feature(nll)]
11+
#![feature(bind_by_move_pattern_guards)]
1112
#![feature(rustc_diagnostic_macros)]
1213

1314
#![recursion_limit="256"]

0 commit comments

Comments
 (0)