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

blank specializing impls don't "lock in" defaults #70442

Closed
nikomatsakis opened this issue Mar 26, 2020 · 3 comments · Fixed by #70535
Closed

blank specializing impls don't "lock in" defaults #70442

nikomatsakis opened this issue Mar 26, 2020 · 3 comments · Fixed by #70535
Assignees
Labels
A-associated-items Area: Associated items such as associated types and consts. A-specialization Area: Trait impl specialization C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Mar 26, 2020

I believe RFC 2532 changed the behavior so that a "blank impl" effectively "locks in" inherited defaults. This means that, in this code, the () impl "locks in" the associated type value of bool:

#![feature(specialization)]

trait Trait {
    type Assoc;
}

impl<T> Trait for T {
    default type Assoc = bool;
}

impl Trait for () {}

fn foo<X: Trait<Assoc=bool>>() {}

fn main() {
    foo::<u8>(); //~ ERROR
    foo::<()>();
}

However, we currently get two errors (playground):

error[E0271]: type mismatch resolving `<u8 as Trait>::Assoc == bool`
  --> src/main.rs:16:5
   |
13 | fn foo<X: Trait<Assoc=bool>>() {}
   |    ---          ---------- required by this bound in `foo`
...
16 |     foo::<u8>();
   |     ^^^^^^^^^ expected `bool`, found associated type
   |
   = note:         expected type `bool`
           found associated type `<u8 as Trait>::Assoc`
   = note: consider constraining the associated type `<u8 as Trait>::Assoc` to `bool`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

error[E0271]: type mismatch resolving `<() as Trait>::Assoc == bool`
  --> src/main.rs:17:5
   |
13 | fn foo<X: Trait<Assoc=bool>>() {}
   |    ---          ---------- required by this bound in `foo`
...
17 |     foo::<()>();
   |     ^^^^^^^^^ expected `bool`, found associated type
   |
   = note:         expected type `bool`
           found associated type `<() as Trait>::Assoc`
   = note: consider constraining the associated type `<() as Trait>::Assoc` to `bool`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
@nikomatsakis nikomatsakis added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-specialization Area: Trait impl specialization F-specialization `#![feature(specialization)]` labels Mar 26, 2020
@nikomatsakis
Copy link
Contributor Author

This was an oversight in #61812 (cc @jonas-schievink -- sorry, I missed this interaction!)

@Centril Centril added F-associated_type_defaults `#![feature(associated_type_defaults)]` A-associated-items Area: Associated items such as associated types and consts. labels Mar 26, 2020
@jonas-schievink jonas-schievink added C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. and removed F-associated_type_defaults `#![feature(associated_type_defaults)]` labels Mar 26, 2020
@jonas-schievink
Copy link
Contributor

Seems like this requires somewhat significant changes to the specialization graph – now we don't just need to know the most specialized impl defining the item, but also whether some applicable sub-impl finalizes that definition.

(this was initially induced by #64564, assoc. type defaults aren't required)

@jonas-schievink
Copy link
Contributor

(this was initially induced by #64564, assoc. type defaults aren't required)

I guess that's not quite true – this is always how specialization behaved, but I meant to change this behavior in #64564

@jonas-schievink jonas-schievink self-assigned this Mar 29, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 1, 2020
…ikomatsakis

Track the finalizing node in the specialization graph

Fixes rust-lang#70419
Fixes rust-lang#70442

r? @eddyb
@bors bors closed this as completed in 0e0d84c Apr 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-specialization Area: Trait impl specialization C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants