Skip to content

Commit

Permalink
feat!: rename struct-specific TypeDefinition -> StructDefinition (noi…
Browse files Browse the repository at this point in the history
…r-lang#5356)

# Description

## Problem\*

`TypeDefinition` is currently struct-specific

## Summary\*



## 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
michaeljklein authored Jul 1, 2024
1 parent 6936a52 commit 7ffccf7
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ impl<'context> Elaborator<'context> {
Interpreter::new(self.interner, &mut self.comptime_scopes, self.crate_id);

let location = Location::new(span, self.file);
let arguments = vec![(Value::TypeDefinition(struct_id), location)];
let arguments = vec![(Value::StructDefinition(struct_id), location)];

let value = interpreter
.call_function(function, arguments, TypeBindings::new(), location)
Expand Down
38 changes: 19 additions & 19 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub(super) fn call_builtin(
"array_len" => array_len(&arguments),
"as_slice" => as_slice(arguments),
"slice_push_back" => slice_push_back(arguments),
"type_def_as_type" => type_def_as_type(interner, arguments),
"type_def_generics" => type_def_generics(interner, arguments),
"type_def_fields" => type_def_fields(interner, arguments),
"struct_def_as_type" => struct_def_as_type(interner, arguments),
"struct_def_generics" => struct_def_generics(interner, arguments),
"struct_def_fields" => struct_def_fields(interner, arguments),
_ => {
let item = format!("Comptime evaluation for builtin function {name}");
Err(InterpreterError::Unimplemented { item, location })
Expand Down Expand Up @@ -63,19 +63,19 @@ fn slice_push_back(mut arguments: Vec<(Value, Location)>) -> IResult<Value> {
}

/// fn as_type(self) -> Quoted
fn type_def_as_type(
fn struct_def_as_type(
interner: &NodeInterner,
mut arguments: Vec<(Value, Location)>,
) -> IResult<Value> {
assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument");
let (type_def, span) = match arguments.pop() {
Some((Value::TypeDefinition(id), location)) => (id, location.span),
let (struct_def, span) = match arguments.pop() {
Some((Value::StructDefinition(id), location)) => (id, location.span),
other => {
unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}")
unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}")
}
};

let struct_def = interner.get_struct(type_def);
let struct_def = interner.get_struct(struct_def);
let struct_def = struct_def.borrow();
let make_token = |name| SpannedToken::new(Token::Ident(name), span);

Expand All @@ -92,19 +92,19 @@ fn type_def_as_type(
}

/// fn generics(self) -> [Quoted]
fn type_def_generics(
fn struct_def_generics(
interner: &NodeInterner,
mut arguments: Vec<(Value, Location)>,
) -> IResult<Value> {
assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument");
let (type_def, span) = match arguments.pop() {
Some((Value::TypeDefinition(id), location)) => (id, location.span),
let (struct_def, span) = match arguments.pop() {
Some((Value::StructDefinition(id), location)) => (id, location.span),
other => {
unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}")
unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}")
}
};

let struct_def = interner.get_struct(type_def);
let struct_def = interner.get_struct(struct_def);

let generics = struct_def
.borrow()
Expand All @@ -121,20 +121,20 @@ fn type_def_generics(
}

/// fn fields(self) -> [(Quoted, Quoted)]
/// Returns (name, type) pairs of each field of this TypeDefinition
fn type_def_fields(
/// Returns (name, type) pairs of each field of this StructDefinition
fn struct_def_fields(
interner: &mut NodeInterner,
mut arguments: Vec<(Value, Location)>,
) -> IResult<Value> {
assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument");
let (type_def, span) = match arguments.pop() {
Some((Value::TypeDefinition(id), location)) => (id, location.span),
let (struct_def, span) = match arguments.pop() {
Some((Value::StructDefinition(id), location)) => (id, location.span),
other => {
unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}")
unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}")
}
};

let struct_def = interner.get_struct(type_def);
let struct_def = interner.get_struct(struct_def);
let struct_def = struct_def.borrow();

let make_token = |name| SpannedToken::new(Token::Ident(name), span);
Expand Down
10 changes: 5 additions & 5 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum Value {
Array(Vector<Value>, Type),
Slice(Vector<Value>, Type),
Code(Rc<Tokens>),
TypeDefinition(StructId),
StructDefinition(StructId),
}

impl Value {
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Value {
Value::Array(_, typ) => return Cow::Borrowed(typ),
Value::Slice(_, typ) => return Cow::Borrowed(typ),
Value::Code(_) => Type::Quoted(QuotedType::Quoted),
Value::TypeDefinition(_) => Type::Quoted(QuotedType::TypeDefinition),
Value::StructDefinition(_) => Type::Quoted(QuotedType::StructDefinition),
Value::Pointer(element) => {
let element = element.borrow().get_type().into_owned();
Type::MutableReference(Box::new(element))
Expand Down Expand Up @@ -192,7 +192,7 @@ impl Value {
}
};
}
Value::Pointer(_) | Value::TypeDefinition(_) => {
Value::Pointer(_) | Value::StructDefinition(_) => {
return Err(InterpreterError::CannotInlineMacro { value: self, location })
}
};
Expand Down Expand Up @@ -298,7 +298,7 @@ impl Value {
HirExpression::Literal(HirLiteral::Slice(HirArrayLiteral::Standard(elements)))
}
Value::Code(block) => HirExpression::Unquote(unwrap_rc(block)),
Value::Pointer(_) | Value::TypeDefinition(_) => {
Value::Pointer(_) | Value::StructDefinition(_) => {
return Err(InterpreterError::CannotInlineMacro { value: self, location })
}
};
Expand Down Expand Up @@ -398,7 +398,7 @@ impl Display for Value {
}
write!(f, " }}")
}
Value::TypeDefinition(_) => write!(f, "(type definition)"),
Value::StructDefinition(_) => write!(f, "(struct definition)"),
}
}
}
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub enum QuotedType {
Quoted,
TopLevelItem,
Type,
TypeDefinition,
StructDefinition,
}

/// A list of TypeVariableIds to bind to a type. Storing the
Expand Down Expand Up @@ -1176,7 +1176,7 @@ impl std::fmt::Display for QuotedType {
QuotedType::Quoted => write!(f, "Quoted"),
QuotedType::TopLevelItem => write!(f, "TopLevelItem"),
QuotedType::Type => write!(f, "Type"),
QuotedType::TypeDefinition => write!(f, "TypeDefinition"),
QuotedType::StructDefinition => write!(f, "StructDefinition"),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ pub enum Keyword {
Trait,
Type,
TypeType,
TypeDefinition,
StructDefinition,
Unchecked,
Unconstrained,
Use,
Expand Down Expand Up @@ -961,7 +961,7 @@ impl fmt::Display for Keyword {
Keyword::Trait => write!(f, "trait"),
Keyword::Type => write!(f, "type"),
Keyword::TypeType => write!(f, "Type"),
Keyword::TypeDefinition => write!(f, "TypeDefinition"),
Keyword::StructDefinition => write!(f, "StructDefinition"),
Keyword::Unchecked => write!(f, "unchecked"),
Keyword::Unconstrained => write!(f, "unconstrained"),
Keyword::Use => write!(f, "use"),
Expand Down Expand Up @@ -1012,7 +1012,7 @@ impl Keyword {
"trait" => Keyword::Trait,
"type" => Keyword::Type,
"Type" => Keyword::TypeType,
"TypeDefinition" => Keyword::TypeDefinition,
"StructDefinition" => Keyword::StructDefinition,
"unchecked" => Keyword::Unchecked,
"unconstrained" => Keyword::Unconstrained,
"use" => Keyword::Use,
Expand Down
10 changes: 5 additions & 5 deletions compiler/noirc_frontend/src/parser/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(super) fn parse_type_inner<'a>(
bool_type(),
string_type(),
expr_type(),
type_definition_type(),
struct_definition_type(),
top_level_item_type(),
type_of_quoted_types(),
quoted_type(),
Expand Down Expand Up @@ -80,10 +80,10 @@ pub(super) fn expr_type() -> impl NoirParser<UnresolvedType> {
.map_with_span(|_, span| UnresolvedTypeData::Quoted(QuotedType::Expr).with_span(span))
}

/// This is the type `TypeDefinition` - the type of a quoted type definition
pub(super) fn type_definition_type() -> impl NoirParser<UnresolvedType> {
keyword(Keyword::TypeDefinition).map_with_span(|_, span| {
UnresolvedTypeData::Quoted(QuotedType::TypeDefinition).with_span(span)
/// This is the type `StructDefinition` - the type of a quoted struct definition
pub(super) fn struct_definition_type() -> impl NoirParser<UnresolvedType> {
keyword(Keyword::StructDefinition).map_with_span(|_, span| {
UnresolvedTypeData::Quoted(QuotedType::StructDefinition).with_span(span)
})
}

Expand Down
14 changes: 7 additions & 7 deletions noir_stdlib/src/meta/type_def.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
impl TypeDefinition {
/// Return a syntactic version of this type definition as a type.
impl StructDefinition {
/// Return a syntactic version of this struct definition as a type.
/// For example, `as_type(quote { type Foo<A, B> { ... } })` would return `Foo<A, B>`
#[builtin(type_def_as_type)]
#[builtin(struct_def_as_type)]
fn as_type(self) -> Quoted {}

/// Return each generic on this type. The names of these generics are unchanged
/// Return each generic on this struct. The names of these generics are unchanged
/// so users may need to keep name collisions in mind if this is used directly in a macro.
#[builtin(type_def_generics)]
#[builtin(struct_def_generics)]
fn generics(self) -> [Quoted] {}

/// Returns (name, type) pairs of each field in this type. Each type is as-is
/// Returns (name, type) pairs of each field in this struct. Each type is as-is
/// with any generic arguments unchanged.
#[builtin(type_def_fields)]
#[builtin(struct_def_fields)]
fn fields(self) -> [(Quoted, Quoted)] {}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[fail_assert]
struct Foo { x: Field }

comptime fn fail_assert(_typ: TypeDefinition) {
comptime fn fail_assert(_typ: StructDefinition) {
assert(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct MyType<A, B, C> {
field2: (B, C),
}

comptime fn my_comptime_fn(typ: TypeDefinition) {
comptime fn my_comptime_fn(typ: StructDefinition) {
let _ = typ.as_type();
assert_eq(typ.generics().len(), 3);
assert_eq(typ.fields().len(), 2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
comptime fn derive_default(typ: TypeDefinition) -> Quoted {
comptime fn derive_default(typ: StructDefinition) -> Quoted {
let generics: [Quoted] = typ.generics();
assert_eq(
generics.len(), 0, "derive_default: Deriving Default on generic types is currently unimplemented"
Expand Down

0 comments on commit 7ffccf7

Please sign in to comment.