You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Associated types bug related to trait objects #30833
Playpen
Relevant code (note that DoesStuff is a trait):
implDerefforFoobar{typeTarget = DoesStuff;fnderef(&self) -> &Self::Target{matchself{&Foobar::Foo(ref e) => {let newref = e.clone();
newref as&DoesStuff// error here// newref as &Self::Target // just works..// newref // returning without `as` works (code below works too)}&Foobar::Bar(ref e) => e,}}}
Error text:
<anon>:26:26:26:31 error: cannot infer an appropriate lifetime for pattern due to conflicting requirements [E0495]
<anon>:26&Foobar::Foo(ref e) => {
^~~~~
<anon>:27:30:27:39 note: first, the lifetime cannot outlive the method call at 27:29...
<anon>:27let newref = e.clone();
^~~~~~~~~
<anon>:27:30:27:31 note: ...so that method receiver is valid for the method call<anon>:27 let newref = e.clone();
^
<anon>:24:38:34:6 note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the block at 24:37...
<anon>:24fn deref(&self) -> &Self::Target{
<anon>:25matchself{
<anon>:26&Foobar::Foo(ref e) => {
<anon>:27let newref = e.clone();
<anon>:28 newref as&DoesStuff// error here<anon>:29// newref as &Self::Target // just works..
...
<anon>:28:17:28:37 note: ...so that trait type parameters matches those specified on the impl (expected `core::ops::CoerceUnsized<&DoesStuff + 'static>`, found `core::ops::CoerceUnsized<&DoesStuff + 'static>`)
<anon>:28 newref as&DoesStuff// error here
^~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101
cc @mbrubeck since on the IRC you've told me that it's worthy filing an issue :)
<anon>:22:5:24:6 error: method `deref` has an incompatible type for trait:
expected bound lifetime parameter ,
found concrete lifetime [E0053]
<anon>:22 fn deref(&self) -> &DoesStuff{
<anon>:23self as &DoesStuff<anon>:24}
<anon>:22:5:24:6 help: see the detailed explanation forE0053
error: aborting due to previous error
playpen: application terminated with error code 101
Note the space before the comma here - expected bound lifetime parameter , - it's interesting.
Changing function signature to fn deref(&self) -> &Self::Target does the trick - the error that is produced is correct. Playpen
<anon>:28:17:28:37 note: ...so that trait type parameters matches those specified on the impl (expected `core::ops::CoerceUnsized<&DoesStuff + 'static>`, found `core::ops::CoerceUnsized<&DoesStuff + 'static>`)
so I don't see type mismatch here.
Don't you agree that it's a bug?
The type on the trait is &'a DoesStuff + 'static. The type on your impl is &'s DoesStuff + 's for some lifetime 's, which is not the same (because 'a != 'static). Therefore the error.
We might want to improve the diagnostics (especially, the "bound lifetime parameter ," sucks), but the compile error is for real.
Triage: the first example currently compiles. Checking godbolt, it has compiled since 1.16
The second playground link, posted by defyrlt, has an error that now looks like the following:
Details
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
--> src/main.rs:22:5
|
22 | fn deref(&self) -> &DoesStuff {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 22:5...
--> src/main.rs:22:5
|
22 | / fn deref(&self) -> &DoesStuff {
23 | | // fn deref(&self) -> &Self::Target {
24 | | self as &DoesStuff
25 | | }
| |_____^
= note: ...but the lifetime must also be valid for the static lifetime...
= note: ...so that the method type is compatible with trait:
expected fn(&Foobar) -> &(dyn DoesStuff + 'static)
found fn(&Foobar) -> &dyn DoesStuff
error: aborting due to previous error
rust version 1.34.0-nightly (c1c3c4e95 2019-01-29)
So the first bug was that &Foo used to be treated as &'$0 Foo + '$0 instead of as &'$0 Foo + '$1 - it started to mean the second interpretation in #39305, which has a test for the new behavior.
The diagnostics bug is probably covered by quite a few diagnostics tests, so there isn't much more to check. Closing.
Activity
defyrlt commentedon Jan 12, 2016
I've discovered something alike on the same code sample.
Relevant code:
fails with
Note the space before the comma here -
expected bound lifetime parameter ,- it's interesting.Changing function signature to
fn deref(&self) -> &Self::Targetdoes the trick - the error that is produced is correct.Playpen
[-]Associated types bug[/-][+]Associated types bug related to trait objects[/+]arielb1 commentedon Jan 12, 2016
This is caused by the implicit lifetime bound in trait objects:
DoesStuffisDoesStuff+'staticwhile&DoesStuffis&'a DoesStuff+'a.defyrlt commentedon Jan 13, 2016
The second one, right? The first one does not seem to be related to implicit lifetime bound.
arielb1 commentedon Jan 13, 2016
DoesStuff+'ais a distinct type fromDoesStuff+'static, so you have a type mismatch.defyrlt commentedon Jan 14, 2016
The first one says
so I don't see type mismatch here.
Don't you agree that it's a bug?
arielb1 commentedon Jan 14, 2016
It is not a bug.
The type on the trait is
&'a DoesStuff + 'static. The type on your impl is&'s DoesStuff + 'sfor some lifetime's, which is not the same (because'a != 'static). Therefore the error.We might want to improve the diagnostics (especially, the "bound lifetime parameter ," sucks), but the compile error is for real.
memoryruins commentedon Jan 30, 2019
Triage: the first example currently compiles. Checking godbolt, it has compiled since
1.16The second playground link, posted by defyrlt, has an error that now looks like the following:
Details
rust version 1.34.0-nightly (c1c3c4e95 2019-01-29)arielb1 commentedon Feb 2, 2019
The first bug was fixed in rustc 1.16, the second (diagnostic bug, which I thought was the only bug) was fixed in 1.11.
It might be worth seeing whether the first bug is covered by our test-suite and which PR fixed it.
arielb1 commentedon Feb 2, 2019
That appears to be #38624
arielb1 commentedon Feb 2, 2019
So the first bug was that
&Fooused to be treated as&'$0 Foo + '$0instead of as&'$0 Foo + '$1- it started to mean the second interpretation in #39305, which has a test for the new behavior.The diagnostics bug is probably covered by quite a few diagnostics tests, so there isn't much more to check. Closing.