Skip to content
Open
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
3 changes: 3 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::session_diagnostics::{
NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector,
ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
};
use crate::target_checking::Policy::AllowSilent;

pub(crate) struct OptimizeParser;

Expand Down Expand Up @@ -362,6 +363,8 @@ impl<S: Stage> NoArgsAttributeParser<S> for NoMangleParser {
Allow(Target::Static),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::TraitImpl)),
AllowSilent(Target::Const), // Handled in the `InvalidNoMangleItems` pass
Error(Target::Closure),
]);
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;
}
Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_attr_parsing/src/target_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ impl AllowedTargets {
pub(crate) fn is_allowed(&self, target: Target) -> AllowedResult {
match self {
AllowedTargets::AllowList(list) => {
if list.contains(&Policy::Allow(target)) {
if list.contains(&Policy::Allow(target))
|| list.contains(&Policy::AllowSilent(target))
{
AllowedResult::Allowed
} else if list.contains(&Policy::Warn(target)) {
AllowedResult::Warn
Expand All @@ -40,7 +42,9 @@ impl AllowedTargets {
}
}
AllowedTargets::AllowListWarnRest(list) => {
if list.contains(&Policy::Allow(target)) {
if list.contains(&Policy::Allow(target))
|| list.contains(&Policy::AllowSilent(target))
{
AllowedResult::Allowed
} else if list.contains(&Policy::Error(target)) {
AllowedResult::Error
Expand All @@ -61,17 +65,26 @@ impl AllowedTargets {
.iter()
.filter_map(|target| match target {
Policy::Allow(target) => Some(*target),
Policy::AllowSilent(_) => None, // Not listed in possible targets
Policy::Warn(_) => None,
Policy::Error(_) => None,
})
.collect()
}
}

/// This policy determines what diagnostics should be emitted based on the `Target` of the attribute.
#[derive(Debug, Eq, PartialEq)]
pub(crate) enum Policy {
/// A target that is allowed.
Allow(Target),
/// A target that is allowed and not listed in the possible targets.
/// This is useful if the target is checked elsewhere.
AllowSilent(Target),
/// Emits a FCW on this target.
/// This is useful if the target was previously allowed but should not be.
Warn(Target),
/// Emits an error on this target.
Error(Target),
}

Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_codegen_ssa/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ codegen_ssa_multiple_main_functions = entry symbol `main` declared multiple time
codegen_ssa_no_field = no field `{$name}`
codegen_ssa_no_mangle_nameless = `#[no_mangle]` cannot be used on {$definition} as it has no name
codegen_ssa_no_module_named =
no module named `{$user_path}` (mangled: {$cgu_name}). available modules: {$cgu_names}
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use rustc_span::{Ident, Span, sym};
use rustc_target::spec::SanitizerSet;

use crate::errors;
use crate::errors::NoMangleNameless;
use crate::target_features::{
check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
};
Expand Down Expand Up @@ -182,14 +181,10 @@ fn process_builtin_attrs(
if tcx.opt_item_name(did.to_def_id()).is_some() {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
} else {
tcx.dcx().emit_err(NoMangleNameless {
span: *attr_span,
definition: format!(
"{} {}",
tcx.def_descr_article(did.to_def_id()),
tcx.def_descr(did.to_def_id())
),
});
tcx.dcx().span_delayed_bug(
*attr_span,
"no_mangle should be on a named function",
);
}
}
AttributeKind::Optimize(optimize, _) => codegen_fn_attrs.optimize = *optimize,
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,14 +1284,6 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_
}
}

#[derive(Diagnostic)]
#[diag(codegen_ssa_no_mangle_nameless)]
pub(crate) struct NoMangleNameless {
#[primary_span]
pub span: Span,
pub definition: String,
}

#[derive(Diagnostic)]
#[diag(codegen_ssa_feature_not_valid)]
pub(crate) struct FeatureNotValid<'a> {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/attributes/no-mangle-closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub struct S([usize; 8]);

pub fn outer_function(x: S, y: S) -> usize {
(#[no_mangle] || y.0[0])()
//~^ ERROR `#[no_mangle]` cannot be used on a closure as it has no name
//~^ ERROR `#[no_mangle]` attribute cannot be used on closures
}
4 changes: 3 additions & 1 deletion tests/ui/attributes/no-mangle-closure.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: `#[no_mangle]` cannot be used on a closure as it has no name
error: `#[no_mangle]` attribute cannot be used on closures
--> $DIR/no-mangle-closure.rs:9:6
|
LL | (#[no_mangle] || y.0[0])()
| ^^^^^^^^^^^^
|
= help: `#[no_mangle]` can be applied to methods, functions, and statics

error: aborting due to 1 previous error

2 changes: 2 additions & 0 deletions tests/ui/issues/issue-45562.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![deny(unused_attributes)]

#[no_mangle] pub static RAH: usize = 5;
//~^ ERROR const items should never be `#[no_mangle]`

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/issues/issue-45562.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![deny(unused_attributes)]

#[no_mangle] pub const RAH: usize = 5;
//~^ ERROR const items should never be `#[no_mangle]`

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-45562.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: const items should never be `#[no_mangle]`
--> $DIR/issue-45562.rs:3:14
--> $DIR/issue-45562.rs:5:14
|
LL | #[no_mangle] pub const RAH: usize = 5;
| ---------^^^^^^^^^^^^^^^^
Expand Down
Loading