From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- .../rustc_error_codes/src/error_codes/E0491.md | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0491.md (limited to 'compiler/rustc_error_codes/src/error_codes/E0491.md') 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! +} +``` -- cgit v1.2.3