Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_attr_parsing/src/attributes/link_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl CombineAttributeParser for LinkParser {
r#"name = "...", import_name_type = "decorated|noprefix|undecorated""#,
r#"name = "...", kind = "dylib|static|...", wasm_import_module = "...", import_name_type = "decorated|noprefix|undecorated""#,
], "https://doc.rust-lang.org/reference/items/external-blocks.html#the-link-attribute");
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
const ALLOWED_TARGETS: AllowedTargets =
AllowedTargets::AllowListWarnRest(&[Allow(Target::ForeignMod)]);
const STABILITY: AttributeStability = AttributeStability::Stable;

fn extend(
Expand Down
18 changes: 8 additions & 10 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
&AttributeKind::Sanitize { on_set, off_set, rtsan: _, span: attr_span } => {
self.check_sanitize(attr_span, on_set | off_set, span, target);
}
AttributeKind::Link(_, attr_span) => self.check_link(hir_id, *attr_span, span, target),
AttributeKind::Link(_, attr_span) => self.check_link(hir_id, *attr_span, target),
AttributeKind::MacroExport { span, .. } => {
self.check_macro_export(hir_id, *span, target)
}
Expand Down Expand Up @@ -1126,21 +1126,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}

/// Checks if `#[link]` is applied to an item other than a foreign module.
fn check_link(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
if target == Target::ForeignMod
&& let hir::Node::Item(item) = self.tcx.hir_node(hir_id)
fn check_link(&self, hir_id: HirId, attr_span: Span, target: Target) {
if target != Target::ForeignMod {
return; // Checked by attribute parser
}

if let hir::Node::Item(item) = self.tcx.hir_node(hir_id)
&& let Item { kind: ItemKind::ForeignMod { abi, .. }, .. } = item
&& !matches!(abi, ExternAbi::Rust)
{
return;
}

self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr_span,
errors::Link { span: (target != Target::ForeignMod).then_some(span) },
);
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr_span, errors::Link);
}

/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,7 @@ pub(crate) struct BothFfiConstAndPure {
#[warning(
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
)]
pub(crate) struct Link {
#[label("not an `extern` block")]
pub span: Option<Span>,
}
pub(crate) struct Link;

#[derive(Diagnostic)]
#[diag("#[rustc_legacy_const_generics] functions must only have const generics")]
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/attributes/attr-on-mac-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ fn main() {
#[should_panic]
//~^ 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
unreachable!();

#[repr()]
Expand Down
27 changes: 18 additions & 9 deletions tests/ui/attributes/attr-on-mac-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,17 @@ LL | #[should_panic]
= help: `#[should_panic]` can only be applied to functions
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[link_name]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:72:5
|
LL | #[link_name = "x"]
| ^^^^^^^^^^^^^^^^^^
|
= help: `#[link_name]` can be applied to foreign functions and foreign statics
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[repr()]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:74:5
--> $DIR/attr-on-mac-call.rs:77:5
|
LL | #[repr()]
| ^^^^^^^^^
Expand All @@ -211,15 +220,15 @@ LL | #[repr()]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: unused attribute
--> $DIR/attr-on-mac-call.rs:74:5
--> $DIR/attr-on-mac-call.rs:77:5
|
LL | #[repr()]
| ^^^^^^^^^ help: remove this attribute
|
= note: using `repr` with an empty list has no effect

warning: `#[repr(u8)]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:79:5
--> $DIR/attr-on-mac-call.rs:82:5
|
LL | #[repr(u8)]
| ^^^^^^^^^^^
Expand All @@ -228,7 +237,7 @@ LL | #[repr(u8)]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[repr(align(...))]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:83:5
--> $DIR/attr-on-mac-call.rs:86:5
|
LL | #[repr(align(8))]
| ^^^^^^^^^^^^^^^^^
Expand All @@ -237,7 +246,7 @@ LL | #[repr(align(8))]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[repr(packed)]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:87:5
--> $DIR/attr-on-mac-call.rs:90:5
|
LL | #[repr(packed)]
| ^^^^^^^^^^^^^^^
Expand All @@ -246,7 +255,7 @@ LL | #[repr(packed)]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[repr(C)]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:91:5
--> $DIR/attr-on-mac-call.rs:94:5
|
LL | #[repr(C)]
| ^^^^^^^^^^
Expand All @@ -255,7 +264,7 @@ LL | #[repr(C)]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[repr(Rust)]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:95:5
--> $DIR/attr-on-mac-call.rs:98:5
|
LL | #[repr(Rust)]
| ^^^^^^^^^^^^^
Expand All @@ -264,13 +273,13 @@ LL | #[repr(Rust)]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[repr(simd)]` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:99:5
--> $DIR/attr-on-mac-call.rs:102:5
|
LL | #[repr(simd)]
| ^^^^^^^^^^^^^
|
= help: `#[repr(simd)]` can only be applied to structs
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: 30 warnings emitted
warning: 31 warnings emitted

2 changes: 1 addition & 1 deletion tests/ui/attributes/malformed-attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
//~^ ERROR malformed
#[link]
//~^ ERROR malformed
//~| WARN attribute should be applied to an `extern` block with non-Rust ABI
//~| WARN attribute cannot be used on
//~| WARN previously accepted
#[link_name]
//~^ ERROR malformed
Expand Down
25 changes: 10 additions & 15 deletions tests/ui/attributes/malformed-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -814,21 +814,6 @@ LL | | #[coroutine = 63] || {}
LL | | }
| |_- not a `const fn`

