Skip to content

Commit

Permalink
Check drop is trivial before checking ty needs drop
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 22, 2024
1 parent af0d566 commit 69a38de
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 180 deletions.
7 changes: 6 additions & 1 deletion compiler/rustc_const_eval/src/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ impl Qualif for NeedsNonConstDrop {
return false;
}

// If this doesn't need drop at all, then don't select `~const Destruct`.
if !ty.needs_drop(cx.tcx, cx.typing_env) {
return false;
}

// We check that the type is `~const Destruct` since that will verify that
// the type is both `~const Drop` (if a drop impl exists for the adt), *and*
// that the components of this type are also `~const Destruct`. This
Expand Down Expand Up @@ -203,7 +208,7 @@ impl Qualif for NeedsNonConstDrop {
// in its value since:
// 1. The destructor may have `~const` bounds which are not present on the type.
// Someone needs to check that those are satisfied.
// While this could be done instead satisfied by checking that the `~const Drop`
// While this could be instead satisfied by checking that the `~const Drop`
// impl holds (i.e. replicating part of the `in_any_value_of_ty` logic above),
// even in this case, we have another problem, which is,
// 2. The destructor may *modify* the operand being dropped, so even if we
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ declare_features! (
(unstable, const_async_blocks, "1.53.0", Some(85368)),
/// Allows `const || {}` closures in const contexts.
(incomplete, const_closures, "1.68.0", Some(106003)),
/// Uwu
/// Allows using `~const Destruct` bounds and calling drop impls in const contexts.
(unstable, const_destruct, "CURRENT_RUSTC_VERSION", Some(133214)),
/// Allows `for _ in _` loops in const contexts.
(unstable, const_for, "1.56.0", Some(87575)),
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

pub mod tls;

use std::assert_matches::assert_matches;
use std::assert_matches::{assert_matches, debug_assert_matches};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -377,14 +377,17 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
}

fn impl_is_const(self, def_id: DefId) -> bool {
debug_assert_matches!(self.def_kind(def_id), DefKind::Impl { of_trait: true });
self.is_conditionally_const(def_id)
}

fn fn_is_const(self, def_id: DefId) -> bool {
debug_assert_matches!(self.def_kind(def_id), DefKind::Fn | DefKind::AssocFn);
self.is_conditionally_const(def_id)
}

fn alias_has_const_conditions(self, def_id: DefId) -> bool {
debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::OpaqueTy);
self.is_conditionally_const(def_id)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const-block-const-bound.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ known-bug: #103507

#![allow(unused)]
#![feature(const_trait_impl, negative_impls)]
#![feature(const_trait_impl, negative_impls, const_destruct)]

use std::marker::Destruct;

Expand Down
25 changes: 2 additions & 23 deletions tests/ui/consts/const-block-const-bound.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
error[E0658]: use of unstable library feature `const_destruct`
--> $DIR/const-block-const-bound.rs:6:5
|
LL | use std::marker::Destruct;
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature `const_destruct`
--> $DIR/const-block-const-bound.rs:8:22
|
LL | const fn f<T: ~const Destruct>(x: T) {}
| ^^^^^^^^
|
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/const-block-const-bound.rs:8:15
|
Expand All @@ -40,7 +20,6 @@ LL | const fn f<T: ~const Destruct>(x: T) {}
| |
| the destructor for this type cannot be evaluated in constant functions

error: aborting due to 5 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0493, E0658.
For more information about an error, try `rustc --explain E0493`.
For more information about this error, try `rustc --explain E0493`.
1 change: 1 addition & 0 deletions tests/ui/consts/fn_trait_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(unboxed_closures)]
#![feature(const_trait_impl)]
#![feature(const_cmp)]
#![feature(const_destruct)]

use std::marker::Destruct;

Expand Down
Loading

0 comments on commit 69a38de

Please sign in to comment.