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

Trait bounds on struct type parameters aren't always enforced (depending on the order of impls) #21837

Closed
SSheldon opened this Issue Feb 1, 2015 · 15 comments

Comments

Projects
None yet
9 participants
@SSheldon
Copy link
Contributor

SSheldon commented Feb 1, 2015

The following compiles without issue, even though the impl of Trait2 is missing a trait bound that T implements Bound:

pub trait Bound { }
pub struct Foo<T: Bound>;

pub trait Trait1 { }
impl<T: Bound> Trait1 for Foo<T> { }

pub trait Trait2 { }
impl<T> Trait2 for Foo<T> { }

fn main() { }

However, if you switch the order of the impls...

pub trait Bound { }
pub struct Foo<T: Bound>;

pub trait Trait2 { }
impl<T> Trait2 for Foo<T> { }

pub trait Trait1 { }
impl<T: Bound> Trait1 for Foo<T> { }

fn main() { }

You suddenly start getting this error:

error: the trait `Bound` is not implemented for the type `T` [E0277]
impl<T> Trait2 for Foo<T> { }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

I did not expect the ordering of the impls to matter here. I'm using rustc 1.0.0-nightly (1d00c545e 2015-01-30 19:56:34 +0000).

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Feb 5, 2015

P-high, not 1.0. (But definitely a weird bug that we would like to fix.)

@pnkfelix pnkfelix added P-medium and removed I-nominated labels Feb 5, 2015

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 5, 2015

cc me

@SSheldon

This comment has been minimized.

Copy link
Contributor Author

SSheldon commented Apr 4, 2015

@nikomatsakis, @pnkfelix: should this be P-backcompat-lang since the compiler accepts code that it should not?

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Apr 4, 2015

@SSheldon I think we decided this is one of those edge cases where the amount of breakage (that fixing this is expected to incur) was deemed acceptable for fixing post 1.0.

At the time this was last triaged, there was a general rule that if something was tagged P-backcompat-lang, then it must also be set for the 1.0 milestone (at the latest).

However, since then during recent triage meetings, we have been more willing to tag things as P-backcompat-lang even if they were not attached to the 1.0 milestone.

So, with that in mind:

triage: P-backcompat-lang, not P-high

@arielb1

This comment has been minimized.

Copy link
Contributor

arielb1 commented Oct 1, 2015

This causes an RFC1214 warning in 1.4+.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 29, 2015

triage: P-medium

Looks like this is already fixed by RFC 1214, as @arielb1 reports.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 29, 2015

Tagging as E-needstest, since we should be able to add a test with WARN messages (and perhaps a #[rustc_error] annotation on main)

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 29, 2015

Tagging as mentor just because I can give advice on how to craft a suitable test (nmatsakis on IRC)

@bltavares

This comment has been minimized.

Copy link
Contributor

bltavares commented Jun 13, 2016

Triaging:

Both examples causes error on nightly rustc 1.11.0-nightly (5c2a5d449 2016-06-11), but this seems to be the expected behaviour.

<anon>:2:16: 2:17 error: parameter `T` is never used [E0392]
<anon>:2 pub struct Foo<T: Bound>;
                        ^
<anon>:2:16: 2:17 help: see the detailed explanation for E0392
<anon>:2:16: 2:17 help: consider removing `T` or using a marker such as `core::marker::PhantomData`
<anon>:8:9: 8:15 error: the trait `Bound` is not implemented for the type `T` [E0277]
<anon>:8 impl<T> Trait2 for Foo<T> { }
                 ^~~~~~
<anon>:8:9: 8:15 help: see the detailed explanation for E0277
<anon>:8:9: 8:15 note: required by `Foo`
error: aborting due to 2 previous errors

@nikomatsakis I would like to help with the test if you don't mind giving initial directions.

@SSheldon

This comment has been minimized.

Copy link
Contributor Author

SSheldon commented Jun 19, 2016

@bltavares, that repro case was from pre-1.0, here's the updated repro case:

pub trait Bound { }
pub struct Foo<T: Bound>(T);

pub trait Trait1 { }
impl<T: Bound> Trait1 for Foo<T> { }

pub trait Trait2 { }
impl<T> Trait2 for Foo<T> { }

fn main() { }

That code will compile with rustc 1.0.0 (a59de37e9 2015-05-13), but fails to compile with rustc 1.9.0 (e4e8b6668 2016-05-18).

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jul 19, 2016

@arielb1 This seems to have been fixed by RFC 1214. Can it be closed?

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Jul 20, 2016

@brson I think it needs a test. I probably should not be assigned to it since I don't plan to spend time making a test.

@pnkfelix pnkfelix removed their assignment Jul 20, 2016

@brson brson added the E-easy label Jul 30, 2016

@DaleBae DaleBae referenced this issue Sep 29, 2016

Open

이슈 선정 #5

@martinhath

This comment has been minimized.

Copy link
Contributor

martinhath commented Oct 2, 2016

So all there is to do is add a test which checks that Ssheldons code does not compile?

martinhath added a commit to martinhath/rust that referenced this issue Oct 3, 2016

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 3, 2016

@martinhath yes :)

martinhath added a commit to martinhath/rust that referenced this issue Oct 3, 2016

Manishearth added a commit to Manishearth/rust that referenced this issue Oct 4, 2016

Rollup merge of rust-lang#36941 - martinhath:issue-21837, r=alexcrichton
Add regression test for Issue rust-lang#21837

This PR adds a regression test for Issue rust-lang#21837, as explained in the comments of the issue.
@SSheldon

This comment has been minimized.

Copy link
Contributor Author

SSheldon commented Oct 8, 2016

Fixed now with a test merged in #36941

@SSheldon SSheldon closed this Oct 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.