rust-lang / rust-lang/rust

Strange type inference for recursive RPIT

Open
#139,406 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-impl-trait C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![allow(unconditional_recursion)]

fn what1<T>(x: T) -> impl Sized {
    what1(x)
}

fn what2<T>(x: T) -> impl Sized {
    what2(what2(x))
}

fn print_return_type<T, U>(_: impl Fn(T) -> U) {
    println!("{}", std::any::type_name::<U>())
}

fn main() {
    print_return_type(what1::<i32>);
    print_return_type(what2::<i32>);
}

I compiled the above code in edition 2024, and got the output:

()
i32

Also, the following code gives a compile error:

#![allow(unconditional_recursion)]

fn what3<T>(x: T) -> impl Sized {
    what3(what3(what3(x)))
}
error[E0792]: expected generic type parameter, found `impl Sized`
 --> src/lib.rs:4:5
  |
3 | fn what3<T>(x: T) -> impl Sized {
  |          - this generic parameter must be used with a generic type parameter
4 |     what3(what3(what3(x)))
  |     ^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0792`.

(See also #139350)

I think that both what1 and what2 should not compile, but I'm not sure. And if it does compiles, they should both return !. However, they return () and i32, respectively. (This happens despite the edition 2024 never type fallback change.) The current behavior doesn't make any sense to me.

Discovered while experimenting with #139402.

@rustbot labels +A-impl-trait

Meta

Reproducible on the playground with nightly rust 1.88.0-nightly (2025-04-04 17ffbc81a30c09419383)

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 with the src/lib.rs reproducer on the referenced nightly compiler and compare the inferred return types of what1, what2, and what3, including the E0792 diagnostic. Read the discussion in #139350 and the related context from #139402, then determine the intended recursive RPIT and never-type-fallback behavior. Done means the behavior is resolved or clearly specified with regression coverage.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.