diff options
Diffstat (limited to 'compiler/rustc_error_codes/src/error_codes/E0623.md')
-rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0623.md | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0623.md b/compiler/rustc_error_codes/src/error_codes/E0623.md new file mode 100644 index 000000000..34db641bb --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0623.md @@ -0,0 +1,72 @@ +A lifetime didn't match what was expected. + +Erroneous code example: + +```compile_fail,E0623 +struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>) +where + T: Convert<'a, 'b>; + +trait Convert<'a, 'b>: Sized { + fn cast(&'a self) -> &'b Self; +} +impl<'long: 'short, 'short, T> Convert<'long, 'short> for T { + fn cast(&'long self) -> &'short T { + self + } +} +// error +fn badboi<'in_, 'out, T>( + x: Foo<'in_, 'out, T>, + sadness: &'in_ T +) -> &'out T { + sadness.cast() +} +``` + +In this example, we tried to set a value with an incompatible lifetime to +another one (`'in_` is unrelated to `'out`). We can solve this issue in +two different ways: + +Either we make `'in_` live at least as long as `'out`: + +``` +struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>) +where + T: Convert<'a, 'b>; + +trait Convert<'a, 'b>: Sized { + fn cast(&'a self) -> &'b Self; +} +impl<'long: 'short, 'short, T> Convert<'long, 'short> for T { + fn cast(&'long self) -> &'short T { + self + } +} +fn badboi<'in_: 'out, 'out, T>( + x: Foo<'in_, 'out, T>, + sadness: &'in_ T +) -> &'out T { + sadness.cast() +} +``` + +Or we use only one lifetime: + +``` +struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>) +where + T: Convert<'a, 'b>; + +trait Convert<'a, 'b>: Sized { + fn cast(&'a self) -> &'b Self; +} +impl<'long: 'short, 'short, T> Convert<'long, 'short> for T { + fn cast(&'long self) -> &'short T { + self + } +} +fn badboi<'out, T>(x: Foo<'out, 'out, T>, sadness: &'out T) -> &'out T { + sadness.cast() +} +``` |