Skip to content

RPIT allows defining use with invalid args #111935

Closed
@aliemjay

Description

@aliemjay

Note: All examples in this post was fixed in #112842. However the issue persists for lifetime arguments. See this comment for more.


The following compiles since v1.62:

fn foo<T>() -> impl Sized {
    let _: () = foo::<u8>();
}

While the equivalent TAIT version doesn't compile because Opaque<u8> == u8 is not a valid defining use for Opaque (because it is ambiguous whether Opaque<T> = T or Opaque<T> = u8):

#![feature(type_alias_impl_trait)]

type Opaque<T> = impl Sized;
fn foo<T>() -> Opaque<T> {
    let _: () = foo::<u8>();
    //~^ ERROR expected generic type parameter, found `u8`
}

I believe that TAIT behavior is the correct one because it's maximally compatible with future changes. Keeping this behavior for RPIT can also result in some surprising errors:

fn foo<T>(val: T) -> impl Sized {
    let _: u8 = foo(0u8);
    val
    //~^ ERROR concrete type differs from previous defining opaque type use
}

This was stabilized (most likely unintentionally) in #94081. Cc @oli-obk.

See related code:

fn check_opaque_type_parameter_valid(

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.P-highHigh priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions