Skip to content

Commit e0c2ff7

Browse files
committed
Allow more cases to match ~const Drop.
1 parent 3b25e92 commit e0c2ff7

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

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

+48-32
Original file line numberDiff line numberDiff line change
@@ -284,32 +284,32 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
284284
// structs and enums.
285285
self.assemble_candidates_from_impls(obligation, &mut candidates);
286286

287-
// For other types, we'll use the builtin rules.
288-
let copy_conditions = self.copy_clone_conditions(obligation);
289-
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates);
290-
} else if lang_items.discriminant_kind_trait() == Some(def_id) {
291-
// `DiscriminantKind` is automatically implemented for every type.
292-
candidates.vec.push(DiscriminantKindCandidate);
293-
} else if lang_items.pointee_trait() == Some(def_id) {
294-
// `Pointee` is automatically implemented for every type.
295-
candidates.vec.push(PointeeCandidate);
296-
} else if lang_items.sized_trait() == Some(def_id) {
297-
// Sized is never implementable by end-users, it is
298-
// always automatically computed.
299-
let sized_conditions = self.sized_conditions(obligation);
300-
self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates);
301-
} else if lang_items.unsize_trait() == Some(def_id) {
302-
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
303-
} else if lang_items.drop_trait() == Some(def_id)
304-
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::ConstIfConst
305-
{
306-
if self.is_in_const_context {
307-
self.assemble_const_drop_candidates(obligation, &mut candidates)?;
308-
} else {
309-
debug!("passing ~const Drop bound; in non-const context");
310-
// `~const Drop` when we are not in a const context has no effect.
311-
candidates.vec.push(ConstDropCandidate)
312-
}
287+
// For other types, we'll use the builtin rules.
288+
let copy_conditions = self.copy_clone_conditions(obligation);
289+
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates);
290+
} else if lang_items.discriminant_kind_trait() == Some(def_id) {
291+
// `DiscriminantKind` is automatically implemented for every type.
292+
candidates.vec.push(DiscriminantKindCandidate);
293+
} else if lang_items.pointee_trait() == Some(def_id) {
294+
// `Pointee` is automatically implemented for every type.
295+
candidates.vec.push(PointeeCandidate);
296+
} else if lang_items.sized_trait() == Some(def_id) {
297+
// Sized is never implementable by end-users, it is
298+
// always automatically computed.
299+
let sized_conditions = self.sized_conditions(obligation);
300+
self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates);
301+
} else if lang_items.unsize_trait() == Some(def_id) {
302+
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
303+
} else if lang_items.drop_trait() == Some(def_id)
304+
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::ConstIfConst
305+
{
306+
if self.is_in_const_context {
307+
self.assemble_const_drop_candidates(obligation, stack, &mut candidates)?;
308+
} else {
309+
debug!("passing ~const Drop bound; in non-const context");
310+
// `~const Drop` when we are not in a const context has no effect.
311+
candidates.vec.push(ConstDropCandidate)
312+
}
313313
} else {
314314
if lang_items.clone_trait() == Some(def_id) {
315315
// Same builtin conditions as `Copy`, i.e., every type which has builtin support
@@ -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,28 @@ 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+
}));
951+
952+
let const_drop_stack = self.push_stack(obligation_stack.list(), &const_drop_obligation);
953+
self.assemble_candidates_from_caller_bounds(&const_drop_stack, &mut new_candidates)?;
954+
955+
if !new_candidates.vec.is_empty() {
940956
noreturn = true;
941957
}
942-
debug!(?copy_candidates.vec, "assemble_const_drop_candidates - copy");
958+
debug!(?new_candidates.vec, "assemble_const_drop_candidates");
943959

944960
match ty.kind() {
945961
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() {}

0 commit comments

Comments
 (0)