Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidzeng0 opened this issue Mar 25, 2025 · 1 comment · Fixed by #138980
Closed
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) A-monomorphization Area: Monomorphization C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@davidzeng0
Copy link
Contributor

davidzeng0 commented Mar 25, 2025

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.

@davidzeng0 davidzeng0 added the C-bug Category: This is a bug. label Mar 25, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 25, 2025
@tmiasko tmiasko added A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-monomorphization Area: Monomorphization and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 25, 2025
@tmiasko
Copy link
Contributor

tmiasko commented Mar 25, 2025

It appears that mono item collector omits references from var debug info.

@tmiasko tmiasko self-assigned this Mar 26, 2025
@tmiasko tmiasko removed their assignment Mar 26, 2025
TaKO8Ki added a commit to TaKO8Ki/rust that referenced this issue Mar 26, 2025
…compiler-errors

Collect items referenced from var_debug_info

The collection is limited to full debuginfo builds to match behavior of FunctionCx::compute_per_local_var_debug_info.

Fixes rust-lang#138942.
jhpratt added a commit to jhpratt/rust that referenced this issue Mar 27, 2025
…compiler-errors

Collect items referenced from var_debug_info

The collection is limited to full debuginfo builds to match behavior of FunctionCx::compute_per_local_var_debug_info.

Fixes rust-lang#138942.
jhpratt added a commit to jhpratt/rust that referenced this issue Mar 27, 2025
…compiler-errors

Collect items referenced from var_debug_info

The collection is limited to full debuginfo builds to match behavior of FunctionCx::compute_per_local_var_debug_info.

Fixes rust-lang#138942.
@bors bors closed this as completed in c33df27 Mar 27, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 27, 2025
Rollup merge of rust-lang#138980 - tmiasko:collect-var-debug-info, r=compiler-errors

Collect items referenced from var_debug_info

The collection is limited to full debuginfo builds to match behavior of FunctionCx::compute_per_local_var_debug_info.

Fixes rust-lang#138942.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) A-monomorphization Area: Monomorphization C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants