Skip to content

Commit 59d4bae

Browse files
committed
Add validation for link attribute position.
1 parent f7bb8e3 commit 59d4bae

File tree

3 files changed

+264
-161
lines changed

3 files changed

+264
-161
lines changed

compiler/rustc_passes/src/check_attr.rs

+21
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl CheckAttrVisitor<'_> {
126126
// lint-only checks
127127
match attr.name_or_empty() {
128128
sym::cold => self.check_cold(hir_id, attr, span, target),
129+
sym::link => self.check_link(hir_id, attr, span, target),
129130
sym::link_name => self.check_link_name(hir_id, attr, span, target),
130131
sym::link_section => self.check_link_section(hir_id, attr, span, target),
131132
sym::no_mangle => self.check_no_mangle(hir_id, attr, span, target),
@@ -1140,6 +1141,26 @@ impl CheckAttrVisitor<'_> {
11401141
}
11411142
}
11421143

1144+
/// Checks if `#[link]` is applied to an item other than a foreign module.
1145+
fn check_link(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
1146+
match target {
1147+
Target::ForeignMod => {}
1148+
_ => {
1149+
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
1150+
let mut diag = lint.build("attribute should be applied to an `extern` block");
1151+
diag.warn(
1152+
"this was previously accepted by the compiler but is \
1153+
being phased out; it will become a hard error in \
1154+
a future release!",
1155+
);
1156+
1157+
diag.span_label(*span, "not an `extern` block");
1158+
diag.emit();
1159+
});
1160+
}
1161+
}
1162+
}
1163+
11431164
/// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
11441165
fn check_link_name(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
11451166
match target {

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//~ NOTE not a function
2-
//~^ NOTE not a foreign function or static
3-
//~^^ NOTE not a function or static
2+
//~| NOTE not a foreign function or static
3+
//~| NOTE not a function or static
4+
//~| NOTE not an `extern` block
45
// This test enumerates as many compiler-builtin ungated attributes as
56
// possible (that is, all the mutually compatible ones), and checks
67
// that we get "expected" (*) warnings for each in the various weird
@@ -59,9 +60,9 @@
5960
#![proc_macro_derive()] //~ WARN `#[proc_macro_derive]` only has an effect
6061
#![doc = "2400"]
6162
#![cold] //~ WARN attribute should be applied to a function
62-
//~^ WARN
63-
// see issue-43106-gating-of-builtin-attrs-error.rs
64-
#![link()]
63+
//~^ WARN this was previously accepted
64+
#![link()] //~ WARN attribute should be applied to an `extern` block
65+
//~^ WARN this was previously accepted
6566
#![link_name = "1900"]
6667
//~^ WARN attribute should be applied to a foreign function
6768
//~^^ WARN this was previously accepted by the compiler
@@ -547,22 +548,38 @@ mod link_section {
547548
}
548549

549550

550-
// Note that this is a `check-pass` test, so it
551-
// will never invoke the linker. These are here nonetheless to point
552-
// out that we allow them at non-crate-level (though I do not know
553-
// whether they have the same effect here as at crate-level).
551+
// Note that this is a `check-pass` test, so it will never invoke the linker.
554552

555553
#[link()]
554+
//~^ WARN attribute should be applied to an `extern` block
555+
//~| WARN this was previously accepted
556556
mod link {
557+
//~^ NOTE not an `extern` block
558+
557559
mod inner { #![link()] }
560+
//~^ WARN attribute should be applied to an `extern` block
561+
//~| WARN this was previously accepted
562+
//~| NOTE not an `extern` block
558563

559564
#[link()] fn f() { }
565+
//~^ WARN attribute should be applied to an `extern` block
566+
//~| WARN this was previously accepted
567+
//~| NOTE not an `extern` block
560568

561569
#[link()] struct S;
570+
//~^ WARN attribute should be applied to an `extern` block
571+
//~| WARN this was previously accepted
572+
//~| NOTE not an `extern` block
562573

563574
#[link()] type T = S;
575+
//~^ WARN attribute should be applied to an `extern` block
576+
//~| WARN this was previously accepted
577+
//~| NOTE not an `extern` block
564578

565579
#[link()] impl S { }
580+
//~^ WARN attribute should be applied to an `extern` block
581+
//~| WARN this was previously accepted
582+
//~| NOTE not an `extern` block
566583
}
567584

568585
struct StructForDeprecated;

0 commit comments

Comments
 (0)