Skip to content

Commit

Permalink
Normalize types when normalizing terms in normalize.rs, and therefore…
Browse files Browse the repository at this point in the history
… avoid some normalization in term.rs and vcgen.rs.
  • Loading branch information
jhjourdan committed Nov 14, 2024
1 parent ff8f1de commit 89e6bdf
Show file tree
Hide file tree
Showing 24 changed files with 12 additions and 121 deletions.
11 changes: 3 additions & 8 deletions creusot/src/backend/logic/vcgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,7 @@ impl<'a, 'tcx> VCGen<'a, 'tcx> {
TermKind::Tuple { fields } => self.build_vc_slice(fields, &|flds| k(Exp::Tuple(flds))),
// Same as for tuples
TermKind::Constructor { variant, fields, .. } => {
let ty =
self.ctx.borrow().normalize_erasing_regions(self.param_env, t.creusot_ty());
let TyKind::Adt(adt, subst) = ty.kind() else { unreachable!() };
let TyKind::Adt(adt, subst) = t.creusot_ty().kind() else { unreachable!() };
self.build_vc_slice(fields, &|fields| {
let ctor = constructor(
*self.names.borrow_mut(),
Expand Down Expand Up @@ -406,9 +404,7 @@ impl<'a, 'tcx> VCGen<'a, 'tcx> {
}),
// VC(A.f, Q) = VC(A, |a| Q(a.f))
TermKind::Projection { lhs, name } => {
let ty =
self.ctx.borrow().normalize_erasing_regions(self.param_env, lhs.creusot_ty());
let field = match ty.kind() {
let field = match lhs.creusot_ty().kind() {
TyKind::Closure(did, substs) => {
self.names.borrow_mut().field(*did, substs, *name).as_ident()
}
Expand Down Expand Up @@ -462,9 +458,8 @@ impl<'a, 'tcx> VCGen<'a, 'tcx> {
Pattern::Constructor { variant, fields, substs } => {
let fields =
fields.into_iter().map(|pat| self.build_pattern_inner(bounds, pat)).collect();
let substs = self.ctx.borrow().normalize_erasing_regions(self.param_env, *substs);
if self.ctx.borrow().def_kind(variant) == DefKind::Variant {
Pat::ConsP(self.names.borrow_mut().constructor(*variant, substs), fields)
Pat::ConsP(self.names.borrow_mut().constructor(*variant, *substs), fields)
} else if fields.len() == 0 {
Pat::TupleP(vec![])
} else {
Expand Down
15 changes: 6 additions & 9 deletions creusot/src/backend/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ impl<'tcx, N: Namer<'tcx>> Lower<'_, 'tcx, N> {
}
}
TermKind::Constructor { variant, fields, .. } => {
let ty = self.names.normalize(self.ctx, term.creusot_ty());
let TyKind::Adt(adt, subst) = ty.kind() else { unreachable!() };
let TyKind::Adt(adt, subst) = term.creusot_ty().kind() else { unreachable!() };
let fields = fields.into_iter().map(|f| self.lower_term(f)).collect();
constructor(self.names, fields, adt.variant(*variant).def_id, subst)
}
Expand Down Expand Up @@ -173,10 +172,9 @@ impl<'tcx, N: Namer<'tcx>> Lower<'_, 'tcx, N> {
Exp::Tuple(fields.into_iter().map(|f| self.lower_term(f)).collect())
}
TermKind::Projection { box lhs, name } => {
let base_ty = self.names.normalize(self.ctx, lhs.creusot_ty());
let lhs = self.lower_term(lhs);
let lhs_low = self.lower_term(lhs);

let field = match base_ty.kind() {
let field = match lhs.creusot_ty().kind() {
TyKind::Closure(did, substs) => self.names.field(*did, substs, *name),
TyKind::Adt(def, substs) => self.names.field(def.did(), substs, *name),
TyKind::Tuple(f) => {
Expand All @@ -185,14 +183,14 @@ impl<'tcx, N: Namer<'tcx>> Lower<'_, 'tcx, N> {

return Exp::Let {
pattern: Pat::TupleP(fields),
arg: Box::new(lhs),
arg: Box::new(lhs_low),
body: Box::new(Exp::var("a")),
};
}
k => unreachable!("Projection from {k:?}"),
};

lhs.field(&field.as_ident())
lhs_low.field(&field.as_ident())
}
TermKind::Closure { body, .. } => {
let TyKind::Closure(id, subst) = term.creusot_ty().kind() else {
Expand Down Expand Up @@ -232,9 +230,8 @@ impl<'tcx, N: Namer<'tcx>> Lower<'_, 'tcx, N> {
match pat {
Pattern::Constructor { variant, fields, substs } => {
let fields = fields.into_iter().map(|pat| self.lower_pat(pat)).collect();
let substs = self.names.normalize(self.ctx, *substs);
if self.ctx.def_kind(variant) == DefKind::Variant {
Pat::ConsP(self.names.constructor(*variant, substs), fields)
Pat::ConsP(self.names.constructor(*variant, *substs), fields)
} else if fields.len() == 0 {
Pat::TupleP(vec![])
} else {
Expand Down
2 changes: 1 addition & 1 deletion creusot/src/translation/function/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ impl<'tcx> BodyTranslator<'_, 'tcx> {
return;
}
// We are indeed in program code.
let func_param_env = self.ctx.param_env(fun_def_id);

// Check that we do not call `GhostBox::into_inner` in normal code
if is_ghost_into_inner(self.ctx.tcx, fun_def_id) {
Expand Down Expand Up @@ -379,6 +378,7 @@ impl<'tcx> BodyTranslator<'_, 'tcx> {
)
.emit();
} else {
let func_param_env = self.ctx.param_env(fun_def_id);
// Check and reject instantiation of a <T: Deref> with a ghost parameter.
let deref_trait_id = self.ctx.require_lang_item(rustc_hir::LangItem::Deref, None);
let infer_ctx = self.ctx.infer_ctxt().build();
Expand Down
4 changes: 1 addition & 3 deletions creusot/src/translation/pearlite/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub(crate) fn normalize<'tcx>(
mut term: Term<'tcx>,
) -> Term<'tcx> {
NormalizeTerm { param_env, tcx }.visit_mut_term(&mut term);
// FIXME: we should normalize here, but it diverges when normalizing specification of partial_eq
// let term = tcx.normalize_erasing_regions(param_env, term);
let term = tcx.normalize_erasing_regions(param_env, term);
term
}

Expand All @@ -38,7 +37,6 @@ impl<'tcx> TermVisitorMut<'tcx> for NormalizeTerm<'tcx> {
*id = method.0;
*subst = method.1;
}
*subst = self.tcx.normalize_erasing_regions(self.param_env, *subst);

if is_box_new(self.tcx, *id) {
let arg = args.remove(0);
Expand Down
3 changes: 1 addition & 2 deletions creusot/src/translation/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ pub(crate) fn pre_sig_of<'tcx>(
ctx: &mut TranslationCtx<'tcx>,
def_id: DefId,
) -> PreSignature<'tcx> {
let param_env = ctx.param_env(def_id);
let (inputs, output) = inputs_and_output(ctx.tcx, def_id);

let mut contract = crate::specification::contract_of(ctx, def_id);
Expand Down Expand Up @@ -466,7 +465,7 @@ pub(crate) fn pre_sig_of<'tcx>(

let term = Term::call(
ctx.tcx,
param_env,
ctx.param_env(def_id),
unnest_id,
unnest_subst,
vec![Term::var(self_, env_ty).cur(), Term::var(self_, env_ty).fin()],
Expand Down
46 changes: 0 additions & 46 deletions creusot/tests/creusot-contracts/creusot-contracts.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions creusot/tests/should_succeed/iterators/01_range.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions creusot/tests/should_succeed/iterators/02_iter_mut.coma

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 89e6bdf

Please sign in to comment.