Skip to content

Commit ae8b460

Browse files
Introduce distinct error codes for precise capturing
1 parent 26bdfef commit ae8b460

File tree

10 files changed

+69
-9
lines changed

10 files changed

+69
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Something other than a type or const parameter has been used when one was
2+
expected.
3+
4+
Erroneous code example:
5+
6+
```compile_fail,E0799
7+
fn bad1() -> impl Sized + use<main> {}
8+
9+
fn bad2(x: ()) -> impl Sized + use<x> {}
10+
11+
fn main() {}
12+
```
13+
14+
In the given examples, for `bad1`, the name `main` corresponds to a function
15+
rather than a type or const parameter. In `bad2`, the name `x` corresponds to
16+
a function argument rather than a type or const parameter.
17+
18+
Only type and const parameters, including `Self`, may be captured by
19+
`use<...>` precise capturing bounds.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
A type or const parameter of the given name is not in scope.
2+
3+
Erroneous code examples:
4+
5+
```compile_fail,E0800
6+
fn missing() -> impl Sized + use<T> {}
7+
```
8+
9+
To fix this error, please verify you didn't misspell the type or const
10+
parameter, or double-check if you forgot to declare the parameter in
11+
the list of generics.

compiler/rustc_error_codes/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ E0795: 0795,
538538
E0796: 0796,
539539
E0797: 0797,
540540
E0798: 0798,
541+
E0799: 0799,
542+
E0800: 0800,
541543
);
542544
)
543545
}

compiler/rustc_hir_analysis/src/errors/precise_captures.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::E0799;
12
use rustc_macros::Diagnostic;
23
use rustc_span::{Span, Symbol};
34

@@ -43,7 +44,7 @@ pub(crate) struct BadPreciseCapture {
4344
}
4445

4546
#[derive(Diagnostic)]
46-
#[diag(hir_analysis_precise_capture_self_alias)]
47+
#[diag(hir_analysis_precise_capture_self_alias, code = E0799)]
4748
pub(crate) struct PreciseCaptureSelfAlias {
4849
#[primary_span]
4950
pub span: Span,

compiler/rustc_resolve/src/late.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,8 @@ impl<'a> PathSource<'a> {
557557
match (self, has_unexpected_resolution) {
558558
(PathSource::Trait(_), true) => E0404,
559559
(PathSource::Trait(_), false) => E0405,
560-
// TODO:
561-
(PathSource::Type | PathSource::PreciseCapturingArg(..), true) => E0573,
562-
(PathSource::Type | PathSource::PreciseCapturingArg(..), false) => E0412,
560+
(PathSource::Type, true) => E0573,
561+
(PathSource::Type, false) => E0412,
563562
(PathSource::Struct, true) => E0574,
564563
(PathSource::Struct, false) => E0422,
565564
(PathSource::Expr(..), true) | (PathSource::Delegation, true) => E0423,
@@ -568,6 +567,8 @@ impl<'a> PathSource<'a> {
568567
(PathSource::Pat | PathSource::TupleStruct(..), false) => E0531,
569568
(PathSource::TraitItem(..), true) => E0575,
570569
(PathSource::TraitItem(..), false) => E0576,
570+
(PathSource::PreciseCapturingArg(..), true) => E0799,
571+
(PathSource::PreciseCapturingArg(..), false) => E0800,
571572
}
572573
}
573574
}

tests/ui/error-codes/E0799.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn test() -> impl Sized + use<main> {}
2+
//~^ ERROR E0799
3+
4+
fn main() {}

tests/ui/error-codes/E0799.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0799]: expected type or const parameter, found function `main`
2+
--> $DIR/E0799.rs:1:31
3+
|
4+
LL | fn test() -> impl Sized + use<main> {}
5+
| ^^^^ not a type or const parameter
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0799`.

tests/ui/error-codes/E0800.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn test() -> impl Sized + use<Missing> {}
2+
//~^ ERROR E0800
3+
4+
fn main() {}

tests/ui/error-codes/E0800.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0800]: cannot find type or const parameter `Missing` in this scope
2+
--> $DIR/E0800.rs:1:31
3+
|
4+
LL | fn test() -> impl Sized + use<Missing> {}
5+
| ^^^^^^^ not found in this scope
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0800`.

tests/ui/impl-trait/precise-capturing/bad-params.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0412]: cannot find type or const parameter `T` in this scope
1+
error[E0800]: cannot find type or const parameter `T` in this scope
22
--> $DIR/bad-params.rs:1:34
33
|
44
LL | fn missing() -> impl Sized + use<T> {}
@@ -17,19 +17,19 @@ LL | fn missing_self() -> impl Sized + use<Self> {}
1717
| |
1818
| `Self` not allowed in a function
1919

20-
error[E0573]: expected type or const parameter, found function `hello`
20+
error[E0799]: expected type or const parameter, found function `hello`
2121
--> $DIR/bad-params.rs:13:32
2222
|
2323
LL | fn hello() -> impl Sized + use<hello> {}
2424
| ^^^^^ not a type or const parameter
2525

26-
error[E0573]: expected type or const parameter, found local variable `x`
26+
error[E0799]: expected type or const parameter, found local variable `x`
2727
--> $DIR/bad-params.rs:16:35
2828
|
2929
LL | fn arg(x: ()) -> impl Sized + use<x> {}
3030
| ^ not a type or const parameter
3131

32-
error: `Self` can't be captured in `use<...>` precise captures list, since it is an alias
32+
error[E0799]: `Self` can't be captured in `use<...>` precise captures list, since it is an alias
3333
--> $DIR/bad-params.rs:9:48
3434
|
3535
LL | impl MyType {
@@ -39,5 +39,5 @@ LL | fn self_is_not_param() -> impl Sized + use<Self> {}
3939

4040
error: aborting due to 5 previous errors
4141

42-
Some errors have detailed explanations: E0411, E0412, E0573.
42+
Some errors have detailed explanations: E0411, E0799, E0800.
4343
For more information about an error, try `rustc --explain E0411`.

0 commit comments

Comments
 (0)