Skip to content

Commit 06a576b

Browse files
committed
the #[track_caller] shim should not inherit #[no_mangle]
1 parent 922958c commit 06a576b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ impl<'tcx> TyCtxt<'tcx> {
2424
}
2525
}
2626

27+
// A shim created by `#[track_caller]` should not inherit any attributes
28+
// that modify the symbol name. Failing to remove these attributes from
29+
// the shim leads to errors like `symbol `foo` is already defined`.
30+
//
31+
// A `ClosureOnceShim` with the track_caller attribute does not have a symbol,
32+
// and therefore can be skipped here.
33+
if let InstanceKind::ReifyShim(_, _) = instance_kind
34+
&& attrs.flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
35+
{
36+
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
37+
attrs.to_mut().flags.remove(CodegenFnAttrFlags::NO_MANGLE);
38+
}
39+
40+
if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
41+
attrs.to_mut().flags.remove(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
42+
}
43+
44+
if attrs.symbol_name.is_some() {
45+
attrs.to_mut().symbol_name = None;
46+
}
47+
}
48+
2749
attrs
2850
}
2951
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ run-pass
2+
#![feature(rustc_attrs)]
3+
4+
// The shim that is generated for a function annotated with `#[track_caller]` should not inherit
5+
// attributes that modify its symbol name. Failing to remove these attributes from the shim
6+
// leads to errors like `symbol `foo` is already defined`.
7+
//
8+
// See also https://github.com/rust-lang/rust/issues/143162.
9+
10+
#[unsafe(no_mangle)]
11+
#[track_caller]
12+
pub fn foo() {}
13+
14+
#[unsafe(export_name = "bar")]
15+
#[track_caller]
16+
pub fn bar() {}
17+
18+
#[rustc_std_internal_symbol]
19+
#[track_caller]
20+
pub fn baz() {}
21+
22+
fn main() {
23+
let _a = foo as fn();
24+
let _b = bar as fn();
25+
let _c = baz as fn();
26+
}

0 commit comments

Comments
 (0)