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
51 changes: 10 additions & 41 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ use crate::lints::{
BuiltinExplicitOutlives, BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote,
BuiltinIncompleteFeatures, BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures,
BuiltinKeywordIdents, BuiltinMissingCopyImpl, BuiltinMissingDebugImpl, BuiltinMissingDoc,
BuiltinMutablesTransmutes, BuiltinNoMangleGeneric, BuiltinNonShorthandFieldPatterns,
BuiltinSpecialModuleNameUsed, BuiltinTrivialBounds, BuiltinTypeAliasBounds,
BuiltinUngatedAsyncFnTrackCaller, BuiltinUnpermittedTypeInit, BuiltinUnpermittedTypeInitSub,
BuiltinUnreachablePub, BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment,
BuiltinUnusedDocCommentSub, BuiltinWhileTrue, EqInternalMethodImplemented, InvalidAsmLabel,
BuiltinMutablesTransmutes, BuiltinNonShorthandFieldPatterns, BuiltinSpecialModuleNameUsed,
BuiltinTrivialBounds, BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller,
BuiltinUnpermittedTypeInit, BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub,
BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub,
BuiltinWhileTrue, EqInternalMethodImplemented, InvalidAsmLabel,
};
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext};
declare_lint! {
Expand Down Expand Up @@ -924,36 +924,7 @@ declare_lint! {
"const items will not have their symbols exported"
}

declare_lint! {
/// The `no_mangle_generic_items` lint detects generic items that must be
/// mangled.
///
/// ### Example
///
/// ```rust
/// #[unsafe(no_mangle)]
/// fn foo<T>(t: T) {}
///
/// #[unsafe(export_name = "bar")]
/// fn bar<T>(t: T) {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// A function with generics must have its symbol mangled to accommodate
/// the generic parameter. The [`no_mangle`] and [`export_name`] attributes
/// have no effect in this situation, and should be removed.
///
/// [`no_mangle`]: https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute
/// [`export_name`]: https://doc.rust-lang.org/reference/abi.html#the-export_name-attribute
NO_MANGLE_GENERIC_ITEMS,
Warn,
"generic items must be mangled"
}

declare_lint_pass!(InvalidNoMangleItems => [NO_MANGLE_CONST_ITEMS, NO_MANGLE_GENERIC_ITEMS]);
declare_lint_pass!(InvalidNoMangleItems => [NO_MANGLE_CONST_ITEMS]);

impl InvalidNoMangleItems {
fn check_no_mangle_on_generic_fn(
Expand All @@ -964,11 +935,10 @@ impl InvalidNoMangleItems {
) {
let generics = cx.tcx.generics_of(def_id);
if generics.requires_monomorphization(cx.tcx) {
cx.emit_span_lint(
NO_MANGLE_GENERIC_ITEMS,
cx.tcx.def_span(def_id),
BuiltinNoMangleGeneric { suggestion: attr_span },
);
cx.tcx.dcx().emit_err(crate::errors::BuiltinNoMangleGeneric {
span: cx.tcx.def_span(def_id),
suggestion: attr_span,
});
}
}
}
Expand Down Expand Up @@ -1607,7 +1577,6 @@ declare_lint_pass!(
ANONYMOUS_PARAMETERS,
UNUSED_DOC_COMMENTS,
NO_MANGLE_CONST_ITEMS,
NO_MANGLE_GENERIC_ITEMS,
MUTABLE_TRANSMUTES,
UNSTABLE_FEATURES,
UNREACHABLE_PUB,
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_lint/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ pub(crate) struct UnknownToolInScopedLint {
pub is_nightly_build: bool,
}

#[derive(Diagnostic)]
#[diag("functions generic over types or consts must be mangled")]
pub(crate) struct BuiltinNoMangleGeneric {
#[primary_span]
pub span: Span,
// Use of `#[no_mangle]` suggests FFI intent; correct
// fix may be to monomorphize source by hand
#[suggestion(
"remove this attribute",
style = "short",
code = "",
applicability = "maybe-incorrect"
)]
pub suggestion: Span,
}

#[derive(Diagnostic)]
#[diag("`...` range patterns are deprecated", code = E0783)]
pub(crate) struct BuiltinEllipsisInclusiveRangePatterns {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@ fn register_builtins(store: &mut LintStore) {
);
store.register_removed("wasm_c_abi", "the wasm C ABI has been fixed");
store.register_removed("soft_unstable", "the general soft-unstable mechanism has been removed");
store.register_removed(
"no_mangle_generic_items",
"converted into hard error, \
generic items must always be mangled",
);
}

fn register_internals(store: &mut LintStore) {
Expand Down
14 changes: 0 additions & 14 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,6 @@ pub(crate) enum BuiltinUnusedDocCommentSub {
BlockHelp,
}

#[derive(Diagnostic)]
#[diag("functions generic over types or consts must be mangled")]
pub(crate) struct BuiltinNoMangleGeneric {
// Use of `#[no_mangle]` suggests FFI intent; correct
// fix may be to monomorphize source by hand
#[suggestion(
"remove this attribute",
style = "short",
code = "",
applicability = "maybe-incorrect"
)]
pub suggestion: Span,
}

