Skip to content

Commit

Permalink
feat: Resolve enums & prepare type system (#7115)
Browse files Browse the repository at this point in the history
Co-authored-by: Ary Borenszweig <asterite@gmail.com>
  • Loading branch information
jfecher and asterite authored Jan 21, 2025
1 parent 8804f0a commit a1cf830
Show file tree
Hide file tree
Showing 42 changed files with 657 additions and 428 deletions.
2 changes: 1 addition & 1 deletion compiler/noirc_driver/src/abi_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub(super) fn abi_type_from_hir_type(context: &Context, typ: &Type) -> AbiType {
AbiType::String { length: size }
}

Type::Struct(def, args) => {
Type::DataType(def, args) => {
let struct_type = def.borrow();
let fields = struct_type.get_fields(args);
let fields =
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ fn compile_contract_inner(
let structs = structs
.into_iter()
.map(|struct_id| {
let typ = context.def_interner.get_struct(struct_id);
let typ = context.def_interner.get_type(struct_id);
let typ = typ.borrow();
let fields = vecmap(typ.get_fields(&[]), |(name, typ)| {
(name, abi_type_from_hir_type(context, &typ))
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ast::{
UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData, Visibility,
};
use crate::node_interner::{
ExprId, InternedExpressionKind, InternedStatementKind, QuotedTypeId, StructId,
ExprId, InternedExpressionKind, InternedStatementKind, QuotedTypeId, TypeId,
};
use crate::token::{Attributes, FmtStrFragment, FunctionAttribute, Token, Tokens};
use crate::{Kind, Type};
Expand Down Expand Up @@ -559,7 +559,7 @@ pub struct ConstructorExpression {
/// This may be filled out during macro expansion
/// so that we can skip re-resolving the type name since it
/// would be lost at that point.
pub struct_type: Option<StructId>,
pub struct_type: Option<TypeId>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/elaborator/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
resolution::errors::ResolverError,
},
hir_def::expr::{HirExpression, HirIdent},
node_interner::{DefinitionKind, DependencyId, FuncId, NodeInterner, StructId, TraitId},
node_interner::{DefinitionKind, DependencyId, FuncId, NodeInterner, TraitId, TypeId},
parser::{Item, ItemKind},
token::{MetaAttribute, SecondaryAttribute},
Type, TypeBindings, UnificationError,
Expand Down Expand Up @@ -512,7 +512,7 @@ impl<'context> Elaborator<'context> {
pub(super) fn run_attributes(
&mut self,
traits: &BTreeMap<TraitId, UnresolvedTrait>,
types: &BTreeMap<StructId, UnresolvedStruct>,
types: &BTreeMap<TypeId, UnresolvedStruct>,
functions: &[UnresolvedFunctions],
module_attributes: &[ModuleAttribute],
) {
Expand Down
12 changes: 6 additions & 6 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
},
node_interner::{DefinitionKind, ExprId, FuncId, InternedStatementKind, TraitMethodId},
token::{FmtStrFragment, Tokens},
Kind, QuotedType, Shared, StructType, Type,
DataType, Kind, QuotedType, Shared, Type,
};

use super::{Elaborator, LambdaContext, UnsafeBlockStatus};
Expand Down Expand Up @@ -614,12 +614,12 @@ impl<'context> Elaborator<'context> {
let is_self_type = last_segment.ident.is_self_type_name();

let (r#type, struct_generics) = if let Some(struct_id) = constructor.struct_type {
let typ = self.interner.get_struct(struct_id);
let typ = self.interner.get_type(struct_id);
let generics = typ.borrow().instantiate(self.interner);
(typ, generics)
} else {
match self.lookup_type_or_error(path) {
Some(Type::Struct(r#type, struct_generics)) => (r#type, struct_generics),
Some(Type::DataType(r#type, struct_generics)) => (r#type, struct_generics),
Some(typ) => {
self.push_err(ResolverError::NonStructUsedInConstructor {
typ: typ.to_string(),
Expand Down Expand Up @@ -659,10 +659,10 @@ impl<'context> Elaborator<'context> {
let reference_location = Location::new(last_segment.ident.span(), self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

(expr, Type::Struct(struct_type, generics))
(expr, Type::DataType(struct_type, generics))
}

pub(super) fn mark_struct_as_constructed(&mut self, struct_type: Shared<StructType>) {
pub(super) fn mark_struct_as_constructed(&mut self, struct_type: Shared<DataType>) {
let struct_type = struct_type.borrow();
let parent_module_id = struct_type.id.parent_module_id(self.def_maps);
self.usage_tracker.mark_as_used(parent_module_id, &struct_type.name);
Expand All @@ -673,7 +673,7 @@ impl<'context> Elaborator<'context> {
/// are part of the struct.
fn resolve_constructor_expr_fields(
&mut self,
struct_type: Shared<StructType>,
struct_type: Shared<DataType>,
field_types: Vec<(String, ItemVisibility, Type)>,
fields: Vec<(Ident, Expression)>,
span: Span,
Expand Down
26 changes: 13 additions & 13 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
},
node_interner::{
DefinitionKind, DependencyId, ExprId, FuncId, FunctionModifiers, GlobalId, NodeInterner,
ReferenceId, StructId, TraitId, TraitImplId, TypeAliasId,
ReferenceId, TraitId, TraitImplId, TypeAliasId, TypeId,
},
token::SecondaryAttribute,
Shared, Type, TypeVariable,
Expand All @@ -43,7 +43,7 @@ use crate::{
hir_def::traits::ResolvedTraitBound,
node_interner::GlobalValue,
usage_tracker::UsageTracker,
StructField, StructType, TypeBindings,
DataType, StructField, TypeBindings,
};

mod comptime;
Expand Down Expand Up @@ -152,7 +152,7 @@ pub struct Elaborator<'context> {
/// struct Wrapped {
/// }
/// ```
resolving_ids: BTreeSet<StructId>,
resolving_ids: BTreeSet<TypeId>,

/// Each constraint in the `where` clause of the function currently being resolved.
trait_bounds: Vec<TraitConstraint>,
Expand Down Expand Up @@ -982,7 +982,7 @@ impl<'context> Elaborator<'context> {
let statements = std::mem::take(&mut func.def.body.statements);
let body = BlockExpression { statements };

let struct_id = if let Some(Type::Struct(struct_type, _)) = &self.self_type {
let struct_id = if let Some(Type::DataType(struct_type, _)) = &self.self_type {
Some(struct_type.borrow().id)
} else {
None
Expand Down Expand Up @@ -1030,7 +1030,7 @@ impl<'context> Elaborator<'context> {
self.mark_type_as_used(typ);
}
}
Type::Struct(struct_type, generics) => {
Type::DataType(struct_type, generics) => {
self.mark_struct_as_constructed(struct_type.clone());
for generic in generics {
self.mark_type_as_used(generic);
Expand Down Expand Up @@ -1507,7 +1507,7 @@ impl<'context> Elaborator<'context> {

let function_ids = functions.function_ids();

if let Type::Struct(struct_type, _) = &self_type {
if let Type::DataType(struct_type, _) = &self_type {
let struct_ref = struct_type.borrow();

// `impl`s are only allowed on types defined within the current crate
Expand Down Expand Up @@ -1602,7 +1602,7 @@ impl<'context> Elaborator<'context> {
}

/// Find the struct in the parent module so we can know its visibility
fn find_struct_visibility(&self, struct_type: &StructType) -> Option<ItemVisibility> {
fn find_struct_visibility(&self, struct_type: &DataType) -> Option<ItemVisibility> {
let parent_module_id = struct_type.id.parent_module_id(self.def_maps);
let parent_module_data = self.get_module(parent_module_id);
let per_ns = parent_module_data.find_name(&struct_type.name);
Expand All @@ -1625,7 +1625,7 @@ impl<'context> Elaborator<'context> {
}
// Public struct functions should not expose private types.
if let Some(struct_visibility) = func_meta.struct_id.and_then(|id| {
let struct_def = self.get_struct(id);
let struct_def = self.get_type(id);
let struct_def = struct_def.borrow();
self.find_struct_visibility(&struct_def)
}) {
Expand All @@ -1644,7 +1644,7 @@ impl<'context> Elaborator<'context> {
span: Span,
) {
match typ {
Type::Struct(struct_type, generics) => {
Type::DataType(struct_type, generics) => {
let struct_type = struct_type.borrow();
let struct_module_id = struct_type.id.module_id();

Expand Down Expand Up @@ -1714,7 +1714,7 @@ impl<'context> Elaborator<'context> {
}
}

fn collect_struct_definitions(&mut self, structs: &BTreeMap<StructId, UnresolvedStruct>) {
fn collect_struct_definitions(&mut self, structs: &BTreeMap<TypeId, UnresolvedStruct>) {
// This is necessary to avoid cloning the entire struct map
// when adding checks after each struct field is resolved.
let struct_ids = structs.keys().copied().collect::<Vec<_>>();
Expand Down Expand Up @@ -1766,7 +1766,7 @@ impl<'context> Elaborator<'context> {
// We need to check after all structs are resolved to
// make sure every struct's fields is accurately set.
for id in struct_ids {
let struct_type = self.interner.get_struct(id);
let struct_type = self.interner.get_type(id);

// Only handle structs without generics as any generics args will be checked
// after monomorphization when performing SSA codegen
Expand All @@ -1786,14 +1786,14 @@ impl<'context> Elaborator<'context> {
pub fn resolve_struct_fields(
&mut self,
unresolved: &NoirStruct,
struct_id: StructId,
struct_id: TypeId,
) -> Vec<StructField> {
self.recover_generics(|this| {
this.current_item = Some(DependencyId::Struct(struct_id));

this.resolving_ids.insert(struct_id);

let struct_def = this.interner.get_struct(struct_id);
let struct_def = this.interner.get_type(struct_id);
this.add_existing_generics(&unresolved.generics, &struct_def.borrow().generics);

let fields = vecmap(&unresolved.fields, |field| {
Expand Down
Loading

0 comments on commit a1cf830

Please sign in to comment.