Skip to content
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

Unreachable post-monomorphization error triggered by -C link-dead-code #131793

Open
joshlf opened this issue Oct 16, 2024 · 1 comment
Open

Unreachable post-monomorphization error triggered by -C link-dead-code #131793

joshlf opened this issue Oct 16, 2024 · 1 comment
Labels
I-monomorphization Issue: An error at monomorphization time. link-dead-code Linkage: using -Clink-dead-code needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@joshlf
Copy link
Contributor

joshlf commented Oct 16, 2024

I have the following code:

#![allow(dead_code)]

trait Foo {
    fn baz() {
        const { panic!() }
    }
}

impl Foo for () {}

If I build normally (using cargo build), it succeeds. If I build passing RUSTFLAGS='-C link-dead-code', Rust attempts to monomorphize Foo::baz and fails despite Foo::baz never being used:

error[E0080]: evaluation of `<() as Foo>::baz::{constant#0}` failed
 --> src/lib.rs:5:17
  |
5 |         const { panic!() }
  |                 ^^^^^^^^ the evaluated program panicked at 'explicit panic', src/lib.rs:5:17
  |
  = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
 --> src/lib.rs:5:9
  |
5 |         const { panic!() }
  |         ^^^^^^^^^^^^^^^^^^

note: the above error was encountered while instantiating `fn <() as Foo>::baz`

Post-monomorphization errors (PMEs) are often used to detect error conditions at compile time that cannot be encoded in the type system proper. We do this in zerocopy 0.8 in certain APIs to ban dynamically-sized types whose trailing element type is zero-sized.

cargo fuzz builds using -C link-dead-code, and so it has been causing build failures for our users. In our specific case, it's possible to work around this because (for entirely coincidental reasons) the issue is only triggered by deprecated APIs that we can likely just remove. However, it's only luck that this hasn't caused more serious breakage for us.

I'm not sure what the right thing to do here is. From our perspective, having -C link-dead-code be more permissive would be good, but I understand why that might not be desirable for other users.

Miscellaneous

Cargo version 1.81.0.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 16, 2024
jswrenn added a commit to google/zerocopy that referenced this issue Oct 16, 2024
These are causing PMEs in the presence of `-C link-dead-code`,
which casues them to be monomorphized for `Self = ()`, thus
producing the problematic ZSTy DST `[()]`.

Ref rust-lang/rust#131793
Fixes #1867
jswrenn added a commit to google/zerocopy that referenced this issue Oct 16, 2024
These are causing PMEs in the presence of `-C link-dead-code`,
which casues them to be monomorphized for `Self = ()`, thus
producing the problematic ZSTy DST `[()]`.

Also bumps version to 0.8.6.

Ref rust-lang/rust#131793
Fixes #1867
jswrenn added a commit to google/zerocopy that referenced this issue Oct 16, 2024
These are causing PMEs in the presence of `-C link-dead-code`,
which casues them to be monomorphized for `Self = ()`, thus
producing the problematic ZSTy DST `[()]`.

Also bumps version to 0.8.6.

Ref rust-lang/rust#131793
Fixes #1867
joshlf pushed a commit to google/zerocopy that referenced this issue Oct 16, 2024
These are causing PMEs in the presence of `-C link-dead-code`,
which casues them to be monomorphized for `Self = ()`, thus
producing the problematic ZSTy DST `[()]`.

Also bumps version to 0.8.6.

Ref rust-lang/rust#131793
Fixes #1867
@the8472
Copy link
Member

the8472 commented Oct 17, 2024

#122301 might be of interest here. Basically: by introducing a generic you can delay the instantiation of the const until it's used with some concrete T. This isn't a guaranteed language property yet but it seems like opinions are leaning towards that way.

@lolbinarycat lolbinarycat added link-dead-code Linkage: using -Clink-dead-code I-monomorphization Issue: An error at monomorphization time. labels Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-monomorphization Issue: An error at monomorphization time. link-dead-code Linkage: using -Clink-dead-code needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

4 participants