mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
fix: Fix parsing of integer/keyword name refs in various places
This commit is contained in:
parent
f499faf72b
commit
df7ab62a06
33 changed files with 351 additions and 133 deletions
|
@ -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`");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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![..]) => {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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![=] => {
|
||||
|
|
|
@ -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
|
||||
},
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue