Skip to content

Commit 92b334a

Browse files
committed
constify const Fn*: Destruct
1 parent 620e36a commit 92b334a

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,12 @@ pub(in crate::solve) fn const_conditions_for_destruct<I: Interner>(
797797
| ty::Infer(ty::InferTy::FloatVar(_) | ty::InferTy::IntVar(_))
798798
| ty::Error(_) => Ok(vec![]),
799799

800-
// Coroutines and closures could implement `[const] Drop`,
800+
// Closures are [const] Destruct when all of their upvars (captures) are [const] Destruct.
801+
ty::Closure(def, args) if cx.closure_is_const(def) => {
802+
let closure_args = args.as_closure();
803+
Ok(vec![ty::TraitRef::new(cx, destruct_def_id, [closure_args.tupled_upvars_ty()])])
804+
}
805+
// Coroutines could implement `[const] Drop`,
801806
// but they don't really need to right now.
802807
ty::Closure(_, _)
803808
| ty::CoroutineClosure(_, _)

compiler/rustc_trait_selection/src/traits/effects.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,17 @@ fn evaluate_host_effect_for_destruct_goal<'tcx>(
472472
| ty::Infer(ty::InferTy::FloatVar(_) | ty::InferTy::IntVar(_))
473473
| ty::Error(_) => thin_vec![],
474474

475-
// Coroutines and closures could implement `[const] Drop`,
475+
// Closures are [const] Destruct when all of their upvars (captures) are [const] Destruct.
476+
ty::Closure(_, args) => {
477+
let closure_args = args.as_closure();
478+
thin_vec![ty::TraitRef::new(tcx, destruct_def_id, [closure_args.tupled_upvars_ty()])]
479+
}
480+
481+
// Coroutines could implement `[const] Drop`,
476482
// but they don't really need to right now.
477-
ty::Closure(_, _)
478-
| ty::CoroutineClosure(_, _)
479-
| ty::Coroutine(_, _)
480-
| ty::CoroutineWitness(_, _) => return Err(EvaluationFailure::NoSolution),
483+
ty::CoroutineClosure(_, _) | ty::Coroutine(_, _) | ty::CoroutineWitness(_, _) => {
484+
return Err(EvaluationFailure::NoSolution);
485+
}
481486

482487
// FIXME(unsafe_binders): Unsafe binders could implement `[const] Drop`
483488
// if their inner type implements it.

library/coretests/tests/array.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,9 @@ fn const_array_ops() {
741741
struct Zst;
742742
assert_eq!([(); 10].try_map(|()| Some(Zst)), Some([const { Zst }; 10]));
743743
}
744+
745+
#[test]
746+
fn extra_const_array_ops() {
747+
let x: [u8; 4] =
748+
const { std::array::from_fn(const |i| i + 4).map(const |x| x * 2).map(const |x| x as _) };
749+
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![feature(const_bool)]
1818
#![feature(const_cell_traits)]
1919
#![feature(const_clone)]
20+
#![feature(const_closures)]
2021
#![feature(const_cmp)]
2122
#![feature(const_convert)]
2223
#![feature(const_default)]

0 commit comments

Comments
 (0)