diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 899d62ecb61..166c6479b15 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -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), _ => { @@ -1070,6 +1073,7 @@ fn trait_impl_trait_generic_args( Ok(Value::Slice(trait_generics, slice_type)) } +// fn as_function_definition(self) -> Option fn typed_expr_as_function_definition( interner: &NodeInterner, arguments: Vec<(Value, Location)>, @@ -1087,6 +1091,28 @@ fn typed_expr_as_function_definition( option(return_type, option_value) } +// fn get_type(self) -> Option +fn typed_expr_get_type( + interner: &NodeInterner, + arguments: Vec<(Value, Location)>, + return_type: Type, + location: Location, +) -> IResult { + 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, diff --git a/docs/docs/noir/standard_library/meta/typed_expr.md b/docs/docs/noir/standard_library/meta/typed_expr.md index eacfd9c1230..24b23f2ec19 100644 --- a/docs/docs/noir/standard_library/meta/typed_expr.md +++ b/docs/docs/noir/standard_library/meta/typed_expr.md @@ -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()`. \ No newline at end of file +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. \ No newline at end of file diff --git a/noir_stdlib/src/meta/typed_expr.nr b/noir_stdlib/src/meta/typed_expr.nr index e01c0d3af67..1d3b073b6da 100644 --- a/noir_stdlib/src/meta/typed_expr.nr +++ b/noir_stdlib/src/meta/typed_expr.nr @@ -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 {} // 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 {} + // docs:end:get_type } diff --git a/test_programs/compile_success_empty/comptime_typed_expr/Nargo.toml b/test_programs/compile_success_empty/comptime_typed_expr/Nargo.toml new file mode 100644 index 00000000000..151b8b8e9d0 --- /dev/null +++ b/test_programs/compile_success_empty/comptime_typed_expr/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "comptime_typed_expr" +type = "bin" +authors = [""] +compiler_version = ">=0.31.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/compile_success_empty/comptime_typed_expr/src/main.nr b/test_programs/compile_success_empty/comptime_typed_expr/src/main.nr new file mode 100644 index 00000000000..ba2594f1da9 --- /dev/null +++ b/test_programs/compile_success_empty/comptime_typed_expr/src/main.nr @@ -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()); + } +}