Skip to content

Commit 8667f93

Browse files
committed
implement const_evaluatable_checked feature MVP
1 parent a1894e4 commit 8667f93

File tree

7 files changed

+122
-17
lines changed

7 files changed

+122
-17
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ declare_features! (
585585
/// Allows `if let` guard in match arms.
586586
(active, if_let_guard, "1.47.0", Some(51114), None),
587587

588+
/// Allows non trivial generic constants which have to be manually propageted upwards.
589+
(active, const_evaluatable_checked, "1.48.0", Some(0), None),
590+
588591
// -------------------------------------------------------------------------
589592
// feature-group-end: actual feature gates
590593
// -------------------------------------------------------------------------
@@ -600,13 +603,14 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
600603
sym::const_generics,
601604
sym::let_chains,
602605
sym::raw_dylib,
606+
sym::const_evaluatable_checked,
603607
sym::const_trait_impl,
604608
sym::const_trait_bound_opt_out,
605609
sym::lazy_normalization_consts,
606610
sym::specialization,
607611
];
608612

609613
/// Some features are not allowed to be used together at the same time, if
610-
/// the two are present, produce an error
614+
/// the two are present, produce an error.
611615
pub const INCOMPATIBLE_FEATURES: &[(Symbol, Symbol)] =
612616
&[(sym::const_generics, sym::min_const_generics)];

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ symbols! {
348348
const_compare_raw_pointers,
349349
const_constructor,
350350
const_eval_limit,
351+
const_evaluatable_checked,
351352
const_extern_fn,
352353
const_fn,
353354
const_fn_transmute,

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
1414
param_env: ty::ParamEnv<'tcx>,
1515
span: Span,
1616
) -> Result<(), ErrorHandled> {
17+
debug!("is_const_evaluatable({:?}, {:?})", def, substs);
18+
if infcx.tcx.features().const_evaluatable_checked {
19+
// FIXME(const_evaluatable_checked): Actually look into generic constants to
20+
// implement const equality.
21+
for pred in param_env.caller_bounds() {
22+
match pred.skip_binders() {
23+
ty::PredicateAtom::ConstEvaluatable(b_def, b_substs) => {
24+
debug!("is_const_evaluatable: caller_bound={:?}, {:?}", b_def, b_substs);
25+
if b_def == def && b_substs == substs {
26+
debug!("is_const_evaluatable: caller_bound ~~> ok");
27+
return Ok(());
28+
}
29+
}
30+
_ => {} // don't care
31+
}
32+
}
33+
}
34+
1735
let future_compat_lint = || {
1836
if let Some(local_def_id) = def.did.as_local() {
1937
infcx.tcx.struct_span_lint_hir(
@@ -38,24 +56,27 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
3856
// See #74595 for more details about this.
3957
let concrete = infcx.const_eval_resolve(param_env, def, substs, None, Some(span));
4058

41-
let def_kind = infcx.tcx.def_kind(def.did);
42-
match def_kind {
43-
DefKind::AnonConst => {
44-
let mir_body = if let Some(def) = def.as_const_arg() {
45-
infcx.tcx.optimized_mir_of_const_arg(def)
46-
} else {
47-
infcx.tcx.optimized_mir(def.did)
48-
};
49-
if mir_body.is_polymorphic && concrete.is_ok() {
50-
future_compat_lint();
51-
}
52-
}
53-
_ => {
54-
if substs.has_param_types_or_consts() && concrete.is_ok() {
55-
future_compat_lint();
59+
if concrete.is_ok() && substs.has_param_types_or_consts() {
60+
match infcx.tcx.def_kind(def.did) {
61+
DefKind::AnonConst => {
62+
let mir_body = if let Some(def) = def.as_const_arg() {
63+
infcx.tcx.optimized_mir_of_const_arg(def)
64+
} else {
65+
infcx.tcx.optimized_mir(def.did)
66+
};
67+
68+
if mir_body.is_polymorphic {
69+
future_compat_lint();
70+
}
5671
}
72+
_ => future_compat_lint(),
5773
}
5874
}
5975

76+
if concrete.is_ok() {
77+
debug!("is_const_evaluatable: concrete ~~> ok");
78+
} else {
79+
debug!("is_const_evaluatable: concrete ~~> err");
80+
}
6081
concrete.map(drop)
6182
}

compiler/rustc_typeck/src/collect.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ use rustc_middle::hir::map::Map;
3737
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
3838
use rustc_middle::mir::mono::Linkage;
3939
use rustc_middle::ty::query::Providers;
40-
use rustc_middle::ty::subst::InternalSubsts;
40+
use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
4141
use rustc_middle::ty::util::Discr;
4242
use rustc_middle::ty::util::IntTypeExt;
4343
use rustc_middle::ty::{self, AdtKind, Const, ToPolyTraitRef, Ty, TyCtxt};
4444
use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness};
45+
use rustc_middle::ty::{TypeFoldable, TypeVisitor};
4546
use rustc_session::config::SanitizerSet;
4647
use rustc_session::lint;
4748
use rustc_session::parse::feature_err;
@@ -50,6 +51,8 @@ use rustc_span::{Span, DUMMY_SP};
5051
use rustc_target::spec::abi;
5152
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
5253

54+
use smallvec::SmallVec;
55+
5356
mod type_of;
5457

5558
struct OnlySelfBounds(bool);
@@ -1672,10 +1675,51 @@ fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicate
16721675
.alloc_from_iter(result.predicates.iter().chain(inferred_outlives).copied());
16731676
}
16741677
}
1678+
1679+
if tcx.features().const_evaluatable_checked {
1680+
let const_evaluatable = const_evaluatable_predicates_of(tcx, def_id, &result);
1681+
if result.predicates.is_empty() {
1682+
result.predicates = tcx.arena.alloc_from_iter(const_evaluatable);
1683+
} else {
1684+
result.predicates = tcx
1685+
.arena
1686+
.alloc_from_iter(result.predicates.iter().copied().chain(const_evaluatable));
1687+
}
1688+
}
1689+
16751690
debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
16761691
result
16771692
}
16781693

1694+
pub fn const_evaluatable_predicates_of<'tcx>(
1695+
tcx: TyCtxt<'tcx>,
1696+
def_id: DefId,
1697+
predicates: &ty::GenericPredicates<'tcx>,
1698+
) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> {
1699+
#[derive(Default)]
1700+
struct ConstCollector<'tcx> {
1701+
ct: SmallVec<[(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>); 4]>,
1702+
}
1703+
1704+
impl<'tcx> TypeVisitor<'tcx> for ConstCollector<'tcx> {
1705+
fn visit_const(&mut self, ct: &'tcx Const<'tcx>) -> bool {
1706+
if let ty::ConstKind::Unevaluated(def, substs, None) = ct.val {
1707+
self.ct.push((def, substs));
1708+
}
1709+
false
1710+
}
1711+
}
1712+
1713+
let mut collector = ConstCollector::default();
1714+
for (pred, _span) in predicates.predicates.iter() {
1715+
pred.visit_with(&mut collector);
1716+
}
1717+
warn!("const_evaluatable_predicates_of({:?}) = {:?}", def_id, collector.ct);
1718+
collector.ct.into_iter().map(move |(def_id, subst)| {
1719+
(ty::PredicateAtom::ConstEvaluatable(def_id, subst).to_predicate(tcx), DUMMY_SP)
1720+
})
1721+
}
1722+
16791723
/// Returns a list of all type predicates (explicit and implicit) for the definition with
16801724
/// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
16811725
/// `Self: Trait` predicates for traits.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
#![feature(const_generics, const_evaluatable_checked)]
3+
#![allow(incomplete_features)]
4+
5+
type Arr<const N: usize> = [u8; N - 1];
6+
7+
fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
8+
Default::default()
9+
}
10+
11+
fn main() {
12+
let x = test::<33>();
13+
assert_eq!(x, [0; 32]);
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(const_generics, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
type Arr<const N: usize> = [u8; N - 1]; //~ ERROR evaluation of constant
5+
6+
fn test<const N: usize>() -> Arr<N> where Arr<N>: Sized {
7+
todo!()
8+
}
9+
10+
fn main() {
11+
test::<0>();
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/simple_fail.rs:4:33
3+
|
4+
LL | type Arr<const N: usize> = [u8; N - 1];
5+
| ^^^^^ attempt to compute `0_usize - 1_usize` which would overflow
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)