Skip to content

Commit 0b27d7b

Browse files
Rollup merge of #157331 - JonathanBrouwer:target-link, r=mejrs
Rewrite target checking for `#[link]` r? @mejrs
2 parents 6b61655 + d8698f5 commit 0b27d7b

11 files changed

Lines changed: 364 additions & 347 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ impl CombineAttributeParser for LinkParser {
7070
r#"name = "...", import_name_type = "decorated|noprefix|undecorated""#,
7171
r#"name = "...", kind = "dylib|static|...", wasm_import_module = "...", import_name_type = "decorated|noprefix|undecorated""#,
7272
], "https://doc.rust-lang.org/reference/items/external-blocks.html#the-link-attribute");
73-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
73+
const ALLOWED_TARGETS: AllowedTargets =
74+
AllowedTargets::AllowListWarnRest(&[Allow(Target::ForeignMod)]);
7475
const STABILITY: AttributeStability = AttributeStability::Stable;
7576

7677
fn extend(

compiler/rustc_passes/src/check_attr.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
230230
&AttributeKind::Sanitize { on_set, off_set, rtsan: _, span: attr_span } => {
231231
self.check_sanitize(attr_span, on_set | off_set, span, target);
232232
}
233-
AttributeKind::Link(_, attr_span) => self.check_link(hir_id, *attr_span, span, target),
233+
AttributeKind::Link(_, attr_span) => self.check_link(hir_id, *attr_span, target),
234234
AttributeKind::MacroExport { span, .. } => {
235235
self.check_macro_export(hir_id, *span, target)
236236
}
@@ -1126,21 +1126,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11261126
}
11271127

11281128
/// Checks if `#[link]` is applied to an item other than a foreign module.
1129-
fn check_link(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
1130-
if target == Target::ForeignMod
1131-
&& let hir::Node::Item(item) = self.tcx.hir_node(hir_id)
1129+
fn check_link(&self, hir_id: HirId, attr_span: Span, target: Target) {
1130+
if target != Target::ForeignMod {
1131+
return; // Checked by attribute parser
1132+
}
1133+
1134+
if let hir::Node::Item(item) = self.tcx.hir_node(hir_id)
11321135
&& let Item { kind: ItemKind::ForeignMod { abi, .. }, .. } = item
11331136
&& !matches!(abi, ExternAbi::Rust)
11341137
{
11351138
return;
11361139
}
11371140

1138-
self.tcx.emit_node_span_lint(
1139-
UNUSED_ATTRIBUTES,
1140-
hir_id,
1141-
attr_span,
1142-
errors::Link { span: (target != Target::ForeignMod).then_some(span) },
1143-
);
1141+
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr_span, errors::Link);
11441142
}
11451143

11461144
/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.

compiler/rustc_passes/src/errors.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ pub(crate) struct BothFfiConstAndPure {
183183
#[warning(
184184
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
185185
)]
186-
pub(crate) struct Link {
187-
#[label("not an `extern` block")]
188-
pub span: Option<Span>,
189-
}
186+
pub(crate) struct Link;
190187

191188
#[derive(Diagnostic)]
192189
#[diag("#[rustc_legacy_const_generics] functions must only have const generics")]

