Skip to content

false positive "unconstrained opaque type" error on impl trait in trait associated type #140222

Open
@glorv

Description

@glorv

I tried this code:

#![feature(impl_trait_in_assoc_type)]

use std::future::Future;

trait T {
    type T1;
    type Fut1: Future<Output=Self::T1> + Send + 'static;

    fn foo(&self) -> Self::Fut1;

    type T2;
    type Fut2: Future<Output=Self::T2> + Send + 'static;
    fn foo2(&self) -> Self::Fut2;
}

struct S;

impl T for S {
    type T1 = i32;
    type Fut1 = impl  Future<Output=Self::T1> + Send + 'static;
    fn foo(&self) -> Self::Fut1 {
        async {
            1
        }
    }

    type T2 = Self::T1;
    type Fut2 = Self::Fut1;
    fn foo2(&self) -> Self::Fut2 {
        self.foo()
    }
}

fn main() {
    let _a = S;
}

playground

This code can compile successfully with toolchain nightly-2023-2023-12-28 but fails with recent nightly toolchain.

I expected to see this happen: explanation
The code should compile with no errror.

Instead, this happened: explanation

Compiling playground v0.0.1 (/playground)
error: item does not constrain `<S as T>::Fut1::{opaque#0}`
  --> src/main.rs:29:8
   |
29 |     fn foo2(&self) -> Self::Fut2 {
   |        ^^^^
   |
   = note: consider removing `#[define_opaque]` or adding an empty `#[define_opaque()]`
note: this opaque type is supposed to be constrained
  --> src/main.rs:20:17
   |
20 |     type Fut1 = impl  Future<Output=Self::T1> + Send + 'static;
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Meta

rustc --version --verbose:

rustc 1.88.0-nightly (934880f58 2025-04-09)
binary: rustc
commit-hash: 934880f586f6ac1f952c7090e2a943fcd7775e7b
commit-date: 2025-04-09
host: aarch64-apple-darwin
release: 1.88.0-nightly
LLVM version: 20.1.2
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-impl_trait_in_assoc_type`#![feature(impl_trait_in_assoc_type)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions