diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/object-lifetime/object-lifetime-default-elision.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/object-lifetime/object-lifetime-default-elision.rs')
-rw-r--r-- | src/test/ui/object-lifetime/object-lifetime-default-elision.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/test/ui/object-lifetime/object-lifetime-default-elision.rs b/src/test/ui/object-lifetime/object-lifetime-default-elision.rs new file mode 100644 index 000000000..f7c0261cf --- /dev/null +++ b/src/test/ui/object-lifetime/object-lifetime-default-elision.rs @@ -0,0 +1,76 @@ +// Test various cases where the old rules under lifetime elision +// yield slightly different results than the new rules. + +#![allow(dead_code)] + +trait SomeTrait { + fn dummy(&self) { } +} + +struct SomeStruct<'a> { + r: Box<dyn SomeTrait+'a> +} + +fn deref<T>(ss: &T) -> T { + // produces the type of a deref without worrying about whether a + // move out would actually be legal + loop { } +} + +fn load0<'a>(ss: &'a Box<dyn SomeTrait>) -> Box<dyn SomeTrait> { + // Under old rules, the fully elaborated types of input/output were: + // + // for<'a,'b> fn(&'a Box<SomeTrait+'b>) -> Box<SomeTrait+'a> + // + // Under new rules the result is: + // + // for<'a> fn(&'a Box<SomeTrait+'static>) -> Box<SomeTrait+'static> + // + // Therefore, no type error. + + deref(ss) +} + +fn load1(ss: &dyn SomeTrait) -> &dyn SomeTrait { + // Under old rules, the fully elaborated types of input/output were: + // + // for<'a,'b> fn(&'a (SomeTrait+'b)) -> &'a (SomeTrait+'a) + // + // Under new rules the result is: + // + // for<'a> fn(&'a (SomeTrait+'a)) -> &'a (SomeTrait+'a) + // + // In both cases, returning `ss` is legal. + + ss +} + +fn load2<'a>(ss: &'a dyn SomeTrait) -> &dyn SomeTrait { + // Same as `load1` but with an explicit name thrown in for fun. + + ss +} + +fn load3<'a,'b>(ss: &'a dyn SomeTrait) -> &'b dyn SomeTrait { + // Under old rules, the fully elaborated types of input/output were: + // + // for<'a,'b,'c>fn(&'a (SomeTrait+'c)) -> &'b (SomeTrait+'a) + // + // Based on the input/output types, the compiler could infer that + // 'c : 'a + // 'b : 'a + // must hold, and therefore it permitted `&'a (Sometrait+'c)` to be + // coerced to `&'b (SomeTrait+'a)`. + // + // Under the newer defaults, though, we get: + // + // for<'a,'b> fn(&'a (SomeTrait+'a)) -> &'b (SomeTrait+'b) + // + // which fails to type check. + + ss + //~^ ERROR lifetime may not live long enough +} + +fn main() { +} |