6965: Properly attach attributes to Param instead of parent ParamList r=matklad a=Veykril

Fixes #2783, fixes #2781

The problem with `let _a = [0,#[cfg(feature = "L")]0];` has already been fixed some time ago it seems:
<details>
  <summary>Syntax Tree for the const item</summary>

```
  LET_STMT@200..236
    LET_KW@200..203 "let"
    WHITESPACE@203..204 " "
    IDENT_PAT@204..206
      NAME@204..206
        IDENT@204..206 "_a"
    WHITESPACE@206..207 " "
    EQ@207..208 "="
    WHITESPACE@208..209 " "
    ARRAY_EXPR@209..235
      L_BRACK@209..210 "["
      LITERAL@210..211
        INT_NUMBER@210..211 "0"
      COMMA@211..212 ","
      LITERAL@212..234
        ATTR@212..233
          POUND@212..213 "#"
          L_BRACK@213..214 "["
          PATH@214..217
            PATH_SEGMENT@214..217
              NAME_REF@214..217
                IDENT@214..217 "cfg"
          TOKEN_TREE@217..232
            L_PAREN@217..218 "("
            IDENT@218..225 "feature"
            WHITESPACE@225..226 " "
            EQ@226..227 "="
            WHITESPACE@227..228 " "
            STRING@228..231 "\"L\""
            R_PAREN@231..232 ")"
          R_BRACK@232..233 "]"
        INT_NUMBER@233..234 "0"
      R_BRACK@234..235 "]"
    SEMICOLON@235..236 ";"
```
</details>

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2020-12-21 08:54:31 +00:00 committed by GitHub
commit 9bb9fbab3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 199 additions and 147 deletions

View file

