Skip to content

Commit

Permalink
Merge 9ab0b9c into 28415ef
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Sep 10, 2024
2 parents 28415ef + 9ab0b9c commit 92bc7ea
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
26 changes: 26 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"typed_expr_as_function_definition" => {
typed_expr_as_function_definition(interner, arguments, return_type, location)
}
"typed_expr_get_type" => {
typed_expr_get_type(interner, arguments, return_type, location)
}
"unresolved_type_is_field" => unresolved_type_is_field(interner, arguments, location),
"zeroed" => zeroed(return_type),
_ => {
Expand Down Expand Up @@ -1070,6 +1073,7 @@ fn trait_impl_trait_generic_args(
Ok(Value::Slice(trait_generics, slice_type))
}

// fn as_function_definition(self) -> Option<FunctionDefinition>
fn typed_expr_as_function_definition(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
Expand All @@ -1087,6 +1091,28 @@ fn typed_expr_as_function_definition(
option(return_type, option_value)
}

// fn get_type(self) -> Option<Type>
fn typed_expr_get_type(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let typed_expr = get_typed_expr(self_argument)?;
let option_value = if let TypedExpr::ExprId(expr_id) = typed_expr {
let typ = interner.id_type(expr_id);
if typ == Type::Error {
None
} else {
Some(Value::Type(typ))
}
} else {
None
};
option(return_type, option_value)
}

// fn is_field(self) -> bool
fn unresolved_type_is_field(
interner: &NodeInterner,
Expand Down
10 changes: 8 additions & 2 deletions docs/docs/noir/standard_library/meta/typed_expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ title: TypedExpr

## Methods

### as_function_definition
### get_type

#include_code as_function_definition noir_stdlib/src/meta/typed_expr.nr rust

If this expression refers to a function definitions, returns it. Otherwise returns `Option::none()`.
If this expression refers to a function definitions, returns it. Otherwise returns `Option::none()`.

### get_type

#include_code get_type noir_stdlib/src/meta/typed_expr.nr rust

Returns the type of the expression, or `Option::none()` if there were errors when the expression was previously resolved.
8 changes: 8 additions & 0 deletions noir_stdlib/src/meta/typed_expr.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
//! Contains methods on the built-in `TypedExpr` type for resolved and type-checked expressions.
use crate::option::Option;

impl TypedExpr {
/// If this expression refers to a function definitions, returns it. Otherwise returns `Option::none()`.
#[builtin(typed_expr_as_function_definition)]
// docs:start:as_function_definition
comptime fn as_function_definition(self) -> Option<FunctionDefinition> {}
// docs:end:as_function_definition

/// Returns the type of the expression, if the expression could be resolved without errors.
#[builtin(typed_expr_get_type)]
// docs:start:get_type
comptime fn get_type(self) -> Option<Type> {}
// docs:end:get_type
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "comptime_typed_expr"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
comptime
{
let expr = quote { [1, 3] }.as_expr().unwrap().resolve(Option::none());
assert_eq(expr.get_type().unwrap(), quote { [Field; 2] }.as_type());
}
}

0 comments on commit 92bc7ea

Please sign in to comment.