Skip to content

Commit da2ed6f

Browse files
author
hyd-dev
committed
Don't report UB for #[no_mangle] on associated functions
1 parent 9946734 commit da2ed6f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
177177
second_crate: tcx.crate_name(cnum),
178178
});
179179
}
180-
if tcx.def_kind(def_id) != DefKind::Fn {
180+
if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
181181
throw_ub_format!(
182182
"attempt to call an exported symbol that is not defined as a function"
183183
);

test-cargo-miri/exported-symbol-dep/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@
22
fn exported_symbol() -> i32 {
33
123456
44
}
5+
6+
pub struct AssocFn;
7+
8+
impl AssocFn {
9+
#[no_mangle]
10+
pub fn assoc_fn_as_exported_symbol() -> i32 {
11+
-123456
12+
}
13+
}

test-cargo-miri/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,22 @@ mod test {
6262
fn exported_symbol() {
6363
extern crate cargo_miri_test;
6464
extern crate exported_symbol;
65+
extern crate issue_rust_86261;
6566
// Test calling exported symbols in (transitive) dependencies.
6667
// Repeat calls to make sure the `Instance` cache is not broken.
6768
for _ in 0..3 {
6869
extern "Rust" {
6970
fn exported_symbol() -> i32;
71+
fn assoc_fn_as_exported_symbol() -> i32;
7072
fn make_true() -> bool;
73+
fn NoMangleStruct();
74+
fn no_mangle_generic();
7175
}
7276
assert_eq!(unsafe { exported_symbol() }, 123456);
77+
assert_eq!(unsafe { assoc_fn_as_exported_symbol() }, -123456);
7378
assert!(unsafe { make_true() });
79+
unsafe { NoMangleStruct() }
80+
unsafe { no_mangle_generic() }
7481
}
7582
}
7683
}

0 commit comments

Comments
 (0)