@ -156,11 +156,13 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker {
let mut saw_expr = false; let mut saw_expr = false;
while !p.at(EOF) && !p.at(T![')']) { while !p.at(EOF) && !p.at(T![')']) {
saw_expr = true; saw_expr = true;
if !p.at_ts(EXPR_FIRST) {
p.error("expected expression"); // test tuple_attrs
// const A: (i64, i64) = (1, #[cfg(test)] 2);
if !expr_with_attrs(p) {
break; break;
} }
expr(p);
if !p.at(T![')']) { if !p.at(T![')']) {
saw_comma = true; saw_comma = true;
p.expect(T![,]); p.expect(T![,]);

View file

@ -47,20 +47,23 @@ fn list_(p: &mut Parser, flavor: Flavor) {
if let FnDef = flavor { if let FnDef = flavor {
// test self_param_outer_attr // test self_param_outer_attr
// fn f(#[must_use] self) {} // fn f(#[must_use] self) {}
let m = p.start();
attributes::outer_attrs(p); attributes::outer_attrs(p);
opt_self_param(p); opt_self_param(p, m);
} }
while !p.at(EOF) && !p.at(ket) { while !p.at(EOF) && !p.at(ket) {
// test param_outer_arg // test param_outer_arg
// fn f(#[attr1] pat: Type) {} // fn f(#[attr1] pat: Type) {}
let m = p.start();
attributes::outer_attrs(p); attributes::outer_attrs(p);
if !p.at_ts(PARAM_FIRST) { if !p.at_ts(PARAM_FIRST) {
p.error("expected value parameter"); p.error("expected value parameter");
m.abandon(p);
break; break;
} }
let param = param(p, flavor); let param = param(p, m, flavor);
if !p.at(ket) { if !p.at(ket) {
p.expect(T![,]); p.expect(T![,]);
} }
@ -77,9 +80,8 @@ const PARAM_FIRST: TokenSet = patterns::PATTERN_FIRST.union(types::TYPE_FIRST);
struct Variadic(bool); struct Variadic(bool);
fn param(p: &mut Parser, flavor: Flavor) -> Variadic { fn param(p: &mut Parser, m: Marker, flavor: Flavor) -> Variadic {
let mut res = Variadic(false); let mut res = Variadic(false);
let m = p.start();
match flavor { match flavor {
// test param_list_vararg // test param_list_vararg
// extern "C" { fn printf(format: *const i8, ...) -> i32; } // extern "C" { fn printf(format: *const i8, ...) -> i32; }
@ -151,10 +153,8 @@ fn variadic_param(p: &mut Parser) -> bool {
// fn d(&'a mut self, x: i32) {} // fn d(&'a mut self, x: i32) {}
// fn e(mut self) {} // fn e(mut self) {}
// } // }
fn opt_self_param(p: &mut Parser) { fn opt_self_param(p: &mut Parser, m: Marker) {
let m;
if p.at(T![self]) || p.at(T![mut]) && p.nth(1) == T![self] { if p.at(T![self]) || p.at(T![mut]) && p.nth(1) == T![self] {
m = p.start();
p.eat(T![mut]); p.eat(T![mut]);
p.eat(T![self]); p.eat(T![self]);
// test arb_self_types // test arb_self_types
@ -174,9 +174,8 @@ fn opt_self_param(p: &mut Parser) {
(T![&], T![mut], T![self], _) => 3, (T![&], T![mut], T![self], _) => 3,
(T![&], LIFETIME_IDENT, T![self], _) => 3, (T![&], LIFETIME_IDENT, T![self], _) => 3,
(T![&], LIFETIME_IDENT, T![mut], T![self]) => 4, (T![&], LIFETIME_IDENT, T![mut], T![self]) => 4,
_ => return, _ => return m.abandon(p),
}; };
m = p.start();
p.bump_any(); p.bump_any();
if p.at(LIFETIME_IDENT) { if p.at(LIFETIME_IDENT) {
lifetime(p); lifetime(p);

View file

@ -6,6 +6,7 @@ SOURCE_FILE@0..26
IDENT@3..4 "f" IDENT@3..4 "f"
PARAM_LIST@4..22 PARAM_LIST@4..22
L_PAREN@4..5 "(" L_PAREN@4..5 "("
SELF_PARAM@5..21
ATTR@5..16 ATTR@5..16
POUND@5..6 "#" POUND@5..6 "#"
L_BRACK@6..7 "[" L_BRACK@6..7 "["
@ -15,7 +16,6 @@ SOURCE_FILE@0..26
IDENT@7..15 "must_use" IDENT@7..15 "must_use"
R_BRACK@15..16 "]" R_BRACK@15..16 "]"
WHITESPACE@16..17 " " WHITESPACE@16..17 " "
SELF_PARAM@17..21
SELF_KW@17..21 "self" SELF_KW@17..21 "self"
R_PAREN@21..22 ")" R_PAREN@21..22 ")"
WHITESPACE@22..23 " " WHITESPACE@22..23 " "

View file

@ -0,0 +1,50 @@
SOURCE_FILE@0..43
CONST@0..42
CONST_KW@0..5 "const"
WHITESPACE@5..6 " "
NAME@6..7
IDENT@6..7 "A"
COLON@7..8 ":"
WHITESPACE@8..9 " "
TUPLE_TYPE@9..19
L_PAREN@9..10 "("
PATH_TYPE@10..13
PATH@10..13
PATH_SEGMENT@10..13
NAME_REF@10..13
IDENT@10..13 "i64"
COMMA@13..14 ","
WHITESPACE@14..15 " "
PATH_TYPE@15..18
PATH@15..18
PATH_SEGMENT@15..18
NAME_REF@15..18
IDENT@15..18 "i64"
R_PAREN@18..19 ")"
WHITESPACE@19..20 " "
EQ@20..21 "="
WHITESPACE@21..22 " "
TUPLE_EXPR@22..41
L_PAREN@22..23 "("
LITERAL@23..24
INT_NUMBER@23..24 "1"
COMMA@24..25 ","
WHITESPACE@25..26 " "
LITERAL@26..40
ATTR@26..38
POUND@26..27 "#"
L_BRACK@27..28 "["
PATH@28..31
PATH_SEGMENT@28..31
NAME_REF@28..31
IDENT@28..31 "cfg"
TOKEN_TREE@31..37
L_PAREN@31..32 "("
IDENT@32..36 "test"
R_PAREN@36..37 ")"
R_BRACK@37..38 "]"
WHITESPACE@38..39 " "
INT_NUMBER@39..40 "2"
R_PAREN@40..41 ")"
SEMICOLON@41..42 ";"
WHITESPACE@42..43 "\n"

View file

@ -0,0 +1 @@
const A: (i64, i64) = (1, #[cfg(test)] 2);

View file

@ -107,6 +107,7 @@ SOURCE_FILE@0..519
IDENT@102..104 "i8" IDENT@102..104 "i8"
COMMA@104..105 "," COMMA@104..105 ","
WHITESPACE@105..106 " " WHITESPACE@105..106 " "
PARAM@106..117
ATTR@106..113 ATTR@106..113
POUND@106..107 "#" POUND@106..107 "#"
L_BRACK@107..108 "[" L_BRACK@107..108 "["
@ -116,7 +117,6 @@ SOURCE_FILE@0..519
IDENT@108..112 "attr" IDENT@108..112 "attr"
R_BRACK@112..113 "]" R_BRACK@112..113 "]"
WHITESPACE@113..114 " " WHITESPACE@113..114 " "
PARAM@114..117
DOT3@114..117 "..." DOT3@114..117 "..."
R_PAREN@117..118 ")" R_PAREN@117..118 ")"
WHITESPACE@118..119 " " WHITESPACE@118..119 " "
@ -153,6 +153,7 @@ SOURCE_FILE@0..519
IDENT@140..145 "FnMut" IDENT@140..145 "FnMut"
PARAM_LIST@145..167 PARAM_LIST@145..167
L_PAREN@145..146 "(" L_PAREN@145..146 "("
PARAM@146..166
ATTR@146..153 ATTR@146..153
POUND@146..147 "#" POUND@146..147 "#"
L_BRACK@147..148 "[" L_BRACK@147..148 "["
@ -162,7 +163,6 @@ SOURCE_FILE@0..519
IDENT@148..152 "attr" IDENT@148..152 "attr"
R_BRACK@152..153 "]" R_BRACK@152..153 "]"
WHITESPACE@153..154 " " WHITESPACE@153..154 " "
PARAM@154..166
REF_TYPE@154..166 REF_TYPE@154..166
AMP@154..155 "&" AMP@154..155 "&"
MUT_KW@155..158 "mut" MUT_KW@155..158 "mut"
@ -224,6 +224,7 @@ SOURCE_FILE@0..519
IDENT@208..211 "u64" IDENT@208..211 "u64"
COMMA@211..212 "," COMMA@211..212 ","
WHITESPACE@212..213 " " WHITESPACE@212..213 " "
PARAM@213..232
ATTR@213..221 ATTR@213..221
POUND@213..214 "#" POUND@213..214 "#"
WHITESPACE@214..215 " " WHITESPACE@214..215 " "
@ -234,7 +235,6 @@ SOURCE_FILE@0..519
IDENT@216..220 "attr" IDENT@216..220 "attr"
R_BRACK@220..221 "]" R_BRACK@220..221 "]"
WHITESPACE@221..222 " " WHITESPACE@221..222 " "
PARAM@222..232
IDENT_PAT@222..227 IDENT_PAT@222..227
MUT_KW@222..225 "mut" MUT_KW@222..225 "mut"
WHITESPACE@225..226 " " WHITESPACE@225..226 " "
@ -271,6 +271,7 @@ SOURCE_FILE@0..519
IDENT@255..256 "f" IDENT@255..256 "f"
PARAM_LIST@256..274 PARAM_LIST@256..274
L_PAREN@256..257 "(" L_PAREN@256..257 "("
SELF_PARAM@257..273
ATTR@257..268 ATTR@257..268
POUND@257..258 "#" POUND@257..258 "#"
L_BRACK@258..259 "[" L_BRACK@258..259 "["
@ -280,7 +281,6 @@ SOURCE_FILE@0..519
IDENT@259..267 "must_use" IDENT@259..267 "must_use"
R_BRACK@267..268 "]" R_BRACK@267..268 "]"
WHITESPACE@268..269 " " WHITESPACE@268..269 " "
SELF_PARAM@269..273
SELF_KW@269..273 "self" SELF_KW@269..273 "self"
R_PAREN@273..274 ")" R_PAREN@273..274 ")"
WHITESPACE@274..275 " " WHITESPACE@274..275 " "
@ -295,6 +295,7 @@ SOURCE_FILE@0..519
IDENT@286..288 "g1" IDENT@286..288 "g1"
PARAM_LIST@288..302 PARAM_LIST@288..302
L_PAREN@288..289 "(" L_PAREN@288..289 "("
SELF_PARAM@289..301
ATTR@289..296 ATTR@289..296
POUND@289..290 "#" POUND@289..290 "#"
L_BRACK@290..291 "[" L_BRACK@290..291 "["
@ -304,7 +305,6 @@ SOURCE_FILE@0..519
IDENT@291..295 "attr" IDENT@291..295 "attr"
R_BRACK@295..296 "]" R_BRACK@295..296 "]"
WHITESPACE@296..297 " " WHITESPACE@296..297 " "
SELF_PARAM@297..301
SELF_KW@297..301 "self" SELF_KW@297..301 "self"
R_PAREN@301..302 ")" R_PAREN@301..302 ")"
WHITESPACE@302..303 " " WHITESPACE@302..303 " "
@ -319,6 +319,7 @@ SOURCE_FILE@0..519
IDENT@314..316 "g2" IDENT@314..316 "g2"
PARAM_LIST@316..331 PARAM_LIST@316..331
L_PAREN@316..317 "(" L_PAREN@316..317 "("
SELF_PARAM@317..330
ATTR@317..324 ATTR@317..324
POUND@317..318 "#" POUND@317..318 "#"
L_BRACK@318..319 "[" L_BRACK@318..319 "["
@ -328,7 +329,6 @@ SOURCE_FILE@0..519
IDENT@319..323 "attr" IDENT@319..323 "attr"
R_BRACK@323..324 "]" R_BRACK@323..324 "]"
WHITESPACE@324..325 " " WHITESPACE@324..325 " "
SELF_PARAM@325..330
AMP@325..326 "&" AMP@325..326 "&"
SELF_KW@326..330 "self" SELF_KW@326..330 "self"
R_PAREN@330..331 ")" R_PAREN@330..331 ")"
@ -350,6 +350,7 @@ SOURCE_FILE@0..519
R_ANGLE@348..349 ">" R_ANGLE@348..349 ">"
PARAM_LIST@349..368 PARAM_LIST@349..368
L_PAREN@349..350 "(" L_PAREN@349..350 "("
SELF_PARAM@350..367
ATTR@350..357 ATTR@350..357
POUND@350..351 "#" POUND@350..351 "#"
L_BRACK@351..352 "[" L_BRACK@351..352 "["
@ -359,7 +360,6 @@ SOURCE_FILE@0..519
IDENT@352..356 "attr" IDENT@352..356 "attr"
R_BRACK@356..357 "]" R_BRACK@356..357 "]"
WHITESPACE@357..358 " " WHITESPACE@357..358 " "
SELF_PARAM@358..367
AMP@358..359 "&" AMP@358..359 "&"
MUT_KW@359..362 "mut" MUT_KW@359..362 "mut"
WHITESPACE@362..363 " " WHITESPACE@362..363 " "
@ -383,6 +383,7 @@ SOURCE_FILE@0..519
R_ANGLE@385..386 ">" R_ANGLE@385..386 ">"
PARAM_LIST@386..404 PARAM_LIST@386..404
L_PAREN@386..387 "(" L_PAREN@386..387 "("
SELF_PARAM@387..403
ATTR@387..394 ATTR@387..394
POUND@387..388 "#" POUND@387..388 "#"
L_BRACK@388..389 "[" L_BRACK@388..389 "["
@ -392,7 +393,6 @@ SOURCE_FILE@0..519
IDENT@389..393 "attr" IDENT@389..393 "attr"
R_BRACK@393..394 "]" R_BRACK@393..394 "]"
WHITESPACE@394..395 " " WHITESPACE@394..395 " "
SELF_PARAM@395..403
AMP@395..396 "&" AMP@395..396 "&"
LIFETIME@396..398 LIFETIME@396..398
LIFETIME_IDENT@396..398 "\'a" LIFETIME_IDENT@396..398 "\'a"
@ -417,6 +417,7 @@ SOURCE_FILE@0..519
R_ANGLE@421..422 ">" R_ANGLE@421..422 ">"
PARAM_LIST@422..444 PARAM_LIST@422..444
L_PAREN@422..423 "(" L_PAREN@422..423 "("
SELF_PARAM@423..443
ATTR@423..430 ATTR@423..430
POUND@423..424 "#" POUND@423..424 "#"
L_BRACK@424..425 "[" L_BRACK@424..425 "["
@ -426,7 +427,6 @@ SOURCE_FILE@0..519
IDENT@425..429 "attr" IDENT@425..429 "attr"
R_BRACK@429..430 "]" R_BRACK@429..430 "]"
WHITESPACE@430..431 " " WHITESPACE@430..431 " "
SELF_PARAM@431..443
AMP@431..432 "&" AMP@431..432 "&"
LIFETIME@432..434 LIFETIME@432..434
LIFETIME_IDENT@432..434 "\'a" LIFETIME_IDENT@432..434 "\'a"
@ -447,6 +447,7 @@ SOURCE_FILE@0..519
IDENT@456..457 "c" IDENT@456..457 "c"
PARAM_LIST@457..477 PARAM_LIST@457..477
L_PAREN@457..458 "(" L_PAREN@457..458 "("
SELF_PARAM@458..476
ATTR@458..465 ATTR@458..465
POUND@458..459 "#" POUND@458..459 "#"
L_BRACK@459..460 "[" L_BRACK@459..460 "["
@ -456,7 +457,6 @@ SOURCE_FILE@0..519
IDENT@460..464 "attr" IDENT@460..464 "attr"
R_BRACK@464..465 "]" R_BRACK@464..465 "]"
WHITESPACE@465..466 " " WHITESPACE@465..466 " "
SELF_PARAM@466..476
SELF_KW@466..470 "self" SELF_KW@466..470 "self"
COLON@470..471 ":" COLON@470..471 ":"
WHITESPACE@471..472 " " WHITESPACE@471..472 " "
@ -478,6 +478,7 @@ SOURCE_FILE@0..519
IDENT@489..490 "d" IDENT@489..490 "d"
PARAM_LIST@490..514 PARAM_LIST@490..514
L_PAREN@490..491 "(" L_PAREN@490..491 "("
SELF_PARAM@491..513
ATTR@491..498 ATTR@491..498
POUND@491..492 "#" POUND@491..492 "#"
L_BRACK@492..493 "[" L_BRACK@492..493 "["
@ -487,7 +488,6 @@ SOURCE_FILE@0..519
IDENT@493..497 "attr" IDENT@493..497 "attr"
R_BRACK@497..498 "]" R_BRACK@497..498 "]"
WHITESPACE@498..499 " " WHITESPACE@498..499 " "
SELF_PARAM@499..513
SELF_KW@499..503 "self" SELF_KW@499..503 "self"
COLON@503..504 ":" COLON@503..504 ":"
WHITESPACE@504..505 " " WHITESPACE@504..505 " "

View file

@ -92,6 +92,7 @@ SOURCE_FILE@0..126
IDENT@88..90 "u8" IDENT@88..90 "u8"
COMMA@90..91 "," COMMA@90..91 ","
WHITESPACE@91..92 " " WHITESPACE@91..92 " "
PARAM@92..120
ATTR@92..105 ATTR@92..105
POUND@92..93 "#" POUND@92..93 "#"
L_BRACK@93..94 "[" L_BRACK@93..94 "["
@ -105,7 +106,6 @@ SOURCE_FILE@0..126
R_PAREN@103..104 ")" R_PAREN@103..104 ")"
R_BRACK@104..105 "]" R_BRACK@104..105 "]"
WHITESPACE@105..106 " " WHITESPACE@105..106 " "
PARAM@106..120
SLICE_PAT@106..115 SLICE_PAT@106..115
L_BRACK@106..107 "[" L_BRACK@106..107 "["
IDENT_PAT@107..108 IDENT_PAT@107..108