remap ParamEnv with obligation

This commit is contained in:
Deadbeef 2022-09-16 11:38:28 +08:00
parent 08aeb1aa9b
commit 5ead742e18
4 changed files with 24 additions and 8 deletions

View file

@ -436,7 +436,6 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
locations,
);
self.cx.param_env = prev;
}
}
}

View file

@ -67,6 +67,14 @@ impl<'tcx> PredicateObligation<'tcx> {
recursion_depth: self.recursion_depth,
})
}
pub fn without_const(mut self, tcx: TyCtxt<'tcx>) -> PredicateObligation<'tcx> {
self.param_env = self.param_env.without_const();
if let ty::PredicateKind::Trait(trait_pred) = self.predicate.kind().skip_binder() && trait_pred.is_const_if_const() {
self.predicate = tcx.mk_predicate(self.predicate.kind().map_bound(|_| ty::PredicateKind::Trait(trait_pred.without_const())));
}
self
}
}
impl<'tcx> TraitObligation<'tcx> {

View file

@ -861,6 +861,11 @@ impl<'tcx> TraitPredicate<'tcx> {
(BoundConstness::ConstIfConst, hir::Constness::NotConst) => false,
}
}
pub fn without_const(mut self) -> Self {
self.constness = BoundConstness::NotConst;
self
}
}
impl<'tcx> PolyTraitPredicate<'tcx> {

View file

@ -1418,12 +1418,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
substs: SubstsRef<'tcx>,
code: impl Fn(usize, Span) -> ObligationCauseCode<'tcx>,
) {
let mut param_env = self.param_env;
match self.tcx.def_kind(def_id) {
let param_env = self.param_env;
let remap = match self.tcx.def_kind(def_id) {
// Associated consts have `Self: ~const Trait` bounds that should be satisfiable when
// `Self: Trait` is satisfied because it does not matter whether the impl is `const`.
// Therefore we have to remap the param env here to be non-const.
hir::def::DefKind::AssocConst => param_env = param_env.without_const(),
hir::def::DefKind::AssocConst => true,
hir::def::DefKind::AssocFn
if self.tcx.def_kind(self.tcx.parent(def_id)) == hir::def::DefKind::Trait =>
{
@ -1437,19 +1438,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
//
// FIXME(fee1-dead) FIXME(const_trait_impl): update this doc when trait methods can satisfy
// `~const FnOnce` or can be coerced to `const fn` pointer.
param_env = param_env.without_const();
}
_ => {}
true
}
_ => false,
};
let (bounds, _) = self.instantiate_bounds(span, def_id, &substs);
for obligation in traits::predicates_for_generics(
for mut obligation in traits::predicates_for_generics(
|idx, predicate_span| {
traits::ObligationCause::new(span, self.body_id, code(idx, predicate_span))
},
param_env,
bounds,
) {
if remap {
obligation = obligation.without_const(self.tcx);
}
self.register_predicate(obligation);
}
}