diff options
Diffstat (limited to 'compiler/rustc_error_codes/src/error_codes/E0312.md')
-rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0312.md | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0312.md b/compiler/rustc_error_codes/src/error_codes/E0312.md new file mode 100644 index 000000000..c5f7cf2e3 --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0312.md @@ -0,0 +1,32 @@ +#### Note: this error code is no longer emitted by the compiler. + +Reference's lifetime of borrowed content doesn't match the expected lifetime. + +Erroneous code example: + +```compile_fail +pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'static str { + if maybestr.is_none() { + "(none)" + } else { + let s: &'a str = maybestr.as_ref().unwrap(); + s // Invalid lifetime! + } +} +``` + +To fix this error, either lessen the expected lifetime or find a way to not have +to use this reference outside of its current scope (by running the code directly +in the same block for example?): + +``` +// In this case, we can fix the issue by switching from "static" lifetime to 'a +pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'a str { + if maybestr.is_none() { + "(none)" + } else { + let s: &'a str = maybestr.as_ref().unwrap(); + s // Ok! + } +} +``` |