Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nicer error message if the user attempts to do let...else if #89974

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_ast::{
};
use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, Local, Stmt};
use rustc_ast::{StmtKind, DUMMY_NODE_ID};
use rustc_errors::{Applicability, PResult};
use rustc_errors::{Applicability, DiagnosticBuilder, PResult};
use rustc_span::source_map::{BytePos, Span};
use rustc_span::symbol::{kw, sym};

Expand Down Expand Up @@ -300,6 +300,12 @@ impl<'a> Parser<'a> {
None => LocalKind::Decl,
Some(init) => {
if self.eat_keyword(kw::Else) {
if self.token.is_keyword(kw::If) {
// `let...else if`. Emit the same error that `parse_block()` would,
// but explicitly point out that this pattern is not allowed.
let msg = "conditional `else if` is not supported for `let...else`";
return Err(self.error_block_no_opening_brace_msg(msg));
}
let els = self.parse_block()?;
self.check_let_else_init_bool_expr(&init);
self.check_let_else_init_trailing_brace(&init);
Expand Down Expand Up @@ -392,10 +398,9 @@ impl<'a> Parser<'a> {
Ok(block)
}

fn error_block_no_opening_brace<T>(&mut self) -> PResult<'a, T> {
fn error_block_no_opening_brace_msg(&mut self, msg: &str) -> DiagnosticBuilder<'a> {
let sp = self.token.span;
let tok = super::token_descr(&self.token);
let mut e = self.struct_span_err(sp, &format!("expected `{{`, found {}", tok));
let mut e = self.struct_span_err(sp, msg);
let do_not_suggest_help = self.token.is_keyword(kw::In) || self.token == token::Colon;

// Check to see if the user has written something like
Expand Down Expand Up @@ -435,7 +440,13 @@ impl<'a> Parser<'a> {
_ => {}
}
e.span_label(sp, "expected `{`");
Err(e)
e
}

fn error_block_no_opening_brace<T>(&mut self) -> PResult<'a, T> {
let tok = super::token_descr(&self.token);
let msg = format!("expected `{{`, found {}", tok);
Err(self.error_block_no_opening_brace_msg(&msg))
}

/// Parses a block. Inner attributes are allowed.
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/let-else/let-else-if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(let_else)]

fn main() {
let Some(_) = Some(()) else if true {
//~^ ERROR conditional `else if` is not supported for `let...else`
return;
} else {
return;
};
}
18 changes: 18 additions & 0 deletions src/test/ui/let-else/let-else-if.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: conditional `else if` is not supported for `let...else`
est31 marked this conversation as resolved.
Show resolved Hide resolved
--> $DIR/let-else-if.rs:4:33
|
LL | let Some(_) = Some(()) else if true {
| ^^ expected `{`
|
help: try placing this code inside a block
|
LL ~ let Some(_) = Some(()) else { if true {
LL +
LL + return;
LL + } else {
LL + return;
LL ~ } };
|

error: aborting due to previous error