Skip to content

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

Closed
@Boscop

Description

@Boscop

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).

Metadata

Metadata

Labels

A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.F-associated_type_defaults`#![feature(associated_type_defaults)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions