expand/resolve: Turn #[derive] into a regular macro attribute

This commit is contained in:
Vadim Petrochenkov 2020-11-14 14:47:14 +03:00
parent ae00b62ceb
commit dbdbd30bf2
58 changed files with 1499 additions and 1258 deletions

View file

@ -0,0 +1,28 @@
// Macro attributes are allowed after `#[derive]` and
// `#[derive]` fully configures the item for following attributes.
// check-pass
// compile-flags: -Z span-debug
// aux-build: test-macros.rs
#![no_std] // Don't load unnecessary hygiene information from std
extern crate std;
#[macro_use]
extern crate test_macros;
#[print_attr]
#[derive(Print)]
struct AttributeDerive {
#[cfg(FALSE)]
field: u8,
}
#[derive(Print)]
#[print_attr]
struct DeriveAttribute {
#[cfg(FALSE)]
field: u8,
}
fn main() {}

View file

@ -0,0 +1,148 @@
PRINT-ATTR INPUT (DISPLAY): #[derive(Print)] struct AttributeDerive { #[cfg(FALSE)] field : u8, }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/attribute-after-derive.rs:15:1: 15:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "derive",
span: $DIR/attribute-after-derive.rs:15:3: 15:9 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "Print",
span: $DIR/attribute-after-derive.rs:15:10: 15:15 (#0),
},
],
span: $DIR/attribute-after-derive.rs:15:9: 15:16 (#0),
},
],
span: $DIR/attribute-after-derive.rs:15:2: 15:17 (#0),
},
Ident {
ident: "struct",
span: $DIR/attribute-after-derive.rs:16:1: 16:7 (#0),
},
Ident {
ident: "AttributeDerive",
span: $DIR/attribute-after-derive.rs:16:8: 16:23 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/attribute-after-derive.rs:17:5: 17:6 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "cfg",
span: $DIR/attribute-after-derive.rs:17:7: 17:10 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "FALSE",
span: $DIR/attribute-after-derive.rs:17:11: 17:16 (#0),
},
],
span: $DIR/attribute-after-derive.rs:17:10: 17:17 (#0),
},
],
span: $DIR/attribute-after-derive.rs:17:6: 17:18 (#0),
},
Ident {
ident: "field",
span: $DIR/attribute-after-derive.rs:18:5: 18:10 (#0),
},
Punct {
ch: ':',
spacing: Alone,
span: $DIR/attribute-after-derive.rs:18:10: 18:11 (#0),
},
Ident {
ident: "u8",
span: $DIR/attribute-after-derive.rs:18:12: 18:14 (#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/attribute-after-derive.rs:18:14: 18:15 (#0),
},
],
span: $DIR/attribute-after-derive.rs:16:24: 19:2 (#0),
},
]
PRINT-DERIVE INPUT (DISPLAY): struct AttributeDerive { }
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/attribute-after-derive.rs:16:1: 19:2 (#0),
},
Ident {
ident: "AttributeDerive",
span: $DIR/attribute-after-derive.rs:16:1: 19:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/attribute-after-derive.rs:16:1: 19:2 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): struct DeriveAttribute { }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
Ident {
ident: "DeriveAttribute",
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
]
PRINT-DERIVE INPUT (DISPLAY): #[print_attr] struct DeriveAttribute { }
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
spacing: Alone,
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "print_attr",
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
],
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
Ident {
ident: "struct",
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
Ident {
ident: "DeriveAttribute",
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [],
span: $DIR/attribute-after-derive.rs:23:1: 26:2 (#0),
},
]

View file

@ -1,14 +0,0 @@
// aux-build:test-macros.rs
#[macro_use]
extern crate test_macros;
#[identity_attr] // OK
#[derive(Clone)]
struct Before;
#[derive(Clone)]
#[identity_attr] //~ ERROR macro attributes must be placed before `#[derive]`
struct After;
fn main() {}

View file

@ -1,8 +0,0 @@
error: macro attributes must be placed before `#[derive]`
--> $DIR/attribute-order-restricted.rs:11:1
|
LL | #[identity_attr]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error

View file

@ -17,6 +17,8 @@ macro_rules! gen_helper_use {
}
#[empty_helper] //~ ERROR `empty_helper` is ambiguous
//~| WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
#[derive(Empty)]
struct S {
#[empty_helper] // OK, no ambiguity, derive helpers have highest priority

View file

@ -1,17 +1,17 @@
error: cannot use a derive helper attribute through an import
--> $DIR/derive-helper-shadowing.rs:40:15
--> $DIR/derive-helper-shadowing.rs:42:15
|
LL | #[renamed]
| ^^^^^^^
|
note: the derive helper attribute imported here
--> $DIR/derive-helper-shadowing.rs:39:17
--> $DIR/derive-helper-shadowing.rs:41:17
|
LL | use empty_helper as renamed;
| ^^^^^^^^^^^^^^^^^^^^^^^
error: cannot find attribute `empty_helper` in this scope
--> $DIR/derive-helper-shadowing.rs:36:22
--> $DIR/derive-helper-shadowing.rs:38:22
|
LL | #[derive(GenHelperUse)]
| ^^^^^^^^^^^^
@ -30,13 +30,13 @@ LL | gen_helper_use!();
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0659]: `empty_helper` is ambiguous (name vs any other name during import resolution)
--> $DIR/derive-helper-shadowing.rs:24:13
--> $DIR/derive-helper-shadowing.rs:26:13
|
LL | use empty_helper;
| ^^^^^^^^^^^^ ambiguous name
|
note: `empty_helper` could refer to the derive helper attribute defined here
--> $DIR/derive-helper-shadowing.rs:20:10
--> $DIR/derive-helper-shadowing.rs:22:10
|
LL | #[derive(Empty)]
| ^^^^^
@ -54,7 +54,7 @@ LL | #[empty_helper]
| ^^^^^^^^^^^^ ambiguous name
|
note: `empty_helper` could refer to the derive helper attribute defined here
--> $DIR/derive-helper-shadowing.rs:20:10
--> $DIR/derive-helper-shadowing.rs:22:10
|
LL | #[derive(Empty)]
| ^^^^^
@ -65,6 +65,19 @@ LL | use test_macros::empty_attr as empty_helper;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use `crate::empty_helper` to refer to this attribute macro unambiguously
error: aborting due to 5 previous errors
warning: derive helper attribute is used before it is introduced
--> $DIR/derive-helper-shadowing.rs:19:3
|
LL | #[empty_helper]
| ^^^^^^^^^^^^
...
LL | #[derive(Empty)]
| ----- the attribute is introduced here
|
= note: `#[warn(legacy_derive_helpers)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
error: aborting due to 5 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0659`.

View file

@ -0,0 +1,12 @@
// check-pass
// aux-build:test-macros.rs
#[macro_use]
extern crate test_macros;
#[derive(Empty)]
#[empty_helper] // OK, this is both derive helper and legacy derive helper
#[derive(Empty)]
struct S;
fn main() {}

View file

@ -0,0 +1,11 @@
// check-pass
#[derive(Clone, Copy)]
#[derive(Debug)] // OK, even if `Copy` is in the different `#[derive]`
#[derive(PartialEq)] // OK too
#[repr(packed)]
struct CacheRecordHeader {
field: u64,
}
fn main() {}

View file

@ -4,8 +4,10 @@
extern crate test_macros;
use test_macros::empty_attr as empty_helper;
#[derive(Empty)]
#[empty_helper] //~ ERROR `empty_helper` is ambiguous
//~| WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
#[derive(Empty)]
struct S;
fn main() {}

View file

@ -1,11 +1,11 @@
error[E0659]: `empty_helper` is ambiguous (derive helper attribute vs any other name)
--> $DIR/helper-attr-blocked-by-import-ambig.rs:8:3
--> $DIR/helper-attr-blocked-by-import-ambig.rs:7:3
|
LL | #[empty_helper]
| ^^^^^^^^^^^^ ambiguous name
|
note: `empty_helper` could refer to the derive helper attribute defined here
--> $DIR/helper-attr-blocked-by-import-ambig.rs:7:10
--> $DIR/helper-attr-blocked-by-import-ambig.rs:10:10
|
LL | #[derive(Empty)]
| ^^^^^
@ -16,6 +16,19 @@ LL | use test_macros::empty_attr as empty_helper;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use `crate::empty_helper` to refer to this attribute macro unambiguously
error: aborting due to previous error
warning: derive helper attribute is used before it is introduced
--> $DIR/helper-attr-blocked-by-import-ambig.rs:7:3
|
LL | #[empty_helper]
| ^^^^^^^^^^^^
...
LL | #[derive(Empty)]
| ----- the attribute is introduced here
|
= note: `#[warn(legacy_derive_helpers)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
error: aborting due to previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0659`.

View file

@ -13,7 +13,8 @@
#[macro_use]
extern crate test_macros;
#[print_helper(a)]
#[print_helper(a)] //~ WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
#[cfg_attr(not(FALSE), allow(dead_code))]
#[print_attr]
#[derive(Print)]

View file

@ -0,0 +1,15 @@
warning: derive helper attribute is used before it is introduced
--> $DIR/issue-75930-derive-cfg.rs:16:3
|
LL | #[print_helper(a)]
| ^^^^^^^^^^^^
...
LL | #[derive(Print)]
| ----- the attribute is introduced here
|
= note: `#[warn(legacy_derive_helpers)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
warning: 1 warning emitted

File diff suppressed because it is too large Load diff

View file

@ -4,10 +4,18 @@
extern crate derive_b;
#[B] //~ ERROR `B` is ambiguous
//~| WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
#[C] //~ ERROR cannot find attribute `C` in this scope
#[B(D)] //~ ERROR `B` is ambiguous
//~| WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
#[B(E = "foo")] //~ ERROR `B` is ambiguous
//~| WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
#[B(arbitrary tokens)] //~ ERROR `B` is ambiguous
//~| WARN derive helper attribute is used before it is introduced
//~| WARN this was previously accepted
#[derive(B)]
struct B;

View file

@ -1,5 +1,5 @@
error: cannot find attribute `C` in this scope
--> $DIR/proc-macro-attributes.rs:7:3
--> $DIR/proc-macro-attributes.rs:9:3
|
LL | #[C]
| ^ help: a derive helper attribute with a similar name exists: `B`
@ -11,41 +11,7 @@ LL | #[B]
| ^ ambiguous name
|
note: `B` could refer to the derive helper attribute defined here
--> $DIR/proc-macro-attributes.rs:11:10
|
LL | #[derive(B)]
| ^
note: `B` could also refer to the derive macro imported here
--> $DIR/proc-macro-attributes.rs:3:1
|
LL | #[macro_use]
| ^^^^^^^^^^^^
error[E0659]: `B` is ambiguous (derive helper attribute vs any other name)
--> $DIR/proc-macro-attributes.rs:8:3
|
LL | #[B(D)]
| ^ ambiguous name
|
note: `B` could refer to the derive helper attribute defined here
--> $DIR/proc-macro-attributes.rs:11:10
|
LL | #[derive(B)]
| ^
note: `B` could also refer to the derive macro imported here
--> $DIR/proc-macro-attributes.rs:3:1
|
LL | #[macro_use]
| ^^^^^^^^^^^^
error[E0659]: `B` is ambiguous (derive helper attribute vs any other name)
--> $DIR/proc-macro-attributes.rs:9:3
|
LL | #[B(E = "foo")]
| ^ ambiguous name
|
note: `B` could refer to the derive helper attribute defined here
--> $DIR/proc-macro-attributes.rs:11:10
--> $DIR/proc-macro-attributes.rs:19:10
|
LL | #[derive(B)]
| ^
@ -58,11 +24,11 @@ LL | #[macro_use]
error[E0659]: `B` is ambiguous (derive helper attribute vs any other name)
--> $DIR/proc-macro-attributes.rs:10:3
|
LL | #[B(arbitrary tokens)]
LL | #[B(D)]
| ^ ambiguous name
|
note: `B` could refer to the derive helper attribute defined here
--> $DIR/proc-macro-attributes.rs:11:10
--> $DIR/proc-macro-attributes.rs:19:10
|
LL | #[derive(B)]
| ^
@ -72,6 +38,89 @@ note: `B` could also refer to the derive macro imported here
LL | #[macro_use]
| ^^^^^^^^^^^^
error: aborting due to 5 previous errors
error[E0659]: `B` is ambiguous (derive helper attribute vs any other name)
--> $DIR/proc-macro-attributes.rs:13:3
|
LL | #[B(E = "foo")]
| ^ ambiguous name
|
note: `B` could refer to the derive helper attribute defined here
--> $DIR/proc-macro-attributes.rs:19:10
|
LL | #[derive(B)]
| ^
note: `B` could also refer to the derive macro imported here
--> $DIR/proc-macro-attributes.rs:3:1
|
LL | #[macro_use]
| ^^^^^^^^^^^^
error[E0659]: `B` is ambiguous (derive helper attribute vs any other name)
--> $DIR/proc-macro-attributes.rs:16:3
|
LL | #[B(arbitrary tokens)]
| ^ ambiguous name
|
note: `B` could refer to the derive helper attribute defined here
--> $DIR/proc-macro-attributes.rs:19:10
|
LL | #[derive(B)]
| ^
note: `B` could also refer to the derive macro imported here
--> $DIR/proc-macro-attributes.rs:3:1
|
LL | #[macro_use]
| ^^^^^^^^^^^^
warning: derive helper attribute is used before it is introduced
--> $DIR/proc-macro-attributes.rs:6:3
|
LL | #[B]
| ^
...
LL | #[derive(B)]
| - the attribute is introduced here
|
= note: `#[warn(legacy_derive_helpers)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
warning: derive helper attribute is used before it is introduced
--> $DIR/proc-macro-attributes.rs:10:3
|
LL | #[B(D)]
| ^
...
LL | #[derive(B)]
| - the attribute is introduced here
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
warning: derive helper attribute is used before it is introduced
--> $DIR/proc-macro-attributes.rs:13:3
|
LL | #[B(E = "foo")]
| ^
...
LL | #[derive(B)]
| - the attribute is introduced here
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
warning: derive helper attribute is used before it is introduced
--> $DIR/proc-macro-attributes.rs:16:3
|
LL | #[B(arbitrary tokens)]
| ^
...
LL | #[derive(B)]
| - the attribute is introduced here
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
error: aborting due to 5 previous errors; 4 warnings emitted
For more information about this error, try `rustc --explain E0659`.

View file

@ -17,9 +17,3 @@ pub fn cfg_attr(_: TokenStream, input: TokenStream) -> TokenStream {
//~^ ERROR name `cfg_attr` is reserved in attribute namespace
input
}
#[proc_macro_attribute]
pub fn derive(_: TokenStream, input: TokenStream) -> TokenStream {
//~^ ERROR name `derive` is reserved in attribute namespace
input
}

View file

@ -10,11 +10,5 @@ error: name `cfg_attr` is reserved in attribute namespace
LL | pub fn cfg_attr(_: TokenStream, input: TokenStream) -> TokenStream {
| ^^^^^^^^
error: name `derive` is reserved in attribute namespace
--> $DIR/reserved-macro-names.rs:22:8
|
LL | pub fn derive(_: TokenStream, input: TokenStream) -> TokenStream {
| ^^^^^^
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors