Skip to content

Commit

Permalink
Merge pull request #568 from hacspec/fix-bad-break-constructor
Browse files Browse the repository at this point in the history
fix(engine/phase/cf-into-monads): use `call_Constructor`, not `call`
  • Loading branch information
W95Psp authored Mar 12, 2024
2 parents 3855c88 + 813dcac commit 1891390
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 38 deletions.
3 changes: 2 additions & 1 deletion engine/lib/phases/phase_cf_into_monads.ml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ struct
| Return { e; _ } ->
let open KnownMonads in
let e = dexpr e in
UB.call Core__ops__control_flow__ControlFlow__Break [ e ] span
UB.call_Constructor Core__ops__control_flow__ControlFlow__Break false
[ e ] span
(to_typ @@ { monad = Some (MException e.typ); typ })
| [%inline_arms
"dexpr'.*" - Let - Match - If - Continue - Break - QuestionMark
Expand Down
13 changes: 13 additions & 0 deletions frontend/exporter/src/types/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,9 +1195,22 @@ impl<'tcx, S: ExprState<'tcx>> SInto<S, Expr> for rustc_middle::thir::Expr<'tcx>
let hir_id = hir_id.map(|hir_id| hir_id.index());
let unrolled = self.unroll_scope(s);
let rustc_middle::thir::Expr { span, kind, ty, .. } = unrolled;
let tcx = s.base().tcx;
let contents = match macro_invocation_of_span(span, s).map(ExprKind::MacroInvokation) {
Some(contents) => contents,
None => match kind {
// Special treatment for `AnonConst`s, particularly for enum discriminants
rustc_middle::thir::ExprKind::NamedConst { def_id, substs, .. }
if substs.is_empty()
&& tcx.def_kind(def_id) == rustc_hir::def::DefKind::AnonConst =>
{
match tcx.const_eval_poly(def_id) {
Ok(value) if let Decorated {contents: box e @ ExprKind::Literal {..}, ..} = const_value_to_constant_expr(s, ty, value, span).into() => {
e
},
_ => kind.sinto(s),
}
}
rustc_middle::thir::ExprKind::NonHirLiteral { lit, .. } => {
let cexpr: ConstantExpr =
(ConstantExprKind::Literal(scalar_int_to_constant_literal(s, lit, ty)))
Expand Down
53 changes: 53 additions & 0 deletions test-harness/src/snapshots/toolchain__enum-repr into-coq.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
source: test-harness/src/harness.rs
expression: snapshot
info:
kind:
Translate:
backend: coq
info:
name: enum-repr
manifest: enum-repr/Cargo.toml
description: ~
spec:
optional: false
broken: false
issue_id: ~
positive: true
snapshot:
stderr: true
stdout: true
include_flag: ~
backend_options: ~
---
exit = 0
stderr = '''
Compiling enum-repr v0.1.0 (WORKSPACE_ROOT/enum-repr)
Finished dev [unoptimized + debuginfo] target(s) in XXs'''
[stdout]
diagnostics = []

[stdout.files]
"Enum_repr.v" = '''
(* File automatically generated by Hacspec *)
From Hacspec Require Import Hacspec_Lib MachineIntegers.
From Coq Require Import ZArith.
Import List.ListNotations.
Open Scope Z_scope.
Open Scope bool_scope.

Inductive t_Foo : Type :=
| Foo_A : t_Foo
| Foo_B : t_Foo
| Foo_C : t_Foo.

(*Not implemented yet? todo(item)*)

Definition f (_ : unit) : int16 :=
let _x := cast ((@repr WORDSIZE16 5).+(@repr WORDSIZE16 0)) : int16 in
cast ((@repr WORDSIZE16 9).+(@repr WORDSIZE16 0)).

Definition ff__CONST : int16 :=
cast ((@repr WORDSIZE16 1).+(@repr WORDSIZE16 0)).
'''
48 changes: 48 additions & 0 deletions test-harness/src/snapshots/toolchain__enum-repr into-fstar.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: test-harness/src/harness.rs
expression: snapshot
info:
kind:
Translate:
backend: fstar
info:
name: enum-repr
manifest: enum-repr/Cargo.toml
description: ~
spec:
optional: false
broken: false
issue_id: ~
positive: true
snapshot:
stderr: true
stdout: true
include_flag: ~
backend_options: ~
---
exit = 0
stderr = '''
Compiling enum-repr v0.1.0 (WORKSPACE_ROOT/enum-repr)
Finished dev [unoptimized + debuginfo] target(s) in XXs'''
[stdout]
diagnostics = []

[stdout.files]
"Enum_repr.fst" = '''
module Enum_repr
#set-options "--fuel 0 --ifuel 1 --z3rlimit 15"
open Core
open FStar.Mul

type t_Foo =
| Foo_A : t_Foo
| Foo_B : t_Foo
| Foo_C : t_Foo

let f (_: Prims.unit) : u16 =
let v__x:u16 = cast (5us +! 0us <: u16) <: u16 in
cast (9us +! 0us <: u16) <: u16

let ff__CONST: u16 = cast (1us +! 0us <: u16) <: u16
'''
89 changes: 89 additions & 0 deletions test-harness/src/snapshots/toolchain__enum-repr into-ssprove.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
source: test-harness/src/harness.rs
expression: snapshot
info:
kind:
Translate:
backend: ssprove
info:
name: enum-repr
manifest: enum-repr/Cargo.toml
description: ~
spec:
optional: false
broken: false
issue_id: ~
positive: true
snapshot:
stderr: true
stdout: true
include_flag: ~
backend_options: ~
---
exit = 0
stderr = '''
Compiling enum-repr v0.1.0 (WORKSPACE_ROOT/enum-repr)
Finished dev [unoptimized + debuginfo] target(s) in XXs'''
[stdout]
diagnostics = []

[stdout.files]
"Enum_repr.v" = '''
(* File automatically generated by Hacspec *)
Set Warnings "-notation-overridden,-ambiguous-paths".
From Crypt Require Import choice_type Package Prelude.
Import PackageNotation.
From extructures Require Import ord fset.
From mathcomp Require Import word_ssrZ word.
From Jasmin Require Import word.

From Coq Require Import ZArith.
From Coq Require Import Strings.String.
Import List.ListNotations.
Open Scope list_scope.
Open Scope Z_scope.
Open Scope bool_scope.

From Hacspec Require Import ChoiceEquality.
From Hacspec Require Import LocationUtility.
From Hacspec Require Import Hacspec_Lib_Comparable.
From Hacspec Require Import Hacspec_Lib_Pre.
From Hacspec Require Import Hacspec_Lib.

Open Scope hacspec_scope.
Import choice.Choice.Exports.

Obligation Tactic := (* try timeout 8 *) solve_ssprove_obligations.

Definition t_Foo : choice_type :=
('unit ∐ 'unit'unit).
Notation "'Foo_A_case'" := (inl (inl tt)) (at level 100).
Equations Foo_A {L : {fset Location}} {I : Interface} : both L I t_Foo :=
Foo_A :=
solve_lift (ret_both (inl (inl (tt : 'unit)) : t_Foo)) : both L I t_Foo.
Fail Next Obligation.
Notation "'Foo_B_case'" := (inl (inr tt)) (at level 100).
Equations Foo_B {L : {fset Location}} {I : Interface} : both L I t_Foo :=
Foo_B :=
solve_lift (ret_both (inl (inr (tt : 'unit)) : t_Foo)) : both L I t_Foo.
Fail Next Obligation.
Notation "'Foo_C_case'" := (inr tt) (at level 100).
Equations Foo_C {L : {fset Location}} {I : Interface} : both L I t_Foo :=
Foo_C :=
solve_lift (ret_both (inr (tt : 'unit) : t_Foo)) : both L I t_Foo.
Fail Next Obligation.

(*Not implemented yet? todo(item)*)

Equations f {L1 : {fset Location}} {I1 : Interface} (_ : both L1 I1 'unit) : both L1 I1 int16 :=
f _ :=
letb v__x := cast_int (WS2 := _) ((ret_both (5 : int16)) .+ (ret_both (0 : int16))) in
solve_lift (cast_int (WS2 := _) ((ret_both (9 : int16)) .+ (ret_both (0 : int16)))) : both L1 I1 int16.
Fail Next Obligation.

Equations ff__CONST {L : {fset Location}} {I : Interface} : both L I int16 :=
ff__CONST :=
solve_lift (cast_int (WS2 := _) ((ret_both (1 : int16)) .+ (ret_both (0 : int16)))) : both L I int16.
Fail Next Obligation.
'''
76 changes: 44 additions & 32 deletions test-harness/src/snapshots/toolchain__side-effects into-fstar.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ let early_returns (x: u32) : u32 =
Rust_primitives.Hax.Control_flow_monad.Mexception.run (let! _:Prims.unit =
if x >. 3ul
then
let! hoist2:Rust_primitives.Hax.t_Never = Core.Ops.Control_flow.ControlFlow.v_Break 0ul in
let! hoist2:Rust_primitives.Hax.t_Never =
Core.Ops.Control_flow.ControlFlow_Break 0ul
<:
Core.Ops.Control_flow.t_ControlFlow u32 Rust_primitives.Hax.t_Never
in
Core.Ops.Control_flow.ControlFlow_Continue (Rust_primitives.Hax.never_to_any hoist2)
<:
Core.Ops.Control_flow.t_ControlFlow u32 Prims.unit
Expand All @@ -70,7 +74,9 @@ let early_returns (x: u32) : u32 =
match true with
| true ->
let! hoist4:Rust_primitives.Hax.t_Never =
Core.Ops.Control_flow.ControlFlow.v_Break 34ul
Core.Ops.Control_flow.ControlFlow_Break 34ul
<:
Core.Ops.Control_flow.t_ControlFlow u32 Rust_primitives.Hax.t_Never
in
Core.Ops.Control_flow.ControlFlow_Continue
(x, Rust_primitives.Hax.never_to_any hoist4 <: (u32 & u32))
Expand All @@ -88,14 +94,10 @@ let early_returns (x: u32) : u32 =
Core.Ops.Control_flow.t_ControlFlow u32 (u32 & u32)
in
let! hoist8:Rust_primitives.Hax.t_Never =
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Num.impl__u32__wrapping_add (Core.Num.impl__u32__wrapping_add
123ul
hoist5
<:
u32)
x
<:
u32)
Core.Ops.Control_flow.ControlFlow_Break
(Core.Num.impl__u32__wrapping_add (Core.Num.impl__u32__wrapping_add 123ul hoist5 <: u32) x)
<:
Core.Ops.Control_flow.t_ControlFlow u32 Rust_primitives.Hax.t_Never
in
Core.Ops.Control_flow.ControlFlow_Continue (Rust_primitives.Hax.never_to_any hoist8)
<:
Expand Down Expand Up @@ -157,9 +159,11 @@ let options (x y: Core.Option.t_Option u8) (z: Core.Option.t_Option u64) : Core.
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8)
(Core.Option.t_Option u8)
| Core.Option.Option_None ->
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Option.Option_None
<:
Core.Option.t_Option u8)
Core.Ops.Control_flow.ControlFlow_Break
(Core.Option.Option_None <: Core.Option.t_Option u8)
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8)
(Core.Option.t_Option u8)
else
match x with
| Core.Option.Option_Some hoist24 ->
Expand All @@ -173,13 +177,17 @@ let options (x y: Core.Option.t_Option u8) (z: Core.Option.t_Option u64) : Core.
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8)
(Core.Option.t_Option u8)
| Core.Option.Option_None ->
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Option.Option_None
<:
Core.Option.t_Option u8))
| Core.Option.Option_None ->
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Option.Option_None
Core.Ops.Control_flow.ControlFlow_Break
(Core.Option.Option_None <: Core.Option.t_Option u8)
<:
Core.Option.t_Option u8)
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8)
(Core.Option.t_Option u8))
| Core.Option.Option_None ->
Core.Ops.Control_flow.ControlFlow_Break
(Core.Option.Option_None <: Core.Option.t_Option u8)
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8)
(Core.Option.t_Option u8)
in
(match hoist26 with
| Core.Option.Option_Some hoist27 ->
Expand All @@ -192,9 +200,10 @@ let options (x y: Core.Option.t_Option u8) (z: Core.Option.t_Option u64) : Core.
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8) u8
| Core.Option.Option_None ->
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Option.Option_None
<:
Core.Option.t_Option u8))
Core.Ops.Control_flow.ControlFlow_Break
(Core.Option.Option_None <: Core.Option.t_Option u8)
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8) u8)
| 4uy ->
(match z with
| Core.Option.Option_Some hoist16 ->
Expand All @@ -203,9 +212,10 @@ let options (x y: Core.Option.t_Option u8) (z: Core.Option.t_Option u64) : Core.
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8) u8
| Core.Option.Option_None ->
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Option.Option_None
<:
Core.Option.t_Option u8))
Core.Ops.Control_flow.ControlFlow_Break
(Core.Option.Option_None <: Core.Option.t_Option u8)
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Option.t_Option u8) u8)
| _ ->
Core.Ops.Control_flow.ControlFlow_Continue 12uy
<:
Expand Down Expand Up @@ -255,10 +265,10 @@ let question_mark (x: u32) : Core.Result.t_Result u32 u32 =
Core.Ops.Control_flow.t_ControlFlow (Core.Result.t_Result u32 u32) u32
| Core.Result.Result_Err err ->
let! _:Prims.unit =
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Result.Result_Err
(Core.Convert.f_from err <: u8 -> u32)
<:
Core.Result.t_Result u32 u32)
Core.Ops.Control_flow.ControlFlow_Break
(Core.Result.Result_Err (Core.Convert.f_from err) <: Core.Result.t_Result u32 u32)
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Result.t_Result u32 u32) Prims.unit
in
Core.Ops.Control_flow.ControlFlow_Continue x
<:
Expand Down Expand Up @@ -295,9 +305,11 @@ let monad_lifting (x: u8) : Core.Result.t_Result t_A t_B =
match Core.Result.Result_Err (B <: t_B) <: Core.Result.t_Result t_A t_B with
| Core.Result.Result_Ok hoist33 ->
let! hoist35:Rust_primitives.Hax.t_Never =
Core.Ops.Control_flow.ControlFlow.v_Break (Core.Result.Result_Ok hoist33
<:
Core.Result.t_Result t_A t_B)
Core.Ops.Control_flow.ControlFlow_Break
(Core.Result.Result_Ok hoist33 <: Core.Result.t_Result t_A t_B)
<:
Core.Ops.Control_flow.t_ControlFlow (Core.Result.t_Result t_A t_B)
Rust_primitives.Hax.t_Never
in
Core.Ops.Control_flow.ControlFlow_Continue (Rust_primitives.Hax.never_to_any hoist35)
<:
Expand Down
Loading

0 comments on commit 1891390

Please sign in to comment.