Skip to content

Commit

Permalink
add more tests and make used(linker/compiler) mutually exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
cynecx committed Feb 8, 2022
1 parent e075586 commit 438826f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
34 changes: 34 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,12 +1741,46 @@ impl CheckAttrVisitor<'_> {
}

fn check_used(&self, attrs: &[Attribute], target: Target) {
let mut used_linker_span = None;
let mut used_compiler_span = None;
for attr in attrs {
if attr.has_name(sym::used) && target != Target::Static {
self.tcx
.sess
.span_err(attr.span, "attribute must be applied to a `static` variable");
}
let inner = attr.meta_item_list();
match inner.as_deref() {
Some([item]) if item.has_name(sym::linker) => {
if used_linker_span.is_none() {
used_linker_span = Some(attr.span);
}
}
Some([item]) if item.has_name(sym::compiler) => {
if used_compiler_span.is_none() {
used_compiler_span = Some(attr.span);
}
}
Some(_) => {
// This error case is handled in rustc_typeck::collect.
}
None => {
// Default case (compiler) when arg isn't defined.
if used_compiler_span.is_none() {
used_compiler_span = Some(attr.span);
}
}
}
}
if let (Some(linker_span), Some(compiler_span)) = (used_linker_span, used_compiler_span) {
let spans = vec![linker_span, compiler_span];
self.tcx
.sess
.struct_span_err(
spans,
"`used(compiler)` and `used(linker)` can't be used together",
)
.emit();
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ignore-tidy-filelength
//! "Collection" is the process of determining the type and other external
//! details of each item in Rust. Collection is specifically concerned
//! with *inter-procedural* things -- for example, for a function
Expand Down
6 changes: 2 additions & 4 deletions src/test/codegen/used_with_arg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// compile-flags: -O

#![crate_type = "lib"]
#![feature(used_with_arg)]

// CHECK: @llvm.used = appending global [1 x i8*]
// CHECK: @llvm.used = appending global [1 x i8*]{{.*}}USED_LINKER
#[used(linker)]
static mut USED_LINKER: [usize; 1] = [0];

// CHECK-NEXT: @llvm.compiler.used = appending global [1 x i8*]
// CHECK-NEXT: @llvm.compiler.used = appending global [1 x i8*]{{.*}}USED_COMPILER
#[used(compiler)]
static mut USED_COMPILER: [usize; 1] = [0];
19 changes: 19 additions & 0 deletions src/test/ui/used_with_arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![feature(used_with_arg)]

#[used(linker)]
static mut USED_LINKER: [usize; 1] = [0];

#[used(compiler)]
static mut USED_COMPILER: [usize; 1] = [0];

#[used(compiler)] //~ ERROR `used(compiler)` and `used(linker)` can't be used together
#[used(linker)]
static mut USED_COMPILER_LINKER2: [usize; 1] = [0];

#[used(compiler)] //~ ERROR `used(compiler)` and `used(linker)` can't be used together
#[used(linker)]
#[used(compiler)]
#[used(linker)]
static mut USED_COMPILER_LINKER3: [usize; 1] = [0];

fn main() {}
18 changes: 18 additions & 0 deletions src/test/ui/used_with_arg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: `used(compiler)` and `used(linker)` can't be used together
--> $DIR/used_with_arg.rs:9:1
|
LL | #[used(compiler)]
| ^^^^^^^^^^^^^^^^^
LL | #[used(linker)]
| ^^^^^^^^^^^^^^^

error: `used(compiler)` and `used(linker)` can't be used together
--> $DIR/used_with_arg.rs:13:1
|
LL | #[used(compiler)]
| ^^^^^^^^^^^^^^^^^
LL | #[used(linker)]
| ^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

6 changes: 6 additions & 0 deletions src/test/ui/used_with_multi_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(used_with_arg)]

#[used(compiler, linker)] //~ expected `used`, `used(compiler)` or `used(linker)`
static mut USED_COMPILER_LINKER: [usize; 1] = [0];

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/used_with_multi_args.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected `used`, `used(compiler)` or `used(linker)`
--> $DIR/used_with_multi_args.rs:3:1
|
LL | #[used(compiler, linker)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit 438826f

Please sign in to comment.