Skip to content

Type inference fails in light of generic type aliases with default parameter. #50822

Closed
@glandium

Description

@glandium

Consider the following source:

pub struct Foo<A: Default, B: Default>(A, B);

impl<A: Default, B: Default> Foo<A, B> {
    fn new(a: A) -> Self { Foo(a, B::default()) }
}

pub type Bar<A, B=usize> = Foo<A, B>;

fn main() {
    let bar = Bar::new(42usize);
}

This fails to build with:

error[E0283]: type annotations required: cannot resolve `_: std::default::Default`
  --> src/main.rs:10:15
   |
10 |     let bar = Bar::new(42usize);
   |               ^^^^^^^^
   |
note: required by `<Foo<A, B>>::new`
  --> src/main.rs:4:5
   |
4  |     fn new(a: A) -> Self { Foo(a, B::default()) }
   |     ^^^^^^^^^^^^^^^^^^^^

IOW, it does want Bar::<usize>::new instead of Bar::new.

pub type Bar<A> = Foo<A, usize> also does Bar::new work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-type-systemArea: Type systemC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions