Rollup merge of #154147 - mati865:raw-dylib-extern-types, r=petrochenkov

Do not attempt generating DllImport for extern types

Fixes https://github.com/rust-lang/rust/issues/154111
This commit is contained in:
Jonathan Brouwer
2026-04-07 17:26:22 +02:00
committed by GitHub
5 changed files with 61 additions and 3 deletions
+5 -3
View File
@@ -224,7 +224,7 @@ fn process_module(&mut self, module: &ForeignModule) {
let dll_imports = match attr.kind {
NativeLibKind::RawDylib { .. } => foreign_items
.iter()
.map(|&child_item| {
.filter_map(|&child_item| {
self.build_dll_import(
abi,
attr.import_name_type.map(|(import_name_type, _)| import_name_type),
@@ -388,7 +388,7 @@ fn build_dll_import(
abi: ExternAbi,
import_name_type: Option<PeImportNameType>,
item: DefId,
) -> DllImport {
) -> Option<DllImport> {
let span = self.tcx.def_span(item);
// This `extern` block should have been checked for general ABI support before, but let's
@@ -465,6 +465,8 @@ fn build_dll_import(
} else {
DllImportSymbolType::Static
}
} else if def_kind == DefKind::ForeignTy {
return None;
} else {
bug!("Unexpected type for raw-dylib: {}", def_kind.descr(item));
};
@@ -482,6 +484,6 @@ fn build_dll_import(
}
};
DllImport { name, import_name_type, calling_convention, span, symbol_type, size }
Some(DllImport { name, import_name_type, calling_convention, span, symbol_type, size })
}
}
@@ -0,0 +1,21 @@
#![feature(extern_types)]
#![feature(raw_dylib_elf)]
use std::ffi::c_char;
#[link(name = "extern", kind = "raw-dylib")]
unsafe extern "C" {
type FOO;
fn create_foo() -> *const FOO;
fn get_foo(foo: *const FOO) -> c_char;
fn set_foo(foo: *const FOO, value: c_char);
}
pub fn main() {
let value = unsafe {
let foo = create_foo();
set_foo(foo, 42);
get_foo(foo)
};
println!("{}", value);
}
@@ -0,0 +1,16 @@
typedef struct FOO {
char val;
} FOO;
FOO* create_foo() {
static FOO foo;
return &foo;
}
void set_foo(FOO* foo, char val) {
foo->val = val;
}
char get_foo(FOO* foo) {
return foo->val;
}
@@ -0,0 +1 @@
42
@@ -0,0 +1,18 @@
//@ only-elf
//@ ignore-cross-compile: Runs a binary.
//@ needs-dynamic-linking
// FIXME(raw_dylib_elf): Debug the failures on other targets.
//@ only-gnu
//@ only-x86_64
//@ ignore-rustc-debug-assertions
use run_make_support::{build_native_dynamic_lib, diff, run, rustc};
fn main() {
rustc().crate_type("bin").crate_name("raw_dylib_test").input("bin.rs").run();
build_native_dynamic_lib("extern");
let out_raw = run("raw_dylib_test").stdout_utf8();
diff().expected_file("output.txt").actual_text("actual", out_raw).normalize(r#"\r"#, "").run();
}