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

indirect restrictions on Self in trait where clauses are not available when the trait is used as a bound #25409

Closed
TimNN opened this issue May 14, 2015 · 7 comments
Labels
A-trait-system Area: Trait system A-type-system Area: Type system P-low Low priority T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@TimNN
Copy link
Contributor

TimNN commented May 14, 2015

trait Bound where u8: Into<Self> {}

// u8: Into<Self> is enforced here:
// error: the trait `core::convert::From<u8>` is not implemented for the type `u16`  [E0277]
impl Bound for u16 {}
// ^~~~~~~~~~~~~~~~~~

fn test<B: Bound>() -> B {
    // but the compiler seems to be unaware of it here:
    // error: the trait `core::convert::From<u8>` is not implemented for the type `B` [E0277]
    0u8.into()
    //  ^~~~~~
}

As you can see, the compiler enforces the bound in the traits where clause when implementing the trait, but seems to be unaware of it when the trait is used itself as a bound.

I don't know if the traits where clause should be valid or not, however if it is valid, the compiler should be aware of the additional information provided by it.

At least if I haven't completely misunderstood something.

@steveklabnik steveklabnik added A-type-system Area: Type system A-trait-system Area: Trait system labels May 18, 2015
@arielb1
Copy link
Contributor

arielb1 commented Jul 27, 2015

This is a known issue - only supertraits are elaborated. This is required to ensure a trait-ref elaborates only finitely many others. As a workaround, you can always duplicate the where-clause.

@TimNN
Copy link
Contributor Author

TimNN commented Jul 27, 2015

Thank you for the information and the suggested workaround, I don't think that idea crossed my mind at the time. Now if I could only remember what I needed this for in the first place...

@brson
Copy link
Contributor

brson commented Jan 26, 2017

This is what stable says today about the test case:

rustc 1.14.0 (e8a012324 2016-12-16)
error: main function not found

error[E0277]: the trait bound `Self: std::marker::Sized` is not satisfied
 --> <anon>:1:1
  |
1 | trait Bound where u8: Into<Self> {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `Self`
  |
  = help: consider adding a `where Self: std::marker::Sized` bound
  = note: required by `std::convert::Into`

error[E0277]: the trait bound `B: std::convert::From<u8>` is not satisfied
  --> <anon>:8:1
   |
8  | fn test<B: Bound>() -> B {
   | ^ the trait `std::convert::From<u8>` is not implemented for `B`
   |
   = help: consider adding a `where B: std::convert::From<u8>` bound
   = note: required because of the requirements on the impl of `std::convert::Into<B>` for `u8`
   = note: required by `Bound`

error: aborting due to 2 previous errors

@brson brson added T-lang Relevant to the language team, which will review and decide on the PR/issue. I-nominated labels Jan 26, 2017
@brson
Copy link
Contributor

brson commented Jan 26, 2017

Lang team can you P-tag?

@brson
Copy link
Contributor

brson commented Jan 26, 2017

NVM P-low

@brson brson added P-low Low priority and removed I-nominated labels Jan 26, 2017
@jonas-schievink
Copy link
Contributor

Dupe of #20671 ?

@Mark-Simulacrum
Copy link
Member

I think so. Closing as a duplicate of #20671.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system A-type-system Area: Type system P-low Low priority T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants