Check const Drop impls considering ConstIfConst bounds

This commit is contained in:
Michael Goulet
2022-01-18 01:43:49 -08:00
parent f9eb0b3b7b
commit 6ed42a7ca4
7 changed files with 227 additions and 167 deletions

View File

@@ -80,7 +80,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
param_env: obligation.param_env.without_const(),
..*obligation
};
obligation = &new_obligation;
}
@@ -159,7 +158,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Ok(ImplSource::TraitUpcasting(data))
}
ConstDropCandidate => Ok(ImplSource::ConstDrop(ImplSourceConstDropData)),
ConstDropCandidate(def_id) => {
let data = self.confirm_const_drop_candidate(obligation, def_id)?;
Ok(ImplSource::ConstDrop(data))
}
}
}
@@ -1087,4 +1089,104 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Ok(ImplSourceBuiltinData { nested })
}
fn confirm_const_drop_candidate(
&mut self,
obligation: &TraitObligation<'tcx>,
impl_def_id: Option<DefId>,
) -> Result<ImplSourceConstDropData<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
// `~const Drop` in a non-const environment is always trivially true, since our type is `Drop`
if obligation.param_env.constness() == Constness::NotConst {
return Ok(ImplSourceConstDropData { nested: vec![] });
}
let tcx = self.tcx();
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
let nested_tys = match *self_ty.skip_binder().kind() {
ty::Bool
| ty::Char
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Infer(ty::IntVar(_))
| ty::Infer(ty::FloatVar(_))
| ty::Str
| ty::RawPtr(_)
| ty::Ref(..)
| ty::FnDef(..)
| ty::FnPtr(_)
| ty::Projection(_) => vec![],
ty::Adt(def, substs) => def.all_fields().map(|f| f.ty(tcx, substs)).collect(),
ty::Array(ty, _) | ty::Slice(ty) => vec![ty],
ty::Tuple(tys) => tys.iter().map(|ty| ty.expect_ty()).collect(),
ty::Closure(_, substs) => vec![substs.as_closure().tupled_upvars_ty()],
ty::Generator(_, substs, _) => {
vec![substs.as_generator().tupled_upvars_ty(), substs.as_generator().witness()]
}
ty::GeneratorWitness(tys) => tcx.erase_late_bound_regions(tys).to_vec(),
ty::Opaque(_, _)
| ty::Dynamic(_, _)
| ty::Error(_)
| ty::Bound(_, _)
| ty::Param(_)
| ty::Placeholder(_)
| ty::Never
| ty::Foreign(_)
| ty::Infer(_) => {
unreachable!();
}
};
info!(
"confirm_const_drop_candidate: self_ty={:?}, nested_tys={:?} impl_def_id={:?}",
self_ty, nested_tys, impl_def_id
);
let cause = obligation.derived_cause(BuiltinDerivedObligation);
let mut nested = vec![];
// If we have a custom `impl const Drop`, then check it like a regular impl candidate.
if let Some(impl_def_id) = impl_def_id {
nested.extend(self.confirm_impl_candidate(obligation, impl_def_id).nested);
}
for ty in nested_tys {
let predicate = self.infcx.commit_unconditionally(|_| {
normalize_with_depth_to(
self,
obligation.param_env,
cause.clone(),
obligation.recursion_depth + 1,
self_ty
.rebind(ty::TraitPredicate {
trait_ref: ty::TraitRef {
def_id: self.tcx().require_lang_item(LangItem::Drop, None),
substs: self.tcx().mk_substs_trait(ty, &[]),
},
constness: ty::BoundConstness::ConstIfConst,
polarity: ty::ImplPolarity::Positive,
})
.to_predicate(tcx),
&mut nested,
)
});
nested.push(Obligation::with_depth(
cause.clone(),
obligation.recursion_depth + 1,
obligation.param_env,
predicate,
));
}
Ok(ImplSourceConstDropData { nested })
}
}