mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Auto merge of #15961 - ohno418:top-level-let-stmt, r=Veykril
Improve error handling for top-level `let` statements This commit addresses the issue of excessive and unrelated errors generated by top-level `let` statements. Now, only a single error is produced, indicating that `let` statements are invalid at the top level. --- Fixes https://github.com/rust-lang/rust-analyzer/issues/14963. While I'm not really sure if handling a particular case in a special manner is appropriate, it would be good to suppress the excessive number of annoying and unrelated errors.
This commit is contained in:
commit
57e90240a5
@ -376,6 +376,16 @@ fn error_block(p: &mut Parser<'_>, message: &str) {
|
|||||||
m.complete(p, ERROR);
|
m.complete(p, ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test_err top_level_let
|
||||||
|
// let ref foo: fn() = 1 + 3;
|
||||||
|
fn error_let_stmt(p: &mut Parser<'_>, message: &str) {
|
||||||
|
assert!(p.at(T![let]));
|
||||||
|
let m = p.start();
|
||||||
|
p.error(message);
|
||||||
|
expressions::let_stmt(p, expressions::Semicolon::Optional);
|
||||||
|
m.complete(p, ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
/// The `parser` passed this is required to at least consume one token if it returns `true`.
|
/// The `parser` passed this is required to at least consume one token if it returns `true`.
|
||||||
/// If the `parser` returns false, parsing will stop.
|
/// If the `parser` returns false, parsing will stop.
|
||||||
fn delimited(
|
fn delimited(
|
||||||
|
@ -59,7 +59,8 @@ pub(super) fn stmt(p: &mut Parser<'_>, semicolon: Semicolon) {
|
|||||||
attributes::outer_attrs(p);
|
attributes::outer_attrs(p);
|
||||||
|
|
||||||
if p.at(T![let]) {
|
if p.at(T![let]) {
|
||||||
let_stmt(p, m, semicolon);
|
let_stmt(p, semicolon);
|
||||||
|
m.complete(p, LET_STMT);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,10 +110,11 @@ pub(super) fn stmt(p: &mut Parser<'_>, semicolon: Semicolon) {
|
|||||||
m.complete(p, EXPR_STMT);
|
m.complete(p, EXPR_STMT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// test let_stmt
|
// test let_stmt
|
||||||
// fn f() { let x: i32 = 92; }
|
// fn f() { let x: i32 = 92; }
|
||||||
fn let_stmt(p: &mut Parser<'_>, m: Marker, with_semi: Semicolon) {
|
pub(super) fn let_stmt(p: &mut Parser<'_>, with_semi: Semicolon) {
|
||||||
p.bump(T![let]);
|
p.bump(T![let]);
|
||||||
patterns::pattern(p);
|
patterns::pattern(p);
|
||||||
if p.at(T![:]) {
|
if p.at(T![:]) {
|
||||||
@ -156,8 +158,6 @@ pub(super) fn stmt(p: &mut Parser<'_>, semicolon: Semicolon) {
|
|||||||
p.expect(T![;]);
|
p.expect(T![;]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.complete(p, LET_STMT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn expr_block_contents(p: &mut Parser<'_>) {
|
pub(super) fn expr_block_contents(p: &mut Parser<'_>) {
|
||||||
|
@ -79,6 +79,7 @@ pub(super) fn item_or_macro(p: &mut Parser<'_>, stop_on_r_curly: bool) {
|
|||||||
e.complete(p, ERROR);
|
e.complete(p, ERROR);
|
||||||
}
|
}
|
||||||
EOF | T!['}'] => p.error("expected an item"),
|
EOF | T!['}'] => p.error("expected an item"),
|
||||||
|
T![let] => error_let_stmt(p, "expected an item"),
|
||||||
_ => p.err_and_bump("expected an item"),
|
_ => p.err_and_bump("expected an item"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
SOURCE_FILE
|
||||||
|
ERROR
|
||||||
|
LET_KW "let"
|
||||||
|
WHITESPACE " "
|
||||||
|
IDENT_PAT
|
||||||
|
REF_KW "ref"
|
||||||
|
WHITESPACE " "
|
||||||
|
NAME
|
||||||
|
IDENT "foo"
|
||||||
|
COLON ":"
|
||||||
|
WHITESPACE " "
|
||||||
|
FN_PTR_TYPE
|
||||||
|
FN_KW "fn"
|
||||||
|
PARAM_LIST
|
||||||
|
L_PAREN "("
|
||||||
|
R_PAREN ")"
|
||||||
|
WHITESPACE " "
|
||||||
|
EQ "="
|
||||||
|
WHITESPACE " "
|
||||||
|
BIN_EXPR
|
||||||
|
LITERAL
|
||||||
|
INT_NUMBER "1"
|
||||||
|
WHITESPACE " "
|
||||||
|
PLUS "+"
|
||||||
|
WHITESPACE " "
|
||||||
|
LITERAL
|
||||||
|
INT_NUMBER "3"
|
||||||
|
SEMICOLON ";"
|
||||||
|
WHITESPACE "\n"
|
||||||
|
error 0: expected an item
|
@ -0,0 +1 @@
|
|||||||
|
let ref foo: fn() = 1 + 3;
|
Loading…
x
Reference in New Issue
Block a user