Skip to content

Commit 987628e

Browse files
Rollup merge of #109347 - cjgillot:issue-109305, r=WaffleLapkin
Skip no_mangle if the item has no name. Fixes #109305
2 parents 789ee5e + 3102722 commit 987628e

File tree

3 files changed

+65
-69
lines changed

3 files changed

+65
-69
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+46-69
Original file line numberDiff line numberDiff line change
@@ -89,44 +89,39 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
8989
};
9090

9191
match name {
92-
sym::cold => {
93-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
94-
}
95-
sym::rustc_allocator => {
96-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR;
97-
}
92+
sym::cold => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
93+
sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
9894
sym::ffi_returns_twice => {
99-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE;
100-
}
101-
sym::ffi_pure => {
102-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE;
103-
}
104-
sym::ffi_const => {
105-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST;
106-
}
107-
sym::rustc_nounwind => {
108-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
109-
}
110-
sym::rustc_reallocator => {
111-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR;
112-
}
113-
sym::rustc_deallocator => {
114-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR;
95+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE
11596
}
97+
sym::ffi_pure => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
98+
sym::ffi_const => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,
99+
sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
100+
sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
101+
sym::rustc_deallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR,
116102
sym::rustc_allocator_zeroed => {
117-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED;
118-
}
119-
sym::naked => {
120-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED;
103+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
121104
}
105+
sym::naked => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
122106
sym::no_mangle => {
123-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
124-
}
125-
sym::no_coverage => {
126-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE;
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+
}
127121
}
122+
sym::no_coverage => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE,
128123
sym::rustc_std_internal_symbol => {
129-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
124+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
130125
}
131126
sym::used => {
132127
let inner = attr.meta_item_list();
@@ -207,11 +202,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
207202
struct_span_err!(tcx.sess, attr.span, E0775, "`#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension")
208203
.emit();
209204
}
210-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY;
211-
}
212-
sym::thread_local => {
213-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
205+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY
214206
}
207+
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
215208
sym::track_caller => {
216209
if !tcx.is_closure(did.to_def_id())
217210
&& let Some(fn_sig) = fn_sig()
@@ -229,7 +222,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
229222
)
230223
.emit();
231224
}
232-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
225+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
233226
}
234227
sym::export_name => {
235228
if let Some(s) = attr.value_str() {
@@ -306,20 +299,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
306299
sym::link_section => {
307300
if let Some(val) = attr.value_str() {
308301
if val.as_str().bytes().any(|b| b == 0) {
309-
let msg = format!(
310-
"illegal null byte in link_section \
311-
value: `{}`",
312-
&val
313-
);
302+
let msg = format!("illegal null byte in link_section value: `{}`", &val);
314303
tcx.sess.span_err(attr.span, &msg);
315304
} else {
316305
codegen_fn_attrs.link_section = Some(val);
317306
}
318307
}
319308
}
320-
sym::link_name => {
321-
codegen_fn_attrs.link_name = attr.value_str();
322-
}
309+
sym::link_name => codegen_fn_attrs.link_name = attr.value_str(),
323310
sym::link_ordinal => {
324311
link_ordinal_span = Some(attr.span);
325312
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
@@ -330,37 +317,27 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
330317
no_sanitize_span = Some(attr.span);
331318
if let Some(list) = attr.meta_item_list() {
332319
for item in list.iter() {
333-
match item.ident().map(|ident| ident.name) {
334-
Some(sym::address) => {
320+
match item.name_or_empty() {
321+
sym::address => {
335322
codegen_fn_attrs.no_sanitize |=
336-
SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS;
337-
}
338-
Some(sym::cfi) => {
339-
codegen_fn_attrs.no_sanitize |= SanitizerSet::CFI;
323+
SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
340324
}
341-
Some(sym::kcfi) => {
342-
codegen_fn_attrs.no_sanitize |= SanitizerSet::KCFI;
325+
sym::cfi => codegen_fn_attrs.no_sanitize |= SanitizerSet::CFI,
326+
sym::kcfi => codegen_fn_attrs.no_sanitize |= SanitizerSet::KCFI,
327+
sym::memory => codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY,
328+
sym::memtag => codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMTAG,
329+
sym::shadow_call_stack => {
330+
codegen_fn_attrs.no_sanitize |= SanitizerSet::SHADOWCALLSTACK
343331
}
344-
Some(sym::memory) => {
345-
codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY;
346-
}
347-
Some(sym::memtag) => {
348-
codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMTAG;
349-
}
350-
Some(sym::shadow_call_stack) => {
351-
codegen_fn_attrs.no_sanitize |= SanitizerSet::SHADOWCALLSTACK;
352-
}
353-
Some(sym::thread) => {
354-
codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD;
355-
}
356-
Some(sym::hwaddress) => {
357-
codegen_fn_attrs.no_sanitize |= SanitizerSet::HWADDRESS;
332+
sym::thread => codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD,
333+
sym::hwaddress => {
334+
codegen_fn_attrs.no_sanitize |= SanitizerSet::HWADDRESS
358335
}
359336
_ => {
360337
tcx.sess
361-
.struct_span_err(item.span(), "invalid argument for `no_sanitize`")
362-
.note("expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`")
363-
.emit();
338+
.struct_span_err(item.span(), "invalid argument for `no_sanitize`")
339+
.note("expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`")
340+
.emit();
364341
}
365342
}
366343
}
+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)