summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_error_codes/src/error_codes/E0491.md
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_error_codes/src/error_codes/E0491.md')
-rw-r--r--compiler/rustc_error_codes/src/error_codes/E0491.md36
1 files changed, 36 insertions, 0 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0491.md b/compiler/rustc_error_codes/src/error_codes/E0491.md
new file mode 100644
index 000000000..d45663f3a
--- /dev/null
+++ b/compiler/rustc_error_codes/src/error_codes/E0491.md
@@ -0,0 +1,36 @@
+A reference has a longer lifetime than the data it references.
+
+Erroneous code example:
+
+```compile_fail,E0491
+struct Foo<'a> {
+ x: fn(&'a i32),
+}
+
+trait Trait<'a, 'b> {
+ type Out;
+}
+
+impl<'a, 'b> Trait<'a, 'b> for usize {
+ type Out = &'a Foo<'b>; // error!
+}
+```
+
+Here, the problem is that the compiler cannot be sure that the `'b` lifetime
+will live longer than `'a`, which should be mandatory in order to be sure that
+`Trait::Out` will always have a reference pointing to an existing type. So in
+this case, we just need to tell the compiler than `'b` must outlive `'a`:
+
+```
+struct Foo<'a> {
+ x: fn(&'a i32),
+}
+
+trait Trait<'a, 'b> {
+ type Out;
+}
+
+impl<'a, 'b: 'a> Trait<'a, 'b> for usize { // we added the lifetime enforcement
+ type Out = &'a Foo<'b>; // it now works!
+}
+```