Skip to content

Commit 6dad074

Browse files
committed
Handle infer vars in anon consts on stable
1 parent 6503543 commit 6dad074

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

compiler/rustc_middle/src/mir/consts.rs

+3
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ impl<'tcx> Const<'tcx> {
325325

326326
match c.kind() {
327327
ConstKind::Value(ty, val) => Ok(tcx.valtree_to_const_val((ty, val))),
328+
ConstKind::Expr(_) => {
329+
bug!("Normalization of `ty::ConstKind::Expr` is unimplemented")
330+
}
328331
_ => Err(tcx.dcx().delayed_bug("Unevaluated `ty::Const` in MIR body").into()),
329332
}
330333
}

compiler/rustc_trait_selection/src/traits/mod.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -578,16 +578,28 @@ pub fn try_evaluate_const<'tcx>(
578578
(args, param_env)
579579
}
580580
}
581-
} else {
582-
// FIXME: We don't check anything on stable as the only way we can wind up with
583-
// an unevaluated constant containing generic parameters is through array repeat
584-
// expression counts which have a future compat lint for usage of generic parameters
585-
// instead of a hard error.
581+
} else if tcx.def_kind(uv.def) == DefKind::AnonConst && uv.has_non_region_infer() {
582+
// FIXME: remove this when `const_evaluatable_unchecked` is a hard error.
583+
//
584+
// Diagnostics will sometimes replace the identity args of anon consts in
585+
// array repeat expr counts with inference variables so we have to handle this
586+
// even though it is not something we should ever actually encounter.
586587
//
587-
// This codepath is however also reachable by `generic_const_exprs` and some other
588-
// feature gates which allow constants in the type system to use generic parameters.
589-
// In theory we should be checking for generic parameters here and returning an error
590-
// in such cases.
588+
// Array repeat expr counts are allowed to syntactically use generic parameters
589+
// but must not actually depend on them in order to evalaute succesfully. This means
590+
// that it is actually fine to evalaute them in their own environment rather than with
591+
// the actually provided generic arguments.
592+
tcx.dcx().delayed_bug(
593+
"Encountered anon const with inference variable args but no error reported",
594+
);
595+
596+
let args = GenericArgs::identity_for_item(tcx, uv.def);
597+
let param_env = tcx.param_env(uv.def);
598+
(args, param_env)
599+
} else {
600+
// FIXME: This codepath is reachable under `associated_const_equality` and in the
601+
// future will be reachable by `min_generic_const_args`. We should handle inference
602+
// variables and generic parameters properly instead of doing nothing.
591603
(uv.args, param_env)
592604
};
593605
let uv = ty::UnevaluatedConst::new(uv.def, args);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for #132955 checking that we handle anon consts with
2+
// inference variables in their generic arguments correctly.
3+
//
4+
// This arose via diagnostics where we would have some failing goal such
5+
// as `[u8; AnonConst<Self>]: PartialEq<Self::A>`, then as part of diagnostics
6+
// we would replace all generic parameters with inference vars which would yield
7+
// a self type of `[u8; AnonConst<?x>]` and then attempt to normalize `AnonConst<?x>`.
8+
9+
pub trait T {
10+
type A;
11+
const P: Self::A;
12+
13+
fn a() {
14+
[0u8; std::mem::size_of::<Self::A>()] == Self::P;
15+
//~^ ERROR: can't compare
16+
//~| ERROR: constant expression depends on a generic parameter
17+
//~| ERROR: constant expression depends on a generic parameter
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/failing_goal_with_repeat_expr_anon_const.rs:14:15
3+
|
4+
LL | [0u8; std::mem::size_of::<Self::A>()] == Self::P;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: constant expression depends on a generic parameter
10+
--> $DIR/failing_goal_with_repeat_expr_anon_const.rs:14:47
11+
|
12+
LL | [0u8; std::mem::size_of::<Self::A>()] == Self::P;
13+
| ^^
14+
|
15+
= note: this may fail depending on what value the parameter takes
16+
17+
error[E0277]: can't compare `[u8; std::mem::size_of::<Self::A>()]` with `<Self as T>::A`
18+
--> $DIR/failing_goal_with_repeat_expr_anon_const.rs:14:47
19+
|
20+
LL | [0u8; std::mem::size_of::<Self::A>()] == Self::P;
21+
| ^^ no implementation for `[u8; std::mem::size_of::<Self::A>()] == <Self as T>::A`
22+
|
23+
= help: the trait `PartialEq<<Self as T>::A>` is not implemented for `[u8; std::mem::size_of::<Self::A>()]`
24+
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
25+
|
26+
LL | pub trait T where [u8; std::mem::size_of::<Self::A>()]: PartialEq<<Self as T>::A> {
27+
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28+
29+
error: aborting due to 3 previous errors
30+
31+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)