Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic parameters can have defaults #307

Closed
philberty opened this issue Mar 26, 2021 · 5 comments · Fixed by #401
Closed

Generic parameters can have defaults #307

philberty opened this issue Mar 26, 2021 · 5 comments · Fixed by #401
Assignees

Comments

@philberty
Copy link
Member

For example this test case: https://github.com/rust-lang/rust/blob/master/src/test/ui/generics/generic-impl-less-params-with-defaults.rs

@philberty philberty self-assigned this Mar 26, 2021
@bjorn3
Copy link

bjorn3 commented Mar 26, 2021

Defaults are not very well supported in rustc. Tyoe inference doesn't fall back to param defaults I believe.

@philberty
Copy link
Member Author

Do you have an example @bjorn3? That is really interesting to know.

@bjorn3
Copy link

bjorn3 commented Mar 26, 2021

struct Box<T, A: Default = ()>(T, A);

impl<T, A: Default> Box<T, A> {
    fn new(x: T) -> Self {
        Box(x, Default::default())
    }
}

fn main() {
    Box::new(1);
}
error[E0283]: type annotations needed
  --> src/main.rs:10:5
   |
4  |     fn new(x: T) -> Self {
   |     -------------------- required by `Box::<T, A>::new`
...
10 |     Box::new(1);
   |     ^^^^^^^^ cannot infer type for type parameter `A`
   |
   = note: cannot satisfy `_: Default`

error: aborting due to previous error

@philberty
Copy link
Member Author

Thanks!

@Soveu
Copy link

Soveu commented Apr 19, 2021

philberty added a commit that referenced this issue Apr 27, 2021
Type parameters can have defaults these need to be name resolved.

Addresses: #307
bors bot added a commit that referenced this issue Apr 28, 2021
397: Name resolve any specified types in TypeParams r=philberty a=philberty

Type parameters can have defaults these need to be name resolved.

Addresses: #307

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
philberty added a commit that referenced this issue Apr 30, 2021
In the case that the GenericArgs to a type/fn are less than the expected
number of required substitutions we need to look for what is the min
number of required substitutions which might be zero if each TypeParam
has a default.

In the event we have less than expected arguments we can substitute the
defaults if available as the GenericArgumentMappings.

Addresses: #307
philberty added a commit that referenced this issue Apr 30, 2021
In the case that the GenericArgs to a type/fn are less than the expected
number of required substitutions we need to look for what is the min
number of required substitutions which might be zero if each TypeParam
has a default.

In the event we have less than expected arguments we can substitute the
defaults if available as the GenericArgumentMappings.

Addresses: #307
philberty added a commit that referenced this issue Apr 30, 2021
In the case that the GenericArgs to a type/fn are less than the expected
number of required substitutions we need to look for what is the min
number of required substitutions which might be zero if each TypeParam
has a default.

In the event we have less than expected arguments we can substitute the
defaults if available as the GenericArgumentMappings.

Addresses: #307
bors bot added a commit that referenced this issue Apr 30, 2021
401: Add defaults support for generics r=philberty a=philberty

This adds in the basic support for default arguments for generics such as:

```rust
struct Foo<A,B=f32>(A,B);
```

or recursive type params such as:

```rust
struct Foo<A, B = (A, A)>(A, B);
```

Fixes #307 

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
@bors bors bot closed this as completed in feaafdf Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants