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
lifetime variables in type bounds not properly enforced #12857
I can't see how you're enforcing the lifetime of anything there. You just have things generic over a lifetime without a mention of the lifetime in the return type (in the trait definition, that is).
You should also check ContravariantLifetime<'a> in generic contexts and replacing it with a non-marker that's contravariant (like &'a T).
IMO that trait should be HKT over a lifetime for this to work at all.
Sorry it took me 5 days to read into it. What I expect to happen is that the region rules require that the lifetime of bar be equal to the enclosing block, which should lead to an error somewhere since foo cannot have such a long lifetime. Might be related to #5121, might not.
A new datapoint: the use of a trait method seems to be necessary to expose the problem here:
pubtraitFromSlice<'a>{fnnew(s:&'a[u8]) -> Self;}structHide<'a>{slice:&'a[u8]}impl<'a>Hide<'a>{fnnew(s:&'a[u8]) -> Hide<'a>{Hide{slice: s }}}impl<'a>FromSlice<'a>forHide<'a>{fnnew(s:&'a[u8]) -> Hide<'a>{Hide::new(s)}}#[cfg(hide_works)]pubfnmain(){let bar :Hide = {let foo = ~[1,2,3,4,5];FromSlice::new(foo.as_slice())};// somehow sidesteps typechecklet _x : ~[u8] = ~[9,9,9,9,9,9,9,9,9,9,9,9];println!("bar.slice = {}, should reject (was [1, 2, 3, 4, 5])", bar.slice);}#[cfg(hide_fails)]pubfnmain(){let bar :Hide = {let foo = ~[1,2,3,4,5];Hide::new(foo.as_slice())};// does not typecheck: `foo` does not live long enoughlet _x : ~[u8] = ~[9,9,9,9,9,9,9,9,9,9,9,9];println!("bar.slice = {}, should reject (was [1, 2, 3, 4, 5])", bar.slice);}
% rustc --cfg hide_fails iss12857-c.rs
iss12857-c.rs:25:19: 25:22 error: `foo` does not live long enough
iss12857-c.rs:25 Hide::new(foo.as_slice())
^~~
iss12857-c.rs:22:16: 31:2 note: reference must be valid for the block at 22:15...
iss12857-c.rs:22 pub fn main () {
iss12857-c.rs:23 let bar : Hide = {
iss12857-c.rs:24 let foo = ~[1, 2, 3, 4, 5];
iss12857-c.rs:25 Hide::new(foo.as_slice())
iss12857-c.rs:26 }; // does not typecheck: `foo` does not live long enough
iss12857-c.rs:27
...
iss12857-c.rs:23:22: 26:6 note: ...but borrowed value is only valid for the block at 23:21
iss12857-c.rs:23 let bar : Hide = {
iss12857-c.rs:24 let foo = ~[1, 2, 3, 4, 5];
iss12857-c.rs:25 Hide::new(foo.as_slice())
iss12857-c.rs:26 }; // does not typecheck: `foo` does not live long enough
error: aborting due to previous error
% rustc --cfg hide_works iss12857-c.rs
% ./iss12857-c
bar.slice = [9, 9, 9, 9, 9], should reject (was [1, 2, 3, 4, 5])
%
Activity
dwrensha commentedon Mar 13, 2014
cc @pnkfelix
eddyb commentedon Mar 13, 2014
I can't see how you're enforcing the lifetime of anything there. You just have things generic over a lifetime without a mention of the lifetime in the return type (in the trait definition, that is).
You should also check
ContravariantLifetime<'a>in generic contexts and replacing it with a non-marker that's contravariant (like&'a T).IMO that trait should be HKT over a lifetime for this to work at all.
dwrensha commentedon Mar 13, 2014
@eddyb I see the same problem if I use a slice instead of a marker type:
dwrensha commentedon Mar 13, 2014
I don't think this example should require higher kinded types. The trait in question is:
Here,
Selfrefers to the type that implementsFromSlice<'a>. In particular, that type can refer to'a, asFooReader<'a>does.dwrensha commentedon Mar 13, 2014
Maybe @nikomatsakis can say if I'm mistaken on this point?
nikomatsakis commentedon Mar 18, 2014
This does look like a bug.
nikomatsakis commentedon Mar 18, 2014
Sorry it took me 5 days to read into it. What I expect to happen is that the region rules require that the lifetime of
barbe equal to the enclosing block, which should lead to an error somewhere sincefoocannot have such a long lifetime. Might be related to #5121, might not.pnkfelix commentedon Mar 23, 2014
I'm looking at this problem now. In particular the second version that avoids using any native pointers.
pnkfelix commentedon Mar 23, 2014
A new datapoint: the use of a trait method seems to be necessary to expose the problem here:
dwrensha commentedon Jun 14, 2014
Here is an updated test case.
pcwalton commentedon Jul 26, 2014
In HEAD I get
error: message does not live long enough. Closing. Please reopen if this is still an issue.fix: Autocomplete for struct fields includes receiver
Auto merge of rust-lang#12861 - DorianListens:dscheidt/complete-field…
fix: Autocomplete for struct fields includes receiver
Auto merge of rust-lang#12857 - WeiTheShinobi:non_canonical_impls, r=y21