rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.96k stars 12.69k forks source link

Making a function `pub` can cause it to fail to compile #72336

Closed Diggsey closed 4 years ago

Diggsey commented 4 years ago

I don't know if this is necessarily a bug, but it's certainly surprising:

trait Test {
    type Result;
}

impl Test for u32 {
    type Result = Foo<u32>;
}

struct Foo<T: Test> {
    foo: Option<T::Result>,
    bar: T,
}

fn make<T: Default + Test>() -> Foo<T> {
    Foo {
        foo: None,
        bar: Default::default(),
    }
}

/*pub */fn test() {
    make::<u32>();
}

Playground link

Uncommenting the pub keyword will cause it to fail to compile with the error:

error[E0391]: cycle detected when computing layout of `Foo<u32>`
  |
  = note: ...which requires computing layout of `std::option::Option<Foo<u32>>`...
  = note: ...which again requires computing layout of `Foo<u32>`, completing the cycle
jonas-schievink commented 4 years ago

It also happens when renaming the function to main, so visiblity is not the issue. Instead, mentioning the invalid type in any place where it needs to be code-gen'd will cause the error. I believe monomorphization errors due to conditionally-infinite types are expected though.

RalfJung commented 4 years ago

Yeah, private unused functions (except for main of a bin crate) are not codegen'd, so you won't get any monomorphization-time errors.

jonas-schievink commented 4 years ago

Closing as expected behavior.