Use trait select logic instead of query

This commit is contained in:
Deadbeef 2021-09-02 10:59:53 +00:00
parent f0a52128ee
commit 1ca83c6451
No known key found for this signature in database
GPG key ID: 027DF9338862ADDD
5 changed files with 100 additions and 31 deletions

View file

@ -988,14 +988,12 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
let mut err_span = self.span;
// Check to see if the type of this place can ever have a drop impl. If not, this
// `Drop` terminator is frivolous.
let ty_needs_drop = dropped_place
.ty(self.body, self.tcx)
.ty
.needs_non_const_drop(self.tcx, self.param_env);
let ty_needs_non_const_drop = qualifs::NeedsNonConstDrop::in_any_value_of_ty(
self.ccx,
dropped_place.ty(self.body, self.tcx).ty,
);
if !ty_needs_drop {
if !ty_needs_non_const_drop {
return;
}

View file

@ -3,10 +3,14 @@
//! See the `Qualif` trait for more info.
use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty};
use rustc_span::DUMMY_SP;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::{
self, ImplSource, Obligation, ObligationCause, SelectionContext,
};
use super::ConstCx;
@ -108,7 +112,28 @@ impl Qualif for NeedsNonConstDrop {
}
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.needs_drop(cx.tcx, cx.param_env)
let trait_ref = ty::TraitRef {
def_id: cx.tcx.require_lang_item(hir::LangItem::Drop, None),
substs: cx.tcx.mk_substs_trait(ty, &[]),
};
let obligation = Obligation::new(
ObligationCause::dummy(),
cx.param_env,
ty::Binder::dummy(ty::TraitPredicate {
trait_ref,
constness: ty::BoundConstness::ConstIfConst,
}),
);
let implsrc = cx.tcx.infer_ctxt().enter(|infcx| {
let mut selcx = SelectionContext::with_constness(&infcx, hir::Constness::Const);
selcx.select(&obligation)
});
match implsrc {
Ok(Some(ImplSource::ConstDrop(_)))
| Ok(Some(ImplSource::Param(_, ty::BoundConstness::ConstIfConst))) => false,
_ => true,
}
}
fn in_adt_inherently(cx: &ConstCx<'_, 'tcx>, adt: &'tcx AdtDef, _: SubstsRef<'tcx>) -> bool {