Skip to content

Commit 2654b69

Browse files
authored
Rollup merge of rust-lang#147467 - JonathanBrouwer:double_warnings, r=JonathanBrouwer
Fix double warnings on `#[no_mangle]` Fixes 2 out of 3 cases in rust-lang#147417 The fix on closures removes the old error and marks closures as an error target. The fix on consts adds `AllowSilent` to to ignore a target, and uses the old error because that one has a nice suggestion. r? `@jdonszelmann`
2 parents 889ab6c + c050bfb commit 2654b69

File tree

10 files changed

+31
-24
lines changed

10 files changed

+31
-24
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 3 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,8 @@ 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
367+
Error(Target::Closure),
365368
]);
366369
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;
367370
}

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

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ codegen_ssa_multiple_main_functions = entry symbol `main` declared multiple time
223223
224224
codegen_ssa_no_field = no field `{$name}`
225225
226-
codegen_ssa_no_mangle_nameless = `#[no_mangle]` cannot be used on {$definition} as it has no name
227-
228226
codegen_ssa_no_module_named =
229227
no module named `{$user_path}` (mangled: {$cgu_name}). available modules: {$cgu_names}
230228

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_span::{Ident, Span, sym};
1919
use rustc_target::spec::SanitizerSet;
2020

2121
use crate::errors;
22-
use crate::errors::NoMangleNameless;
2322
use crate::target_features::{
2423
check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
2524
};
@@ -182,14 +181,10 @@ fn process_builtin_attrs(
182181
if tcx.opt_item_name(did.to_def_id()).is_some() {
183182
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
184183
} else {
185-
tcx.dcx().emit_err(NoMangleNameless {
186-
span: *attr_span,
187-
definition: format!(
188-
"{} {}",
189-
tcx.def_descr_article(did.to_def_id()),
190-
tcx.def_descr(did.to_def_id())
191-
),
192-
});
184+
tcx.dcx().span_delayed_bug(
185+
*attr_span,
186+
"no_mangle should be on a named function",
187+
);
193188
}
194189
}
195190
AttributeKind::Optimize(optimize, _) => codegen_fn_attrs.optimize = *optimize,

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,14 +1284,6 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_
12841284
}
12851285
}
12861286

1287-
#[derive(Diagnostic)]
1288-
#[diag(codegen_ssa_no_mangle_nameless)]
1289-
pub(crate) struct NoMangleNameless {
1290-
#[primary_span]
1291-
pub span: Span,
1292-
pub definition: String,
1293-
}
1294-
12951287
#[derive(Diagnostic)]
12961288
#[diag(codegen_ssa_feature_not_valid)]
12971289
pub(crate) struct FeatureNotValid<'a> {

tests/ui/attributes/no-mangle-closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ pub struct S([usize; 8]);
77

88
pub fn outer_function(x: S, y: S) -> usize {
99
(#[no_mangle] || y.0[0])()
10-
//~^ ERROR `#[no_mangle]` cannot be used on a closure as it has no name
10+
//~^ ERROR `#[no_mangle]` attribute cannot be used on closures
1111
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: `#[no_mangle]` cannot be used on a closure as it has no name
1+
error: `#[no_mangle]` attribute cannot be used on closures
22
--> $DIR/no-mangle-closure.rs:9:6
33
|
44
LL | (#[no_mangle] || y.0[0])()
55
| ^^^^^^^^^^^^
6+
|
7+
= help: `#[no_mangle]` can be applied to functions, methods, and statics
68

79
error: aborting due to 1 previous error
810

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)