Skip to content

candidate selection for normalization and trait goals disagree #133044

Open
@lcnr

Description

@lcnr
Contributor
#![feature(discriminant_kind)]
use std::marker::DiscriminantKind;

fn trait_bound<T: DiscriminantKind>() {}
fn normalize<T: DiscriminantKind<Discriminant = u8>>() {}

fn foo<'a, 'b>()
where
    &'b (): DiscriminantKind<Discriminant = u8>,    
{
    trait_bound::<&'a ()>();
}

fn bar<'a, 'b>()
where
    &'b (): DiscriminantKind<Discriminant = u8>,    
{
    normalize::<&'a ()>();
}

foo compiles, bar does not:

error: lifetime may not live long enough
  --> src/lib.rs:18:5
   |
14 | fn bar<'a, 'b>()
   |        --  -- lifetime `'b` defined here
   |        |
   |        lifetime `'a` defined here
...
18 |     normalize::<&'a ()>();
   |     ^^^^^^^^^^^^^^^^^^^ requires that `'b` must outlive `'a`
   |
   = help: consider adding the following bound: `'b: 'a`

error: lifetime may not live long enough
  --> src/lib.rs:18:5
   |
14 | fn bar<'a, 'b>()
   |        --  -- lifetime `'b` defined here
   |        |
   |        lifetime `'a` defined here
...
18 |     normalize::<&'a ()>();
   |     ^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'b`
   |
   = help: consider adding the following bound: `'a: 'b`

help: `'b` and `'a` must be the same: replace one with the other

Candidate selection for the trait goal prefers the trivial builtin impl. Normalization instead prefers the where-bound. This is inconsistent and means that whether we use the associated items impacts whether a trait bound holds.

It impacts all trivial builtin traits with associated types, I don't think this effects stable rn as either the trait is unstable or the builtin impls only exist for unnameable types. Nominating for t-types vibeck

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 14, 2024
added
I-types-nominatedNominated for discussion during a types team meeting.
T-typesRelevant to the types team, which will review and decide on the PR/issue.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 14, 2024
lcnr

lcnr commented on Nov 14, 2024

@lcnr
ContributorAuthor

I realize this also affects our preference of impls over global where-bounds:

trait Trait {
    type Assoc;
}

impl Trait for &u32 {
    type Assoc = u32;
}

fn trait_bound<T: Trait>() {}
fn normalize<T: Trait<Assoc = u32>>() {}

fn foo<'a>()
where
    &'static u32: Trait<Assoc = u32>,
{
    trait_bound::<&'a u32>(); // ok
    normalize::<&'a u32>(); // error
}
changed the title [-]normalization does not prefer trivial builtin impls over where-bounds[/-] [+]candidate selection for normalization and trait goals disagree[/+] on Nov 14, 2024
added
I-types-nominatedNominated for discussion during a types team meeting.
P-lowLow priority
and removed
I-types-nominatedNominated for discussion during a types team meeting.
on Nov 14, 2024
lcnr

lcnr commented on Nov 26, 2024

@lcnr
ContributorAuthor

we also have the same issue with alias-bound/where-bound mismatch

trait Bound<'a> {
    type Assoc;
}
trait Trait {
    type Assoc: Bound<'static, Assoc = u32>;
}

fn heck<'a, T: Trait<Assoc: Bound<'a>>>(x: <T::Assoc as Bound<'a>>::Assoc) {
    drop(x);
}
lcnr

lcnr commented on Nov 26, 2024

@lcnr
Author
removed
I-types-nominatedNominated for discussion during a types team meeting.
on Mar 10, 2025
added a commit that references this issue on Mar 13, 2025

Auto merge of rust-lang#138176 - compiler-errors:rigid-sized-obl, r=<…

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemP-lowLow priorityT-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

        Participants

        @lcnr@rustbot

        Issue actions

          candidate selection for normalization and trait goals disagree · Issue #133044 · rust-lang/rust