Skip to content

Commit 641e147

Browse files
authored
Merge pull request #1504 from cryspen/rengine-rust-engine-reflection-ocaml-reviewable
Rust Engine: transport the Rust AST to OCaml
2 parents 75184e1 + e5d6519 commit 641e147

File tree

8 files changed

+57
-4
lines changed

8 files changed

+57
-4
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ hax-lib-macros-types = { path = "hax-lib/macros/types", version = "=0.3.1" }
8181
hax-lib = { path = "hax-lib", version = "=0.3.1" }
8282
hax-engine-names = { path = "engine/names", version = "=0.3.1" }
8383
hax-types = { path = "hax-types", version = "=0.3.1" }
84+
rust-printer = { path = "rust-printer" }
8485

8586
[workspace.metadata.release]
8687
owners = ["github:cryspen:tools"]

cli/subcommands/Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,21 @@ extension-traits = "1.0.1"
4848
serde.workspace = true
4949
serde_json.workspace = true
5050
hax-types.workspace = true
51+
rust-printer.workspace = true
5152
schemars.workspace = true
5253
hax-frontend-exporter.workspace = true
53-
hax-lib-macros-types = {workspace = true, features = ["schemars"]}
54+
hax-lib-macros-types = { workspace = true, features = ["schemars"] }
5455
version_check = "0.9"
5556
toml = "0.8"
5657

5758
[package.metadata.release]
58-
pre-release-hook = ["dune", "build", "--root", "../../engine", "hax-engine.opam"]
59+
pre-release-hook = [
60+
"dune",
61+
"build",
62+
"--root",
63+
"../../engine",
64+
"hax-engine.opam",
65+
]
5966

6067
[[package.metadata.release.pre-release-replacements]]
6168
file = "../../engine/dune-project"

cli/subcommands/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn json_schema_static_asset() {
2424
hax_types::engine_api::protocol::FromEngine,
2525
hax_types::engine_api::protocol::ToEngine,
2626
hax_lib_macros_types::AttrPayload,
27+
rust_printer::ast::Item,
2728
));
2829
schema.schema.metadata.get_or_insert_default().id = Some(hax_types::HAX_VERSION.into());
2930
serde_json::to_writer(

engine/lib/rust_engine_types.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(** This module re-exports and renames a subset of `Types`.
2+
`Types` contains both the modules from the frontend and from the Rust engine.
3+
Thus, some types are deduplicated, and get renamed.
4+
*)
5+
6+
module Renamed = struct
7+
type arm = Types.arm2
8+
type attribute = Types.attribute2
9+
type attribute_kind = Types.attribute_kind2
10+
type binding_mode = Types.binding_mode2
11+
type borrow_kind = Types.borrow_kind2
12+
type def_id = Types.def_id2
13+
type expr_kind = Types.expr_kind2
14+
type impl_expr = Types.impl_expr2
15+
type param = Types.param2
16+
type pat_kind = Types.pat_kind2
17+
type projection_predicate = Types.projection_predicate2
18+
type region = Types.region2
19+
type span = Types.span2
20+
end
21+
22+
include Types
23+
include Renamed

engine/utils/ocaml_of_json_schema/ocaml_of_json_schema.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ let exporters = {
438438
},
439439
},
440440
empty_struct: {
441-
guard: def => (eq(keys(def), new Set(["type"])) && def.type == 'object'),
441+
guard: def => (eq(keys(def), new Set(["type"])) && (def.type == 'object' || def.type == 'null')),
442442
f: (name, _) => {
443443
return {
444444
type: `EmptyStruct${name}`,
@@ -447,6 +447,21 @@ let exporters = {
447447
};
448448
},
449449
},
450+
newtype: {
451+
guard: def => !exporters['empty_struct'].guard(def) && is_type.expr(def, ["try-parse"]),
452+
f: (name, o) => {
453+
let path = [name + '-newtype-ref'];
454+
let te = is_type.expr(o, path);
455+
let ocaml_type = ocaml_of_type_expr(te, path);
456+
let arms = ocaml_arms_of_type_expr(te, path);
457+
let to_json = ocaml_yojson_of_type_expr(te, `(let Newtype${name} inner = o in inner)`, path);
458+
return {
459+
type: `Newtype${name} of ${ocaml_type}`,
460+
parse: `Newtype${name}(${mk_match('o', arms, path)})`,
461+
to_json,
462+
};
463+
},
464+
},
450465
// object is a *flat* record
451466
object: {
452467
guard: def => (eq(keys(def), new Set(["type", "required", "properties"]))

rust-printer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ readme.workspace = true
1212
hax-frontend-exporter = { path = "../frontend/exporter", default-features = false }
1313
serde = { workspace = true, features = ["derive"] }
1414
hax-rust-engine-macros = { path = "macros" }
15+
schemars.workspace = true

rust-printer/macros/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ pub fn derive_group_for_ast(_attr: TokenStream, item: TokenStream) -> TokenStrea
5252
/// Derive the necessary [de]serialization related traits for nodes in the AST.
5353
#[proc_macro_attribute]
5454
pub fn derive_group_for_ast_serialization(_attr: TokenStream, item: TokenStream) -> TokenStream {
55-
add_derive(item, quote! {::serde::Deserialize, ::serde::Serialize})
55+
add_derive(
56+
item,
57+
quote! {::serde::Deserialize, ::serde::Serialize, ::schemars::JsonSchema},
58+
)
5659
}
5760

5861
/// Derive the basic necessary traits for nodes in the AST.

0 commit comments

Comments
 (0)