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 @@ -54,11 +54,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, LintContext};

Expand Down Expand Up @@ -870,36 +870,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 @@ -910,11 +881,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::diagnostics::BuiltinNoMangleGeneric {
span: cx.tcx.def_span(def_id),
suggestion: attr_span,
});
}
}
}
Expand Down Expand Up @@ -1553,7 +1523,6 @@ pub mod soft {
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/diagnostics.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 @@ -695,6 +695,11 @@ fn register_builtins(store: &mut LintStore) {
"converted into hard error, \
see <https://github.com/rust-lang/rust/issues/78586> for more information",
);
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 @@ -225,20 +225,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
10 changes: 1 addition & 9 deletions src/tools/miri/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,7 @@ pub fn iter_exported_symbols<'tcx>(
if !(used || codegen_attrs.contains_extern_indicator()) {
continue;
}
// FIXME: `#[no_mangle]` makes no sense on a generic item, but still causes it to be
// considered "extern". Remove this once `no_mangle_generic_items` is a hard error.
let mono = {
let generics = tcx.generics_of(def_id);
!generics.requires_monomorphization(tcx)
};
if mono {
f(LOCAL_CRATE, def_id.into(), used)?;
}
f(LOCAL_CRATE, def_id.into(), used)?;
}

// Next, all our dependencies.
Expand Down
12 changes: 4 additions & 8 deletions 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)]
Comment thread
RalfJung marked this conversation as resolved.
#![allow(unused_imports, unused_attributes)]

// Regression test for https://github.com/rust-lang/rust/issues/86261:
// `#[no_mangle]` on a `use` item.
Expand All @@ -14,10 +14,6 @@ pub struct NoMangleStruct;
#[export_name = "NoMangleStruct"]
fn no_mangle_struct() {}

// `#[no_mangle]` on a generic function can also cause ICEs.
#[no_mangle]
fn no_mangle_generic<T>() {}

// Same as `no_mangle_struct()` but for the `no_mangle_generic()` generic function.
#[export_name = "no_mangle_generic"]
fn no_mangle_generic2() {}
// Same as `no_mangle_struct()` but for the `no_mangle_struct()` function.
#[export_name = "no_mangle_struct"]
fn no_mangle_alias() {}
4 changes: 2 additions & 2 deletions src/tools/miri/test-cargo-miri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ mod test {
fn assoc_fn_as_exported_symbol() -> i32;
fn make_true() -> bool;
fn NoMangleStruct();
fn no_mangle_generic();
fn no_mangle_struct();
}
assert_eq!(unsafe { exported_symbol() }, 123456);
assert_eq!(unsafe { assoc_fn_as_exported_symbol() }, -123456);
assert!(unsafe { make_true() });
unsafe { NoMangleStruct() }
unsafe { no_mangle_generic() }
unsafe { no_mangle_struct() }
}
}

Expand Down
22 changes: 0 additions & 22 deletions src/tools/miri/tests/pass/issues/issue-154385-no-mangle-generic.rs

This file was deleted.

This file was deleted.

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 backtrace_with_baz_in_it<F>(mut cb: F, data: u32) where F: FnMut(u32) {
cb(data);
}

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

#[no_mangle]
pub fn backtrace_with_foo_in_it<F>(cb: F, data: u32) where F: FnMut(u32) {
backtrace_with_bar_in_it(cb, data);
}
Expand Down
10 changes: 4 additions & 6 deletions tests/ui/backtrace/line-tables-only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ fn assert_contains(
expected_line: u32,
) {
// The formatted frames look like this:
// `{ fn: "backtrace_with_baz_in_it", file: ".../tests/ui/backtrace/auxiliary/line-tables-only-helper.rs", line: 5 }`
// or this:
// `{ fn: "line_tables_only_helper::backtrace_with_baz_in_it<line_tables_only_helper::capture_backtrace::closure_env$0>", file: "...\tests\ui\backtrace\auxiliary\line-tables-only-helper.rs", line: 5 },`
// `{ fn: "line_tables_only_helper::backtrace_with_baz_in_it::<line_tables_only_helper::capture_backtrace::{closure#0}>", file: ".../tests/ui/backtrace/auxiliary/line-tables-only-helper.rs", line: 4 },
// Make sure we match the right part when searching for the function name and line number.
let expected_line_str = format!("line: {expected_line} ");
eprintln!("{:#?}", backtrace);
Expand Down Expand Up @@ -63,8 +61,8 @@ fn main() {
// And with #143208 we also lost `bar` in the line tables.
#[cfg(not(all(target_pointer_width = "32", target_env = "msvc")))]
{
assert_contains(&backtrace, "backtrace_with_foo_in_it", "line-tables-only-helper.rs", 15);
assert_contains(&backtrace, "backtrace_with_bar_in_it", "line-tables-only-helper.rs", 10);
assert_contains(&backtrace, "backtrace_with_foo_in_it", "line-tables-only-helper.rs", 12);
assert_contains(&backtrace, "backtrace_with_bar_in_it", "line-tables-only-helper.rs", 8);
}
assert_contains(&backtrace, "backtrace_with_baz_in_it", "line-tables-only-helper.rs", 5);
assert_contains(&backtrace, "backtrace_with_baz_in_it", "line-tables-only-helper.rs", 4);
}
Loading
Loading