rust-lang / rust-lang/rust

candidate selection for normalization and trait goals disagree

Open
#133,044 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-type-system P-low T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

#![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

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 compiling the Rust example in the issue and compare the successful foo call with the failing bar call. Investigate candidate selection for the trait goal and associated-type normalization; done means both paths handle the trivial builtin impl consistently, with a regression test covering the discrepancy.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.