Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,8 @@ pub(crate) struct InvalidTarget {
pub attr_span: Span,
pub name: AttrPath,
pub target: &'static str,
#[context]
pub target_span: Span,
pub applied: DiagArgValue,
pub only: &'static str,
pub attribute_args: String,
Expand Down
34 changes: 21 additions & 13 deletions compiler/rustc_attr_parsing/src/target_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,31 @@ impl<'sess> AttributeParser<'sess> {
let (applied, only) = allowed_targets_applied(allowed_targets, cx.target, cx.features);
let is_diagnostic_attr = cx.attr_path.segments[0] == sym::diagnostic;

let span = if attribute_args.is_empty() {
// Example: for the attribute `#[inline]`, name+attribute_args gives "inline",
// and the path span covers `inline` which is just what we want.
cx.attr_path.span
} else {
// Example 1: for the attribute `#[repr(C)]`, name+attribute_args gives
// "repr(C)", and the inner span covers `repr(C)` which is just what we want.
//
// Example 2: for the attribute `#[repr(C, packed)]`, name+attribute_args gives
// "repr(C)", and the inner span covers `repr(C, packed)` which doesn't match
// exactly but is as close as we can get.
cx.inner_span
};
let target_span = if span.eq_ctxt(cx.target_span) && cx.target != Target::Crate {
cx.target_span.shrink_to_lo()
} else {
span
};

let diag = InvalidTarget {
span: if attribute_args.is_empty() {
// Example: for the attribute `#[inline]`, name+attribute_args gives "inline",
// and the path span covers `inline` which is just what we want.
cx.attr_path.span
} else {
// Example 1: for the attribute `#[repr(C)]`, name+attribute_args gives
// "repr(C)", and the inner span covers `repr(C)` which is just what we want.
//
// Example 2: for the attribute `#[repr(C, packed)]`, name+attribute_args gives
// "repr(C)", and the inner span covers `repr(C, packed)` which doesn't match
// exactly but is as close as we can get.
cx.inner_span
},
span,
attr_span: cx.attr_span,
name: cx.attr_path.clone(),
target: cx.target.plural_name(),
target_span,
only: if only { "only " } else { "" },
applied: DiagArgValue::StrListSepByAnd(applied.into_iter().map(Cow::Owned).collect()),
attribute_args: attribute_args.to_string(),
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/abi/debug.generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ error: the `rustc_abi` attribute cannot be used on constants
|
LL | #[rustc_abi(debug)]
| ^^^^^^^^^
LL | const C: () = ();
|
= help: the `rustc_abi` attribute can be applied to functions and type aliases

Expand All @@ -11,6 +12,7 @@ error: the `rustc_abi` attribute cannot be used on associated consts
|
LL | #[rustc_abi(debug)]
| ^^^^^^^^^
LL | const C: () = ();
|
= help: the `rustc_abi` attribute can be applied to functions and type aliases

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/abi/debug.loongarch64.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ error: the `rustc_abi` attribute cannot be used on constants
|
LL | #[rustc_abi(debug)]
| ^^^^^^^^^
LL | const C: () = ();
|
= help: the `rustc_abi` attribute can be applied to functions and type aliases

Expand All @@ -11,6 +12,7 @@ error: the `rustc_abi` attribute cannot be used on associated consts
|
LL | #[rustc_abi(debug)]
| ^^^^^^^^^
LL | const C: () = ();
|
= help: the `rustc_abi` attribute can be applied to functions and type aliases

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/abi/debug.riscv64.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ error: the `rustc_abi` attribute cannot be used on constants
|
LL | #[rustc_abi(debug)]
| ^^^^^^^^^
LL | const C: () = ();
|
= help: the `rustc_abi` attribute can be applied to functions and type aliases

Expand All @@ -11,6 +12,7 @@ error: the `rustc_abi` attribute cannot be used on associated consts
|
LL | #[rustc_abi(debug)]
| ^^^^^^^^^
LL | const C: () = ();
|
= help: the `rustc_abi` attribute can be applied to functions and type aliases

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/asm/invalid-repr-simd-on-enum-148634.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error: the `repr(simd)` attribute cannot be used on enums
|
LL | #[repr(simd)]
| ^^^^^^^^^^
...
LL | enum Es {}
|
= help: the `repr(simd)` attribute can only be applied to structs

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/asm/naked-functions/invalid-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ error: the `naked` attribute cannot be used on foreign functions
|
LL | #[unsafe(naked)]
| ^^^^^
LL | fn f();
|
= help: the `naked` attribute can only be applied to functions with a body

Expand All @@ -25,6 +26,8 @@ error: the `naked` attribute cannot be used on structs
|
LL | #[unsafe(naked)]
| ^^^^^
LL | #[repr(C)]
LL | struct S {
|
= help: the `naked` attribute can only be applied to functions

Expand All @@ -33,6 +36,7 @@ error: the `naked` attribute cannot be used on struct fields
|
LL | #[unsafe(naked)]
| ^^^^^
LL | a: u32,
|
= help: the `naked` attribute can only be applied to functions

Expand All @@ -41,6 +45,7 @@ error: the `naked` attribute cannot be used on required trait methods
|
LL | #[unsafe(naked)]
| ^^^^^
LL | extern "C" fn invoke(&self);
|
= help: the `naked` attribute can only be applied to functions with a body

Expand All @@ -49,6 +54,7 @@ error: the `naked` attribute cannot be used on closures
|
LL | #[unsafe(naked)]
| ^^^^^
LL | || {};
|
= help: the `naked` attribute can be applied to functions and methods

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/asm/naked-functions/invalid-repr-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error: the `repr(C)` attribute cannot be used on functions
|
LL | #[repr(C)]
| ^^^^^^^
...
LL | extern "C" fn example1() {
|
= help: the `repr(C)` attribute can only be applied to data types

Expand All @@ -11,6 +13,8 @@ error: the `repr(transparent)` attribute cannot be used on functions
|
LL | #[repr(transparent)]
| ^^^^^^^^^^^^^^^^^
...
LL | extern "C" fn example2() {
|
= help: the `repr(transparent)` attribute can only be applied to data types

Expand All @@ -19,6 +23,8 @@ error: the `repr(C)` attribute cannot be used on functions
|
LL | #[repr(C)]
| ^^^^^^^
...
LL | extern "C" fn example3() {
|
= help: the `repr(C)` attribute can only be applied to data types

Expand All @@ -27,6 +33,8 @@ error: the `repr(C)` attribute cannot be used on functions
|
LL | #[repr(C, packed)]
| ^^^^^^^^^^^^^^^
...
LL | extern "C" fn example4() {
|
= help: the `repr(C)` attribute can only be applied to data types

Expand All @@ -35,6 +43,8 @@ error: the `repr(packed)` attribute cannot be used on functions
|
LL | #[repr(C, packed)]
| ^^^^^^^^^^^^^^^
...
LL | extern "C" fn example4() {
|
= help: the `repr(packed)` attribute can only be applied to data types

Expand All @@ -43,6 +53,8 @@ error: the `repr(u8)` attribute cannot be used on functions
|
LL | #[repr(u8)]
| ^^^^^^^^
...
LL | extern "C" fn example5() {
|
= help: the `repr(u8)` attribute can only be applied to enums

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/attributes/align-on-fields-143987.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ error: the `rustc_align` attribute cannot be used on struct fields
|
LL | #[rustc_align(8)]
| ^^^^^^^^^^^
LL | x: usize,
|
= help: the `rustc_align` attribute can only be applied to functions

Expand All @@ -23,6 +24,7 @@ error: the `rustc_align` attribute cannot be used on struct fields
|
LL | #[rustc_align(8)]
| ^^^^^^^^^^^
LL | x: usize8,
|
= help: the `rustc_align` attribute can only be applied to functions

Expand All @@ -31,6 +33,7 @@ error: the `rustc_align` attribute cannot be used on struct fields
|
LL | #[rustc_align(32)]
| ^^^^^^^^^^^
LL | u32
|
= help: the `rustc_align` attribute can only be applied to functions

Expand Down
Loading
Loading