Skip to content

Commit cdc0e5f

Browse files
Fix double error for #[no_mangle] on consts
1 parent 393162a commit cdc0e5f

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::session_diagnostics::{
66
NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector,
77
ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
88
};
9+
use crate::target_checking::Policy::AllowSilent;
910

1011
pub(crate) struct OptimizeParser;
1112

@@ -362,6 +363,7 @@ impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
362363
Allow(Target::Static),
363364
Allow(Target::Method(MethodKind::Inherent)),
364365
Allow(Target::Method(MethodKind::TraitImpl)),
366+
AllowSilent(Target::Const), // Handled in the `InvalidNoMangleItems` pass
365367
Error(Target::Closure),
366368
]);
367369
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ impl AllowedTargets {
3131
pub(crate) fn is_allowed(&self, target: Target) -> AllowedResult {
3232
match self {
3333
AllowedTargets::AllowList(list) => {
34-
if list.contains(&Policy::Allow(target)) {
34+
if list.contains(&Policy::Allow(target))
35+
|| list.contains(&Policy::AllowSilent(target))
36+
{
3537
AllowedResult::Allowed
3638
} else if list.contains(&Policy::Warn(target)) {
3739
AllowedResult::Warn
@@ -40,7 +42,9 @@ impl AllowedTargets {
4042
}
4143
}
4244
AllowedTargets::AllowListWarnRest(list) => {
43-
if list.contains(&Policy::Allow(target)) {
45+
if list.contains(&Policy::Allow(target))
46+
|| list.contains(&Policy::AllowSilent(target))
47+
{
4448
AllowedResult::Allowed
4549
} else if list.contains(&Policy::Error(target)) {
4650
AllowedResult::Error
@@ -61,17 +65,26 @@ impl AllowedTargets {
6165
.iter()
6266
.filter_map(|target| match target {
6367
Policy::Allow(target) => Some(*target),
68+
Policy::AllowSilent(_) => None, // Not listed in possible targets
6469
Policy::Warn(_) => None,
6570
Policy::Error(_) => None,
6671
})
6772
.collect()
6873
}
6974
}
7075

76+
/// This policy determines what diagnostics should be emitted based on the `Target` of the attribute.
7177
#[derive(Debug, Eq, PartialEq)]
7278
pub(crate) enum Policy {
79+
/// A target that is allowed.
7380
Allow(Target),
81+
/// A target that is allowed and not listed in the possible targets.
82+
/// This is useful if the target is checked elsewhere.
83+
AllowSilent(Target),
84+
/// Emits a FCW on this target.
85+
/// This is useful if the target was previously allowed but should not be.
7486
Warn(Target),
87+
/// Emits an error on this target.
7588
Error(Target),
7689
}
7790

tests/ui/issues/issue-45562.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ run-rustfix
22

3+
#![deny(unused_attributes)]
4+
35
#[no_mangle] pub static RAH: usize = 5;
46
//~^ ERROR const items should never be `#[no_mangle]`
57

tests/ui/issues/issue-45562.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ run-rustfix
22

3+
#![deny(unused_attributes)]
4+
35
#[no_mangle] pub const RAH: usize = 5;
46
//~^ ERROR const items should never be `#[no_mangle]`
57

tests/ui/issues/issue-45562.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: const items should never be `#[no_mangle]`
2-
--> $DIR/issue-45562.rs:3:14
2+
--> $DIR/issue-45562.rs:5:14
33
|
44
LL | #[no_mangle] pub const RAH: usize = 5;
55
| ---------^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)