Skip to content

Binding associated type requires redundancy. #40093

New issue

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

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

Already on GitHub? # to your account

Closed
frankmcsherry opened this issue Feb 25, 2017 · 1 comment · Fixed by #81485
Closed

Binding associated type requires redundancy. #40093

frankmcsherry opened this issue Feb 25, 2017 · 1 comment · Fixed by #81485
Labels
A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@frankmcsherry
Copy link
Contributor

frankmcsherry commented Feb 25, 2017

I've been told this may be intended behavior, in which case treat this as a vote for "I think it shouldn't be". :)

If you have a trait with a constraint on an associated type,

pub trait Test {
    type Item;
    type Bundle: From<Self::Item>;
}

You can use the trait without constraints, like so

fn works<T>()
    where T: Test,
{ }

but if you instantiate any associated types it breaks.

fn fails<T>() 
    where T: Test<Item = String>
{ } 
// error[E0277]: the trait bound `<T as Test>::Bundle: std::convert::From<std::string::String>` is not satisfied
// ..
//   = help: consider adding a `where <T as Test>::Bundle: std::convert::From<std::string::String>` bound

You can solve the problem by copy/paste with the trait constraint.

fn works<T>()
    where T: Test<Item = String>,
          T::Bundle: From<T::Item>,
{ }

I've been pointed at 28055, related to super traits (same, maybe not?). I think the invoked argument is that "too many inferred constraints leads to spooky action at a distance", which I understand.

At the moment, I very much feel like the maintainability trade-off is in totally the wrong direction, though. If this associated type changes, or goes away, or has a new constraint, all existing code that may not care about the type breaks until you copy/paste in the (I feel) redundant requirements the trait already imposes.

Edit: see also #34106.

@Mark-Simulacrum Mark-Simulacrum added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 27, 2017
@estebank
Copy link
Contributor

estebank commented Nov 5, 2019

Currently we suggest the appropriate type constraint:

error[E0277]: the trait bound `<T as Test>::Bundle: std::convert::From<std::string::String>` is not satisfied
  --> src/lib.rs:10:1
   |
1  |   pub trait Test {
   |   -------------- required by `Test`
...
10 | / fn fails<T>() 
11 | |     where T: Test<Item = String>
   | |                                 - help: consider further restricting the associated type: `, <T as Test>::Bundle: std::convert::From<std::string::String>`
12 | | { } 
   | |___^ the trait `std::convert::From<std::string::String>` is not implemented for `<T as Test>::Bundle`

There are multiple other tickets to make the code work, but now at least we direct people in the right direction.

@estebank estebank added A-associated-items Area: Associated items (types, constants & functions) F-associated_type_bounds `#![feature(associated_type_bounds)]` labels Nov 5, 2019
@jonas-schievink jonas-schievink removed the F-associated_type_bounds `#![feature(associated_type_bounds)]` label Jan 28, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this issue Feb 2, 2021
Add some tests for associated-type-bounds issues

Closes rust-lang#38917
Closes rust-lang#40093
Closes rust-lang#43475
Closes rust-lang#63591

rust-lang#47897 is likely closable too, but it needs an MCVE
~~rust-lang#38917, rust-lang#40093, rust-lang#43475, rust-lang#47897 all are mislabeled and shouldn't have the `F-associated-type-bounds` label~~

~~rust-lang#71685 is also mislabeled as commented on in that thread~~
@bors bors closed this as completed in a61e6ab Feb 2, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants