Skip to content

Commit

Permalink
chore: use resolved type instead of needing Constructor.struct_type
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Feb 24, 2025
1 parent 7aa23ec commit df1dddb
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 41 deletions.
15 changes: 2 additions & 13 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::ast::{
Ident, ItemVisibility, Path, Pattern, Statement, StatementKind, UnresolvedTraitConstraint,
UnresolvedType, UnresolvedTypeData, Visibility,
};
use crate::node_interner::{
ExprId, InternedExpressionKind, InternedStatementKind, QuotedTypeId, TypeId,
};
use crate::node_interner::{ExprId, InternedExpressionKind, InternedStatementKind, QuotedTypeId};
use crate::token::{Attributes, FmtStrFragment, FunctionAttribute, Token, Tokens};
use crate::{Kind, Type};
use acvm::{acir::AcirField, FieldElement};
Expand Down Expand Up @@ -223,11 +221,7 @@ impl ExpressionKind {
pub fn constructor(
(typ, fields): (UnresolvedType, Vec<(Ident, Expression)>),
) -> ExpressionKind {
ExpressionKind::Constructor(Box::new(ConstructorExpression {
typ,
fields,
struct_type: None,
}))
ExpressionKind::Constructor(Box::new(ConstructorExpression { typ, fields }))
}
}

Expand Down Expand Up @@ -545,11 +539,6 @@ pub struct MethodCallExpression {
pub struct ConstructorExpression {
pub typ: UnresolvedType,
pub fields: Vec<(Ident, Expression)>,

/// 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<TypeId>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down
1 change: 0 additions & 1 deletion compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,6 @@ impl Pattern {
kind: ExpressionKind::Constructor(Box::new(ConstructorExpression {
typ: UnresolvedType::from_path(path.clone()),
fields,
struct_type: None,
})),
location: *location,
})
Expand Down
12 changes: 3 additions & 9 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,15 +760,9 @@ impl<'context> Elaborator<'context> {

let last_segment = path.last_segment();

let typ = if let Some(struct_id) = constructor.struct_type {
let typ = self.interner.get_type(struct_id);
let generics = typ.borrow().instantiate(self.interner);
Type::DataType(typ, generics)
} else {
match self.lookup_type_or_error(path) {
Some(typ) => typ,
None => return (HirExpression::Error, Type::Error),
}
let typ = match self.lookup_type_or_error(path) {
Some(typ) => typ,
None => return (HirExpression::Error, Type::Error),
};

self.elaborate_constructor_with_type(typ, constructor.fields, location, Some(last_segment))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,10 @@ impl HirExpression {
let fields = vecmap(constructor.fields.clone(), |(name, expr): (Ident, ExprId)| {
(name, expr.to_display_ast(interner))
});
let struct_type = None;

ExpressionKind::Constructor(Box::new(ConstructorExpression {
typ: UnresolvedType::from_path(type_name),
fields,
struct_type,
}))
}
HirExpression::MemberAccess(access) => {
Expand Down
20 changes: 10 additions & 10 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use strum_macros::Display;
use crate::{
ast::{
ArrayLiteral, BlockExpression, ConstructorExpression, Expression, ExpressionKind, Ident,
IntegerBitSize, LValue, Literal, Path, Pattern, Signedness, Statement, StatementKind,
IntegerBitSize, LValue, Literal, Pattern, Signedness, Statement, StatementKind,
UnresolvedType, UnresolvedTypeData,
},
elaborator::Elaborator,
Expand Down Expand Up @@ -249,18 +249,18 @@ impl Value {
Ok((Ident::new(unwrap_rc(name), location), field))
})?;

let struct_type = match typ.follow_bindings_shallow().as_ref() {
Type::DataType(def, _) => Some(def.borrow().id),
let typ = match typ.follow_bindings_shallow().as_ref() {
Type::DataType(data_type, generics) => {
Type::DataType(data_type.clone(), generics.clone())
}
_ => return Err(InterpreterError::NonStructInConstructor { typ, location }),
};

// Since we've provided the struct_type, the path should be ignored.
let type_name = Path::from_single(String::new(), location);
ExpressionKind::Constructor(Box::new(ConstructorExpression {
typ: UnresolvedType::from_path(type_name),
fields,
struct_type,
}))
let quoted_type_id = elaborator.interner.push_quoted_type(typ);

let typ = UnresolvedTypeData::Resolved(quoted_type_id);
let typ = UnresolvedType { typ, location };
ExpressionKind::Constructor(Box::new(ConstructorExpression { typ, fields }))
}
value @ Value::Enum(..) => {
let hir = value.into_hir_expression(elaborator.interner, location)?;
Expand Down
6 changes: 1 addition & 5 deletions compiler/noirc_frontend/src/parser/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,7 @@ impl<'a> Parser<'a> {
Self::parse_constructor_field,
);

ExpressionKind::Constructor(Box::new(ConstructorExpression {
typ,
fields,
struct_type: None,
}))
ExpressionKind::Constructor(Box::new(ConstructorExpression { typ, fields }))
}

fn parse_constructor_field(&mut self) -> Option<(Ident, Expression)> {
Expand Down
1 change: 0 additions & 1 deletion tooling/lsp/src/with_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ fn constructor_expression_with_file(
fields: vecmap(expr.fields, |(ident, expression)| {
(ident_with_file(ident, file), expression_with_file(expression, file))
}),
struct_type: expr.struct_type,
}
}

Expand Down

0 comments on commit df1dddb

Please sign in to comment.