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: Resolve enums & prepare type system #7115

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
24 changes: 12 additions & 12 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 @@ -146,7 +146,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 @@ -976,7 +976,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 @@ -1024,7 +1024,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 @@ -1501,7 +1501,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 @@ -1596,7 +1596,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 Down Expand Up @@ -1638,7 +1638,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 @@ -1708,7 +1708,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 @@ -1760,7 +1760,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 @@ -1780,14 +1780,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
14 changes: 7 additions & 7 deletions compiler/noirc_frontend/src/elaborator/path_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::hir::resolution::errors::ResolverError;
use crate::hir::resolution::visibility::item_in_module_is_visible;

use crate::locations::ReferencesTracker;
use crate::node_interner::{FuncId, GlobalId, StructId, TraitId, TypeAliasId};
use crate::node_interner::{FuncId, GlobalId, TraitId, TypeAliasId, TypeId};
use crate::{Shared, Type, TypeAlias};

use super::types::SELF_TYPE_NAME;
Expand All @@ -27,12 +27,12 @@ pub(crate) struct PathResolution {
#[derive(Debug, Clone)]
pub enum PathResolutionItem {
Module(ModuleId),
Struct(StructId),
Struct(TypeId),
TypeAlias(TypeAliasId),
Trait(TraitId),
Global(GlobalId),
ModuleFunction(FuncId),
StructFunction(StructId, Option<Turbofish>, FuncId),
StructFunction(TypeId, Option<Turbofish>, FuncId),
TypeAliasFunction(TypeAliasId, Option<Turbofish>, FuncId),
TraitFunction(TraitId, Option<Turbofish>, FuncId),
}
Expand Down Expand Up @@ -80,7 +80,7 @@ pub struct Turbofish {
#[derive(Debug)]
enum IntermediatePathResolutionItem {
Module,
Struct(StructId, Option<Turbofish>),
Struct(TypeId, Option<Turbofish>),
TypeAlias(TypeAliasId, Option<Turbofish>),
Trait(TraitId, Option<Turbofish>),
}
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<'context> Elaborator<'context> {
let mut module_id = self.module_id();

if path.kind == PathKind::Plain && path.first_name() == Some(SELF_TYPE_NAME) {
if let Some(Type::Struct(struct_type, _)) = &self.self_type {
if let Some(Type::DataType(struct_type, _)) = &self.self_type {
let struct_type = struct_type.borrow();
if path.segments.len() == 1 {
return Ok(PathResolution {
Expand Down Expand Up @@ -373,7 +373,7 @@ impl<'context> Elaborator<'context> {
}

fn self_type_module_id(&self) -> Option<ModuleId> {
if let Some(Type::Struct(struct_type, _)) = &self.self_type {
if let Some(Type::DataType(struct_type, _)) = &self.self_type {
Some(struct_type.borrow().id.module_id())
} else {
None
Expand Down Expand Up @@ -478,7 +478,7 @@ fn get_type_alias_module_def_id(type_alias: &Shared<TypeAlias>) -> Option<Module
let type_alias = type_alias.borrow();

match &type_alias.typ {
Type::Struct(struct_id, _generics) => Some(struct_id.borrow().id.module_id()),
Type::DataType(struct_id, _generics) => Some(struct_id.borrow().id.module_id()),
Type::Alias(type_alias, _generics) => get_type_alias_module_def_id(type_alias),
Type::Error => None,
_ => {
Expand Down
14 changes: 7 additions & 7 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
stmt::HirPattern,
},
node_interner::{DefinitionId, DefinitionKind, ExprId, FuncId, GlobalId, TraitImplKind},
Kind, Shared, StructType, Type, TypeAlias, TypeBindings,
DataType, Kind, Shared, Type, TypeAlias, TypeBindings,
};

use super::{path_resolution::PathResolutionItem, Elaborator, ResolverMeta};
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'context> Elaborator<'context> {
};

let (struct_type, generics) = match self.lookup_type_or_error(name) {
Some(Type::Struct(struct_type, struct_generics)) => (struct_type, struct_generics),
Some(Type::DataType(struct_type, struct_generics)) => (struct_type, struct_generics),
None => return error_identifier(self),
Some(typ) => {
let typ = typ.to_string();
Expand All @@ -210,7 +210,7 @@ impl<'context> Elaborator<'context> {
turbofish_span,
);

let actual_type = Type::Struct(struct_type.clone(), generics);
let actual_type = Type::DataType(struct_type.clone(), generics);
let location = Location::new(span, self.file);

self.unify(&actual_type, &expected_type, || TypeCheckError::TypeMismatchWithSource {
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<'context> Elaborator<'context> {
#[allow(clippy::too_many_arguments)]
fn resolve_constructor_pattern_fields(
&mut self,
struct_type: Shared<StructType>,
struct_type: Shared<DataType>,
fields: Vec<(Ident, Pattern)>,
span: Span,
expected_type: Type,
Expand Down Expand Up @@ -434,7 +434,7 @@ impl<'context> Elaborator<'context> {

pub(super) fn resolve_struct_turbofish_generics(
&mut self,
struct_type: &StructType,
struct_type: &DataType,
generics: Vec<Type>,
unresolved_turbofish: Option<Vec<UnresolvedType>>,
span: Span,
Expand Down Expand Up @@ -575,7 +575,7 @@ impl<'context> Elaborator<'context> {
fn resolve_item_turbofish(&mut self, item: PathResolutionItem) -> Vec<Type> {
match item {
PathResolutionItem::StructFunction(struct_id, Some(generics), _func_id) => {
let struct_type = self.interner.get_struct(struct_id);
let struct_type = self.interner.get_type(struct_id);
let struct_type = struct_type.borrow();
let struct_generics = struct_type.instantiate(self.interner);
self.resolve_struct_turbofish_generics(
Expand Down Expand Up @@ -886,7 +886,7 @@ impl<'context> Elaborator<'context> {
fn get_type_alias_generics(type_alias: &TypeAlias, generics: &[Type]) -> Vec<Type> {
let typ = type_alias.get_type(generics);
match typ {
Type::Struct(_, generics) => generics,
Type::DataType(_, generics) => generics,
Type::Alias(type_alias, generics) => {
get_type_alias_generics(&type_alias.borrow(), &generics)
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
expr::{HirCapturedVar, HirIdent},
traits::Trait,
},
node_interner::{DefinitionId, StructId, TraitId},
Shared, StructType,
node_interner::{DefinitionId, TraitId, TypeId},
DataType, Shared,
};
use crate::{Type, TypeAlias};

Expand All @@ -37,8 +37,8 @@ impl<'context> Elaborator<'context> {
current_module
}

pub(super) fn get_struct(&self, type_id: StructId) -> Shared<StructType> {
self.interner.get_struct(type_id)
pub(super) fn get_struct(&self, type_id: TypeId) -> Shared<DataType> {
self.interner.get_type(type_id)
}

pub(super) fn get_trait_mut(&mut self, trait_id: TraitId) -> &mut Trait {
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<'context> Elaborator<'context> {
}

/// Lookup a given struct type by name.
pub fn lookup_struct_or_error(&mut self, path: Path) -> Option<Shared<StructType>> {
pub fn lookup_struct_or_error(&mut self, path: Path) -> Option<Shared<DataType>> {
let span = path.span();
match self.resolve_path_or_error(path) {
Ok(item) => {
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<'context> Elaborator<'context> {
Ok(PathResolutionItem::Struct(struct_id)) => {
let struct_type = self.get_struct(struct_id);
let generics = struct_type.borrow().instantiate(self.interner);
Some(Type::Struct(struct_type, generics))
Some(Type::DataType(struct_type, generics))
}
Ok(PathResolutionItem::TypeAlias(alias_id)) => {
let alias = self.interner.get_type_alias(alias_id);
Expand Down
6 changes: 3 additions & 3 deletions compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
},
},
node_interner::{DefinitionId, DefinitionKind, GlobalId, StmtId},
StructType, Type,
DataType, Type,
};

use super::{lints, Elaborator};
Expand Down Expand Up @@ -478,7 +478,7 @@ impl<'context> Elaborator<'context> {
let lhs_type = lhs_type.follow_bindings();

match &lhs_type {
Type::Struct(s, args) => {
Type::DataType(s, args) => {
let s = s.borrow();
if let Some((field, visibility, index)) = s.get_field(field_name, args) {
let reference_location = Location::new(span, self.file);
Expand Down Expand Up @@ -542,7 +542,7 @@ impl<'context> Elaborator<'context> {

pub(super) fn check_struct_field_visibility(
&mut self,
struct_type: &StructType,
struct_type: &DataType,
field_name: &str,
visibility: ItemVisibility,
span: Span,
Expand Down
Loading
Loading