fix: Fix parsing of dyn T in generic arg on 2015 edition

This commit is contained in:
Lukas Wirth 2024-12-05 20:25:05 +01:00
parent 2fd06545a6
commit a049941c1a
7 changed files with 68 additions and 3 deletions

View file

@ -45,7 +45,7 @@ pub(crate) const GENERIC_ARG_FIRST: TokenSet = TokenSet::new(&[
const GENERIC_ARG_RECOVERY_SET: TokenSet = TokenSet::new(&[T![>], T![,]]); const GENERIC_ARG_RECOVERY_SET: TokenSet = TokenSet::new(&[T![>], T![,]]);
// test generic_arg // test generic_arg
// type T = S<i32>; // type T = S<i32, dyn T, fn()>;
pub(crate) fn generic_arg(p: &mut Parser<'_>) -> bool { pub(crate) fn generic_arg(p: &mut Parser<'_>) -> bool {
match p.current() { match p.current() {
LIFETIME_IDENT if !p.nth_at(1, T![+]) => lifetime_arg(p), LIFETIME_IDENT if !p.nth_at(1, T![+]) => lifetime_arg(p),
@ -57,6 +57,9 @@ pub(crate) fn generic_arg(p: &mut Parser<'_>) -> bool {
// type ParenthesizedArgs = Foo<Item(T), Item::(T), Item(T): Bound, Item::(T): Bound, Item(T) = Item, Item::(T) = Item>; // type ParenthesizedArgs = Foo<Item(T), Item::(T), Item(T): Bound, Item::(T): Bound, Item(T) = Item, Item::(T) = Item>;
// type RTN = Foo<Item(..), Item(..), Item(..): Bound, Item(..): Bound, Item(..) = Item, Item(..) = Item>; // type RTN = Foo<Item(..), Item(..), Item(..): Bound, Item(..): Bound, Item(..) = Item, Item(..) = Item>;
// test edition_2015_dyn_prefix_inside_generic_arg 2015
// type A = Foo<dyn T>;
T![ident] if !p.edition().at_least_2018() && types::is_dyn_weak(p) => type_arg(p),
// test macro_inside_generic_arg // test macro_inside_generic_arg
// type A = Foo<syn::Token![_]>; // type A = Foo<syn::Token![_]>;
k if PATH_NAME_REF_KINDS.contains(k) => { k if PATH_NAME_REF_KINDS.contains(k) => {

View file

@ -58,7 +58,7 @@ fn type_with_bounds_cond(p: &mut Parser<'_>, allow_bounds: bool) {
} }
} }
fn is_dyn_weak(p: &Parser<'_>) -> bool { pub(crate) fn is_dyn_weak(p: &Parser<'_>) -> bool {
const WEAK_DYN_PATH_FIRST: TokenSet = TokenSet::new(&[ const WEAK_DYN_PATH_FIRST: TokenSet = TokenSet::new(&[
IDENT, IDENT,
T![self], T![self],

View file

@ -190,6 +190,13 @@ mod ok {
); );
} }
#[test] #[test]
fn edition_2015_dyn_prefix_inside_generic_arg() {
run_and_expect_no_errors_with_edition(
"test_data/parser/inline/ok/edition_2015_dyn_prefix_inside_generic_arg.rs",
crate::Edition::Edition2015,
);
}
#[test]
fn effect_blocks() { run_and_expect_no_errors("test_data/parser/inline/ok/effect_blocks.rs"); } fn effect_blocks() { run_and_expect_no_errors("test_data/parser/inline/ok/effect_blocks.rs"); }
#[test] #[test]
fn exclusive_range_pat() { fn exclusive_range_pat() {

View file

@ -0,0 +1,32 @@
SOURCE_FILE
TYPE_ALIAS
COMMENT "// 2015"
WHITESPACE "\n"
TYPE_KW "type"
WHITESPACE " "
NAME
IDENT "A"
WHITESPACE " "
EQ "="
WHITESPACE " "
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "Foo"
GENERIC_ARG_LIST
L_ANGLE "<"
TYPE_ARG
DYN_TRAIT_TYPE
DYN_KW "dyn"
WHITESPACE " "
TYPE_BOUND_LIST
TYPE_BOUND
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "T"
R_ANGLE ">"
SEMICOLON ";"
WHITESPACE "\n"

View file

@ -0,0 +1,2 @@
// 2015
type A = Foo<dyn T>;

View file

@ -20,6 +20,27 @@ SOURCE_FILE
PATH_SEGMENT PATH_SEGMENT
NAME_REF NAME_REF
IDENT "i32" IDENT "i32"
COMMA ","
WHITESPACE " "
TYPE_ARG
DYN_TRAIT_TYPE
DYN_KW "dyn"
WHITESPACE " "
TYPE_BOUND_LIST
TYPE_BOUND
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "T"
COMMA ","
WHITESPACE " "
TYPE_ARG
FN_PTR_TYPE
FN_KW "fn"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
R_ANGLE ">" R_ANGLE ">"
SEMICOLON ";" SEMICOLON ";"
WHITESPACE "\n" WHITESPACE "\n"

View file

@ -1 +1 @@
type T = S<i32>; type T = S<i32, dyn T, fn()>;