Skip to content

Commit 8b451e4

Browse files
committed
Auto merge of #151152 - nik-contrib:helper_attr_builtin, r=<try>
Add FCW for derive helper attributes that will conflict with built-in attributes try-job: test-various
2 parents d00ba92 + 846e4ee commit 8b451e4

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

compiler/rustc_attr_parsing/src/attributes/proc_macro_attrs.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use rustc_hir::lints::AttributeLintKind;
2+
use rustc_session::lint::builtin::AMBIGUOUS_DERIVE_HELPERS;
3+
14
use super::prelude::*;
25

36
const PROC_MACRO_ALLOWED_TARGETS: AllowedTargets =
@@ -126,6 +129,13 @@ fn parse_derive_like<S: Stage>(
126129
cx.expected_identifier(ident.span);
127130
return None;
128131
}
132+
if rustc_feature::is_builtin_attr_name(ident.name) {
133+
cx.emit_lint(
134+
AMBIGUOUS_DERIVE_HELPERS,
135+
AttributeLintKind::AmbiguousDeriveHelpers,
136+
ident.span,
137+
);
138+
}
129139
attributes.push(ident.name);
130140
}
131141
}

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ pub fn decorate_attribute_lint(
383383
lints::DocAutoCfgExpectsHideOrShow.decorate_lint(diag)
384384
}
385385

386+
&AttributeLintKind::AmbiguousDeriveHelpers => {
387+
lints::AmbiguousDeriveHelpers.decorate_lint(diag)
388+
}
389+
386390
&AttributeLintKind::DocAutoCfgHideShowUnexpectedItem { attr_name } => {
387391
lints::DocAutoCfgHideShowUnexpectedItem { attr_name }.decorate_lint(diag)
388392
}

compiler/rustc_lint/src/lints.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3752,6 +3752,10 @@ pub(crate) struct DocAliasDuplicated {
37523752
#[diag("only `hide` or `show` are allowed in `#[doc(auto_cfg(...))]`")]
37533753
pub(crate) struct DocAutoCfgExpectsHideOrShow;
37543754

3755+
#[derive(LintDiagnostic)]
3756+
#[diag("there exists a built-in attribute with the same name")]
3757+
pub(crate) struct AmbiguousDeriveHelpers;
3758+
37553759
#[derive(LintDiagnostic)]
37563760
#[diag("`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/value items")]
37573761
pub(crate) struct DocAutoCfgHideShowUnexpectedItem {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare_lint_pass! {
1717
AARCH64_SOFTFLOAT_NEON,
1818
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
1919
AMBIGUOUS_ASSOCIATED_ITEMS,
20+
AMBIGUOUS_DERIVE_HELPERS,
2021
AMBIGUOUS_GLOB_IMPORTED_TRAITS,
2122
AMBIGUOUS_GLOB_IMPORTS,
2223
AMBIGUOUS_GLOB_REEXPORTS,
@@ -4267,6 +4268,75 @@ declare_lint! {
42674268
};
42684269
}
42694270

4271+
declare_lint! {
4272+
/// The `ambiguous_derive_helpers` lint detects cases where a derive macro's helper attribute
4273+
/// is the same name as that of a built-in attribute.
4274+
///
4275+
/// ### Example
4276+
///
4277+
/// ```rust,ignore (proc-macro)
4278+
/// #![crate_type = "proc-macro"]
4279+
/// #![deny(ambiguous_derive_helpers)]
4280+
///
4281+
/// use proc_macro::TokenStream;
4282+
///
4283+
/// #[proc_macro_derive(Trait, attributes(ignore))]
4284+
/// pub fn example(input: TokenStream) -> TokenStream {
4285+
/// TokenStream::new()
4286+
/// }
4287+
/// ```
4288+
///
4289+
/// Produces:
4290+
///
4291+
/// ```text
4292+
/// warning: there exists a built-in attribute with the same name
4293+
/// --> file.rs:5:39
4294+
/// |
4295+
/// 5 | #[proc_macro_derive(Trait, attributes(ignore))]
4296+
/// | ^^^^^^
4297+
/// |
4298+
/// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4299+
/// = note: for more information, see issue #151152 <https://github.com/rust-lang/rust/issues/151152>
4300+
/// = note: `#[deny(ambiguous_derive_helpers)]` (part of `#[deny(future_incompatible)]`) on by default
4301+
/// ```
4302+
///
4303+
/// ### Explanation
4304+
///
4305+
/// Attempting to use this helper attribute will throw an error:
4306+
///
4307+
/// ```rust,ignore (needs-dependency)
4308+
/// #[derive(Trait)]
4309+
/// struct Example {
4310+
/// #[ignore]
4311+
/// fields: ()
4312+
/// }
4313+
/// ```
4314+
///
4315+
/// Produces:
4316+
///
4317+
/// ```text
4318+
/// error[E0659]: `ignore` is ambiguous
4319+
/// --> src/lib.rs:5:7
4320+
/// |
4321+
/// 5 | #[ignore]
4322+
/// | ^^^^^^ ambiguous name
4323+
/// |
4324+
/// = note: ambiguous because of a name conflict with a builtin attribute
4325+
/// = note: `ignore` could refer to a built-in attribute
4326+
/// note: `ignore` could also refer to the derive helper attribute defined here
4327+
/// --> src/lib.rs:3:10
4328+
/// |
4329+
/// 3 | #[derive(Trait)]
4330+
/// | ^^^^^
4331+
/// ```
4332+
pub AMBIGUOUS_DERIVE_HELPERS,
4333+
Warn,
4334+
"detects derive helper attributes that are ambiguous with built-in attributes",
4335+
@future_incompatible = FutureIncompatibleInfo {
4336+
reason: fcw!(FutureReleaseError #151276),
4337+
};
4338+
}
4339+
42704340
declare_lint! {
42714341
/// The `private_interfaces` lint detects types in a primary interface of an item,
42724342
/// that are more private than the item itself. Primary interface of an item is all

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ pub enum AttributeLintKind {
800800
attr_name: Symbol,
801801
},
802802
DocInvalid,
803+
AmbiguousDeriveHelpers,
803804
DocUnknownInclude {
804805
span: Span,
805806
inner: &'static str,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ force-host
2+
//@ no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
#![deny(ambiguous_derive_helpers)]
6+
7+
extern crate proc_macro;
8+
9+
use proc_macro::TokenStream;
10+
11+
#[proc_macro_derive(Trait, attributes(ignore))] //~ ERROR there exists a built-in attribute with the same name
12+
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13+
pub fn deriving(input: TokenStream) -> TokenStream {
14+
TokenStream::new()
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: there exists a built-in attribute with the same name
2+
--> $DIR/ambiguous_derive_helpers.rs:11:39
3+
|
4+
LL | #[proc_macro_derive(Trait, attributes(ignore))]
5+
| ^^^^^^
6+
|
7+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8+
= note: for more information, see issue #151276 <https://github.com/rust-lang/rust/issues/151276>
9+
note: the lint level is defined here
10+
--> $DIR/ambiguous_derive_helpers.rs:5:9
11+
|
12+
LL | #![deny(ambiguous_derive_helpers)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+

0 commit comments

Comments
 (0)