Skip to content

Commit fc9d578

Browse files
committed
expand: Preserve order of inert attributes during expansion
1 parent c02d210 commit fc9d578

File tree

5 files changed

+55
-57
lines changed

5 files changed

+55
-57
lines changed

compiler/rustc_expand/src/expand.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ pub enum InvocationKind {
301301
},
302302
Attr {
303303
attr: ast::Attribute,
304+
// Re-insertion position for inert attributes.
305+
pos: usize,
304306
item: Annotatable,
305307
// Required for resolving derive helper attributes.
306308
derives: Vec<Path>,
@@ -690,7 +692,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
690692
}
691693
_ => unreachable!(),
692694
},
693-
InvocationKind::Attr { attr, mut item, derives } => match ext {
695+
InvocationKind::Attr { attr, pos, mut item, derives } => match ext {
694696
SyntaxExtensionKind::Attr(expander) => {
695697
self.gate_proc_macro_input(&item);
696698
self.gate_proc_macro_attr_item(span, &item);
@@ -721,7 +723,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
721723
ExpandResult::Retry(item) => {
722724
// Reassemble the original invocation for retrying.
723725
return ExpandResult::Retry(Invocation {
724-
kind: InvocationKind::Attr { attr, item, derives },
726+
kind: InvocationKind::Attr { attr, pos, item, derives },
725727
..invoc
726728
});
727729
}
@@ -739,7 +741,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
739741
if *mark_used {
740742
self.cx.sess.mark_attr_used(&attr);
741743
}
742-
item.visit_attrs(|attrs| attrs.push(attr));
744+
item.visit_attrs(|attrs| attrs.insert(pos, attr));
743745
fragment_kind.expect_from_annotatables(iter::once(item))
744746
}
745747
_ => unreachable!(),
@@ -1000,17 +1002,20 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10001002

10011003
fn collect_attr(
10021004
&mut self,
1003-
(attr, derives): (ast::Attribute, Vec<Path>),
1005+
(attr, pos, derives): (ast::Attribute, usize, Vec<Path>),
10041006
item: Annotatable,
10051007
kind: AstFragmentKind,
10061008
) -> AstFragment {
1007-
self.collect(kind, InvocationKind::Attr { attr, item, derives })
1009+
self.collect(kind, InvocationKind::Attr { attr, pos, item, derives })
10081010
}
10091011

10101012
/// If `item` is an attribute invocation, remove the attribute and return it together with
1011-
/// derives following it. We have to collect the derives in order to resolve legacy derive
1012-
/// helpers (helpers written before derives that introduce them).
1013-
fn take_first_attr(&mut self, item: &mut impl HasAttrs) -> Option<(ast::Attribute, Vec<Path>)> {
1013+
/// its position and derives following it. We have to collect the derives in order to resolve
1014+
/// legacy derive helpers (helpers written before derives that introduce them).
1015+
fn take_first_attr(
1016+
&mut self,
1017+
item: &mut impl HasAttrs,
1018+
) -> Option<(ast::Attribute, usize, Vec<Path>)> {
10141019
let mut attr = None;
10151020

10161021
item.visit_attrs(|attrs| {
@@ -1033,7 +1038,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10331038
})
10341039
.collect();
10351040

1036-
(attr, following_derives)
1041+
(attr, attr_pos, following_derives)
10371042
})
10381043
});
10391044

src/test/ui/proc-macro/derive-helper-legacy-spurious.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
extern crate test_macros;
77

88
#[derive(Empty)] //~ ERROR cannot determine resolution for the attribute macro `derive`
9-
#[empty_helper] //~ WARN derive helper attribute is used before it is introduced
10-
//~| WARN this was previously accepted
9+
#[empty_helper] //~ ERROR cannot find attribute `empty_helper` in this scope
1110
struct Foo {}
1211

1312
fn main() {}

src/test/ui/proc-macro/derive-helper-legacy-spurious.stderr

+2-8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ LL | #[derive(Empty)]
1212
|
1313
= note: import resolution is stuck, try simplifying macro imports
1414

15-
warning: derive helper attribute is used before it is introduced
15+
error: cannot find attribute `empty_helper` in this scope
1616
--> $DIR/derive-helper-legacy-spurious.rs:9:3
1717
|
18-
LL | #[derive(Empty)]
19-
| ----- the attribute is introduced here
2018
LL | #[empty_helper]
2119
| ^^^^^^^^^^^^
22-
|
23-
= note: `#[warn(legacy_derive_helpers)]` on by default
24-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
25-
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
2620

27-
error: aborting due to 2 previous errors; 1 warning emitted
21+
error: aborting due to 3 previous errors
2822

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PRINT-ATTR INPUT (DISPLAY): /// 1
2-
#[doc = "3"] #[doc = "4"] #[rustfmt :: attr5] /// 6
3-
#[print_attr(nodebug)] #[rustfmt :: attr2] struct S ;
4-
PRINT-ATTR RE-COLLECTED (DISPLAY): #[doc = " 1"] #[doc = "3"] #[doc = "4"] #[rustfmt :: attr5] #[doc = " 6"]
5-
#[print_attr(nodebug)] #[rustfmt :: attr2] struct S ;
6-
PRINT-ATTR INPUT (DISPLAY): #[doc = " 1"] #[doc = "3"] #[doc = "4"] #[doc = " 6"] #[rustfmt :: attr2]
7-
#[rustfmt :: attr5] struct S ;
2+
#[rustfmt :: attr2] #[doc = "3"] #[doc = "4"] #[rustfmt :: attr5] /// 6
3+
#[print_attr(nodebug)] struct S ;
4+
PRINT-ATTR RE-COLLECTED (DISPLAY): #[doc = " 1"] #[rustfmt :: attr2] #[doc = "3"] #[doc = "4"]
5+
#[rustfmt :: attr5] #[doc = " 6"] #[print_attr(nodebug)] struct S ;
6+
PRINT-ATTR INPUT (DISPLAY): #[doc = " 1"] #[rustfmt :: attr2] #[doc = "3"] #[doc = "4"]
7+
#[rustfmt :: attr5] #[doc = " 6"] struct S ;

