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

Omitting return type of trait method to use () doesn't work with associated_type_defaults when default is () #54182

Closed
Boscop opened this issue Sep 13, 2018 · 1 comment · Fixed by #61812
Assignees
Labels
A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. F-associated_type_defaults `#![feature(associated_type_defaults)]` 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

@Boscop
Copy link

Boscop commented Sep 13, 2018

Omitting the return type of a trait method to use () doesn't work with associated_type_defaults when the default is () (type R = ();):

https://play.rust-lang.org/?gist=2b56fc463679d93f1c24a8af3495c776&version=nightly&mode=debug&edition=2015

error[E0053]: method `overload` has an incompatible type for trait
  --> src/main.rs:36:39
   |
30 |         fn overload(self) -> Self::R;
   |                              ------- type in trait
...
36 |         fn overload(self) /*-> Self::R*/ {
   |                                          ^ expected associated type, found ()
   |
   = note: expected type `fn((i32, bool)) -> <(i32, bool) as overload::Overload>::R`
              found type `fn((i32, bool))`

error[E0053]: method `overload` has an incompatible type for trait
  --> src/main.rs:53:39
   |
30 |         fn overload(self) -> Self::R;
   |                              ------- type in trait
...
53 |         fn overload(self) /*-> Self::R*/ {
   |                                          ^ expected associated type, found ()
   |
   = note: expected type `fn((i32, bool, f32)) -> <(i32, bool, f32) as overload::Overload>::R`
              found type `fn((i32, bool, f32))`

Even when I uncomment both occurrences of /*-> Self::R*/, it doesn't work:

error[E0308]: mismatched types
  --> src/main.rs:36:35
   |
36 |           fn overload(self) -> Self::R {
   |  ______________________________________^
37 | |             let (a, b) = self; // destructure args
38 | |             println!("i32 and bool {:?}", (a, b));
39 | |         }
   | |_________^ expected associated type, found ()
   |
   = note: expected type `<(i32, bool) as overload::Overload>::R`
              found type `()`

error[E0308]: mismatched types
  --> src/main.rs:53:35
   |
53 |           fn overload(self) -> Self::R {
   |  ______________________________________^
54 | |             let (a, b, c) = self; // destructure args
55 | |             println!("i32 and bool and f32 {:?}", (a, b, c));
56 | |         }
   | |_________^ expected associated type, found ()
   |
   = note: expected type `<(i32, bool, f32) as overload::Overload>::R`
              found type `()`

I also have to uncomment both occurrences of // type R = (); to get it to compile.
Which defeats the purpose of using associated_type_defaults here :/

I think this should work as intended though.


And when the default type is not () (e.g. type R = i32;), it should still work to only write fn overload(self) -> Self::R { without overriding type R in this impl (if one wants to use the default R here).

@memoryruins memoryruins added the A-associated-items Area: Associated items (types, constants & functions) label Sep 15, 2018
@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 7, 2019
@jonas-schievink
Copy link
Contributor

jonas-schievink commented Jun 9, 2019

Heh, it's even worse than that:

#![feature(associated_type_defaults)]

trait Tr {
    type Assoc = ();
}

impl Tr for () {}

fn f(thing: <() as Tr>::Assoc) {
    let c: () = thing;
}
error[E0308]: mismatched types
  --> src/lib.rs:11:17
   |
11 |     let c: () = thing;
   |                 ^^^^^ expected (), found associated type
   |
   = note: expected type `()`
              found type `<() as Tr>::Assoc`

This may be fixed as part of #29661, but I'm not sure, so leaving this open.

EDIT: Yes, #61812 fixes this

@jonas-schievink jonas-schievink added F-associated_type_defaults `#![feature(associated_type_defaults)]` requires-nightly This issue requires a nightly compiler in some way. labels Aug 29, 2019
@jonas-schievink jonas-schievink self-assigned this Sep 15, 2019
@bors bors closed this as completed in 3a0d106 Feb 26, 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 (types, constants & functions) C-bug Category: This is a bug. F-associated_type_defaults `#![feature(associated_type_defaults)]` 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