Skip to content

Commit a93c0da

Browse files
authored
Rollup merge of rust-lang#64347 - GuillaumeGomez:E0312, r=oli-obk
Add long error explanation for E0312 Part of rust-lang#61137.
2 parents d021dba + cf6a1fe commit a93c0da

16 files changed

+50
-3
lines changed

src/librustc/error_codes.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,39 @@ struct Foo<T: 'static> {
13471347
```
13481348
"##,
13491349

1350+
E0312: r##"
1351+
Reference's lifetime of borrowed content doesn't match the expected lifetime.
1352+
1353+
Erroneous code example:
1354+
1355+
```compile_fail,E0312
1356+
pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'static str {
1357+
if maybestr.is_none() {
1358+
"(none)"
1359+
} else {
1360+
let s: &'a str = maybestr.as_ref().unwrap();
1361+
s // Invalid lifetime!
1362+
}
1363+
}
1364+
```
1365+
1366+
To fix this error, either lessen the expected lifetime or find a way to not have
1367+
to use this reference outside of its current scope (by running the code directly
1368+
in the same block for example?):
1369+
1370+
```
1371+
// In this case, we can fix the issue by switching from "static" lifetime to 'a
1372+
pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'a str {
1373+
if maybestr.is_none() {
1374+
"(none)"
1375+
} else {
1376+
let s: &'a str = maybestr.as_ref().unwrap();
1377+
s // Ok!
1378+
}
1379+
}
1380+
```
1381+
"##,
1382+
13501383
E0317: r##"
13511384
This error occurs when an `if` expression without an `else` block is used in a
13521385
context where a type other than `()` is expected, for example a `let`
@@ -2202,7 +2235,6 @@ static X: u32 = 42;
22022235
// E0304, // expected signed integer constant
22032236
// E0305, // expected constant
22042237
E0311, // thing may not live long enough
2205-
E0312, // lifetime of reference outlives lifetime of borrowed content
22062238
E0313, // lifetime of borrowed pointer outlives lifetime of captured
22072239
// variable
22082240
E0314, // closure outlives stack frame

src/test/ui/issues/issue-10291.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ LL | fn test<'x>(x: &'x isize) {
2020

2121
error: aborting due to previous error
2222

23+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/issues/issue-52533.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ LL | foo(|a, b| b)
1717

1818
error: aborting due to previous error
1919

20+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/lub-if.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ LL | pub fn opt_str3<'a>(maybestr: &'a Option<String>) -> &'static str {
2626

2727
error: aborting due to 2 previous errors
2828

29+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/lub-match.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ LL | pub fn opt_str3<'a>(maybestr: &'a Option<String>) -> &'static str {
2626

2727
error: aborting due to 2 previous errors
2828

29+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/nll/issue-52742.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ LL | | }
2020

2121
error: aborting due to previous error
2222

23+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/nll/issue-55401.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ LL | fn static_to_a_to_static_through_ref_in_tuple<'a>(x: &'a u32) -> &'static u
1313

1414
error: aborting due to previous error
1515

16+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/nll/user-annotations/constant-in-expr-normalize.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ LL | fn foo<'a>(_: &'a u32) -> &'static u32 {
1313

1414
error: aborting due to previous error
1515

16+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/nll/user-annotations/constant-in-expr-trait-item-1.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ LL | fn foo<'a>(_: &'a u32) -> &'static u32 {
1313

1414
error: aborting due to previous error
1515

16+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/nll/user-annotations/constant-in-expr-trait-item-2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ LL | fn foo<'a, T: Foo<'a>>() -> &'static u32 {
1313

1414
error: aborting due to previous error
1515

16+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/regions/regions-early-bound-error-method.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ LL | fn or<'b,G:GetRef<'b>>(&self, g2: G) -> &'a isize {
1717

1818
error: aborting due to previous error
1919

20+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/regions/regions-early-bound-error.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ LL | fn get<'a,'b,G:GetRef<'a, isize>>(g1: G, b: &'b isize) -> &'b isize {
1717

1818
error: aborting due to previous error
1919

20+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/regions/regions-nested-fns.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ LL | fn nested<'x>(x: &'x isize) {
5757

5858
error: aborting due to 2 previous errors
5959

60+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/regions/regions-static-bound.migrate.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ LL | static_id_indirect(&v);
3030

3131
error: aborting due to 3 previous errors
3232

33-
For more information about this error, try `rustc --explain E0621`.
33+
Some errors have detailed explanations: E0312, E0621.
34+
For more information about an error, try `rustc --explain E0312`.

src/test/ui/unboxed-closures/unboxed-closures-infer-argument-types-two-region-pointers.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ LL | | });
2323

2424
error: aborting due to previous error
2525

26+
For more information about this error, try `rustc --explain E0312`.

src/test/ui/wf/wf-static-method.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,5 @@ LL | <IndirectEvil>::static_evil(b)
105105

106106
error: aborting due to 5 previous errors
107107

108-
For more information about this error, try `rustc --explain E0478`.
108+
Some errors have detailed explanations: E0312, E0478.
109+
For more information about an error, try `rustc --explain E0312`.

0 commit comments

Comments
 (0)