Skip to content

Commit 278a36a

Browse files
authored
Remove AST-to-JSON snapshot export feature and its tests (#29597)
Remove the extensive but unused AST export feature along with its very large tests.
1 parent 3b9f0c8 commit 278a36a

53 files changed

Lines changed: 15 additions & 912669 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/ast/src/errors.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,13 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use std::{
18-
error::Error as ErrorArg,
19-
fmt::{Debug, Display},
20-
};
17+
use std::fmt::Display;
2118

2219
use leo_errors::Backtraced;
2320

2421
const CODE_PREFIX: &str = "AST";
2522
const CODE_MASK: i32 = 2000;
2623

27-
pub(crate) fn failed_to_convert_ast_to_json_string(error: &dyn ErrorArg) -> Backtraced {
28-
Backtraced::error(CODE_PREFIX, CODE_MASK, format!("failed to convert the AST to a JSON string: {error}"))
29-
.with_help("This is an internal serialization failure. Re-run the build; if it persists, please file an issue.")
30-
}
31-
32-
pub(crate) fn failed_to_create_ast_json_file(path: &dyn Debug, error: &dyn ErrorArg) -> Backtraced {
33-
Backtraced::error(CODE_PREFIX, CODE_MASK + 1, format!("failed to create AST JSON file `{path:?}`: {error}"))
34-
.with_help("Check that the build output directory exists and is writable.")
35-
}
36-
37-
pub(crate) fn failed_to_write_ast_to_json_file(path: &dyn Debug, error: &dyn ErrorArg) -> Backtraced {
38-
Backtraced::error(CODE_PREFIX, CODE_MASK + 2, format!("failed to write the AST to JSON file `{path:?}`: {error}"))
39-
.with_help("Check that the build output directory is writable and has enough free space.")
40-
}
41-
42-
pub(crate) fn failed_to_read_json_string_to_ast(error: &dyn ErrorArg) -> Backtraced {
43-
Backtraced::error(CODE_PREFIX, CODE_MASK + 3, format!("failed to deserialize a JSON string into an AST: {error}"))
44-
.with_help("The cached AST JSON is corrupt or was produced by an incompatible Leo version. Run `leo clean` and rebuild.")
45-
}
46-
47-
pub(crate) fn failed_to_read_json_file(path: &dyn Debug, error: &dyn ErrorArg) -> Backtraced {
48-
Backtraced::error(
49-
CODE_PREFIX,
50-
CODE_MASK + 4,
51-
format!("failed to deserialize JSON file `{path:?}` into an AST: {error}"),
52-
)
53-
.with_help(
54-
"The cached AST JSON is corrupt or was produced by an incompatible Leo version. Run `leo clean` and rebuild.",
55-
)
56-
}
57-
58-
pub(crate) fn failed_to_convert_ast_to_json_value(error: &dyn ErrorArg) -> Backtraced {
59-
Backtraced::error(CODE_PREFIX, CODE_MASK + 5, format!("failed to convert the AST to a JSON value: {error}"))
60-
.with_help("This is an internal serialization failure. Re-run the build; if it persists, please file an issue.")
61-
}
62-
6324
pub(crate) fn invalid_network_name(network: impl Display) -> Backtraced {
6425
Backtraced::error(CODE_PREFIX, CODE_MASK + 18, format!("invalid network name: `{network}`"))
6526
.with_help("Valid network names are `testnet`, `mainnet`, and `canary`.")

crates/ast/src/program/mod.rs

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
mod program_scope;
2020
pub use program_scope::*;
2121

22-
use leo_errors::Result;
2322
use leo_span::Symbol;
2423

2524
use crate::{Module, ProgramId, Stub};
@@ -70,49 +69,6 @@ impl Default for Program {
7069
}
7170
}
7271

73-
impl Program {
74-
/// Serializes the ast into a JSON string.
75-
pub fn to_json_string(&self) -> Result<String> {
76-
Ok(serde_json::to_string_pretty(&self).map_err(|e| crate::errors::failed_to_convert_ast_to_json_string(&e))?)
77-
}
78-
79-
// Converts the ast into a JSON value.
80-
// Note that there is no corresponding `from_json_value` function
81-
// since we modify JSON values leaving them unable to be converted
82-
// back into Programs.
83-
pub fn to_json_value(&self) -> Result<serde_json::Value> {
84-
Ok(serde_json::to_value(self).map_err(|e| crate::errors::failed_to_convert_ast_to_json_value(&e))?)
85-
}
86-
87-
/// Serializes the ast into a JSON file.
88-
pub fn to_json_file(&self, path: std::path::PathBuf, file_name: &str) -> Result<()> {
89-
write_ast_json(self, path, file_name)
90-
}
91-
92-
/// Serializes the ast into a JSON value and removes keys from object mappings before writing to a file.
93-
pub fn to_json_file_without_keys(
94-
&self,
95-
path: std::path::PathBuf,
96-
file_name: &str,
97-
excluded_keys: &[&str],
98-
) -> Result<()> {
99-
write_ast_json_filtered(self, path, file_name, excluded_keys)
100-
}
101-
102-
/// Deserializes the JSON string into a ast.
103-
pub fn from_json_string(json: &str) -> Result<Self> {
104-
let ast: Program =
105-
serde_json::from_str(json).map_err(|e| crate::errors::failed_to_read_json_string_to_ast(&e))?;
106-
Ok(ast)
107-
}
108-
109-
/// Deserializes the JSON string into a ast from a file.
110-
pub fn from_json_file(path: std::path::PathBuf) -> Result<Self> {
111-
let data = std::fs::read_to_string(&path).map_err(|e| crate::errors::failed_to_read_json_file(&path, &e))?;
112-
Self::from_json_string(&data)
113-
}
114-
}
115-
11672
/// Helper function to recursively filter keys from AST JSON.
11773
pub fn remove_key_from_json(value: serde_json::Value, key: &str) -> serde_json::Value {
11874
match value {
@@ -157,36 +113,6 @@ pub fn normalize_json_value(value: serde_json::Value) -> serde_json::Value {
157113
}
158114
}
159115

160-
/// Serializes an AST node (`Program` or `Library`) into a pretty JSON file, spans included.
161-
pub fn write_ast_json<T: Serialize>(value: &T, mut path: std::path::PathBuf, file_name: &str) -> Result<()> {
162-
path.push(file_name);
163-
let file = std::fs::File::create(&path).map_err(|e| crate::errors::failed_to_create_ast_json_file(&path, &e))?;
164-
let writer = std::io::BufWriter::new(file);
165-
Ok(serde_json::to_writer_pretty(writer, value)
166-
.map_err(|e| crate::errors::failed_to_write_ast_to_json_file(&path, &e))?)
167-
}
168-
169-
/// Serializes an AST node to a JSON file, stripping `excluded_keys` and normalizing first.
170-
pub fn write_ast_json_filtered<T: Serialize>(
171-
value: &T,
172-
mut path: std::path::PathBuf,
173-
file_name: &str,
174-
excluded_keys: &[&str],
175-
) -> Result<()> {
176-
path.push(file_name);
177-
let file = std::fs::File::create(&path).map_err(|e| crate::errors::failed_to_create_ast_json_file(&path, &e))?;
178-
let writer = std::io::BufWriter::new(file);
179-
180-
let mut value = serde_json::to_value(value).map_err(|e| crate::errors::failed_to_convert_ast_to_json_value(&e))?;
181-
for key in excluded_keys {
182-
value = remove_key_from_json(value, key);
183-
}
184-
value = normalize_json_value(value);
185-
186-
Ok(serde_json::to_writer_pretty(writer, &value)
187-
.map_err(|e| crate::errors::failed_to_write_ast_to_json_file(&path, &e))?)
188-
}
189-
190116
/// Serde helpers for `IndexMap<Vec<Symbol>, V>` maps keyed by module paths.
191117
///
192118
/// JSON object keys must be strings, so the `Vec<Symbol>` path is joined into a single

crates/compiler/benchmarks/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ use leo_parser::parse_program;
2828
use leo_span::{Symbol, source_map::FileName, with_session_globals};
2929
use snarkvm::prelude::{CanaryV0, MainnetV0, TestnetV0};
3030

31-
use std::{
32-
fs,
33-
path::{Path, PathBuf},
34-
rc::Rc,
35-
};
31+
use std::{fs, path::Path, rc::Rc};
3632

3733
use indexmap::IndexMap;
3834
use walkdir::WalkDir;
@@ -278,7 +274,6 @@ pub fn create_compiler(fixture: &FixtureData) -> Compiler {
278274
false,
279275
Handler::default(),
280276
Rc::new(NodeBuilder::default()),
281-
PathBuf::default(),
282277
Some(CompilerOptions::default()),
283278
fixture.import_stubs.clone(),
284279
fixture.network,
@@ -294,7 +289,6 @@ pub fn create_parse_only_compiler(fixture: &FixtureData) -> Compiler {
294289
false,
295290
Handler::default(),
296291
Rc::new(NodeBuilder::default()),
297-
PathBuf::default(),
298292
Some(CompilerOptions::default()),
299293
IndexMap::new(),
300294
fixture.network,

crates/compiler/src/compiler.rs

Lines changed: 5 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//!
1919
//! The [`Compiler`] type compiles Leo programs into R1CS circuits.
2020
21-
use crate::{AstSnapshots, CompilerOptions, errors};
21+
use crate::{CompilerOptions, errors};
2222

2323
use leo_ast::{AleoProgram, FunctionStub, Identifier, Library, NetworkName, NodeBuilder, ProgramId, Stub};
2424
pub use leo_ast::{Ast, DiGraph, Program};
@@ -92,8 +92,6 @@ pub struct Compiled {
9292

9393
/// The primary entry point of the Leo compiler.
9494
pub struct Compiler {
95-
/// The path to where the compiler outputs all generated files.
96-
output_directory: PathBuf,
9795
/// The name of the compilation unit (program or library).
9896
pub unit_name: Option<String>,
9997
/// When set, recompile under this on-chain name instead of the one the source
@@ -178,11 +176,6 @@ impl Compiler {
178176

179177
self.state.ast = Ast::Program(program);
180178

181-
if self.compiler_options.initial_ast {
182-
self.write_ast_to_json("initial.json")?;
183-
self.write_ast("initial.ast")?;
184-
}
185-
186179
Ok(())
187180
}
188181

@@ -253,11 +246,6 @@ impl Compiler {
253246
self.unit_name = Some(library_name.to_string());
254247
}
255248

256-
if self.compiler_options.initial_ast {
257-
self.write_ast_to_json("initial.json")?;
258-
self.write_ast("initial.ast")?;
259-
}
260-
261249
Ok(())
262250
}
263251

@@ -329,7 +317,6 @@ impl Compiler {
329317
is_test: bool,
330318
handler: Handler,
331319
node_builder: Rc<NodeBuilder>,
332-
output_directory: PathBuf,
333320
compiler_options: Option<CompilerOptions>,
334321
import_stubs: IndexMap<Symbol, Stub>,
335322
network: NetworkName,
@@ -342,7 +329,6 @@ impl Compiler {
342329
network,
343330
..Default::default()
344331
},
345-
output_directory,
346332
unit_name: expected_unit_name,
347333
rename: None,
348334
compiler_options: compiler_options.unwrap_or_default(),
@@ -356,23 +342,13 @@ impl Compiler {
356342
}
357343

358344
/// Runs a compiler pass and checks whether the caller still wants the
359-
/// result once the pass and any requested snapshots have completed.
345+
/// result once the pass has completed.
360346
fn do_pass_with_check<P: Pass, C>(&mut self, input: P::Input, should_continue: &mut C) -> Result<P::Output>
361347
where
362348
C: FnMut() -> Result<()>,
363349
{
364350
let output = P::do_pass(input, &mut self.state)?;
365351

366-
let write = match &self.compiler_options.ast_snapshots {
367-
AstSnapshots::All => true,
368-
AstSnapshots::Some(passes) => passes.contains(P::NAME),
369-
};
370-
371-
if write {
372-
self.write_ast_to_json(&format!("{}.json", P::NAME))?;
373-
self.write_ast(&format!("{}.ast", P::NAME))?;
374-
}
375-
376352
should_continue()?;
377353
Ok(output)
378354
}
@@ -738,48 +714,6 @@ impl Compiler {
738714
}
739715
}
740716

741-
/// Writes the AST to a JSON file under the unit's snapshots directory.
742-
fn write_ast_to_json(&self, filename: &str) -> Result<()> {
743-
// No snapshots directory configured (parse-only preflight or LSP); skip rather than dump into the CWD.
744-
if self.output_directory.as_os_str().is_empty() {
745-
return Ok(());
746-
}
747-
// Snapshots are opt-in; create the directory lazily on first write.
748-
fs::create_dir_all(&self.output_directory)
749-
.map_err(|e| crate::errors::failed_ast_file(self.output_directory.display(), e))?;
750-
let dir = self.output_directory.clone();
751-
if self.compiler_options.ast_spans_enabled {
752-
match &self.state.ast {
753-
Ast::Program(program) => leo_ast::write_ast_json(program, dir, filename)?,
754-
Ast::Library(library) => leo_ast::write_ast_json(library, dir, filename)?,
755-
}
756-
} else {
757-
match &self.state.ast {
758-
Ast::Program(program) => leo_ast::write_ast_json_filtered(program, dir, filename, &["_span", "span"])?,
759-
Ast::Library(library) => leo_ast::write_ast_json_filtered(library, dir, filename, &["_span", "span"])?,
760-
}
761-
}
762-
Ok(())
763-
}
764-
765-
/// Writes the AST to a file (Leo syntax, not JSON) under the unit's snapshots directory.
766-
fn write_ast(&self, filename: &str) -> Result<()> {
767-
// No snapshots directory configured (parse-only preflight or LSP); skip rather than dump into the CWD.
768-
if self.output_directory.as_os_str().is_empty() {
769-
return Ok(());
770-
}
771-
// Snapshots are opt-in; create the directory lazily on first write.
772-
fs::create_dir_all(&self.output_directory)
773-
.map_err(|e| crate::errors::failed_ast_file(self.output_directory.display(), e))?;
774-
let full_filename = self.output_directory.join(filename);
775-
776-
let contents = self.state.ast.to_string();
777-
778-
fs::write(&full_filename, contents).map_err(|e| crate::errors::failed_ast_file(full_filename.display(), e))?;
779-
780-
Ok(())
781-
}
782-
783717
/// Resolves and registers all import stubs for the current program.
784718
///
785719
/// This method performs a graph traversal over the program’s import relationships to:
@@ -914,11 +848,9 @@ impl Compiler {
914848
false,
915849
handler,
916850
node_builder,
917-
PathBuf::new(),
918851
Some(CompilerOptions {
919852
// avoid infinite recursion
920853
no_std: true,
921-
..CompilerOptions::default()
922854
}),
923855
IndexMap::new(),
924856
network,
@@ -1228,7 +1160,6 @@ fn load_source_dependency_stub(
12281160
false,
12291161
handler,
12301162
node_builder,
1231-
PathBuf::default(),
12321163
Some(CompilerOptions::default()),
12331164
IndexMap::new(),
12341165
network,
@@ -1351,16 +1282,8 @@ mod tests {
13511282

13521283
let handler = Handler::default();
13531284
let node_builder = Rc::new(NodeBuilder::default());
1354-
let mut compiler = Compiler::new(
1355-
None,
1356-
false,
1357-
handler,
1358-
node_builder,
1359-
PathBuf::from("/unused"),
1360-
None,
1361-
IndexMap::new(),
1362-
NetworkName::TestnetV0,
1363-
);
1285+
let mut compiler =
1286+
Compiler::new(None, false, handler, node_builder, None, IndexMap::new(), NetworkName::TestnetV0);
13641287

13651288
let library = compiler
13661289
.parse_library_from_directory_with_file_source(
@@ -1402,7 +1325,6 @@ mod tests {
14021325
false,
14031326
handler,
14041327
node_builder,
1405-
PathBuf::from("/unused"),
14061328
None,
14071329
IndexMap::new(),
14081330
NetworkName::TestnetV0,
@@ -1447,7 +1369,6 @@ mod tests {
14471369
false,
14481370
handler,
14491371
node_builder,
1450-
PathBuf::from("/unused"),
14511372
None,
14521373
IndexMap::new(),
14531374
NetworkName::TestnetV0,
@@ -1479,7 +1400,6 @@ mod tests {
14791400
false,
14801401
handler,
14811402
node_builder,
1482-
PathBuf::from("/unused"),
14831403
None,
14841404
IndexMap::new(),
14851405
NetworkName::TestnetV0,
@@ -1528,9 +1448,8 @@ mod tests {
15281448
false,
15291449
handler,
15301450
node_builder,
1531-
PathBuf::new(),
15321451
// `no_std` avoids injecting `std` into itself.
1533-
Some(crate::CompilerOptions { no_std: true, ..Default::default() }),
1452+
Some(crate::CompilerOptions { no_std: true }),
15341453
IndexMap::new(),
15351454
NetworkName::TestnetV0,
15361455
);

crates/compiler/src/errors.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ pub(crate) fn imported_program_not_found(
6060
.with_help(format!("Add `{dependency_name}` as a dependency in `program.json`. Run `leo add --help` for details."))
6161
}
6262

63-
pub(crate) fn failed_ast_file(filename: impl Display, error: impl Display) -> Backtraced {
64-
Backtraced::error(CODE_PREFIX, CODE_MASK + 11, format!("failed to write AST to file `{filename}`: {error}"))
65-
.with_help("Verify the build output directory exists, is writable, and has enough free space.")
66-
}
67-
6863
// Duplicated from package — same message, different code.
6964
pub(crate) fn failed_path(path: impl Display, err: impl Display) -> Backtraced {
7065
Backtraced::error(CODE_PREFIX, CODE_MASK + 16, format!("cannot find path `{path}`: {err}"))

0 commit comments

Comments
 (0)