rust-lang / rust-lang/rust

trivial bounds check checks `is_global` pre-norm, normalizes in env, then proves trait bound in empty env

Open
#154,145 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-trait-system C-bug I-ICE T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

trait Trait {
    type Assoc;
}

impl<T> Trait for T {
    type Assoc = T;
}

fn foo<T>()
where
    (): Trait<Assoc = T>,
    <() as Trait>::Assoc: Sized,
{}

results in

error[E0277]: the size for values of type `T` cannot be known at compilation time
  --> <source>:12:5
   |
 9 | fn foo<T>()
   |        - this type parameter needs to be `Sized`
...
12 |     <() as Trait>::Assoc: Sized,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: see issue #48214

https://github.com/rust-lang/rust/blob/461e9738a47e313e4457957fa95ff6a19a4b88d4/compiler/rustc_hir_analysis/src/check/wfcheck.rs#L2305-L2328

This is wrong as normalization can introduce generic params even if the alias is concrete. This can also result in ICE:

trait Trait {
    type Assoc;
}

impl<T> Trait for T {
    type Assoc = T;
}

trait IceBaby {}
impl<const N: usize> IceBaby for [u8; N] {}

fn foo<const N: usize>()
where
    (): Trait<Assoc = [u8; N]>,
    <() as Trait>::Assoc: IceBaby,
{}
error: internal compiler error: /rustc-dev/4a4ef493e3a1488c6e321570238084b38948f6db/compiler/rustc_middle/src/ty/sty.rs:374:13: cannot find `N/#0` in param-env: ParamEnv {
    caller_bounds: [],
}

thread 'rustc' (3) panicked at /rustc-dev/4a4ef493e3a1488c6e321570238084b38948f6db/compiler/rustc_middle/src/ty/sty.rs:374:13:

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 at compiler/rustc_hir_analysis/src/check/wfcheck.rs around lines 2305-2328 and reproduce the two examples from the issue. Trace how the bounds check uses is_global before normalization, then verify behavior after normalization introduces generic parameters. Done means the false E0277 is avoided and the const-generic example no longer causes an internal compiler error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.