Skip to content

Commit e96f858

Browse files
authored
Merge pull request #1526 from cryspen/rengine-def_id-names
Rust Engine: expose common `DefId`s in the Rust engine
2 parents 9799e24 + 58d88cb commit e96f858

File tree

11 files changed

+4589
-21
lines changed

11 files changed

+4589
-21
lines changed

Cargo.lock

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

cli/subcommands/src/cargo_hax.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ fn run_engine(
264264
input: haxmeta.items,
265265
impl_infos: haxmeta.impl_infos,
266266
};
267-
let mut hax_engine_command = if let Backend::Lean { .. } = &engine_options.backend.backend {
267+
let mut hax_engine_command = if let Backend::Lean { .. }
268+
| Backend::GenerateRustEngineNames { .. } =
269+
&engine_options.backend.backend
270+
{
268271
find_rust_hax_engine(message_format)
269272
} else {
270273
find_hax_engine(message_format)

engine/bin/lib.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ let run (options : Types.engine_options) : Types.output =
145145
failwith
146146
"The OCaml hax engine should never be called for lean. The Lean \
147147
backend uses the newer rust engine. Please report this issue on \
148-
our GitHub repository: https://github.com/cryspen/hax.")
148+
our GitHub repository: https://github.com/cryspen/hax."
149+
| GenerateRustEngineNames ->
150+
failwith
151+
"The OCaml hax engine should never be called with \
152+
`GenerateRustEngineNames`, it is an rust engine only internal \
153+
command.")
149154
in
150155
{
151156
diagnostics = List.map ~f:Diagnostics.to_thir_diagnostic diagnostics;

hax-types/src/cli_options/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ pub enum Backend<E: Extension> {
185185
/// Use the Lean backend (warning: work in progress!)
186186
#[clap(hide = true)]
187187
Lean,
188+
/// Extract `DefId`s of the crate as a Rust module tree.
189+
/// This is a command that regenerates code for the rust engine.
190+
#[clap(hide = true)]
191+
GenerateRustEngineNames,
188192
}
189193

190194
impl fmt::Display for Backend<()> {
@@ -196,6 +200,7 @@ impl fmt::Display for Backend<()> {
196200
Backend::Easycrypt { .. } => write!(f, "easycrypt"),
197201
Backend::ProVerif { .. } => write!(f, "proverif"),
198202
Backend::Lean { .. } => write!(f, "lean"),
203+
Backend::GenerateRustEngineNames { .. } => write!(f, "generate_rust_engine_names"),
199204
}
200205
}
201206
}

justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ expand *FLAGS:
4545
| ocamlformat --impl - \
4646
| just _pager
4747

48+
# Regenerate names in the Rust engine. Writes to `rust-engine/src/names/generated.rs`.
49+
regenerate-names:
50+
#!/usr/bin/env bash
51+
OUTPUT_FILE=rust-engine/src/names/generated.rs
52+
cargo hax -C --manifest-path engine/names/Cargo.toml \; into --output-dir $(dirname -- $OUTPUT_FILE) generate-rust-engine-names
53+
rustfmt "$OUTPUT_FILE"
4854

4955
# Format all the code
5056
fmt:

rust-engine/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ schemars.workspace = true
1818
serde-jsonlines = "0.5.0"
1919
serde_stacker = "0.1.12"
2020
pretty = "0.12"
21+
itertools.workspace = true

rust-engine/src/ast/identifiers.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::symbol::Symbol;
88
use hax_rust_engine_macros::*;
99
use std::fmt;
1010

11-
mod global_id {
11+
/// The global identifiers of hax.
12+
pub mod global_id {
1213
use hax_frontend_exporter::{DefKind, DisambiguatedDefPathItem};
1314
use hax_rust_engine_macros::*;
1415

@@ -123,8 +124,22 @@ mod global_id {
123124
}
124125
}
125126
}
126-
}
127127

128+
impl PartialEq<DefId> for GlobalId {
129+
fn eq(&self, other: &DefId) -> bool {
130+
if let Self::Concrete(concrete) = self {
131+
&concrete.def_id.def_id == other
132+
} else {
133+
false
134+
}
135+
}
136+
}
137+
impl PartialEq<GlobalId> for DefId {
138+
fn eq(&self, other: &GlobalId) -> bool {
139+
other == self
140+
}
141+
}
142+
}
128143
/// Local identifier
129144
#[derive_group_for_ast]
130145
pub struct LocalId(pub Symbol);

rust-engine/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
pub mod ast;
1212
pub mod hax_io;
1313
pub mod lean;
14+
pub mod names;
1415
pub mod ocaml_engine;
1516
pub mod printer;
1617
pub mod symbol;

rust-engine/src/main.rs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hax_rust_engine::{
44
ocaml_engine::{ExtendedToEngine, Response},
55
printer::Allocator,
66
};
7-
use hax_types::engine_api::File;
7+
use hax_types::{cli_options::Backend, engine_api::File};
88

99
use pretty::{DocAllocator, DocBuilder};
1010

@@ -13,22 +13,7 @@ fn krate_name(items: &Vec<Item>) -> String {
1313
head_item.ident.krate()
1414
}
1515

16-
fn main() {
17-
let ExtendedToEngine::Query(input) = hax_rust_engine::hax_io::read() else {
18-
panic!()
19-
};
20-
let (value, _map) = input.destruct();
21-
22-
let query = hax_rust_engine::ocaml_engine::Query {
23-
hax_version: value.hax_version,
24-
impl_infos: value.impl_infos,
25-
kind: hax_rust_engine::ocaml_engine::QueryKind::ImportThir { input: value.input },
26-
};
27-
28-
let Some(Response::ImportThir { output: items }) = query.execute() else {
29-
panic!()
30-
};
31-
16+
fn lean_backend(items: Vec<Item>) {
3217
let krate = krate_name(&items);
3318

3419
// For now, the main function always calls the Lean backend
@@ -57,3 +42,39 @@ fn main() {
5742
sourcemap: None,
5843
}));
5944
}
45+
46+
fn main() {
47+
let ExtendedToEngine::Query(input) = hax_rust_engine::hax_io::read() else {
48+
panic!()
49+
};
50+
let (value, _map) = input.destruct();
51+
52+
let query = hax_rust_engine::ocaml_engine::Query {
53+
hax_version: value.hax_version,
54+
impl_infos: value.impl_infos,
55+
kind: hax_rust_engine::ocaml_engine::QueryKind::ImportThir { input: value.input },
56+
};
57+
58+
let Some(Response::ImportThir { output: items }) = query.execute() else {
59+
panic!()
60+
};
61+
62+
match &value.backend.backend {
63+
Backend::Fstar { .. }
64+
| Backend::Coq { .. }
65+
| Backend::Ssprove { .. }
66+
| Backend::Easycrypt { .. }
67+
| Backend::ProVerif { .. } => panic!(
68+
"The Rust engine cannot be called with backend {}.",
69+
value.backend.backend
70+
),
71+
Backend::Lean => lean_backend(items),
72+
Backend::GenerateRustEngineNames => hax_rust_engine::hax_io::write(
73+
&hax_types::engine_api::protocol::FromEngine::File(File {
74+
path: "generated.rs".into(),
75+
contents: hax_rust_engine::names::codegen::export_def_ids_to_mod(items),
76+
sourcemap: None,
77+
}),
78+
),
79+
}
80+
}

0 commit comments

Comments
 (0)