Skip to content

Commit bf84eb5

Browse files
committed
Auto merge of #67850 - GuillaumeGomez:err-codes-checkup, r=Mark-Simulacrum
Error codes checkup and rustdoc test fix This PR does a few things: * fix how rustdoc checks that an error code has been thrown (it only checked for "E0XXX" so if it appeared in the output because the file has it in its name or wherever, it passed the test, which was incorrect) * fix the failing code examples that weren't throwing the expected error code
2 parents af95804 + 5b7d64e commit bf84eb5

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
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
```

src/librustdoc/test.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ fn run_test(
300300
eprint!("{}", self.0);
301301
}
302302
}
303-
304303
let out = str::from_utf8(&output.stderr).unwrap();
305304
let _bomb = Bomb(&out);
306305
match (output.status.success(), compile_fail) {
@@ -310,7 +309,7 @@ fn run_test(
310309
(true, false) => {}
311310
(false, true) => {
312311
if !error_codes.is_empty() {
313-
error_codes.retain(|err| !out.contains(err));
312+
error_codes.retain(|err| !out.contains(&format!("error[{}]: ", err)));
314313

315314
if !error_codes.is_empty() {
316315
return Err(TestFailure::MissingErrorCodes(error_codes));

0 commit comments

Comments
 (0)