diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2019-09-10 13:48:30 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2019-09-12 12:58:44 +0200 |
| commit | 144b8450735f61270cae223c1fd12e2f605c5b34 (patch) | |
| tree | bfe9dc05791b80bef0c1c145843d9afe5ae63e3b | |
| parent | c9edc02e8320a9e5799d185910ece7d491f524e6 (diff) | |
| download | rust-144b8450735f61270cae223c1fd12e2f605c5b34.tar.gz rust-144b8450735f61270cae223c1fd12e2f605c5b34.zip | |
Add long error explanation for E0312
| -rw-r--r-- | src/librustc/error_codes.rs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/librustc/error_codes.rs b/src/librustc/error_codes.rs index eee33846139..f6564f1fcd4 100644 --- a/src/librustc/error_codes.rs +++ b/src/librustc/error_codes.rs @@ -1347,6 +1347,39 @@ struct Foo<T: 'static> { ``` "##, +E0312: r##" +Reference's lifetime of borrowed content doesn't match the expected lifetime. + +Erroneous code example: + +```compile_fail,E0312 +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! + } +} +``` +"##, + E0317: r##" This error occurs when an `if` expression without an `else` block is used in a context where a type other than `()` is expected, for example a `let` @@ -2202,7 +2235,6 @@ static X: u32 = 42; // E0304, // expected signed integer constant // E0305, // expected constant E0311, // thing may not live long enough - E0312, // lifetime of reference outlives lifetime of borrowed content E0313, // lifetime of borrowed pointer outlives lifetime of captured // variable E0314, // closure outlives stack frame |
