Skip to content

Commit 3102722

Browse files
committed
Skip no_mangle if the item has no name.
1 parent 84487b2 commit 3102722

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,22 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
103103
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
104104
}
105105
sym::naked => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
106-
sym::no_mangle => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE,
106+
sym::no_mangle => {
107+
if tcx.opt_item_name(did.to_def_id()).is_some() {
108+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE
109+
} else {
110+
tcx.sess
111+
.struct_span_err(
112+
attr.span,
113+
format!(
114+
"`#[no_mangle]` cannot be used on {} {} as it has no name",
115+
tcx.def_descr_article(did.to_def_id()),
116+
tcx.def_descr(did.to_def_id()),
117+
),
118+
)
119+
.emit();
120+
}
121+
}
107122
sym::no_coverage => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE,
108123
sym::rustc_std_internal_symbol => {
109124
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Check that we do not ICE when `no_mangle` is applied to something that has no name.
2+
3+
#![crate_type = "lib"]
4+
#![feature(stmt_expr_attributes)]
5+
6+
pub struct S([usize; 8]);
7+
8+
pub fn outer_function(x: S, y: S) -> usize {
9+
(#[no_mangle] || y.0[0])()
10+
//~^ ERROR `#[no_mangle]` cannot be used on a closure as it has no name
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `#[no_mangle]` cannot be used on a closure as it has no name
2+
--> $DIR/no-mangle-closure.rs:9:6
3+
|
4+
LL | (#[no_mangle] || y.0[0])()
5+
| ^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)