Skip to content

impl_trait_in_bindings: Use function return type for binding? #60367

Closed
@phil-opp

Description

@phil-opp

I tried to lazily initialize a static with an impl trait value returned by a function (playground):

#![feature(impl_trait_in_bindings)]

static mut TEST: Option<impl core::fmt::Debug> = None;

fn main() {
    unsafe { TEST = Some(foo()) }
}

fn foo() -> impl core::fmt::Debug {
    0u32
}

This errors with:

error[E0282]: type annotations needed
 --> src/main.rs:3:25
  |
3 | static mut TEST: Option<impl core::fmt::Debug> = None;
  |                         ^^^^^^^^^^^^^^^^^^^^^ cannot infer type

error[E0308]: mismatched types
 --> src/main.rs:6:26
  |
6 |     unsafe { TEST = Some(foo()) }
  |                          ^^^^^ expected opaque type, found a different opaque type
  |
  = note: expected type `impl std::fmt::Debug` (opaque type)
             found type `impl std::fmt::Debug` (opaque type)

So it can't infer a type that is assigned later. Is this a fundamental limitation or just an incompleteness of the current implementation?

Also, is there a way to name the return type of an function so that I can do e.g. static mut TEST: Option<foo::RETURN_TYPE>?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions