Skip to content

Commit 6daa9f1

Browse files
committed
Only check for errors in predicate when skipping impl assembly
Prior to PR rust-lang#91205, checking for errors in the overall obligation would check checking the `ParamEnv`, due to an incorrect `super_visit_with` impl. With this bug fixed, we will now bail out of impl candidate assembly if the `ParamEnv` contains any error types. In practice, this appears to be overly conservative - when an error occurs early in compilation, we end up giving up early for some predicates that we could have successfully evaluated without overflow. By only checking for errors in the predicate itself, we avoid causing additional spurious 'type annotations needed' errors after a 'real' error has already occurred. With this PR, the diagnostic changes caused by PR rust-lang#91205 are reverted.
1 parent 1e79d79 commit 6daa9f1

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
536536
// This helps us avoid overflow: see issue #72839
537537
// Since compilation is already guaranteed to fail, this is just
538538
// to try to show the 'nicest' possible errors to the user.
539-
if obligation.references_error() {
539+
// We don't check for errors in the `ParamEnv` - in practice,
540+
// it seems to cause us to be overly aggressive in deciding
541+
// to give up searching for candidates, leading to spurious errors.
542+
if obligation.predicate.references_error() {
540543
return;
541544
}
542545

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: generic parameters may not be used in const operations
2-
--> $DIR/issue-72787.rs:12:17
2+
--> $DIR/issue-72787.rs:11:17
33
|
44
LL | Condition<{ LHS <= RHS }>: True
55
| ^^^ cannot perform const operation using `LHS`
@@ -8,7 +8,7 @@ LL | Condition<{ LHS <= RHS }>: True
88
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
11-
--> $DIR/issue-72787.rs:12:24
11+
--> $DIR/issue-72787.rs:11:24
1212
|
1313
LL | Condition<{ LHS <= RHS }>: True
1414
| ^^^ cannot perform const operation using `RHS`
@@ -17,7 +17,7 @@ LL | Condition<{ LHS <= RHS }>: True
1717
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
1818

1919
error: generic parameters may not be used in const operations
20-
--> $DIR/issue-72787.rs:26:25
20+
--> $DIR/issue-72787.rs:25:25
2121
|
2222
LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
2323
| ^ cannot perform const operation using `I`
@@ -26,7 +26,7 @@ LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
2626
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
2727

2828
error: generic parameters may not be used in const operations
29-
--> $DIR/issue-72787.rs:26:36
29+
--> $DIR/issue-72787.rs:25:36
3030
|
3131
LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
3232
| ^ cannot perform const operation using `J`
@@ -35,29 +35,21 @@ LL | IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
3535
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
3636

3737
error[E0283]: type annotations needed
38-
--> $DIR/issue-72787.rs:10:38
39-
|
40-
LL | impl<const LHS: u32, const RHS: u32> True for IsLessOrEqual<LHS, RHS> where
41-
| ^^^^ cannot infer type for struct `IsLessOrEqual<LHS, RHS>`
42-
|
43-
= note: cannot satisfy `IsLessOrEqual<LHS, RHS>: True`
44-
45-
error[E0283]: type annotations needed
46-
--> $DIR/issue-72787.rs:22:26
38+
--> $DIR/issue-72787.rs:21:26
4739
|
4840
LL | IsLessOrEqual<I, 8>: True,
4941
| ^^^^ cannot infer type for struct `IsLessOrEqual<I, 8_u32>`
5042
|
5143
= note: cannot satisfy `IsLessOrEqual<I, 8_u32>: True`
5244

5345
error[E0283]: type annotations needed
54-
--> $DIR/issue-72787.rs:22:26
46+
--> $DIR/issue-72787.rs:21:26
5547
|
5648
LL | IsLessOrEqual<I, 8>: True,
5749
| ^^^^ cannot infer type for struct `IsLessOrEqual<I, 8_u32>`
5850
|
5951
= note: cannot satisfy `IsLessOrEqual<I, 8_u32>: True`
6052

61-
error: aborting due to 7 previous errors
53+
error: aborting due to 6 previous errors
6254

6355
For more information about this error, try `rustc --explain E0283`.

src/test/ui/const-generics/generic_const_exprs/issue-72787.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub struct Condition<const CONDITION: bool>;
88
pub trait True {}
99

1010
impl<const LHS: u32, const RHS: u32> True for IsLessOrEqual<LHS, RHS> where
11-
//[min]~^ ERROR type annotations needed
1211
Condition<{ LHS <= RHS }>: True
1312
//[min]~^ Error generic parameters may not be used in const operations
1413
//[min]~| Error generic parameters may not be used in const operations

src/test/ui/issues/issue-77919.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ struct Multiply<N, M> {
1010
}
1111
impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
1212
//~^ ERROR cannot find type `VAL` in this scope
13-
//~| ERROR type annotations needed
13+
//~| ERROR not all trait items implemented, missing: `VAL`

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
1717
| |
1818
| help: you might be missing a type parameter: `, VAL`
1919

20-
error[E0283]: type annotations needed
21-
--> $DIR/issue-77919.rs:11:12
20+
error[E0046]: not all trait items implemented, missing: `VAL`
21+
--> $DIR/issue-77919.rs:11:1
2222
|
23+
LL | const VAL: T;
24+
| ------------- `VAL` from trait
25+
...
2326
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
24-
| ^^^^^^^^^^^^^^ cannot infer type for struct `Multiply<N, M>`
25-
|
26-
= note: cannot satisfy `Multiply<N, M>: TypeVal<usize>`
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
2728

2829
error: aborting due to 3 previous errors
2930

30-
Some errors have detailed explanations: E0283, E0412.
31-
For more information about an error, try `rustc --explain E0283`.
31+
Some errors have detailed explanations: E0046, E0412.
32+
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)