File tree 4 files changed +29
-18
lines changed
librustc_error_codes/error_codes
4 files changed +29
-18
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ trait Foo {
12
12
pub trait Bar : Foo {} // error: private trait in public interface
13
13
pub struct Bar2<T: Foo>(pub T); // same error
14
14
pub fn foo<T: Foo> (t: T) {} // same error
15
+
16
+ fn main() {}
15
17
```
16
18
17
19
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
26
28
pub trait Bar : Foo {} // ok!
27
29
pub struct Bar2<T: Foo>(pub T); // ok!
28
30
pub fn foo<T: Foo> (t: T) {} // ok!
31
+
32
+ fn main() {}
29
33
```
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ mod Foo {
12
12
Bar(0)
13
13
}
14
14
}
15
+
16
+ fn main() {}
15
17
```
16
18
17
19
To solve this error, please ensure that the type is also public. The type
@@ -27,4 +29,6 @@ mod Foo {
27
29
Bar(0)
28
30
}
29
31
}
32
+
33
+ fn main() {}
30
34
```
Original file line number Diff line number Diff line change @@ -3,30 +3,34 @@ A reference has a longer lifetime than the data it references.
3
3
Erroneous code example:
4
4
5
5
``` compile_fail,E0491
6
- trait SomeTrait <'a> {
7
- type Output;
6
+ struct Foo <'a> {
7
+ x: fn(&'a i32),
8
8
}
9
9
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!
12
16
}
13
17
```
14
18
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 ` :
20
23
21
24
```
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;
24
31
}
25
32
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!
31
35
}
32
36
```
Original file line number Diff line number Diff line change @@ -300,7 +300,6 @@ fn run_test(
300
300
eprint ! ( "{}" , self . 0 ) ;
301
301
}
302
302
}
303
-
304
303
let out = str:: from_utf8 ( & output. stderr ) . unwrap ( ) ;
305
304
let _bomb = Bomb ( & out) ;
306
305
match ( output. status . success ( ) , compile_fail) {
@@ -310,7 +309,7 @@ fn run_test(
310
309
( true , false ) => { }
311
310
( false , true ) => {
312
311
if !error_codes. is_empty ( ) {
313
- error_codes. retain ( |err| !out. contains ( err) ) ;
312
+ error_codes. retain ( |err| !out. contains ( & format ! ( "error[{}]: " , err) ) ) ;
314
313
315
314
if !error_codes. is_empty ( ) {
316
315
return Err ( TestFailure :: MissingErrorCodes ( error_codes) ) ;
You can’t perform that action at this time.
0 commit comments