Skip to content

Commit 2270bde

Browse files
authored
Rollup merge of #109495 - compiler-errors:new-solver-destruct, r=eholk,lcnr
Implement non-const `Destruct` trait in new solver Makes it so that we can call stdlib methods like `Option::map` in **non-const** environments, since *many* stdlib methods have `Destruct` bounds 😅 This doesn't bother to implement `const Destruct` yet, but it shouldn't be too hard to do so. Just didn't bother since we already don't have much support for const traits in the new solver anyways. I'd be happy to add skeleton support for `const Destruct`, though, if the reviewer desires.
2 parents 98254c5 + 8390c61 commit 2270bde

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<TyCtxt<'tcx>> + Copy + Eq {
212212
ecx: &mut EvalCtxt<'_, 'tcx>,
213213
goal: Goal<'tcx, Self>,
214214
) -> QueryResult<'tcx>;
215+
216+
fn consider_builtin_destruct_candidate(
217+
ecx: &mut EvalCtxt<'_, 'tcx>,
218+
goal: Goal<'tcx, Self>,
219+
) -> QueryResult<'tcx>;
215220
}
216221

217222
impl<'tcx> EvalCtxt<'_, 'tcx> {
@@ -340,6 +345,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
340345
G::consider_builtin_unsize_candidate(self, goal)
341346
} else if lang_items.discriminant_kind_trait() == Some(trait_def_id) {
342347
G::consider_builtin_discriminant_kind_candidate(self, goal)
348+
} else if lang_items.destruct_trait() == Some(trait_def_id) {
349+
G::consider_builtin_destruct_candidate(self, goal)
343350
} else {
344351
Err(NoSolution)
345352
};

compiler/rustc_trait_selection/src/solve/project_goals.rs

+7
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
487487
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
488488
})
489489
}
490+
491+
fn consider_builtin_destruct_candidate(
492+
_ecx: &mut EvalCtxt<'_, 'tcx>,
493+
goal: Goal<'tcx, Self>,
494+
) -> QueryResult<'tcx> {
495+
bug!("`Destruct` does not have an associated type: {:?}", goal);
496+
}
490497
}
491498

492499
/// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code.

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+14
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,20 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
534534
// `DiscriminantKind` is automatically implemented for every type.
535535
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
536536
}
537+
538+
fn consider_builtin_destruct_candidate(
539+
ecx: &mut EvalCtxt<'_, 'tcx>,
540+
goal: Goal<'tcx, Self>,
541+
) -> QueryResult<'tcx> {
542+
if !goal.param_env.is_const() {
543+
// `Destruct` is automatically implemented for every type in
544+
// non-const environments.
545+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
546+
} else {
547+
// FIXME(-Ztrait-solver=next): Implement this when we get const working in the new solver
548+
Err(NoSolution)
549+
}
550+
}
537551
}
538552

539553
impl<'tcx> EvalCtxt<'_, 'tcx> {

tests/ui/traits/new-solver/canonical-int-var-eq-in-response.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: -Ztrait-solver=next
12
// check-pass
23

34
trait Mirror {
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile-flags: -Ztrait-solver=next
2+
// check-pass
3+
4+
#![feature(const_trait_impl)]
5+
6+
fn foo(_: impl std::marker::Destruct) {}
7+
8+
struct MyAdt;
9+
10+
fn main() {
11+
foo(1);
12+
foo(MyAdt);
13+
}

0 commit comments

Comments
 (0)