rust-lang / rust-lang/rust

trait implementation conflicts depending on source crate with generic_const_exprs

Open
#118,343 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug F-generic_const_exprs P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

crate foo

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

pub trait KnownSize: Sized {
    const N: usize = core::mem::size_of::<Self>();
}
impl<T: Sized> KnownSize for T {}

pub trait SizeIs<const N: usize> {}
impl<T: KnownSize> SizeIs<{ T::N }> for T {}

struct X(u32);

// Uncommenting either of these lines breaks due to conflict with blanket impls above
// impl KnownSize for X {}
// impl SizeIs<4> for X {}

crate bar

use foo::*;

struct X(u32);

// Uncommenting this line breaks due to conflict with blanket impl in `foo`
// impl KnownSize for X {}

// However this line works! (and is required if I want to use the `X: SizeIs<4>` impl)
impl SizeIs<4> for X {}

I expected to see this happen:
impl SizeIs<4> for X {} should work in both foo and bar, or in neither.
impl KnownSize for X {} and impl SizeIs<4> for X {} should either both work or neither work (in bar).

Meta

rustc --version --verbose:

rustc 1.76.0-nightly (6b771f6b5 2023-11-15)
binary: rustc
commit-hash: 6b771f6b5a6c8b03b6322a9c77ac77cb346148f0
commit-date: 2023-11-15
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two-crate examples with the recorded rustc 1.76.0-nightly version, comparing the coherence errors for the KnownSize and SizeIs implementations in crates foo and bar. Trace the compiler's trait implementation conflict handling for generic_const_exprs; done means the source-crate-dependent behavior is explained and the expected consistency is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.