#[derive(Diagnostic)]
#[diag("const items should never be `#[no_mangle]`")]
pub(crate) struct BuiltinConstNoMangle {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/test-cargo-miri/issue-rust-86261/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(unused_imports, unused_attributes, no_mangle_generic_items)]
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

This looks like we should keep allowing the lint here until this is a hard error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, the PR title is outdated. It is a hard error now, I see. Please update the PR title and description.

#![allow(unused_imports, unused_attributes)]

// Regression test for https://github.com/rust-lang/rust/issues/86261:
// `#[no_mangle]` on a `use` item.
Expand Down
3 changes: 1 addition & 2 deletions tests/codegen-llvm/avr/avr-func-addrspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn arbitrary_black_box(ptr: &usize, _: &mut u32) -> Result<(), ()> {
}

#[inline(never)]
#[no_mangle]
fn call_through_fn_trait(a: &mut impl Fn<(), Output = ()>) {
(*a)()
}
Expand All @@ -49,7 +48,7 @@ pub extern "C" fn test() {

// A call through the Fn trait must use address space 1.
//
// CHECK: call{{.+}}addrspace(1) void @call_through_fn_trait({{.*}})
// CHECK: call{{.+}}addrspace(1) void @{{.*call_through_fn_trait.*}}({{.*}})
call_through_fn_trait(&mut update_bar_value);

// A call through a global variable must use address space 1.
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/backtrace/auxiliary/line-tables-only-helper.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
//@ compile-flags: -Cstrip=none -Cdebuginfo=line-tables-only

#[no_mangle]
pub fn baz<F>(mut cb: F, data: u32) where F: FnMut(u32) {
cb(data);
}

#[no_mangle]
pub fn bar<F>(cb: F, data: u32) where F: FnMut(u32) {
baz(cb, data);
}

#[no_mangle]
pub fn foo<F>(cb: F, data: u32) where F: FnMut(u32) {
bar(cb, data);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/backtrace/line-tables-only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ fn main() {
// line tables?
#[cfg(not(all(target_pointer_width = "32", target_env = "msvc")))]
{
assert_contains(&backtrace, "foo", "line-tables-only-helper.rs", 5);
assert_contains(&backtrace, "foo", "line-tables-only-helper.rs", 6);
}
assert_contains(&backtrace, "bar", "line-tables-only-helper.rs", 10);
assert_contains(&backtrace, "baz", "line-tables-only-helper.rs", 5);
assert_contains(&backtrace, "bar", "line-tables-only-helper.rs", 11);
assert_contains(&backtrace, "baz", "line-tables-only-helper.rs", 6);
}
157 changes: 0 additions & 157 deletions tests/ui/generics/export-name-on-generics.fixed

This file was deleted.

2 changes: 0 additions & 2 deletions tests/ui/generics/export-name-on-generics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@ run-rustfix
#![allow(dead_code, mismatched_lifetime_syntaxes)]
#![deny(no_mangle_generic_items)]

#[export_name = "foo"]
pub fn foo<T>() {} //~ ERROR functions generic over types or consts must be mangled
Expand Down
Loading
Loading