Skip to content

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

Description

@lcnr
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

if pred.is_global() && !pred.has_type_flags(TypeFlags::HAS_BINDER_VARS) {
let pred = self.normalize(span, None, pred);
// only use the span of the predicate clause (#90869)
let hir_node = tcx.hir_node_by_def_id(self.body_def_id);
if let Some(hir::Generics { predicates, .. }) = hir_node.generics() {
span = predicates
.iter()
// There seems to be no better way to find out which predicate we are in
.find(|pred| pred.span.contains(obligation_span))
.map(|pred| pred.span)
.unwrap_or(obligation_span);
}
let obligation = Obligation::new(
tcx,
traits::ObligationCause::new(
span,
self.body_def_id,
ObligationCauseCode::TrivialBound,
),
empty_env,
pred,
);

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions