Skip to content

Commit fa92ce8

Browse files
authored
Rollup merge of rust-lang#99449 - compiler-errors:assoc-const-missing-item, r=lcnr
Do not resolve associated const when there is no provided value Fixes rust-lang#98629, since now we just delay a bug when we're not able to evaluate a const item due to the value not actually being provided by anything. This means compilation proceeds forward to where the "missing item in impl" error is emitted. ---- The root issue here is that when we're looking for the defining `LeafDef` in `resolve_associated_item`, we end up getting the trait's AssocItem instead of the impl's AssocItem (which does not exist). This resolution "succeeds" even if the trait's item has no default value, and then since this item has no value to evaluate, it turns into a const eval error. This root issue becomes problematic (as in rust-lang#98629) when this const eval error happens in wfcheck (for example, due to normalizing the param-env of something that references this const). Since this happens sooner than the check that an impl actually provides all of the items that a trait requires (which happens during later typecheck), we end up aborting compilation early with only this un-informative message. I'm not exactly sure _why_ this bug arises due to rust-lang#96591 -- perhaps valtrees are evaluated more eagerly than in the old system? r? ``@oli-obk`` or ``@lcnr`` since y'all are familiar with const eval and reviewed rust-lang#96591, though feel free to reassign. This is a regression from stable to beta, so I would be open to considering this for beta backport. It seems correct to me, especially given the improvements in the other UI tests this PR touches, but may have some side-effects that I'm unaware of...?
2 parents 2c0cb8f + c8165c5 commit fa92ce8

File tree

8 files changed

+49
-34
lines changed

8 files changed

+49
-34
lines changed

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,20 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
185185
}
186186
let concrete = infcx.const_eval_resolve(param_env, uv.expand(), Some(span));
187187
match concrete {
188-
Err(ErrorHandled::TooGeneric) => Err(if !uv.has_infer_types_or_consts() {
188+
Err(ErrorHandled::TooGeneric) => Err(if uv.has_infer_types_or_consts() {
189+
NotConstEvaluatable::MentionsInfer
190+
} else if uv.has_param_types_or_consts() {
189191
infcx
190192
.tcx
191193
.sess
192194
.delay_span_bug(span, &format!("unexpected `TooGeneric` for {:?}", uv));
193195
NotConstEvaluatable::MentionsParam
194196
} else {
195-
NotConstEvaluatable::MentionsInfer
197+
let guar = infcx.tcx.sess.delay_span_bug(
198+
span,
199+
format!("Missing value for constant, but no error reported?"),
200+
);
201+
NotConstEvaluatable::Error(guar)
196202
}),
197203
Err(ErrorHandled::Linted) => {
198204
let reported = infcx
@@ -240,8 +246,11 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
240246

241247
Err(ErrorHandled::TooGeneric) => Err(if uv.has_infer_types_or_consts() {
242248
NotConstEvaluatable::MentionsInfer
243-
} else {
249+
} else if uv.has_param_types_or_consts() {
244250
NotConstEvaluatable::MentionsParam
251+
} else {
252+
let guar = infcx.tcx.sess.delay_span_bug(span, format!("Missing value for constant, but no error reported?"));
253+
NotConstEvaluatable::Error(guar)
245254
}),
246255
Err(ErrorHandled::Linted) => {
247256
let reported =

compiler/rustc_ty_utils/src/instance.rs

+5
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ fn resolve_associated_item<'tcx>(
280280
return Ok(None);
281281
}
282282

283+
// If the item does not have a value, then we cannot return an instance.
284+
if !leaf_def.item.defaultness.has_value() {
285+
return Ok(None);
286+
}
287+
283288
let substs = tcx.erase_regions(substs);
284289

285290
// Check if we just resolved an associated `const` declaration from

src/test/ui/const-generics/issues/issue-86530.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ where
1515
fn unit_literals() {
1616
z(" ");
1717
//~^ ERROR: the trait bound `&str: X` is not satisfied
18-
//~| ERROR: unconstrained generic constant
1918
}
2019

2120
fn main() {}

src/test/ui/const-generics/issues/issue-86530.stderr

+1-17
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ LL | where
1515
LL | T: X,
1616
| ^ required by this bound in `z`
1717

18-
error: unconstrained generic constant
19-
--> $DIR/issue-86530.rs:16:5
20-
|
21-
LL | z(" ");
22-
| ^
23-
|
24-
= help: try adding a `where` bound using this expression: `where [(); T::Y]:`
25-
note: required by a bound in `z`
26-
--> $DIR/issue-86530.rs:11:10
27-
|
28-
LL | fn z<T>(t: T)
29-
| - required by a bound in this
30-
...
31-
LL | [(); T::Y]: ,
32-
| ^^^^ required by this bound in `z`
33-
34-
error: aborting due to 2 previous errors
18+
error: aborting due to previous error
3519

3620
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(const_trait_impl)]
2+
3+
trait Trait {
4+
const N: usize;
5+
}
6+
7+
impl const Trait for i32 {}
8+
//~^ ERROR not all trait items implemented, missing: `N`
9+
10+
fn f()
11+
where
12+
[(); <i32 as Trait>::N]:,
13+
{}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0046]: not all trait items implemented, missing: `N`
2+
--> $DIR/issue-98629.rs:7:1
3+
|
4+
LL | const N: usize;
5+
| -------------- `N` from trait
6+
...
7+
LL | impl const Trait for i32 {}
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^ missing `N` in implementation
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0046`.

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fn main() {
22
[1; <Multiply<Five, Five>>::VAL];
3-
//~^ ERROR: constant expression depends on a generic parameter
43
}
54
trait TypeVal<T> {
65
const VAL: T;

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

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0412]: cannot find type `PhantomData` in this scope
2-
--> $DIR/issue-77919.rs:10:9
2+
--> $DIR/issue-77919.rs:9:9
33
|
44
LL | _n: PhantomData,
55
| ^^^^^^^^^^^ not found in this scope
@@ -10,31 +10,23 @@ LL | use std::marker::PhantomData;
1010
|
1111

1212
error[E0412]: cannot find type `VAL` in this scope
13-
--> $DIR/issue-77919.rs:12:63
13+
--> $DIR/issue-77919.rs:11:63
1414
|
1515
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
1616
| - ^^^ not found in this scope
1717
| |
1818
| help: you might be missing a type parameter: `, VAL`
1919

2020
error[E0046]: not all trait items implemented, missing: `VAL`
21-
--> $DIR/issue-77919.rs:12:1
21+
--> $DIR/issue-77919.rs:11:1
2222
|
2323
LL | const VAL: T;
2424
| ------------ `VAL` from trait
2525
...
2626
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
2828

29-
error: constant expression depends on a generic parameter
30-
--> $DIR/issue-77919.rs:2:9
31-
|
32-
LL | [1; <Multiply<Five, Five>>::VAL];
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
34-
|
35-
= note: this may fail depending on what value the parameter takes
36-
37-
error: aborting due to 4 previous errors
29+
error: aborting due to 3 previous errors
3830

3931
Some errors have detailed explanations: E0046, E0412.
4032
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)