From 00d372ae26e253776e1247817cea28fe19366a65 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 10 Sep 2024 13:48:57 -0300 Subject: [PATCH 1/3] Add `TypedExpr::get_type` --- .../src/hir/comptime/interpreter/builtin.rs | 26 +++++++++++++++++++ .../noir/standard_library/meta/typed_expr.md | 10 +++++-- noir_stdlib/src/meta/typed_expr.nr | 5 ++++ .../comptime_typed_expr/Nargo.toml | 7 +++++ .../comptime_typed_expr/src/main.nr | 7 +++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test_programs/compile_success_empty/comptime_typed_expr/Nargo.toml create mode 100644 test_programs/compile_success_empty/comptime_typed_expr/src/main.nr 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..6c743d31199 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, if the expression could be resolved without errors. \ 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..9b5822a0f7f 100644 --- a/noir_stdlib/src/meta/typed_expr.nr +++ b/noir_stdlib/src/meta/typed_expr.nr @@ -5,4 +5,9 @@ impl TypedExpr { // docs:start:as_function_definition comptime fn as_function_definition(self) -> Option {} // docs:end:as_function_definition + + #[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()); + } +} From 550de876697f55c682e40d18f69189e9201fd4c4 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 10 Sep 2024 13:50:42 -0300 Subject: [PATCH 2/3] Add doc comments to typed_expr.nr --- noir_stdlib/src/meta/typed_expr.nr | 3 +++ 1 file changed, 3 insertions(+) diff --git a/noir_stdlib/src/meta/typed_expr.nr b/noir_stdlib/src/meta/typed_expr.nr index 9b5822a0f7f..1d3b073b6da 100644 --- a/noir_stdlib/src/meta/typed_expr.nr +++ b/noir_stdlib/src/meta/typed_expr.nr @@ -1,11 +1,14 @@ +//! 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 {} From 9ab0b9cf8672c6881ce64fdc9416d4b2c4d8ece7 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 10 Sep 2024 14:26:12 -0300 Subject: [PATCH 3/3] Update docs/docs/noir/standard_library/meta/typed_expr.md Co-authored-by: jfecher --- docs/docs/noir/standard_library/meta/typed_expr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/noir/standard_library/meta/typed_expr.md b/docs/docs/noir/standard_library/meta/typed_expr.md index 6c743d31199..24b23f2ec19 100644 --- a/docs/docs/noir/standard_library/meta/typed_expr.md +++ b/docs/docs/noir/standard_library/meta/typed_expr.md @@ -16,4 +16,4 @@ If this expression refers to a function definitions, returns it. Otherwise retur #include_code get_type noir_stdlib/src/meta/typed_expr.nr rust -Returns the type of the expression, if the expression could be resolved without errors. \ No newline at end of file +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