add more tests and make used(linker/compiler) mutually exclusive
This commit is contained in:
parent
e075586d4f
commit
438826fd1a
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
src/test/ui/used_with_arg.rs
Normal file
19
src/test/ui/used_with_arg.rs
Normal file
@ -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
src/test/ui/used_with_arg.stderr
Normal file
18
src/test/ui/used_with_arg.stderr
Normal file
@ -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
src/test/ui/used_with_multi_args.rs
Normal file
6
src/test/ui/used_with_multi_args.rs
Normal file
@ -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
src/test/ui/used_with_multi_args.stderr
Normal file
8
src/test/ui/used_with_multi_args.stderr
Normal file
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user