Skip to content

Commit

Permalink
Foreign types are trivially drop
Browse files Browse the repository at this point in the history
- Also rename a trivial_const_drop to match style of other functions in
  the util module.
- Also add a test for `const Drop` that doesn't depend on a `~const`
  bound.
- Also comment a bit why we remove the const bound during dropck impl
  check.
  • Loading branch information
compiler-errors committed Jan 20, 2022
1 parent 8547f57 commit b7e4433
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Qualif for NeedsNonConstDrop {

fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
// Avoid selecting for simple cases, such as builtin types.
if ty::util::trivial_const_drop(ty) {
if ty::util::is_trivially_const_drop(ty) {
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ pub fn needs_drop_components<'tcx>(
}
}

pub fn trivial_const_drop<'tcx>(ty: Ty<'tcx>) -> bool {
pub fn is_trivially_const_drop<'tcx>(ty: Ty<'tcx>) -> bool {
match *ty.kind() {
ty::Bool
| ty::Char
Expand All @@ -1055,25 +1055,25 @@ pub fn trivial_const_drop<'tcx>(ty: Ty<'tcx>) -> bool {
| ty::Ref(..)
| ty::FnDef(..)
| ty::FnPtr(_)
| ty::Never => true,
| ty::Never
| ty::Foreign(_) => true,

ty::Opaque(..)
| ty::Dynamic(..)
| ty::Error(_)
| ty::Bound(..)
| ty::Param(_)
| ty::Placeholder(_)
| ty::Foreign(_)
| ty::Projection(_)
| ty::Infer(_) => false,

// Not trivial because they have components, and instead of looking inside,
// we'll just perform trait selection.
ty::Closure(..) | ty::Generator(..) | ty::GeneratorWitness(_) | ty::Adt(..) => false,

ty::Array(ty, _) | ty::Slice(ty) => trivial_const_drop(ty),
ty::Array(ty, _) | ty::Slice(ty) => is_trivially_const_drop(ty),

ty::Tuple(tys) => tys.iter().all(|ty| trivial_const_drop(ty.expect_ty())),
ty::Tuple(tys) => tys.iter().all(|ty| is_trivially_const_drop(ty.expect_ty())),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| ty::Bound(..)
| ty::Param(_)
| ty::Placeholder(_)
| ty::Foreign(_)
| ty::Projection(_) => {
// We don't know if these are `~const Drop`, at least
// not structurally... so don't push a candidate.
Expand All @@ -951,6 +950,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| ty::FnDef(..)
| ty::FnPtr(_)
| ty::Never
| ty::Foreign(_)
| ty::Array(..)
| ty::Slice(_)
| ty::Closure(..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| ty::Ref(..)
| ty::FnDef(..)
| ty::FnPtr(_)
| ty::Never => {}
| ty::Never
| ty::Foreign(_) => {}

// These types are built-in, so we can fast-track by registering
// nested predicates for their constituient type(s)
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_typeck/src/check/dropck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
let predicate = predicate.kind();
let p = p.kind();
match (predicate.skip_binder(), p.skip_binder()) {
(ty::PredicateKind::Trait(a), ty::PredicateKind::Trait(b)) => relator
.relate(
predicate.rebind(ty::TraitPredicate {
constness: ty::BoundConstness::NotConst,
..a
}),
p.rebind(b),
)
.is_ok(),
(ty::PredicateKind::Trait(a), ty::PredicateKind::Trait(b)) => {
// Since struct predicates cannot have ~const, project the impl predicate
// onto one that ignores the constness. This is equivalent to saying that
// we match a `Trait` bound on the struct with a `Trait` or `~const Trait`
// in the impl.
let non_const_a =
ty::TraitPredicate { constness: ty::BoundConstness::NotConst, ..a };
relator.relate(predicate.rebind(non_const_a), p.rebind(b)).is_ok()
}
(ty::PredicateKind::Projection(a), ty::PredicateKind::Projection(b)) => {
relator.relate(predicate.rebind(a), p.rebind(b)).is_ok()
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/rfc-2632-const-trait-impl/const-drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ mod t {
impl const SomeTrait for () {
fn foo() {}
}
// non-const impl
impl SomeTrait for i32 {
fn foo() {}
}

pub struct ConstDropWithBound<T: SomeTrait>(pub core::marker::PhantomData<T>);

Expand All @@ -61,6 +65,14 @@ mod t {
T::foo();
}
}

pub struct ConstDropWithNonconstBound<T: SomeTrait>(pub core::marker::PhantomData<T>);

impl<T: SomeTrait> const Drop for ConstDropWithNonconstBound<T> {
fn drop(&mut self) {
// Note: we DON'T use the `T: SomeTrait` bound
}
}
}

use t::*;
Expand All @@ -78,6 +90,7 @@ implements_const_drop! {
&1,
&1 as *const i32,
ConstDropWithBound::<()>,
ConstDropWithNonconstBound::<i32>,
Result::<i32, !>::Ok(1),
}

Expand Down

0 comments on commit b7e4433

Please sign in to comment.