rust-lang / rust-lang/rust

TAIT applies type constraints backwards where variable type is already fully constrained

Open
#119,513 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug F-type_alias_impl_trait T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This code works:

use core::ptr;
fn foo() -> impl Fn() {
    {
        fn bar(_: *const impl Sized) {}
        let p = ptr::null();
        if false {
            // This constrains the type of the pointer p
            return unsafe { ptr::read(p) };
        }
        bar(p);
    }
    || {}
}

However, the equivalent code using TAIT doesn't work.(edit: Yes, I see, this is not equivalent and the issue seems to be more about the incorrect error message and not that it doesn't work) Calling foo with argument p should constrain the impl Trait, but the constraints are applied backwards instead:

#![feature(type_alias_impl_trait)]
use core::ptr;
fn foo() -> impl Fn() {
    {
        type ReturnType = impl Sized;
        fn bar(_: *const ReturnType) {}
        let p = ptr::null();
        if false {
            // This constrains the type of the pointer p
            return unsafe { ptr::read(p) };
        }
        bar(p);
    }
    || {}
}
error[E0277]: expected a `Fn()` closure, found `ReturnType`
 --> src/main.rs:3:13
  |
3 | fn foo() -> impl Fn() {
  |             ^^^^^^^^^ expected an `Fn()` closure, found `ReturnType`
  |
  = help: the trait `Fn<()>` is not implemented for `ReturnType`
  = note: wrap the `ReturnType` in a closure with no arguments: `|| { /* code */ }`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `fooobar` (bin "fooobar") due to 1 previous error

rustc --version --verbose:

rustc 1.77.0-nightly (e51e98dde 2023-12-31)
binary: rustc
commit-hash: e51e98dde6a60637b6a71b8105245b629ac3fe77
commit-date: 2023-12-31
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

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 running the reduced reproducer in src/main.rs with the reported nightly rustc version and compare the TAIT diagnostic with the non-TAIT example. Trace the compiler's TAIT and type-constraint handling to determine why the constraint direction produces the E0277 message; done means the constraint is handled correctly or the diagnostic accurately identifies the issue.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.