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

Trait bounds are not respected when type aliases are used in struct expressions #36286

Closed
petrochenkov opened this issue Sep 5, 2016 · 0 comments · Fixed by #37035
Closed
Labels
A-type-system Area: Type system

Comments

@petrochenkov
Copy link
Contributor

petrochenkov commented Sep 5, 2016

The following code compiles on stable/beta/nightly:

#![allow(unused)]

struct S<T: Clone> { a: T }

struct NoClone;
// Okay, this is https://github.com/rust-lang/rust/issues/21903
type A = S<NoClone>; 

fn main() {
    // But this is something new.
    // I'd expect an error "the trait bound `NoClone: std::clone::Clone` is not satisfied", but nothing is reported.
    let s = A { a: NoClone }; 

    // The error is properly reported when type alias is not used.
    // let s = S { a: NoClone };  // ERROR: the trait bound `NoClone: std::clone::Clone` is not satisfied
}
@Aatch Aatch added the A-type-system Area: Type system label Sep 6, 2016
bors added a commit that referenced this issue Oct 28, 2016
Support `Self` in struct expressions and patterns

Struct expressions and patterns generally support type aliases `Alias { field: 10 }` i.e. they already have to work with `ty::Ty` to do their job. `Self` is a type alias (when it's not a type parameter) => struct expressions and patterns should support `Self`.

Typical example:
```
impl MyStruct {
    fn new() -> Self {
        Self { a: 10, b: "Hello" }
    }
}
```

The first commit does some preparations and cleanups, see the commit message for  details.
This also fixes couple of bugs related to aliases in struct paths (fixes #36286).

EDIT:
Since struct expressions and patterns always work with `ty::Ty` now, associated paths in them are also supported. If associated type `A::B` successfully resolves to a struct (or union) type, then `A::B { /* fields */ }` is a valid expression/pattern. This will become more important when enum variants are treated as [associated items](#26264 (comment)).

r? @eddyb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants