Skip to content

Commit 90dd7c0

Browse files
authored
Rollup merge of #89359 - fee1-dead:const-it, r=oli-obk
Various fixes for const_trait_impl A few problems I found while making `Iterator` easier to const-implement. 1. More generous `~const Drop` check. We check for nested fields with caller bounds. For example, an ADT type with fields of types `A`, `B`, `C`, check if all of them are either: - Bounded (`A: ~const Drop`, `B: Copy`) - Known to be able to destruct at compile time (`C = i32`, `struct C(i32)`, `C = some_fn`) 2. Don't treat trait functions marked with `#[default_method_body_is_const]` as stable const fns when checking `const_for` and `const_try` feature gates. I think anyone can review this, so no r? this time.
2 parents 23a4366 + 4f29f3c commit 90dd7c0

File tree

5 files changed

+106
-11
lines changed

5 files changed

+106
-11
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,12 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
10041004
}
10051005

10061006
let mut err_span = self.span;
1007+
let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty;
10071008

1008-
let ty_needs_non_const_drop = qualifs::NeedsNonConstDrop::in_any_value_of_ty(
1009-
self.ccx,
1010-
dropped_place.ty(self.body, self.tcx).ty,
1011-
);
1009+
let ty_needs_non_const_drop =
1010+
qualifs::NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place);
1011+
1012+
debug!(?ty_of_dropped_place, ?ty_needs_non_const_drop);
10121013

10131014
if !ty_needs_non_const_drop {
10141015
return;

compiler/rustc_passes/src/check_const.rs

+6
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ impl<'tcx> CheckConstVisitor<'tcx> {
173173
None => return true,
174174
};
175175

176+
// If the function belongs to a trait, then it must enable the const_trait_impl
177+
// feature to use that trait function (with a const default body).
178+
if tcx.trait_of_item(def_id).is_some() {
179+
return true;
180+
}
181+
176182
// If this crate is not using stability attributes, or this function is not claiming to be a
177183
// stable `const fn`, that is all that is required.
178184
if !tcx.features().staged_api || tcx.has_attr(def_id, sym::rustc_const_unstable) {

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

+24-7
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
304304
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::ConstIfConst
305305
{
306306
if self.is_in_const_context {
307-
self.assemble_const_drop_candidates(obligation, &mut candidates)?;
307+
self.assemble_const_drop_candidates(obligation, stack, &mut candidates)?;
308308
} else {
309309
debug!("passing ~const Drop bound; in non-const context");
310310
// `~const Drop` when we are not in a const context has no effect.
@@ -911,9 +911,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
911911
}
912912
}
913913

914-
fn assemble_const_drop_candidates(
914+
fn assemble_const_drop_candidates<'a>(
915915
&mut self,
916916
obligation: &TraitObligation<'tcx>,
917+
obligation_stack: &TraitObligationStack<'a, 'tcx>,
917918
candidates: &mut SelectionCandidateSet<'tcx>,
918919
) -> Result<(), SelectionError<'tcx>> {
919920
let mut stack: Vec<(Ty<'tcx>, usize)> = vec![(obligation.self_ty().skip_binder(), 0)];
@@ -922,7 +923,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
922923
let mut noreturn = false;
923924

924925
self.check_recursion_depth(depth, obligation)?;
925-
let mut copy_candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
926+
let mut new_candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
926927
let mut copy_obligation =
927928
obligation.with(obligation.predicate.rebind(ty::TraitPredicate {
928929
trait_ref: ty::TraitRef {
@@ -933,13 +934,29 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
933934
polarity: ty::ImplPolarity::Positive,
934935
}));
935936
copy_obligation.recursion_depth = depth + 1;
936-
self.assemble_candidates_from_impls(&copy_obligation, &mut copy_candidates);
937+
self.assemble_candidates_from_impls(&copy_obligation, &mut new_candidates);
937938
let copy_conditions = self.copy_clone_conditions(&copy_obligation);
938-
self.assemble_builtin_bound_candidates(copy_conditions, &mut copy_candidates);
939-
if !copy_candidates.vec.is_empty() {
939+
self.assemble_builtin_bound_candidates(copy_conditions, &mut new_candidates);
940+
let copy_stack = self.push_stack(obligation_stack.list(), &copy_obligation);
941+
self.assemble_candidates_from_caller_bounds(&copy_stack, &mut new_candidates)?;
942+
943+
let const_drop_obligation =
944+
obligation.with(obligation.predicate.rebind(ty::TraitPredicate {
945+
trait_ref: ty::TraitRef {
946+
def_id: self.tcx().require_lang_item(hir::LangItem::Drop, None),
947+
substs: self.tcx().mk_substs_trait(ty, &[]),
948+
},
949+
constness: ty::BoundConstness::ConstIfConst,
950+
polarity: ty::ImplPolarity::Positive,
951+
}));
952+
953+
let const_drop_stack = self.push_stack(obligation_stack.list(), &const_drop_obligation);
954+
self.assemble_candidates_from_caller_bounds(&const_drop_stack, &mut new_candidates)?;
955+
956+
if !new_candidates.vec.is_empty() {
940957
noreturn = true;
941958
}
942-
debug!(?copy_candidates.vec, "assemble_const_drop_candidates - copy");
959+
debug!(?new_candidates.vec, "assemble_const_drop_candidates");
943960

944961
match ty.kind() {
945962
ty::Int(_)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-pass
2+
3+
#![feature(const_trait_impl)]
4+
#![feature(const_fn_trait_bound)]
5+
#![feature(const_precise_live_drops)]
6+
7+
const fn foo<T, E>(res: Result<T, E>) -> Option<T> where E: ~const Drop {
8+
match res {
9+
Ok(t) => Some(t),
10+
Err(_e) => None,
11+
}
12+
}
13+
14+
pub struct Foo<T>(T);
15+
16+
const fn baz<T: ~const Drop, E: ~const Drop>(res: Result<Foo<T>, Foo<E>>) -> Option<Foo<T>> {
17+
foo(res)
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// check-pass
2+
3+
#![feature(staged_api)]
4+
#![feature(const_trait_impl)]
5+
#![feature(const_fn_trait_bound)]
6+
#![feature(const_t_try)]
7+
#![feature(const_try)]
8+
#![feature(try_trait_v2)]
9+
10+
#![stable(feature = "foo", since = "1.0")]
11+
12+
use std::ops::{ControlFlow, FromResidual, Try};
13+
14+
#[stable(feature = "foo", since = "1.0")]
15+
pub struct T;
16+
17+
#[stable(feature = "foo", since = "1.0")]
18+
#[rustc_const_unstable(feature = "const_t_try", issue = "none")]
19+
impl const Try for T {
20+
type Output = T;
21+
type Residual = T;
22+
23+
fn from_output(t: T) -> T {
24+
t
25+
}
26+
27+
fn branch(self) -> ControlFlow<T, T> {
28+
ControlFlow::Continue(self)
29+
}
30+
}
31+
32+
#[stable(feature = "foo", since = "1.0")]
33+
#[rustc_const_unstable(feature = "const_t_try", issue = "none")]
34+
impl const FromResidual for T {
35+
fn from_residual(t: T) -> T {
36+
t
37+
}
38+
}
39+
40+
#[stable(feature = "foo", since = "1.0")]
41+
pub trait Tr {
42+
#[default_method_body_is_const]
43+
#[stable(feature = "foo", since = "1.0")]
44+
fn bar() -> T {
45+
T?
46+
// Should be allowed.
47+
// Must enable unstable features to call this trait fn in const contexts.
48+
}
49+
}
50+
51+
fn main() {}

0 commit comments

Comments
 (0)