tests/ui/attributes/attr-on-mac-call.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ fn main() {
6969
#[should_panic]
7070
//~^ WARN attribute cannot be used on macro calls
7171
//~| WARN previously accepted
72+
#[link_name = "x"]
73+
//~^ WARN attribute cannot be used on macro calls
74+
//~| WARN previously accepted
7275
unreachable!();
7376

7477
#[repr()]

tests/ui/attributes/attr-on-mac-call.stderr

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,17 @@ LL | #[should_panic]
201201
= help: `#[should_panic]` can only be applied to functions
202202
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
203203

204+
warning: `#[link_name]` attribute cannot be used on macro calls
205+
--> $DIR/attr-on-mac-call.rs:72:5
206+
|
207+
LL | #[link_name = "x"]
208+
| ^^^^^^^^^^^^^^^^^^
209+
|
210+
= help: `#[link_name]` can be applied to foreign functions and foreign statics
211+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
212+
204213
warning: `#[repr()]` attribute cannot be used on macro calls
205-
--> $DIR/attr-on-mac-call.rs:74:5
214+
--> $DIR/attr-on-mac-call.rs:77:5
206215
|
207216
LL | #[repr()]
208217
| ^^^^^^^^^
@@ -211,15 +220,15 @@ LL | #[repr()]
211220
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
212221

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

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

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

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

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

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

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

275-
warning: 30 warnings emitted
284+
warning: 31 warnings emitted
276285

tests/ui/attributes/malformed-attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
//~^ ERROR malformed
8181
#[link]
8282
//~^ ERROR malformed
83-
//~| WARN attribute should be applied to an `extern` block with non-Rust ABI
83+
//~| WARN attribute cannot be used on
8484
//~| WARN previously accepted
8585
#[link_name]
8686
//~^ ERROR malformed

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -814,21 +814,6 @@ LL | | #[coroutine = 63] || {}
814814
LL | | }
815815
| |_- not a `const fn`
816816

817-
warning: attribute should be applied to an `extern` block with non-Rust ABI
818-
--> $DIR/malformed-attrs.rs:81:1
819-
|
820-
LL | #[link]
821-
| ^^^^^^^
822-
...
823-
LL | / fn test() {
824-
LL | | #[coroutine = 63] || {}
825-
... |
826-
LL | | }
827-
| |_- not an `extern` block
828-
|
829-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
830-
= note: requested on the command line with `-W unused-attributes`
831-
832817
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)]`
833818
--> $DIR/malformed-attrs.rs:41:1
834819
|
@@ -865,13 +850,23 @@ LL | | #[coroutine = 63] || {}
865850
... |
866851
LL | | }
867852
| |_^
853+
= note: requested on the command line with `-W unused-attributes`
868854

869855
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)]`
870856
--> $DIR/malformed-attrs.rs:75:1
871857
|
872858
LL | #[doc]
873859
| ^^^^^^
874860

861+
warning: `#[link]` attribute cannot be used on functions
862+
--> $DIR/malformed-attrs.rs:81:1
863+
|
864+
LL | #[link]
865+
| ^^^^^^^
866+
|
867+
= help: `#[link]` can only be applied to foreign modules
868+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
869+
875870
warning: `#[link_name]` attribute cannot be used on functions
876871
--> $DIR/malformed-attrs.rs:85:1
877872
|

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//~ NOTE not an `extern` block
21
// This test enumerates as many compiler-builtin ungated attributes as
32
// possible (that is, all the mutually compatible ones), and checks
43
// that we get "expected" (*) warnings for each in the various weird
@@ -67,8 +66,10 @@
6766
//~| WARN previously accepted
6867
//~| HELP can only be applied to
6968
//~| HELP remove the attribute
70-
#![link(name = "x")] //~ WARN attribute should be applied to an `extern` block
71-
//~^ WARN this was previously accepted
69+
#![link(name = "x")] //~ WARN attribute cannot be used on
70+
//~| WARN this was previously accepted
71+
//~| HELP can only be applied to foreign modules
72+
//~| HELP remove the attribute
7273
#![link_name = "1900"]
7374
//~^ WARN attribute cannot be used on
7475
//~| WARN previously accepted
@@ -683,35 +684,40 @@ mod link_section {
683684
// Note that this is a `check-pass` test, so it will never invoke the linker.
684685

685686
#[link(name = "x")]
686-
//~^ WARN attribute should be applied to an `extern` block
687+
//~^ WARN attribute cannot be used on
687688
//~| WARN this was previously accepted
689+
//~| HELP can only be applied to foreign modules
690+
//~| HELP remove the attribute
688691
mod link {
689-
//~^ NOTE not an `extern` block
690-
691692
mod inner { #![link(name = "x")] }
692-
//~^ WARN attribute should be applied to an `extern` block
693+
//~^ WARN attribute cannot be used on
693694
//~| WARN this was previously accepted
694-
//~| NOTE not an `extern` block
695+
//~| HELP can only be applied to foreign modules
696+
//~| HELP remove the attribute
695697

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

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

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

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

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

0 commit comments

Comments
 (0)