Skip to content

Commit

Permalink
Unrolled build for rust-lang#128989
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#128989 - s7tya:check-linkage-attribute-pos, r=petrochenkov

Emit an error for invalid use of the linkage attribute

fixes rust-lang#128486

Currently, the use of the linkage attribute for Mod, Impl,... is incorrectly permitted. This PR will correct this issue by generating errors, and I've also added some UI test cases for it.

Related: rust-lang#128552.
  • Loading branch information
rust-timer authored Aug 17, 2024
2 parents 9b318d2 + 3c8dad1 commit 34a0712
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ passes_link_section =
.warn = {-passes_previously_accepted}
.label = not a function or static
passes_linkage =
attribute should be applied to a function or static
.label = not a function definition or static
passes_macro_export =
`#[macro_export]` only has an effect on macro definitions
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::coroutine, ..] => {
self.check_coroutine(attr, target);
}
[sym::linkage, ..] => self.check_linkage(attr, span, target),
[
// ok
sym::allow
Expand All @@ -256,7 +257,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| sym::cfi_encoding // FIXME(cfi_encoding)
| sym::may_dangle // FIXME(dropck_eyepatch)
| sym::pointee // FIXME(derive_smart_pointer)
| sym::linkage // FIXME(linkage)
| sym::omit_gdb_pretty_printer_section // FIXME(omit_gdb_pretty_printer_section)
| sym::used // handled elsewhere to restrict to static items
| sym::repr // handled elsewhere to restrict to type decls items
Expand Down Expand Up @@ -2347,6 +2347,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
}

fn check_linkage(&self, attr: &Attribute, span: Span, target: Target) {
match target {
Target::Fn
| Target::Method(..)
| Target::Static
| Target::ForeignStatic
| Target::ForeignFn => {}
_ => {
self.dcx().emit_err(errors::Linkage { attr_span: attr.span, span });
}
}
}
}

impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,15 @@ pub struct CoroutineOnNonClosure {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_linkage)]
pub struct Linkage {
#[primary_span]
pub attr_span: Span,
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_empty_confusables)]
pub(crate) struct EmptyConfusables {
Expand Down
42 changes: 42 additions & 0 deletions tests/ui/attributes/linkage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![feature(linkage)]
#![feature(stmt_expr_attributes)]
#![deny(unused_attributes)]
#![allow(dead_code)]

#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
type InvalidTy = ();

#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
mod invalid_module {}

#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
struct F;

#[linkage = "weak"] //~ ERROR attribute should be applied to a function or static
impl F {
#[linkage = "weak"]
fn valid(&self) {}
}

#[linkage = "weak"]
fn f() {
#[linkage = "weak"]
{
1
};
//~^^^^ ERROR attribute should be applied to a function or static
}

extern "C" {
#[linkage = "weak"]
static A: *const ();

#[linkage = "weak"]
fn bar();
}

fn main() {
let _ = #[linkage = "weak"]
(|| 1);
//~^^ ERROR attribute should be applied to a function or static
}
55 changes: 55 additions & 0 deletions tests/ui/attributes/linkage.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
error: attribute should be applied to a function or static
--> $DIR/linkage.rs:6:1
|
LL | #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^
LL | type InvalidTy = ();
| -------------------- not a function definition or static

error: attribute should be applied to a function or static
--> $DIR/linkage.rs:9:1
|
LL | #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^
LL | mod invalid_module {}
| --------------------- not a function definition or static

error: attribute should be applied to a function or static
--> $DIR/linkage.rs:12:1
|
LL | #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^
LL | struct F;
| --------- not a function definition or static

error: attribute should be applied to a function or static
--> $DIR/linkage.rs:15:1
|
LL | #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^
LL | / impl F {
LL | | #[linkage = "weak"]
LL | | fn valid(&self) {}
LL | | }
| |_- not a function definition or static

error: attribute should be applied to a function or static
--> $DIR/linkage.rs:23:5
|
LL | #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^
LL | / {
LL | | 1
LL | | };
| |_____- not a function definition or static

error: attribute should be applied to a function or static
--> $DIR/linkage.rs:39:13
|
LL | let _ = #[linkage = "weak"]
| ^^^^^^^^^^^^^^^^^^^
LL | (|| 1);
| ------ not a function definition or static

error: aborting due to 6 previous errors

0 comments on commit 34a0712

Please sign in to comment.