From 144b8450735f61270cae223c1fd12e2f605c5b34 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 10 Sep 2019 13:48:30 +0200 Subject: [PATCH 1/2] Add long error explanation for E0312 --- src/librustc/error_codes.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/librustc/error_codes.rs b/src/librustc/error_codes.rs index eee33846139e6..f6564f1fcd4c1 100644 --- a/src/librustc/error_codes.rs +++ b/src/librustc/error_codes.rs @@ -1347,6 +1347,39 @@ struct Foo { ``` "##, +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) -> &'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) -> &'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 From cf6a1feda65525b947bfd33279c8b74c00f098c6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 12 Sep 2019 13:25:42 +0200 Subject: [PATCH 2/2] update ui tests --- src/test/ui/issues/issue-10291.stderr | 1 + src/test/ui/issues/issue-52533.stderr | 1 + src/test/ui/lub-if.stderr | 1 + src/test/ui/lub-match.stderr | 1 + src/test/ui/nll/issue-52742.stderr | 1 + src/test/ui/nll/issue-55401.stderr | 1 + .../ui/nll/user-annotations/constant-in-expr-normalize.stderr | 1 + .../nll/user-annotations/constant-in-expr-trait-item-1.stderr | 1 + .../nll/user-annotations/constant-in-expr-trait-item-2.stderr | 1 + src/test/ui/regions/regions-early-bound-error-method.stderr | 1 + src/test/ui/regions/regions-early-bound-error.stderr | 1 + src/test/ui/regions/regions-nested-fns.stderr | 1 + src/test/ui/regions/regions-static-bound.migrate.stderr | 3 ++- ...ed-closures-infer-argument-types-two-region-pointers.stderr | 1 + src/test/ui/wf/wf-static-method.stderr | 3 ++- 15 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/test/ui/issues/issue-10291.stderr b/src/test/ui/issues/issue-10291.stderr index 5e63469da59f5..a836593e0da10 100644 --- a/src/test/ui/issues/issue-10291.stderr +++ b/src/test/ui/issues/issue-10291.stderr @@ -20,3 +20,4 @@ LL | fn test<'x>(x: &'x isize) { error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/issues/issue-52533.stderr b/src/test/ui/issues/issue-52533.stderr index 1ed740c421e0f..586548002072e 100644 --- a/src/test/ui/issues/issue-52533.stderr +++ b/src/test/ui/issues/issue-52533.stderr @@ -17,3 +17,4 @@ LL | foo(|a, b| b) error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/lub-if.stderr b/src/test/ui/lub-if.stderr index bb772d4c6c695..26f756c91833b 100644 --- a/src/test/ui/lub-if.stderr +++ b/src/test/ui/lub-if.stderr @@ -26,3 +26,4 @@ LL | pub fn opt_str3<'a>(maybestr: &'a Option) -> &'static str { error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/lub-match.stderr b/src/test/ui/lub-match.stderr index 090af25143670..0cb0a23c6f2df 100644 --- a/src/test/ui/lub-match.stderr +++ b/src/test/ui/lub-match.stderr @@ -26,3 +26,4 @@ LL | pub fn opt_str3<'a>(maybestr: &'a Option) -> &'static str { error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/nll/issue-52742.stderr b/src/test/ui/nll/issue-52742.stderr index b982915800294..90a35177f4c3b 100644 --- a/src/test/ui/nll/issue-52742.stderr +++ b/src/test/ui/nll/issue-52742.stderr @@ -20,3 +20,4 @@ LL | | } error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/nll/issue-55401.stderr b/src/test/ui/nll/issue-55401.stderr index 50debc6386f6a..4ec16ba055a4c 100644 --- a/src/test/ui/nll/issue-55401.stderr +++ b/src/test/ui/nll/issue-55401.stderr @@ -13,3 +13,4 @@ LL | fn static_to_a_to_static_through_ref_in_tuple<'a>(x: &'a u32) -> &'static u error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/nll/user-annotations/constant-in-expr-normalize.stderr b/src/test/ui/nll/user-annotations/constant-in-expr-normalize.stderr index f49d68458bea5..0a8ad4221c986 100644 --- a/src/test/ui/nll/user-annotations/constant-in-expr-normalize.stderr +++ b/src/test/ui/nll/user-annotations/constant-in-expr-normalize.stderr @@ -13,3 +13,4 @@ LL | fn foo<'a>(_: &'a u32) -> &'static u32 { error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-1.stderr b/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-1.stderr index 451bcf41e42a9..d596aaf098f77 100644 --- a/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-1.stderr +++ b/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-1.stderr @@ -13,3 +13,4 @@ LL | fn foo<'a>(_: &'a u32) -> &'static u32 { error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-2.stderr b/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-2.stderr index d129e55e1e6f6..80ff9a043d4d5 100644 --- a/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-2.stderr +++ b/src/test/ui/nll/user-annotations/constant-in-expr-trait-item-2.stderr @@ -13,3 +13,4 @@ LL | fn foo<'a, T: Foo<'a>>() -> &'static u32 { error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/regions/regions-early-bound-error-method.stderr b/src/test/ui/regions/regions-early-bound-error-method.stderr index 2e5f55f8742cc..7b9f2c9503b2f 100644 --- a/src/test/ui/regions/regions-early-bound-error-method.stderr +++ b/src/test/ui/regions/regions-early-bound-error-method.stderr @@ -17,3 +17,4 @@ LL | fn or<'b,G:GetRef<'b>>(&self, g2: G) -> &'a isize { error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/regions/regions-early-bound-error.stderr b/src/test/ui/regions/regions-early-bound-error.stderr index bc52f4bef7eaa..a68355b78f54c 100644 --- a/src/test/ui/regions/regions-early-bound-error.stderr +++ b/src/test/ui/regions/regions-early-bound-error.stderr @@ -17,3 +17,4 @@ LL | fn get<'a,'b,G:GetRef<'a, isize>>(g1: G, b: &'b isize) -> &'b isize { error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/regions/regions-nested-fns.stderr b/src/test/ui/regions/regions-nested-fns.stderr index 15c9c9ca4ddbb..904dee6998c9b 100644 --- a/src/test/ui/regions/regions-nested-fns.stderr +++ b/src/test/ui/regions/regions-nested-fns.stderr @@ -57,3 +57,4 @@ LL | fn nested<'x>(x: &'x isize) { error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/regions/regions-static-bound.migrate.stderr b/src/test/ui/regions/regions-static-bound.migrate.stderr index fc8cca929d389..21ead8b768f47 100644 --- a/src/test/ui/regions/regions-static-bound.migrate.stderr +++ b/src/test/ui/regions/regions-static-bound.migrate.stderr @@ -30,4 +30,5 @@ LL | static_id_indirect(&v); error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0621`. +Some errors have detailed explanations: E0312, E0621. +For more information about an error, try `rustc --explain E0312`. diff --git a/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr b/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr index 728efadf4196e..526055ba04b65 100644 --- a/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr +++ b/src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr @@ -23,3 +23,4 @@ LL | | }); error: aborting due to previous error +For more information about this error, try `rustc --explain E0312`. diff --git a/src/test/ui/wf/wf-static-method.stderr b/src/test/ui/wf/wf-static-method.stderr index f82526aa88ebf..3ec90f00448a9 100644 --- a/src/test/ui/wf/wf-static-method.stderr +++ b/src/test/ui/wf/wf-static-method.stderr @@ -105,4 +105,5 @@ LL | ::static_evil(b) error: aborting due to 5 previous errors -For more information about this error, try `rustc --explain E0478`. +Some errors have detailed explanations: E0312, E0478. +For more information about an error, try `rustc --explain E0312`.