src/test/ui/proc-macro/issue-75930-derive-cfg.stdout

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): #[allow(dead_code)] #[derive(Print)] #[print_helper(b)] #[print_helper(a)]
1+
PRINT-ATTR INPUT (DISPLAY): #[print_helper(a)] #[allow(dead_code)] #[derive(Print)] #[print_helper(b)]
22
struct Foo < #[cfg(FALSE)] A, B >
33
{
44
#[cfg(FALSE)] first : String, #[cfg_attr(FALSE, deny(warnings))] second :
@@ -23,6 +23,31 @@ struct Foo < #[cfg(FALSE)] A, B >
2323
}], #[print_helper(d)] fourth : B
2424
}
2525
PRINT-ATTR INPUT (DEBUG): TokenStream [
26+
Punct {
27+
ch: '#',
28+
spacing: Alone,
29+
span: $DIR/issue-75930-derive-cfg.rs:16:1: 16:2 (#0),
30+
},
31+
Group {
32+
delimiter: Bracket,
33+
stream: TokenStream [
34+
Ident {
35+
ident: "print_helper",
36+
span: $DIR/issue-75930-derive-cfg.rs:16:3: 16:15 (#0),
37+
},
38+
Group {
39+
delimiter: Parenthesis,
40+
stream: TokenStream [
41+
Ident {
42+
ident: "a",
43+
span: $DIR/issue-75930-derive-cfg.rs:16:16: 16:17 (#0),
44+
},
45+
],
46+
span: $DIR/issue-75930-derive-cfg.rs:16:15: 16:18 (#0),
47+
},
48+
],
49+
span: $DIR/issue-75930-derive-cfg.rs:16:2: 16:19 (#0),
50+
},
2651
Punct {
2752
ch: '#',
2853
spacing: Alone,
@@ -98,31 +123,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
98123
],
99124
span: $DIR/issue-75930-derive-cfg.rs:21:2: 21:19 (#0),
100125
},
101-
Punct {
102-
ch: '#',
103-
spacing: Alone,
104-
span: $DIR/issue-75930-derive-cfg.rs:16:1: 16:2 (#0),
105-
},
106-
Group {
107-
delimiter: Bracket,
108-
stream: TokenStream [
109-
Ident {
110-
ident: "print_helper",
111-
span: $DIR/issue-75930-derive-cfg.rs:16:3: 16:15 (#0),
112-
},
113-
Group {
114-
delimiter: Parenthesis,
115-
stream: TokenStream [
116-
Ident {
117-
ident: "a",
118-
span: $DIR/issue-75930-derive-cfg.rs:16:16: 16:17 (#0),
119-
},
120-
],
121-
span: $DIR/issue-75930-derive-cfg.rs:16:15: 16:18 (#0),
122-
},
123-
],
124-
span: $DIR/issue-75930-derive-cfg.rs:16:2: 16:19 (#0),
125-
},
126126
Ident {
127127
ident: "struct",
128128
span: $DIR/issue-75930-derive-cfg.rs:22:1: 22:7 (#0),
@@ -1194,7 +1194,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
11941194
span: $DIR/issue-75930-derive-cfg.rs:22:32: 65:2 (#0),
11951195
},
11961196
]
1197-
PRINT-DERIVE INPUT (DISPLAY): #[allow(dead_code)] #[print_helper(b)] #[print_helper(a)] struct Foo < B >
1197+
PRINT-DERIVE INPUT (DISPLAY): #[print_helper(a)] #[allow(dead_code)] #[print_helper(b)] struct Foo < B >
11981198
{
11991199
second : bool, third :
12001200
[u8 ;
@@ -1217,14 +1217,14 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
12171217
delimiter: Bracket,
12181218
stream: TokenStream [
12191219
Ident {
1220-
ident: "allow",
1220+
ident: "print_helper",
12211221
span: $DIR/issue-75930-derive-cfg.rs:22:1: 65:2 (#0),
12221222
},
12231223
Group {
12241224
delimiter: Parenthesis,
12251225
stream: TokenStream [
12261226
Ident {
1227-
ident: "dead_code",
1227+
ident: "a",
12281228
span: $DIR/issue-75930-derive-cfg.rs:22:1: 65:2 (#0),
12291229
},
12301230
],
@@ -1242,14 +1242,14 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
12421242
delimiter: Bracket,
12431243
stream: TokenStream [
12441244
Ident {
1245-
ident: "print_helper",
1245+
ident: "allow",
12461246
span: $DIR/issue-75930-derive-cfg.rs:22:1: 65:2 (#0),
12471247
},
12481248
Group {
12491249
delimiter: Parenthesis,
12501250
stream: TokenStream [
12511251
Ident {
1252-
ident: "b",
1252+
ident: "dead_code",
12531253
span: $DIR/issue-75930-derive-cfg.rs:22:1: 65:2 (#0),
12541254
},
12551255
],
@@ -1274,7 +1274,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
12741274
delimiter: Parenthesis,
12751275
stream: TokenStream [
12761276
Ident {
1277-
ident: "a",
1277+
ident: "b",
12781278
span: $DIR/issue-75930-derive-cfg.rs:22:1: 65:2 (#0),
12791279
},
12801280
],

0 commit comments

Comments
 (0)