warning: attribute should be applied to an `extern` block with non-Rust ABI
--> $DIR/malformed-attrs.rs:81:1
|
LL | #[link]
| ^^^^^^^
...
LL | / fn test() {
LL | | #[coroutine = 63] || {}
... |
LL | | }
| |_- not an `extern` block
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: requested on the command line with `-W unused-attributes`

error: valid forms for the attribute are `#[doc = "string"]`, `#[doc(alias)]`, `#[doc(attribute)]`, `#[doc(auto_cfg)]`, `#[doc(cfg)]`, `#[doc(fake_variadic)]`, `#[doc(hidden)]`, `#[doc(html_favicon_url)]`, `#[doc(html_logo_url)]`, `#[doc(html_no_source)]`, `#[doc(html_playground_url)]`, `#[doc(html_root_url)]`, `#[doc(include)]`, `#[doc(inline)]`, `#[doc(issue_tracker_base_url)]`, `#[doc(keyword)]`, `#[doc(masked)]`, `#[doc(no_default_passes)]`, `#[doc(no_inline)]`, `#[doc(notable_trait)]`, `#[doc(passes)]`, `#[doc(plugins)]`, `#[doc(rust_logo)]`, `#[doc(search_unbox)]`, `#[doc(spotlight)]`, and `#[doc(test)]`
--> $DIR/malformed-attrs.rs:41:1
|
Expand Down Expand Up @@ -865,13 +850,23 @@ LL | | #[coroutine = 63] || {}
... |
LL | | }
| |_^
= note: requested on the command line with `-W unused-attributes`

error: valid forms for the attribute are `#[doc = "string"]`, `#[doc(alias)]`, `#[doc(attribute)]`, `#[doc(auto_cfg)]`, `#[doc(cfg)]`, `#[doc(fake_variadic)]`, `#[doc(hidden)]`, `#[doc(html_favicon_url)]`, `#[doc(html_logo_url)]`, `#[doc(html_no_source)]`, `#[doc(html_playground_url)]`, `#[doc(html_root_url)]`, `#[doc(include)]`, `#[doc(inline)]`, `#[doc(issue_tracker_base_url)]`, `#[doc(keyword)]`, `#[doc(masked)]`, `#[doc(no_default_passes)]`, `#[doc(no_inline)]`, `#[doc(notable_trait)]`, `#[doc(passes)]`, `#[doc(plugins)]`, `#[doc(rust_logo)]`, `#[doc(search_unbox)]`, `#[doc(spotlight)]`, and `#[doc(test)]`
--> $DIR/malformed-attrs.rs:75:1
|
LL | #[doc]
| ^^^^^^

warning: `#[link]` attribute cannot be used on functions
--> $DIR/malformed-attrs.rs:81:1
|
LL | #[link]
| ^^^^^^^
|
= help: `#[link]` can only be applied to foreign modules
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

warning: `#[link_name]` attribute cannot be used on functions
--> $DIR/malformed-attrs.rs:85:1
|
Expand Down
38 changes: 22 additions & 16 deletions tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//~ NOTE not an `extern` block
// This test enumerates as many compiler-builtin ungated attributes as
// possible (that is, all the mutually compatible ones), and checks
// that we get "expected" (*) warnings for each in the various weird
Expand Down Expand Up @@ -67,8 +66,10 @@
//~| WARN previously accepted
//~| HELP can only be applied to
//~| HELP remove the attribute
#![link(name = "x")] //~ WARN attribute should be applied to an `extern` block
//~^ WARN this was previously accepted
#![link(name = "x")] //~ WARN attribute cannot be used on
//~| WARN this was previously accepted
//~| HELP can only be applied to foreign modules
//~| HELP remove the attribute
#![link_name = "1900"]
//~^ WARN attribute cannot be used on
//~| WARN previously accepted
Expand Down Expand Up @@ -683,35 +684,40 @@ mod link_section {
// Note that this is a `check-pass` test, so it will never invoke the linker.

#[link(name = "x")]
//~^ WARN attribute should be applied to an `extern` block
//~^ WARN attribute cannot be used on
//~| WARN this was previously accepted
//~| HELP can only be applied to foreign modules
//~| HELP remove the attribute
mod link {
//~^ NOTE not an `extern` block

mod inner { #![link(name = "x")] }
//~^ WARN attribute should be applied to an `extern` block
//~^ WARN attribute cannot be used on
//~| WARN this was previously accepted
//~| NOTE not an `extern` block
//~| HELP can only be applied to foreign modules
//~| HELP remove the attribute

#[link(name = "x")] fn f() { }
//~^ WARN attribute should be applied to an `extern` block
//~^ WARN attribute cannot be used on
//~| WARN this was previously accepted
//~| NOTE not an `extern` block
//~| HELP can only be applied to foreign modules
//~| HELP remove the attribute

#[link(name = "x")] struct S;
//~^ WARN attribute should be applied to an `extern` block
//~^ WARN attribute cannot be used on
//~| WARN this was previously accepted
//~| NOTE not an `extern` block
//~| HELP can only be applied to foreign modules
//~| HELP remove the attribute

#[link(name = "x")] type T = S;
//~^ WARN attribute should be applied to an `extern` block
//~^ WARN attribute cannot be used on
//~| WARN this was previously accepted
//~| NOTE not an `extern` block
//~| HELP can only be applied to foreign modules
//~| HELP remove the attribute

#[link(name = "x")] impl S { }
//~^ WARN attribute should be applied to an `extern` block
//~^ WARN attribute cannot be used on
//~| WARN this was previously accepted
//~| NOTE not an `extern` block
//~| HELP can only be applied to foreign modules
//~| HELP remove the attribute

#[link(name = "x")] extern "Rust" {}
//~^ WARN attribute should be applied to an `extern` block
Expand Down
Loading
Loading