Skip to content

Undefined hidden symbol compiling code with generics and const reference to static #138942

Closed
@davidzeng0

Description

@davidzeng0

I tried this code:

fn test<T>()
where
	T: Trait
{
	let pointer = T::STRUCT;
}

#[repr(C)]
#[derive(Debug)]
struct Struct {
	function: fn()
}

static STRUCT: Struct = Struct { function: test::<()>  };

trait Trait {
	const STRUCT: &'static Struct;
}

impl Trait for () {
	const STRUCT: &'static Struct = &STRUCT;
}

fn main() {
    println!("{:?}", <() as Trait>::STRUCT);
}

I expected to see this happen: prints

Struct { function: 0x<addr> }

Instead, this happened:

error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "-m64" "/tmp/rustcc6uHbt/symbols.o" "<10 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,liballoc-*,librustc_std_workspace_core-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/tmp/rustcc6uHbt/raw-dylibs" "-B<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,-znostart-stop-gc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/crate/root/test2/target/debug/deps/test2-4da9f5e7d055ae9a" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: rust-lld: error: undefined hidden symbol: test2::STRUCT::h113f20394fd71d07
          >>> referenced by main.rs:5 (src/main.rs:5)
          >>>               /crate/root/test2/target/debug/deps/test2-4da9f5e7d055ae9a.973g56r0h88wi65qwm2rwg85k.rcgu.o:(test2::test::h45a33cc559ef8b90)
          collect2: error: ld returned 1 exit status

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (f8c27dfe1 2025-03-24)
binary: rustc
commit-hash: f8c27dfe1a2e7fb538fd91dad53de06992c7c967
commit-date: 2025-03-24
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.1

With help from others, I have confirmed this bug is also reproduced on windows, on both the stable and nightly compilers. The code compiles fine on playground, godbolt, and miri, but not with cargo build.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)A-monomorphizationArea: MonomorphizationC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions