-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Fix target checking for some attributes on macro calls #156569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,7 +14,7 @@ use crate::errors::{ | |||||
| UnsupportedAttributesInWhere, | ||||||
| }; | ||||||
| use crate::session_diagnostics::InvalidTarget; | ||||||
| use crate::target_checking::Policy::Allow; | ||||||
| use crate::target_checking::Policy::{Allow, Warn}; | ||||||
|
|
||||||
| #[derive(Debug)] | ||||||
| pub(crate) enum AllowedTargets { | ||||||
|
|
@@ -394,10 +394,81 @@ fn filter_targets( | |||||
| added_fake_targets.push(target_group_name); | ||||||
| } | ||||||
|
|
||||||
| /// This is a list of default targets to which a attribute can be applied | ||||||
| /// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
two nits:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These attributes don't have target checks during parsing because they need access to hir, not because we haven't gotten around to moving them around. Unless that's something you're actively planning to change, we shouldn't say "yet". |
||||||
| /// This excludes `Target::MacroCall`, as attributes on macro calls are otherwise not checked for parsed attributes. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or something similar |
||||||
| pub(crate) const CHECKED_LATER: &'static [Policy] = { | ||||||
| use Policy::Allow; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Redundant import. |
||||||
| &[ | ||||||
| Allow(Target::ExternCrate), | ||||||
| Allow(Target::Use), | ||||||
| Allow(Target::Static), | ||||||
| Allow(Target::Const), | ||||||
| Allow(Target::Fn), | ||||||
| Allow(Target::Closure), | ||||||
| Allow(Target::Mod), | ||||||
| Allow(Target::ForeignMod), | ||||||
| Allow(Target::GlobalAsm), | ||||||
| Allow(Target::TyAlias), | ||||||
| Allow(Target::Enum), | ||||||
| Allow(Target::Variant), | ||||||
| Allow(Target::Struct), | ||||||
| Allow(Target::Field), | ||||||
| Allow(Target::Union), | ||||||
| Allow(Target::Trait), | ||||||
| Allow(Target::TraitAlias), | ||||||
| Allow(Target::Impl { of_trait: false }), | ||||||
| Allow(Target::Impl { of_trait: true }), | ||||||
| Allow(Target::Expression), | ||||||
| Allow(Target::Statement), | ||||||
| Allow(Target::Arm), | ||||||
| Allow(Target::AssocConst), | ||||||
| Allow(Target::Method(MethodKind::Inherent)), | ||||||
| Allow(Target::Method(MethodKind::Trait { body: false })), | ||||||
| Allow(Target::Method(MethodKind::Trait { body: true })), | ||||||
| Allow(Target::Method(MethodKind::TraitImpl)), | ||||||
| Allow(Target::AssocTy), | ||||||
| Allow(Target::ForeignFn), | ||||||
| Allow(Target::ForeignStatic), | ||||||
| Allow(Target::ForeignTy), | ||||||
| Allow(Target::MacroDef), | ||||||
| Allow(Target::Param), | ||||||
| Allow(Target::PatField), | ||||||
| Allow(Target::ExprField), | ||||||
| Allow(Target::WherePredicate), | ||||||
| Allow(Target::Crate), | ||||||
| Allow(Target::Delegation { mac: false }), | ||||||
| Allow(Target::Delegation { mac: true }), | ||||||
| Allow(Target::GenericParam { | ||||||
| kind: rustc_hir::target::GenericParamKind::Const, | ||||||
| has_default: false, | ||||||
| }), | ||||||
| Allow(Target::GenericParam { | ||||||
| kind: rustc_hir::target::GenericParamKind::Const, | ||||||
| has_default: true, | ||||||
| }), | ||||||
| Allow(Target::GenericParam { | ||||||
| kind: rustc_hir::target::GenericParamKind::Lifetime, | ||||||
| has_default: false, | ||||||
| }), | ||||||
| Allow(Target::GenericParam { | ||||||
| kind: rustc_hir::target::GenericParamKind::Lifetime, | ||||||
| has_default: true, | ||||||
| }), | ||||||
| Allow(Target::GenericParam { | ||||||
| kind: rustc_hir::target::GenericParamKind::Type, | ||||||
| has_default: false, | ||||||
| }), | ||||||
| Allow(Target::GenericParam { | ||||||
| kind: rustc_hir::target::GenericParamKind::Type, | ||||||
| has_default: true, | ||||||
| }), | ||||||
| Warn(Target::MacroCall), | ||||||
| ] | ||||||
| }; | ||||||
|
|
||||||
| /// This is the list of all targets to which a attribute can be applied | ||||||
| /// This is used for: | ||||||
| /// - `rustc_dummy`, which can be applied to all targets | ||||||
| /// - Attributes that are not parted to the new target system yet can use this list as a placeholder | ||||||
| /// This is used for attributes that are actually allowed on all targets, such as `rustc_dummy` | ||||||
| pub(crate) const ALL_TARGETS: &'static [Policy] = { | ||||||
| use Policy::Allow; | ||||||
| &[ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //@ check-pass | ||
| #![feature(sanitize)] | ||
| #![feature(register_tool)] | ||
| #![feature(export_stable)] | ||
| #![feature(lang_items)] | ||
| #![feature(dropck_eyepatch)] | ||
|
Comment on lines
+2
to
+6
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose we just error for all these unstable attributes on macro call positions, no need to bother with a lint or fcw. |
||
| #![feature(diagnostic_on_const)] | ||
| #![feature(diagnostic_on_move)] | ||
| #![feature(diagnostic_on_unknown)] | ||
| #![feature(diagnostic_on_unmatch_args)] | ||
| #![warn(unused)] | ||
|
|
||
| macro_rules! test { () => {} } | ||
|
|
||
| #[doc = ""] | ||
| //~^ WARN unused doc comment | ||
| #[diagnostic::do_not_recommend] | ||
| //~^ WARN can only be placed on trait implementations | ||
| #[diagnostic::on_const] | ||
| //~^ WARN can only be applied to non-const trait implementations | ||
| #[diagnostic::on_move] | ||
| //~^ WARN can only be applied to enums, structs or unions | ||
| #[diagnostic::on_unimplemented] | ||
| //~^ WARN can only be applied to trait definitions | ||
| #[diagnostic::on_unknown] | ||
| //~^ WARN can only be applied to `use` statements | ||
| #[diagnostic::on_unmatch_args] | ||
| //~^ WARN can only be applied to macro definitions | ||
| #[sanitize()] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| #[register_tool(test)] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| #[link(name = "x")] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| #[export_stable] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| #[repr(align(64))] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| #[lang = "sized"] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| #[panic_handler] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| #[may_dangle] | ||
| //~^ WARN attribute cannot be used on macro calls | ||
| //~| WARN previously accepted | ||
| test!(); | ||
|
|
||
| fn main() {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please merge this file into https://github.com/rust-lang/rust/blob/main/tests/ui/attributes/attr-on-mac-call.rs instead. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| warning: unused doc comment | ||
| --> $DIR/macro-call-attr.rs:15:1 | ||
| | | ||
| LL | #[doc = ""] | ||
| | ^^^^^^^^^^^ rustdoc does not generate documentation for macro invocations | ||
| | | ||
| = help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion | ||
| note: the lint level is defined here | ||
| --> $DIR/macro-call-attr.rs:11:9 | ||
| | | ||
| LL | #![warn(unused)] | ||
| | ^^^^^^ | ||
| = note: `#[warn(unused_doc_comments)]` implied by `#[warn(unused)]` | ||
|
|
||
| warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations | ||
| --> $DIR/macro-call-attr.rs:17:1 | ||
| | | ||
| LL | #[diagnostic::do_not_recommend] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ... | ||
| LL | test!(); | ||
| | ------- not a trait implementation | ||
| | | ||
| = note: `#[warn(misplaced_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default | ||
|
|
||
| warning: `#[diagnostic::on_const]` can only be applied to non-const trait implementations | ||
| --> $DIR/macro-call-attr.rs:19:1 | ||
| | | ||
| LL | #[diagnostic::on_const] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ... | ||
| LL | test!(); | ||
| | ------- not a trait implementation | ||
|
|
||
| warning: `#[diagnostic::on_move]` can only be applied to enums, structs or unions | ||
| --> $DIR/macro-call-attr.rs:21:1 | ||
| | | ||
| LL | #[diagnostic::on_move] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| warning: `#[diagnostic::on_unimplemented]` can only be applied to trait definitions | ||
| --> $DIR/macro-call-attr.rs:23:1 | ||
| | | ||
| LL | #[diagnostic::on_unimplemented] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| warning: `#[diagnostic::on_unknown]` can only be applied to `use` statements | ||
| --> $DIR/macro-call-attr.rs:25:1 | ||
| | | ||
| LL | #[diagnostic::on_unknown] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ... | ||
| LL | test!(); | ||
| | ------- not an import | ||
|
|
||
| warning: `#[diagnostic::on_unmatch_args]` can only be applied to macro definitions | ||
| --> $DIR/macro-call-attr.rs:27:1 | ||
| | | ||
| LL | #[diagnostic::on_unmatch_args] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| warning: `#[sanitize]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:29:1 | ||
| | | ||
| LL | #[sanitize()] | ||
| | ^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[sanitize]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
| = note: `#[warn(unused_attributes)]` implied by `#[warn(unused)]` | ||
|
|
||
| warning: `#[register_tool]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:32:1 | ||
| | | ||
| LL | #[register_tool(test)] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[register_tool]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
|
|
||
| warning: `#[link]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:35:1 | ||
| | | ||
| LL | #[link(name = "x")] | ||
| | ^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[link]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't be saying this huge list of "valid" targets. |
||
|
|
||
| warning: `#[export_stable]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:38:1 | ||
| | | ||
| LL | #[export_stable] | ||
| | ^^^^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[export_stable]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
|
|
||
| warning: `#[repr]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:41:1 | ||
| | | ||
| LL | #[repr(align(64))] | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[repr]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
|
|
||
| warning: `#[lang]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:44:1 | ||
| | | ||
| LL | #[lang = "sized"] | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[lang]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
|
|
||
| warning: `#[panic_handler]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:47:1 | ||
| | | ||
| LL | #[panic_handler] | ||
| | ^^^^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[panic_handler]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
|
|
||
| warning: `#[may_dangle]` attribute cannot be used on macro calls | ||
| --> $DIR/macro-call-attr.rs:50:1 | ||
| | | ||
| LL | #[may_dangle] | ||
| | ^^^^^^^^^^^^^ | ||
| | | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = help: `#[may_dangle]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates | ||
|
|
||
| warning: 15 warnings emitted | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not have this big paragraph included in the module level docs :)