Skip to content

Commit 5b7d64e

Browse files
Fix error codes explanation' code examples
1 parent 9876f6b commit 5b7d64e

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

src/librustc_error_codes/error_codes/E0445.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ trait Foo {
1212
pub trait Bar : Foo {} // error: private trait in public interface
1313
pub struct Bar2<T: Foo>(pub T); // same error
1414
pub fn foo<T: Foo> (t: T) {} // same error
15+
16+
fn main() {}
1517
```
1618

1719
To solve this error, please ensure that the trait is also public. The trait
@@ -26,4 +28,6 @@ pub trait Foo { // we set the Foo trait public
2628
pub trait Bar : Foo {} // ok!
2729
pub struct Bar2<T: Foo>(pub T); // ok!
2830
pub fn foo<T: Foo> (t: T) {} // ok!
31+
32+
fn main() {}
2933
```

src/librustc_error_codes/error_codes/E0446.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod Foo {
1212
Bar(0)
1313
}
1414
}
15+
16+
fn main() {}
1517
```
1618

1719
To solve this error, please ensure that the type is also public. The type
@@ -27,4 +29,6 @@ mod Foo {
2729
Bar(0)
2830
}
2931
}
32+
33+
fn main() {}
3034
```

src/librustc_error_codes/error_codes/E0491.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@ A reference has a longer lifetime than the data it references.
33
Erroneous code example:
44

55
```compile_fail,E0491
6-
trait SomeTrait<'a> {
7-
type Output;
6+
struct Foo<'a> {
7+
x: fn(&'a i32),
88
}
99
10-
impl<'a, T> SomeTrait<'a> for T {
11-
type Output = &'a T; // compile error E0491
10+
trait Trait<'a, 'b> {
11+
type Out;
12+
}
13+
14+
impl<'a, 'b> Trait<'a, 'b> for usize {
15+
type Out = &'a Foo<'b>; // error!
1216
}
1317
```
1418

15-
Here, the problem is that a reference type like `&'a T` is only valid
16-
if all the data in T outlives the lifetime `'a`. But this impl as written
17-
is applicable to any lifetime `'a` and any type `T` -- we have no guarantee
18-
that `T` outlives `'a`. To fix this, you can add a where clause like
19-
`where T: 'a`.
19+
Here, the problem is that the compiler cannot be sure that the `'b` lifetime
20+
will live longer than `'a`, which should be mandatory in order to be sure that
21+
`Trait::Out` will always have a reference pointing to an existing type. So in
22+
this case, we just need to tell the compiler than `'b` must outlive `'a`:
2023

2124
```
22-
trait SomeTrait<'a> {
23-
type Output;
25+
struct Foo<'a> {
26+
x: fn(&'a i32),
27+
}
28+
29+
trait Trait<'a, 'b> {
30+
type Out;
2431
}
2532
26-
impl<'a, T> SomeTrait<'a> for T
27-
where
28-
T: 'a,
29-
{
30-
type Output = &'a T; // compile error E0491
33+
impl<'a, 'b: 'a> Trait<'a, 'b> for usize { // we added the lifetime enforcement
34+
type Out = &'a Foo<'b>; // it now works!
3135
}
3236
```

0 commit comments

Comments
 (0)