Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nargo expand to show code after macro expansions #7613

Draft
wants to merge 37 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
12612bf
Start working on a tool to show code after macro expansions
asterite Mar 6, 2025
b7aab10
Move printing code to a Printer type
asterite Mar 7, 2025
f87c0f4
Show structs
asterite Mar 7, 2025
f0436a2
Also move `show_module` to printer
asterite Mar 7, 2025
f79d35d
Nested modules and cleaner output
asterite Mar 7, 2025
67ad4fb
Sort definitions
asterite Mar 7, 2025
170ed2c
Show globals
asterite Mar 7, 2025
c364c58
Anticipate showing types will be more complex than just `to_string()`
asterite Mar 7, 2025
8a8516f
Show type aliases
asterite Mar 7, 2025
741a293
Show traits (without methods yet)
asterite Mar 7, 2025
12bc89c
Show trait functions
asterite Mar 7, 2025
c4fffeb
Show function where clause
asterite Mar 7, 2025
42771d8
Show enums
asterite Mar 7, 2025
3ab7ac6
clippy
asterite Mar 7, 2025
3803c9c
Merge branch 'master' into ab/macro-expander
asterite Mar 7, 2025
ea428c5
Show comptime structs
asterite Mar 7, 2025
9518e99
Show comptime tuples
asterite Mar 7, 2025
a36de4e
Show comptime arrays
asterite Mar 7, 2025
b01cc4c
Show slices
asterite Mar 7, 2025
b8fe29b
mod vs contract
asterite Mar 7, 2025
af0560f
Show comptime CtString
asterite Mar 7, 2025
b044113
Show quoted values
asterite Mar 7, 2025
3483817
Show format strings
asterite Mar 7, 2025
658b51d
Show comptime function references
asterite Mar 7, 2025
c7620c2
Show comptime enum values
asterite Mar 7, 2025
9fe171d
check_package -> expand_package
asterite Mar 7, 2025
eeb0cff
Start showing impl methods
asterite Mar 7, 2025
0269ba4
Show impl generics
asterite Mar 7, 2025
b8acd24
Remove unused parameter
asterite Mar 7, 2025
d552c53
Show trait impls next to trait, for primitive types
asterite Mar 7, 2025
e243934
We actually need to show trait impls for non-primitive types too
asterite Mar 7, 2025
0acf35a
Show all trait impls
asterite Mar 7, 2025
340f946
clippy
asterite Mar 7, 2025
8d783f4
Apparently not all functions store a block
asterite Mar 7, 2025
faecd5a
Some comptime values are impossible to represent
asterite Mar 7, 2025
df3d8f6
Merge branch 'master' into ab/macro-expander
asterite Mar 7, 2025
a27c087
Fix after merge
asterite Mar 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,10 +1511,12 @@ impl<'context> Elaborator<'context> {

let resolved_trait_impl = Shared::new(TraitImpl {
ident,
location,
typ: self_type.clone(),
trait_id,
trait_generics,
file: trait_impl.file_id,
crate_id: self.crate_id,
where_clause,
methods,
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/comptime/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Display for TokensPrettyPrinter<'_, '_> {
}
}

pub(super) fn tokens_to_string(tokens: &[LocatedToken], interner: &NodeInterner) -> String {
pub fn tokens_to_string(tokens: &[LocatedToken], interner: &NodeInterner) -> String {
TokensPrettyPrinter { tokens, interner, indent: 0 }.to_string()
}

Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir/comptime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod interpreter;
mod tests;
mod value;

pub use display::tokens_to_string;
pub use errors::{ComptimeError, InterpreterError};
pub use interpreter::Interpreter;
pub use value::Value;
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/hir_def/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub struct Trait {
#[derive(Debug)]
pub struct TraitImpl {
pub ident: Ident,
pub location: Location,
pub typ: Type,
pub trait_id: TraitId,

Expand All @@ -95,6 +96,7 @@ pub struct TraitImpl {
pub trait_generics: Vec<Type>,

pub file: FileId,
pub crate_id: CrateId,
pub methods: Vec<FuncId>, // methods[i] is the implementation of trait.methods[i] for Type typ

/// The where clause, if present, contains each trait requirement which must
Expand Down
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt;
use std::hash::Hash;
use std::marker::Copy;
Expand Down Expand Up @@ -216,12 +217,12 @@
interned_statement_kinds: noirc_arena::Arena<StatementKind>,

// Interned `UnresolvedTypeData`s during comptime code.
interned_unresolved_type_datas: noirc_arena::Arena<UnresolvedTypeData>,

Check warning on line 220 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)

// Interned `Pattern`s during comptime code.
interned_patterns: noirc_arena::Arena<Pattern>,

/// Determins whether to run in LSP mode. In LSP mode references are tracked.

Check warning on line 225 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Determins)
pub(crate) lsp_mode: bool,

/// Store the location of the references in the graph.
Expand Down Expand Up @@ -697,7 +698,7 @@
quoted_types: Default::default(),
interned_expression_kinds: Default::default(),
interned_statement_kinds: Default::default(),
interned_unresolved_type_datas: Default::default(),

Check warning on line 701 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
interned_patterns: Default::default(),
lsp_mode: false,
location_indices: LocationIndices::default(),
Expand Down Expand Up @@ -1450,6 +1451,14 @@
self.trait_implementations[&id].clone()
}

pub fn get_trait_implementations_in_crate(&self, crate_id: CrateId) -> HashSet<TraitImplId> {
let trait_impls = self.trait_implementations.iter();
let trait_impls = trait_impls.filter_map(|(id, trait_impl)| {
if trait_impl.borrow().crate_id == crate_id { Some(*id) } else { None }
});
trait_impls.collect()
}

/// If the given function belongs to a trait impl, return its trait method id.
/// Otherwise, return None.
pub fn get_trait_method_id(&self, function: FuncId) -> Option<TraitMethodId> {
Expand Down Expand Up @@ -2184,11 +2193,11 @@
&mut self,
typ: UnresolvedTypeData,
) -> InternedUnresolvedTypeData {
InternedUnresolvedTypeData(self.interned_unresolved_type_datas.insert(typ))

Check warning on line 2196 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

pub fn get_unresolved_type_data(&self, id: InternedUnresolvedTypeData) -> &UnresolvedTypeData {
&self.interned_unresolved_type_datas[id.0]

Check warning on line 2200 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

/// Returns the type of an operator (which is always a function), along with its return type.
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ color-eyre.workspace = true
tokio = { version = "1.0", features = ["io-std", "rt"] }
dap.workspace = true
clap-markdown = { git = "https://github.com/noir-lang/clap-markdown", rev = "450d759532c88f0dba70891ceecdbc9ff8f25d2b", optional = true }
rustc-hash = "1.1.0"

notify = "6.1.1"
notify-debouncer-full = "0.3.1"
Expand Down
71 changes: 71 additions & 0 deletions tooling/nargo_cli/src/cli/expand_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use clap::Args;
use fm::FileManager;
use nargo::{
errors::CompileError, insert_all_files_for_workspace_into_file_manager, package::Package,
parse_all, prepare_package, workspace::Workspace,
};
use nargo_toml::PackageSelection;
use noirc_driver::CompileOptions;
use noirc_frontend::hir::{ParsedFiles, def_map::ModuleId};
use printer::Printer;

use crate::errors::CliError;

use super::{LockType, PackageOptions, WorkspaceCommand, check_cmd::check_crate_and_report_errors};

mod printer;

/// Expands macros
#[derive(Debug, Clone, Args)]
pub(crate) struct ExpandCommand {
#[clap(flatten)]
pub(super) package_options: PackageOptions,

#[clap(flatten)]
compile_options: CompileOptions,
}

impl WorkspaceCommand for ExpandCommand {
fn package_selection(&self) -> PackageSelection {
self.package_options.package_selection()
}
fn lock_type(&self) -> LockType {
// Creates a `Prover.toml` template if it doesn't exist, otherwise only writes if `allow_overwrite` is true,
// so it shouldn't lead to accidental conflicts. Doesn't produce compilation artifacts.
LockType::None
}
}

pub(crate) fn run(args: ExpandCommand, workspace: Workspace) -> Result<(), CliError> {
let mut workspace_file_manager = workspace.new_file_manager();
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);

for package in &workspace {
expand_package(&workspace_file_manager, &parsed_files, package, &args.compile_options)?;
}

Ok(())
}

fn expand_package(
file_manager: &FileManager,
parsed_files: &ParsedFiles,
package: &Package,
compile_options: &CompileOptions,
) -> Result<(), CompileError> {
let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);
check_crate_and_report_errors(&mut context, crate_id, compile_options)?;

let def_map = &context.def_maps[&crate_id];
let root_module_id = def_map.root();
let module_id = ModuleId { krate: crate_id, local_id: root_module_id };

let mut string = String::new();
let mut printer = Printer::new(crate_id, &context.def_interner, def_map, &mut string);
printer.show_module(module_id);
printer.show_stray_trait_impls();
println!("{}", string);

Ok(())
}
Loading
Loading