Skip to content

Commit ac85a4d

Browse files
authored
Rollup merge of #76559 - lcnr:const-evaluatable, r=oli-obk
add the `const_evaluatable_checked` feature Implements a rather small subset of rust-lang/compiler-team#340 Unlike the MCP, this does not try to compare different constant, but instead only adds the constants found in where clauses to the predicates of a function. This PR adds the feature gate `const_evaluatable_checked`, without which nothing should change. r? @oli-obk @eddyb
2 parents f9df658 + 300b0ac commit ac85a4d

File tree

9 files changed

+137
-17
lines changed

9 files changed

+137
-17
lines changed

compiler/rustc_feature/src/active.rs

+5-1
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(76560), 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

+1
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

+32-15
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,23 @@ 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+
debug!(?concrete, "is_const_evaluatable");
6077
concrete.map(drop)
6178
}

compiler/rustc_typeck/src/collect.rs

+40-1
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,46 @@ 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+
result.predicates =
1682+
tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(const_evaluatable));
1683+
}
1684+
16751685
debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
16761686
result
16771687
}
16781688

1689+
pub fn const_evaluatable_predicates_of<'tcx>(
1690+
tcx: TyCtxt<'tcx>,
1691+
def_id: DefId,
1692+
predicates: &ty::GenericPredicates<'tcx>,
1693+
) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> {
1694+
#[derive(Default)]
1695+
struct ConstCollector<'tcx> {
1696+
ct: SmallVec<[(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>); 4]>,
1697+
}
1698+
1699+
impl<'tcx> TypeVisitor<'tcx> for ConstCollector<'tcx> {
1700+
fn visit_const(&mut self, ct: &'tcx Const<'tcx>) -> bool {
1701+
if let ty::ConstKind::Unevaluated(def, substs, None) = ct.val {
1702+
self.ct.push((def, substs));
1703+
}
1704+
false
1705+
}
1706+
}
1707+
1708+
let mut collector = ConstCollector::default();
1709+
for (pred, _span) in predicates.predicates.iter() {
1710+
pred.visit_with(&mut collector);
1711+
}
1712+
warn!("const_evaluatable_predicates_of({:?}) = {:?}", def_id, collector.ct);
1713+
collector.ct.into_iter().map(move |(def_id, subst)| {
1714+
(ty::PredicateAtom::ConstEvaluatable(def_id, subst).to_predicate(tcx), DUMMY_SP)
1715+
})
1716+
}
1717+
16791718
/// Returns a list of all type predicates (explicit and implicit) for the definition with
16801719
/// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
16811720
/// `Self: Trait` predicates for traits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(const_generics)]
2+
#![allow(incomplete_features)]
3+
4+
type Arr<const N: usize> = [u8; N - 1];
5+
6+
fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
7+
//~^ ERROR constant expression depends
8+
Default::default()
9+
}
10+
11+
fn main() {
12+
let x = test::<33>();
13+
assert_eq!(x, [0; 32]);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/feature-gate-const_evaluatable_checked.rs:6:30
3+
|
4+
LL | fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
5+
| ^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
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+
}
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+
}
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)