fix: Fix parsing of integer/keyword name refs in various places

This commit is contained in:
Lukas Wirth 2024-12-05 14:37:38 +01:00
parent f499faf72b
commit df7ab62a06
33 changed files with 351 additions and 133 deletions

View file

@ -1733,7 +1733,7 @@ m!(C("0"));
macro_rules! m {
($k:expr) => { fn f() { K::$k; } }
}
/* parse error: expected identifier */
/* parse error: expected identifier, `self`, `super`, `crate`, or `Self` */
/* parse error: expected SEMICOLON */
/* parse error: expected SEMICOLON */
/* parse error: expected expression, item or let statement */

View file

@ -307,13 +307,49 @@ fn name(p: &mut Parser<'_>) {
name_r(p, TokenSet::EMPTY);
}
fn name_ref(p: &mut Parser<'_>) {
if p.at(IDENT) {
fn name_ref_or_self(p: &mut Parser<'_>) {
if matches!(p.current(), T![ident] | T![self]) {
let m = p.start();
p.bump(IDENT);
p.bump_any();
m.complete(p, NAME_REF);
} else {
p.err_and_bump("expected identifier");
p.err_and_bump("expected identifier or `self`");
}
}
fn name_ref_or_upper_self(p: &mut Parser<'_>) {
if matches!(p.current(), T![ident] | T![Self]) {
let m = p.start();
p.bump_any();
m.complete(p, NAME_REF);
} else {
p.err_and_bump("expected identifier or `Self`");
}
}
const PATH_NAME_REF_KINDS: TokenSet =
TokenSet::new(&[IDENT, T![self], T![super], T![crate], T![Self]]);
fn name_ref_mod_path(p: &mut Parser<'_>) {
if p.at_ts(PATH_NAME_REF_KINDS) {
let m = p.start();
p.bump_any();
m.complete(p, NAME_REF);
} else {
p.err_and_bump("expected identifier, `self`, `super`, `crate`, or `Self`");
}
}
const PATH_NAME_REF_OR_INDEX_KINDS: TokenSet =
PATH_NAME_REF_KINDS.union(TokenSet::new(&[INT_NUMBER]));
fn name_ref_mod_path_or_index(p: &mut Parser<'_>) {
if p.at_ts(PATH_NAME_REF_OR_INDEX_KINDS) {
let m = p.start();
p.bump_any();
m.complete(p, NAME_REF);
} else {
p.err_and_bump("expected integer, identifier, `self`, `super`, `crate`, or `Self`");
}
}

View file

@ -449,7 +449,9 @@ fn postfix_dot_expr<const FLOAT_RECOVERY: bool>(
let nth1 = if FLOAT_RECOVERY { 0 } else { 1 };
let nth2 = if FLOAT_RECOVERY { 1 } else { 2 };
if p.nth(nth1) == IDENT && (p.nth(nth2) == T!['('] || p.nth_at(nth2, T![::])) {
if PATH_NAME_REF_KINDS.contains(p.nth(nth1))
&& (p.nth(nth2) == T!['('] || p.nth_at(nth2, T![::]))
{
return Ok(method_call_expr::<FLOAT_RECOVERY>(p, lhs));
}
@ -510,21 +512,26 @@ fn index_expr(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
// y.bar::<T>(1, 2,);
// x.0.0.call();
// x.0. call();
// x.0()
// }
fn method_call_expr<const FLOAT_RECOVERY: bool>(
p: &mut Parser<'_>,
lhs: CompletedMarker,
) -> CompletedMarker {
if FLOAT_RECOVERY {
assert!(p.nth(0) == IDENT && (p.nth(1) == T!['('] || p.nth_at(1, T![::])));
assert!(p.at_ts(PATH_NAME_REF_KINDS) && (p.nth(1) == T!['('] || p.nth_at(1, T![::])));
} else {
assert!(p.at(T![.]) && p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth_at(2, T![::])));
assert!(
p.at(T![.])
&& PATH_NAME_REF_KINDS.contains(p.nth(1))
&& (p.nth(2) == T!['('] || p.nth_at(2, T![::]))
);
}
let m = lhs.precede(p);
if !FLOAT_RECOVERY {
p.bump(T![.]);
}
name_ref(p);
name_ref_mod_path(p);
generic_args::opt_generic_arg_list_expr(p);
if p.at(T!['(']) {
arg_list(p);
@ -543,6 +550,8 @@ fn method_call_expr<const FLOAT_RECOVERY: bool>(
// test field_expr
// fn foo() {
// x.self;
// x.Self;
// x.foo;
// x.0.bar;
// x.0.1;
@ -560,8 +569,8 @@ fn field_expr<const FLOAT_RECOVERY: bool>(
if !FLOAT_RECOVERY {
p.bump(T![.]);
}
if p.at(IDENT) || p.at(INT_NUMBER) {
name_ref_or_index(p);
if p.at_ts(PATH_NAME_REF_OR_INDEX_KINDS) {
name_ref_mod_path_or_index(p);
} else if p.at(FLOAT_NUMBER) {
return match p.split_float(m) {
(true, m) => {
@ -679,34 +688,37 @@ pub(crate) fn record_expr_field_list(p: &mut Parser<'_>) {
IDENT | INT_NUMBER if p.nth_at(1, T![::]) => {
// test_err record_literal_missing_ellipsis_recovery
// fn main() {
// S { S::default() }
// S { S::default() };
// S { 0::default() };
// }
m.abandon(p);
p.expect(T![..]);
expr(p);
}
IDENT | INT_NUMBER if p.nth_at(1, T![..]) => {
// test_err record_literal_before_ellipsis_recovery
// fn main() {
// S { field ..S::default() }
// S { 0 ..S::default() }
// }
name_ref_or_index(p);
p.error("expected `:`");
m.complete(p, RECORD_EXPR_FIELD);
}
IDENT | INT_NUMBER => {
if p.nth_at(1, T![..]) {
// test_err record_literal_before_ellipsis_recovery
// fn main() {
// S { field ..S::default() }
// }
// test_err record_literal_field_eq_recovery
// fn main() {
// S { field = foo }
// S { 0 = foo }
// }
if p.nth_at(1, T![:]) {
name_ref_or_index(p);
p.error("expected `:`");
} else {
// test_err record_literal_field_eq_recovery
// fn main() {
// S { field = foo }
// }
if p.nth_at(1, T![:]) {
name_ref_or_index(p);
p.bump(T![:]);
} else if p.nth_at(1, T![=]) {
name_ref_or_index(p);
p.err_and_bump("expected `:`");
}
expr(p);
p.bump(T![:]);
} else if p.nth_at(1, T![=]) {
name_ref_or_index(p);
p.err_and_bump("expected `:`");
}
expr(p);
m.complete(p, RECORD_EXPR_FIELD);
}
T![.] if p.at(T![..]) => {

View file

@ -259,13 +259,7 @@ fn builtin_expr(p: &mut Parser<'_>) -> Option<CompletedMarker> {
type_(p);
p.expect(T![,]);
while !p.at(EOF) && !p.at(T![')']) {
if p.at(IDENT) || p.at(INT_NUMBER) {
name_ref_or_index(p);
// } else if p.at(FLOAT_NUMBER) {
// FIXME: needs float hack
} else {
p.err_and_bump("expected field name or number");
}
name_ref_mod_path_or_index(p);
if !p.at(T![')']) {
p.expect(T![.]);
}
@ -465,9 +459,9 @@ fn parse_clobber_abi(p: &mut Parser<'_>) {
fn parse_reg(p: &mut Parser<'_>) {
p.expect(T!['(']);
if p.at(T![ident]) {
if p.at_ts(PATH_NAME_REF_KINDS) {
let m = p.start();
name_ref(p);
name_ref_mod_path(p);
m.complete(p, ASM_REG_SPEC);
} else if p.at(T![string]) {
let m = p.start();

View file

@ -59,9 +59,9 @@ pub(crate) fn generic_arg(p: &mut Parser<'_>) -> bool {
// test macro_inside_generic_arg
// type A = Foo<syn::Token![_]>;
IDENT => {
k if PATH_NAME_REF_KINDS.contains(k) => {
let m = p.start();
name_ref(p);
name_ref_mod_path(p);
paths::opt_path_type_args(p);
match p.current() {
T![=] => {

View file

@ -145,6 +145,9 @@ fn type_bound(p: &mut Parser<'_>) -> bool {
T![for] => types::for_type(p, false),
// test precise_capturing
// fn captures<'a: 'a, 'b: 'b, T>() -> impl Sized + use<'b, T, Self> {}
// test_err precise_capturing_invalid
// type T = impl use<self, 1>;
T![use] if p.nth_at(1, T![<]) => {
p.bump_any();
let m = p.start();
@ -156,14 +159,10 @@ fn type_bound(p: &mut Parser<'_>) -> bool {
|| "expected identifier or lifetime".into(),
TokenSet::new(&[T![Self], IDENT, LIFETIME_IDENT]),
|p| {
if p.at(T![Self]) {
let m = p.start();
p.bump(T![Self]);
m.complete(p, NAME_REF);
} else if p.at(LIFETIME_IDENT) {
if p.at(LIFETIME_IDENT) {
lifetime(p);
} else {
name_ref(p);
name_ref_or_upper_self(p);
}
true
},

View file

@ -254,22 +254,16 @@ fn opt_item_without_modifiers(p: &mut Parser<'_>, m: Marker) -> Result<(), Marke
// test extern_crate
// extern crate foo;
// extern crate self;
fn extern_crate(p: &mut Parser<'_>, m: Marker) {
p.bump(T![extern]);
p.bump(T![crate]);
if p.at(T![self]) {
// test extern_crate_self
// extern crate self;
let m = p.start();
p.bump(T![self]);
m.complete(p, NAME_REF);
} else {
name_ref(p);
}
name_ref_or_self(p);
// test extern_crate_rename
// extern crate foo as bar;
// extern crate self as bar;
opt_rename(p);
p.expect(T![;]);
m.complete(p, EXTERN_CRATE);

View file

@ -107,37 +107,32 @@ fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) -> Option<Completed
}
} else {
let mut empty = if first { !p.eat(T![::]) } else { true };
match p.current() {
IDENT => {
name_ref(p);
opt_path_args(p, mode);
}
if p.at_ts(PATH_NAME_REF_KINDS) {
// test crate_path
// use crate::foo;
T![self] | T![super] | T![crate] | T![Self] => {
let m = p.start();
p.bump_any();
m.complete(p, NAME_REF);
}
_ => {
let recover_set = match mode {
Mode::Use => items::ITEM_RECOVERY_SET,
Mode::Attr => {
items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![']'], T![=], T![#]]))
}
Mode::Vis => items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')']])),
Mode::Type => TYPE_PATH_SEGMENT_RECOVERY_SET,
Mode::Expr => EXPR_PATH_SEGMENT_RECOVERY_SET,
};
empty &= p.err_recover("expected identifier", recover_set);
if empty {
// test_err empty_segment
// use crate::;
m.abandon(p);
return None;
name_ref_mod_path(p);
opt_path_args(p, mode);
} else {
let recover_set = match mode {
Mode::Use => items::ITEM_RECOVERY_SET,
Mode::Attr => {
items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![']'], T![=], T![#]]))
}
Mode::Vis => items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')']])),
Mode::Type => TYPE_PATH_SEGMENT_RECOVERY_SET,
Mode::Expr => EXPR_PATH_SEGMENT_RECOVERY_SET,
};
empty &= p.err_recover(
"expected identifier, `self`, `super`, `crate`, or `Self`",
recover_set,
);
if empty {
// test_err empty_segment
// use crate::;
m.abandon(p);
return None;
}
};
}
}
Some(m.complete(p, PATH_SEGMENT))
}

View file

@ -210,10 +210,6 @@ mod ok {
run_and_expect_no_errors("test_data/parser/inline/ok/extern_crate_rename.rs");
}
#[test]
fn extern_crate_self() {
run_and_expect_no_errors("test_data/parser/inline/ok/extern_crate_self.rs");
}
#[test]
fn field_expr() { run_and_expect_no_errors("test_data/parser/inline/ok/field_expr.rs"); }
#[test]
fn fn_() { run_and_expect_no_errors("test_data/parser/inline/ok/fn_.rs"); }
@ -774,6 +770,10 @@ mod err {
run_and_expect_errors("test_data/parser/inline/err/pointer_type_no_mutability.rs");
}
#[test]
fn precise_capturing_invalid() {
run_and_expect_errors("test_data/parser/inline/err/precise_capturing_invalid.rs");
}
#[test]
fn pub_expr() { run_and_expect_errors("test_data/parser/inline/err/pub_expr.rs"); }
#[test]
fn record_literal_before_ellipsis_recovery() {

View file

@ -13,4 +13,4 @@ SOURCE_FILE
ERROR
INT_NUMBER "92"
SEMICOLON ";"
error 9: expected identifier
error 9: expected identifier, `self`, `super`, `crate`, or `Self`

View file

@ -115,10 +115,10 @@ SOURCE_FILE
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 30: expected identifier
error 30: expected identifier, `self`, `super`, `crate`, or `Self`
error 31: expected COMMA
error 37: expected expression
error 75: expected identifier
error 75: expected identifier, `self`, `super`, `crate`, or `Self`
error 76: expected SEMICOLON
error 82: expected expression
error 83: expected SEMICOLON

View file

@ -98,7 +98,7 @@ SOURCE_FILE
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 25: expected identifier
error 25: expected identifier, `self`, `super`, `crate`, or `Self`
error 39: expected COMMA
error 39: expected expression
error 55: expected expression

View file

@ -11,4 +11,4 @@ SOURCE_FILE
IDENT "S"
SEMICOLON ";"
WHITESPACE "\n"
error 4: expected identifier
error 4: expected identifier, `self`, `super`, `crate`, or `Self`

View file

@ -11,4 +11,4 @@ SOURCE_FILE
COLON2 "::"
SEMICOLON ";"
WHITESPACE "\n"
error 11: expected identifier
error 11: expected identifier, `self`, `super`, `crate`, or `Self`

View file

@ -66,18 +66,18 @@ SOURCE_FILE
EQ "="
R_BRACK "]"
WHITESPACE "\n"
error 3: expected identifier
error 3: expected identifier, `self`, `super`, `crate`, or `Self`
error 11: expected expression
error 11: expected expression
error 20: expected identifier
error 28: expected identifier
error 20: expected identifier, `self`, `super`, `crate`, or `Self`
error 28: expected identifier, `self`, `super`, `crate`, or `Self`
error 30: expected expression
error 30: expected expression
error 41: expected L_PAREN
error 41: expected identifier
error 41: expected identifier, `self`, `super`, `crate`, or `Self`
error 41: expected R_PAREN
error 52: expected L_PAREN
error 52: expected identifier
error 52: expected identifier, `self`, `super`, `crate`, or `Self`
error 54: expected expression
error 54: expected expression
error 54: expected R_PAREN

View file

@ -0,0 +1,28 @@
SOURCE_FILE
TYPE_ALIAS
TYPE_KW "type"
WHITESPACE " "
NAME
IDENT "T"
WHITESPACE " "
EQ "="
WHITESPACE " "
IMPL_TRAIT_TYPE
IMPL_KW "impl"
WHITESPACE " "
TYPE_BOUND_LIST
TYPE_BOUND
USE_KW "use"
USE_BOUND_GENERIC_ARGS
L_ANGLE "<"
ERROR
SELF_KW "self"
COMMA ","
WHITESPACE " "
ERROR
INT_NUMBER "1"
R_ANGLE ">"
SEMICOLON ";"
WHITESPACE "\n"
error 18: expected identifier or `Self`
error 24: expected identifier or `Self`

View file

@ -0,0 +1 @@
type T = impl use<self, 1>;

View file

@ -12,6 +12,38 @@ SOURCE_FILE
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
EXPR_STMT
RECORD_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_EXPR_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_EXPR_FIELD
NAME_REF
IDENT "field"
WHITESPACE " "
DOT2 ".."
CALL_EXPR
PATH_EXPR
PATH
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
COLON2 "::"
PATH_SEGMENT
NAME_REF
IDENT "default"
ARG_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n "
RECORD_EXPR
PATH
PATH_SEGMENT
@ -23,7 +55,7 @@ SOURCE_FILE
WHITESPACE " "
RECORD_EXPR_FIELD
NAME_REF
IDENT "field"
INT_NUMBER "0"
WHITESPACE " "
DOT2 ".."
CALL_EXPR
@ -47,3 +79,6 @@ SOURCE_FILE
WHITESPACE "\n"
error 25: expected `:`
error 25: expected COMMA
error 42: expected SEMICOLON
error 52: expected `:`
error 52: expected COMMA

View file

@ -1,3 +1,4 @@
fn main() {
S { field ..S::default() }
S { 0 ..S::default() }
}

View file

@ -12,6 +12,31 @@ SOURCE_FILE
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
EXPR_STMT
RECORD_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_EXPR_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_EXPR_FIELD
NAME_REF
IDENT "field"
WHITESPACE " "
ERROR
EQ "="
WHITESPACE " "
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "foo"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n "
RECORD_EXPR
PATH
PATH_SEGMENT
@ -23,7 +48,7 @@ SOURCE_FILE
WHITESPACE " "
RECORD_EXPR_FIELD
NAME_REF
IDENT "field"
INT_NUMBER "0"
WHITESPACE " "
ERROR
EQ "="
@ -39,3 +64,5 @@ SOURCE_FILE
R_CURLY "}"
WHITESPACE "\n"
error 26: expected `:`
error 33: expected SEMICOLON
error 44: expected `:`

View file

@ -1,3 +1,4 @@
fn main() {
S { field = foo }
S { 0 = foo }
}

View file

@ -12,32 +12,70 @@ SOURCE_FILE
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
RECORD_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_EXPR_FIELD_LIST
L_CURLY "{"
EXPR_STMT
RECORD_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
CALL_EXPR
PATH_EXPR
PATH
RECORD_EXPR_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
CALL_EXPR
PATH_EXPR
PATH
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
COLON2 "::"
PATH_SEGMENT
NAME_REF
IDENT "S"
COLON2 "::"
PATH_SEGMENT
NAME_REF
IDENT "default"
ARG_LIST
L_PAREN "("
R_PAREN ")"
IDENT "default"
ARG_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
RECORD_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
R_CURLY "}"
RECORD_EXPR_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
LITERAL
INT_NUMBER "0"
ERROR
COLON ":"
ERROR
COLON ":"
RECORD_EXPR_FIELD
CALL_EXPR
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "default"
ARG_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 19: expected DOT2
error 43: expected DOT2
error 45: expected COMMA
error 45: expected identifier
error 46: expected COMMA
error 46: expected identifier
error 47: expected COMMA

View file

@ -1,3 +1,4 @@
fn main() {
S { S::default() }
S { S::default() };
S { 0::default() };
}

View file

@ -8,3 +8,12 @@ SOURCE_FILE
IDENT "foo"
SEMICOLON ";"
WHITESPACE "\n"
EXTERN_CRATE
EXTERN_KW "extern"
WHITESPACE " "
CRATE_KW "crate"
WHITESPACE " "
NAME_REF
SELF_KW "self"
SEMICOLON ";"
WHITESPACE "\n"

View file

@ -1 +1,2 @@
extern crate foo;
extern crate self;

View file

@ -14,3 +14,18 @@ SOURCE_FILE
IDENT "bar"
SEMICOLON ";"
WHITESPACE "\n"
EXTERN_CRATE
EXTERN_KW "extern"
WHITESPACE " "
CRATE_KW "crate"
WHITESPACE " "
NAME_REF
SELF_KW "self"
WHITESPACE " "
RENAME
AS_KW "as"
WHITESPACE " "
NAME
IDENT "bar"
SEMICOLON ";"
WHITESPACE "\n"

View file

@ -1 +1,2 @@
extern crate foo as bar;
extern crate self as bar;

View file

@ -1,10 +0,0 @@
SOURCE_FILE
EXTERN_CRATE
EXTERN_KW "extern"
WHITESPACE " "
CRATE_KW "crate"
WHITESPACE " "
NAME_REF
SELF_KW "self"
SEMICOLON ";"
WHITESPACE "\n"

View file

@ -1 +0,0 @@
extern crate self;

View file

@ -12,6 +12,30 @@ SOURCE_FILE
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
EXPR_STMT
FIELD_EXPR
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "x"
DOT "."
NAME_REF
SELF_KW "self"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
FIELD_EXPR
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "x"
DOT "."
NAME_REF
SELF_TYPE_KW "Self"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
FIELD_EXPR
PATH_EXPR

View file

@ -1,4 +1,6 @@
fn foo() {
x.self;
x.Self;
x.foo;
x.0.bar;
x.0.1;

View file

@ -101,6 +101,20 @@ SOURCE_FILE
L_PAREN "("
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n "
CALL_EXPR
FIELD_EXPR
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "x"
DOT "."
NAME_REF
INT_NUMBER "0"
ARG_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"

View file

@ -3,4 +3,5 @@ fn foo() {
y.bar::<T>(1, 2,);
x.0.0.call();
x.0. call();
x.0()
}