Skip to content

impl trait: code that doesn't constrain a RPIT infers ()?  #96460

Closed
@Dirbaio

Description

@Dirbaio
use core::marker::PhantomData;

fn weird() -> PhantomData<impl Sized> {
    PhantomData
}

The above code [playground] fails on stable with the following error, but compiles on nightlies after #94081 . It seems to me it shouldn't, the type is not constrained at all.

error[[E0720]](https://doc.rust-lang.org/nightly/error-index.html#E0720): cannot resolve opaque type
 [--> src/lib.rs:3:27
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
3 | fn weird() -> PhantomData<impl Sized> {
  |                           ^^^^^^^^^^ recursive opaque type
4 |     PhantomData
  |     ----------- returning here with type `PhantomData<impl Sized>`

It seems to be inferring the opaque type to be (). For example, if you change it to impl Future:

error[E0277]: `()` is not a future
 --> src/lib.rs:4:27
  |
4 | fn weird() -> PhantomData<impl Future> {
  |                           ^^^^^^^^^^^ `()` is not a future

Metadata

Metadata

Assignees

Labels

A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-inferenceArea: Type inferenceP-highHigh priority

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions