Skip to content

Commit

Permalink
fix: Print comptime::Value better (#5655)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

## Summary\*

Several Value variants are just ids which we previously couldn't print
well. I've added a helper type to hold a NodeInterner so that we can
print the names of these values as well. This will now print, e.g. `Eq`
instead of `(trait definition)`

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
jfecher authored Aug 2, 2024
1 parent e592a10 commit 081101b
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 318 deletions.
6 changes: 2 additions & 4 deletions compiler/noirc_frontend/src/elaborator/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ impl<'context> Elaborator<'context> {
elaborator.introduce_generics_into_scope(meta.all_generics.clone());
}

elaborator.comptime_scopes = std::mem::take(&mut self.comptime_scopes);
elaborator.populate_scope_from_comptime_scopes();

let result = f(&mut elaborator);
elaborator.check_and_pop_function_context();

self.comptime_scopes = elaborator.comptime_scopes;
self.errors.append(&mut elaborator.errors);
result
}
Expand All @@ -74,7 +72,7 @@ impl<'context> Elaborator<'context> {
// Take the comptime scope to be our runtime scope.
// Iterate from global scope to the most local scope so that the
// later definitions will naturally shadow the former.
for scope in &self.comptime_scopes {
for scope in &self.interner.comptime_scopes {
for definition_id in scope.keys() {
let definition = self.interner.definition(*definition_id);
let name = definition.name.clone();
Expand Down Expand Up @@ -156,7 +154,7 @@ impl<'context> Elaborator<'context> {

if value != Value::Unit {
let items = value
.into_top_level_items(location)
.into_top_level_items(location, self.interner)
.map_err(|error| error.into_compilation_error_pair())?;

self.add_items(items, generated_items, location);
Expand Down
11 changes: 1 addition & 10 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
use crate::{
ast::{FunctionKind, UnresolvedTraitConstraint},
hir::{
comptime::Value,
def_collector::dc_crate::{
filter_literal_globals, CompilationError, ImplMap, UnresolvedGlobal, UnresolvedStruct,
UnresolvedTypeAlias,
Expand All @@ -27,8 +26,7 @@ use crate::{
SecondaryAttribute, StructId,
},
node_interner::{
DefinitionId, DefinitionKind, DependencyId, ExprId, FuncId, GlobalId, ReferenceId, TraitId,
TypeAliasId,
DefinitionKind, DependencyId, ExprId, FuncId, GlobalId, ReferenceId, TraitId, TypeAliasId,
},
Shared, Type, TypeVariable,
};
Expand Down Expand Up @@ -68,7 +66,6 @@ mod unquote;
use fm::FileId;
use iter_extended::vecmap;
use noirc_errors::{Location, Span};
use rustc_hash::FxHashMap as HashMap;

use self::traits::check_trait_impl_method_matches_declaration;

Expand Down Expand Up @@ -157,11 +154,6 @@ pub struct Elaborator<'context> {

crate_id: CrateId,

/// Each value currently in scope in the comptime interpreter.
/// Each element of the Vec represents a scope with every scope together making
/// up all currently visible definitions. The first scope is always the global scope.
pub(crate) comptime_scopes: Vec<HashMap<DefinitionId, Value>>,

/// The scope of --debug-comptime, or None if unset
debug_comptime_in_file: Option<FileId>,

Expand Down Expand Up @@ -209,7 +201,6 @@ impl<'context> Elaborator<'context> {
trait_bounds: Vec::new(),
function_context: vec![FunctionContext::default()],
current_trait_impl: None,
comptime_scopes: vec![HashMap::default()],
debug_comptime_in_file,
unresolved_globals: BTreeMap::new(),
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ impl<'context> Elaborator<'context> {

pub fn push_scope(&mut self) {
self.scopes.start_scope();
self.comptime_scopes.push(Default::default());
self.interner.comptime_scopes.push(Default::default());
}

pub fn pop_scope(&mut self) {
let scope = self.scopes.end_scope();
self.comptime_scopes.pop();
self.interner.comptime_scopes.pop();
self.check_for_unused_variables_in_scope_tree(scope.into());
}

Expand Down
Loading

0 comments on commit 081101b

Please sign in to comment.