Skip to content

Test needed for linker error in associated function with generic_const_exprs #96699

Closed
@Iwancof

Description

@Iwancof

In the situation using the generic_const_exprs feature to specify the number of array elements, a linker error occurred in the Trait implementation.
We minimized it as much as possible, but it could be more simplified.

This is just a guess, but I think it is probably because the function signature is wrong.
The error content claims that there is no "compiler_bug2::Trait1::crash_here", although it should be "<compiler_bug2::Struct1<[i32; 1]> as compiler_bug2::Trait1<[i32; 1]>>::crash_here".

Code

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

const fn min(a: usize, b: usize) -> usize {
    if a < b {
        a
    } else {
        b
    }
}

trait Trait1<Inner1>
where
    Self: Sized,
{
    fn crash_here()
    where
        Inner1: Default,
    {
        Inner1::default();
    }
}

struct Struct1<T>(T);
impl<T> Trait1<T> for Struct1<T> {}

trait Trait2<Inner2>
where
    Self: Sized,
{
    type Assoc: Trait1<Inner2>;

    fn call_crash()
    where
        Inner2: Default,
    {
        // if Inner2 implements Default, we can call crash_here.
        Self::Assoc::crash_here();
    }
}

struct Struct2<const SIZE1: usize, const SIZE2: usize> {}
/*
where
    [(); min(SIZE1, SIZE2)]:,
{
    elem: [i32; min(SIZE1, SIZE2)],
}
*/

impl<const SIZE1: usize, const SIZE2: usize> Trait2<[i32; min(SIZE1, SIZE2)]>
    for Struct2<SIZE1, SIZE2>
{
    type Assoc = Struct1<[i32; min(SIZE1, SIZE2)]>;
    // dose Struct1<[i32; min(SIZE1, SIZE2)]> implement Default?
}

fn main() {
    pattern2();

    print_fully_name(<Struct2<1, 2> as Trait2<[i32; min(1, 2)]>>::Assoc::crash_here);
    // <compiler_bug2::Struct1<[i32; 1]> as compiler_bug2::Trait1<[i32; 1]>>::crash_here
}

fn pattern1() {
    // no crash
    <Struct2<1, 2> as Trait2<[i32; min(1, 2)]>>::Assoc::crash_here();
    <Struct2<1, 2> as Trait2<[i32; min(1, 2)]>>::call_crash();
}

fn pattern2() {
    // crash
    <Struct2<1, 2> as Trait2<[i32; min(1, 2)]>>::call_crash();

    // undefined reference to `compiler_bug2::Trait1::crash_here'
}

fn pattern3() {
    // no crash
    <Struct2<1, 2> as Trait2<[i32; min(1, 2)]>>::Assoc::crash_here();
}

fn print_fully_name<T>(_: T) {
    println!("{}", std::any::type_name::<T>());
}

Meta

rustc --version --verbose:


rustc 1.62.0-nightly (e1b71feb5 2022-05-03)
binary: rustc
commit-hash: e1b71feb592ba64805689e2b15b9fa570182c442
commit-date: 2022-05-03
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.1

Error output


error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcccDCGE/symbols.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.0.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.1.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.10.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.2.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.3.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.4.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.5.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.6.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.7.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.8.rcgu.o" "compiler_bug2.compiler_bug2.0850f77e-cgu.9.rcgu.o" "compiler_bug2.42ye6huydnlg6fs2.rcgu.o" "-Wl,--as-needed" "-L" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-202fc93d8ccaebf2.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-1333333cbe389678.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-93491bde8b3642ba.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-d338f5690e3fda2f.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-2cd7f06709609788.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-05bd833c6cc845b5.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-5543e955d2b2e107.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-a963b8f78c0365f5.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-626bd4749ba5679b.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-1c5c08d77aa4ee1f.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-43c97e136c6f66b3.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-3ad551729ddf5bdf.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-46de9b9399df1cae.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-aa03de290f9594ce.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-020914c5936c5f85.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-89782a6344bc3ddf.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-2a6a2797f7a73818.rlib" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-0e3656b1fda5fd7b.rlib" "-Wl,--end-group" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-16d69221f10b0282.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "compiler_bug2" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: /usr/bin/ld: compiler_bug2.compiler_bug2.0850f77e-cgu.3.rcgu.o: in function `compiler_bug2::Trait2::call_crash':
          compiler_bug2.0850f77e-cgu.3:(.text._ZN13compiler_bug26Trait210call_crash17hf0a368e161c9e687E+0x2): undefined reference to `compiler_bug2::Trait1::crash_here'
          /usr/bin/ld: compiler_bug2: hidden symbol `_ZN13compiler_bug26Trait110crash_here17hea054f4e014057d2E' isn't defined
          /usr/bin/ld: final link failed: bad value
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: aborting due to previous error; 2 warnings emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-generic_const_exprs`#![feature(generic_const_exprs)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-incomplete-featuresThis issue requires the use of incomplete features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions