From 30e1338f372cc622f1725e17d40199fe77d4b46b Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Mon, 4 Sep 2023 12:09:54 +0300 Subject: [PATCH] chore: remove unneeded type --- crates/noirc_frontend/src/ast/expression.rs | 32 ++----------------- crates/noirc_frontend/src/ast/function.rs | 11 ++----- crates/noirc_frontend/src/ast/traits.rs | 7 ++-- .../src/hir/def_collector/dc_crate.rs | 14 +++----- .../src/hir/def_collector/dc_mod.rs | 9 +++--- .../src/hir/def_map/aztec_library.rs | 13 ++++---- .../src/hir/type_check/errors.rs | 11 +++---- .../noirc_frontend/src/hir/type_check/mod.rs | 4 +-- crates/noirc_frontend/src/hir_def/function.rs | 4 +-- crates/noirc_frontend/src/hir_def/types.rs | 2 +- crates/noirc_frontend/src/parser/parser.rs | 14 ++++---- 11 files changed, 40 insertions(+), 81 deletions(-) diff --git a/crates/noirc_frontend/src/ast/expression.rs b/crates/noirc_frontend/src/ast/expression.rs index 6c29c89051f..6193faa79f9 100644 --- a/crates/noirc_frontend/src/ast/expression.rs +++ b/crates/noirc_frontend/src/ast/expression.rs @@ -3,7 +3,7 @@ use std::fmt::Display; use crate::token::{Attribute, Token}; use crate::{ Distinctness, Ident, Path, Pattern, Recoverable, Statement, TraitConstraint, UnresolvedType, - UnresolvedTypeData, Visibility, + Visibility, }; use acvm::FieldElement; use iter_extended::vecmap; @@ -367,19 +367,11 @@ pub struct FunctionDefinition { pub body: BlockExpression, pub span: Span, pub where_clause: Vec, - pub return_type: FunctionReturnType, + pub return_type: UnresolvedType, pub return_visibility: Visibility, pub return_distinctness: Distinctness, } -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum FunctionReturnType { - /// Returns type is not specified. - Default(Span), - /// Everything else. - Ty(UnresolvedType, Span), -} - /// Describes the types of smart contract functions that are allowed. /// - All Noir programs in the non-contract context can be seen as `Secret`. #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -633,7 +625,7 @@ impl FunctionDefinition { parameters: &[(Ident, UnresolvedType)], body: &BlockExpression, where_clause: &[TraitConstraint], - return_type: &FunctionReturnType, + return_type: &UnresolvedType, ) -> FunctionDefinition { let p = parameters .iter() @@ -687,21 +679,3 @@ impl Display for FunctionDefinition { ) } } - -impl FunctionReturnType { - pub fn get_type(&self) -> &UnresolvedTypeData { - match self { - FunctionReturnType::Default(_span) => &UnresolvedTypeData::Unit, - FunctionReturnType::Ty(typ, _span) => &typ.typ, - } - } -} - -impl Display for FunctionReturnType { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - FunctionReturnType::Default(_) => f.write_str(""), - FunctionReturnType::Ty(ty, _) => write!(f, "{ty}"), - } - } -} diff --git a/crates/noirc_frontend/src/ast/function.rs b/crates/noirc_frontend/src/ast/function.rs index ea6d92f0afd..4770fc4f35a 100644 --- a/crates/noirc_frontend/src/ast/function.rs +++ b/crates/noirc_frontend/src/ast/function.rs @@ -2,9 +2,9 @@ use std::fmt::Display; use noirc_errors::Span; -use crate::{token::Attribute, FunctionReturnType, Ident, Pattern, Visibility}; +use crate::{token::Attribute, Ident, Pattern, Visibility}; -use super::{FunctionDefinition, UnresolvedType, UnresolvedTypeData}; +use super::{FunctionDefinition, UnresolvedType}; // A NoirFunction can be either a foreign low level function or a function definition // A closure / function definition will be stored under a name, so we do not differentiate between their variants @@ -43,12 +43,7 @@ impl NoirFunction { } pub fn return_type(&self) -> UnresolvedType { - match &self.def.return_type { - FunctionReturnType::Default(_) => { - UnresolvedType::without_span(UnresolvedTypeData::Unit) - } - FunctionReturnType::Ty(ty, _) => ty.clone(), - } + self.def.return_type.clone() } pub fn name(&self) -> &str { &self.name_ident().0.contents diff --git a/crates/noirc_frontend/src/ast/traits.rs b/crates/noirc_frontend/src/ast/traits.rs index 93587c13b92..017b00517fe 100644 --- a/crates/noirc_frontend/src/ast/traits.rs +++ b/crates/noirc_frontend/src/ast/traits.rs @@ -3,10 +3,7 @@ use std::fmt::Display; use iter_extended::vecmap; use noirc_errors::Span; -use crate::{ - BlockExpression, Expression, FunctionReturnType, Ident, NoirFunction, UnresolvedGenerics, - UnresolvedType, -}; +use crate::{BlockExpression, Expression, Ident, NoirFunction, UnresolvedGenerics, UnresolvedType}; /// AST node for trait definitions: /// `trait name { ... items ... }` @@ -27,7 +24,7 @@ pub enum TraitItem { name: Ident, generics: Vec, parameters: Vec<(Ident, UnresolvedType)>, - return_type: FunctionReturnType, + return_type: UnresolvedType, where_clause: Vec, body: Option, }, diff --git a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs index bc0d8d6cfc0..3f9dfd82712 100644 --- a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -13,9 +13,9 @@ use crate::hir::type_check::{type_check_func, TypeChecker}; use crate::hir::Context; use crate::node_interner::{FuncId, NodeInterner, StmtId, StructId, TraitId, TypeAliasId}; use crate::{ - ExpressionKind, FunctionReturnType, Generics, Ident, LetStatement, Literal, NoirFunction, - NoirStruct, NoirTrait, NoirTypeAlias, ParsedModule, Shared, StructType, TraitItem, - TraitItemType, Type, TypeBinding, UnresolvedGenerics, UnresolvedType, + ExpressionKind, Generics, Ident, LetStatement, Literal, NoirFunction, NoirStruct, NoirTrait, + NoirTypeAlias, ParsedModule, Shared, StructType, TraitItem, TraitItemType, Type, TypeBinding, + UnresolvedGenerics, UnresolvedType, }; use fm::FileId; use iter_extended::vecmap; @@ -438,12 +438,8 @@ fn resolve_trait_methods( { let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file); let arguments = vecmap(parameters, |param| resolver.resolve_type(param.1.clone())); - let resolved_return_type = match return_type { - FunctionReturnType::Default(_) => None, - FunctionReturnType::Ty(unresolved_type, _span) => { - Some(resolver.resolve_type(unresolved_type.clone())) - } - }; + let resolved_return_type = resolver.resolve_type(return_type.clone()); + let name = name.clone(); // TODO let generics: Generics = vec![]; diff --git a/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs b/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs index 809623623ee..9bbe228426f 100644 --- a/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -6,9 +6,8 @@ use crate::{ hir::def_collector::dc_crate::{UnresolvedStruct, UnresolvedTrait}, node_interner::TraitId, parser::SubModule, - FunctionDefinition, FunctionReturnType, Ident, LetStatement, NoirFunction, NoirStruct, - NoirTrait, NoirTypeAlias, ParsedModule, TraitImpl, TraitImplItem, TraitItem, TypeImpl, - UnresolvedType, + FunctionDefinition, Ident, LetStatement, NoirFunction, NoirStruct, NoirTrait, NoirTypeAlias, + ParsedModule, TraitImpl, TraitImplItem, TraitItem, TypeImpl, UnresolvedType, }; use super::{ @@ -101,11 +100,11 @@ fn check_trait_method_implementation_parameters( } fn check_trait_method_implementation_return_type( - expected_return_type: &FunctionReturnType, + expected_return_type: &UnresolvedType, impl_method: &NoirFunction, trait_name: &str, ) -> Result<(), DefCollectorErrorKind> { - if expected_return_type.get_type() == impl_method.def.return_type.get_type() { + if expected_return_type.typ == impl_method.def.return_type.typ { Ok(()) } else { Err(DefCollectorErrorKind::MismatchTraitImplementationReturnType { diff --git a/crates/noirc_frontend/src/hir/def_map/aztec_library.rs b/crates/noirc_frontend/src/hir/def_map/aztec_library.rs index f357ba5b740..f2edf76f7cd 100644 --- a/crates/noirc_frontend/src/hir/def_map/aztec_library.rs +++ b/crates/noirc_frontend/src/hir/def_map/aztec_library.rs @@ -4,10 +4,10 @@ use noirc_errors::{CustomDiagnostic, Span}; use crate::graph::CrateId; use crate::{ hir::Context, token::Attribute, BlockExpression, CallExpression, CastExpression, Distinctness, - Expression, ExpressionKind, ForExpression, FunctionReturnType, Ident, ImportStatement, - IndexExpression, LetStatement, Literal, MemberAccessExpression, MethodCallExpression, - NoirFunction, ParsedModule, Path, PathKind, Pattern, Statement, UnresolvedType, - UnresolvedTypeData, Visibility, + Expression, ExpressionKind, ForExpression, Ident, ImportStatement, IndexExpression, + LetStatement, Literal, MemberAccessExpression, MethodCallExpression, NoirFunction, + ParsedModule, Path, PathKind, Pattern, Statement, UnresolvedType, UnresolvedTypeData, + Visibility, }; use noirc_errors::FileDiagnostic; @@ -502,11 +502,10 @@ fn make_castable_return_type(expression: Expression) -> Statement { /// fn foo() { /// // ... /// } -pub(crate) fn create_return_type(ty: &str) -> FunctionReturnType { +pub(crate) fn create_return_type(ty: &str) -> UnresolvedType { let return_path = chained_path!("aztec", "abi", ty); - let ty = make_type(UnresolvedTypeData::Named(return_path, vec![])); - FunctionReturnType::Ty(ty, Span::default()) + make_type(UnresolvedTypeData::Named(return_path, vec![])) } /// Create Context Finish diff --git a/crates/noirc_frontend/src/hir/type_check/errors.rs b/crates/noirc_frontend/src/hir/type_check/errors.rs index cd8d87435c9..4ada6bff9bb 100644 --- a/crates/noirc_frontend/src/hir/type_check/errors.rs +++ b/crates/noirc_frontend/src/hir/type_check/errors.rs @@ -6,8 +6,9 @@ use thiserror::Error; use crate::hir::resolution::errors::ResolverError; use crate::hir_def::expr::HirBinaryOp; use crate::hir_def::types::Type; -use crate::FunctionReturnType; use crate::Signedness; +use crate::UnresolvedType; +use crate::UnresolvedTypeData; #[derive(Error, Debug, Clone, PartialEq, Eq)] pub enum Source { @@ -26,7 +27,7 @@ pub enum Source { #[error("BinOp")] BinOp, #[error("Return")] - Return(FunctionReturnType, Span), + Return(UnresolvedType, Span), } #[derive(Error, Debug, Clone, PartialEq, Eq)] @@ -205,13 +206,11 @@ impl From for Diagnostic { Source::Comparison => format!("Unsupported types for comparison: {expected} and {actual}"), Source::BinOp => format!("Unsupported types for binary operation: {expected} and {actual}"), Source::Return(ret_ty, expr_span) => { - let ret_ty_span = match ret_ty { - FunctionReturnType::Default(span) | FunctionReturnType::Ty(_, span) => span - }; + let ret_ty_span = ret_ty.span.expect("return type should always have a span"); let mut diagnostic = Diagnostic::simple_error(format!("expected type {expected}, found type {actual}"), format!("expected {expected} because of return type"), ret_ty_span); - if let FunctionReturnType::Default(_) = ret_ty { + if let UnresolvedTypeData::Unit = ret_ty.typ { diagnostic.add_note(format!("help: try adding a return type: `-> {actual}`")); } diff --git a/crates/noirc_frontend/src/hir/type_check/mod.rs b/crates/noirc_frontend/src/hir/type_check/mod.rs index 8596a9cc28c..a465f1d5f44 100644 --- a/crates/noirc_frontend/src/hir/type_check/mod.rs +++ b/crates/noirc_frontend/src/hir/type_check/mod.rs @@ -194,7 +194,7 @@ mod test { }, parse_program, FunctionKind, Path, }; - use crate::{BinaryOpKind, Distinctness, FunctionReturnType, Visibility}; + use crate::{BinaryOpKind, Distinctness, UnresolvedTypeData, Visibility}; #[test] fn basic_let() { @@ -275,7 +275,7 @@ mod test { return_visibility: Visibility::Private, return_distinctness: Distinctness::DuplicationAllowed, has_body: true, - return_type: FunctionReturnType::Default(Span::default()), + return_type: crate::UnresolvedType { typ: UnresolvedTypeData::Unit, span: None }, }; interner.push_fn_meta(func_meta, func_id); diff --git a/crates/noirc_frontend/src/hir_def/function.rs b/crates/noirc_frontend/src/hir_def/function.rs index 5ef2e89d81f..8e3f210d800 100644 --- a/crates/noirc_frontend/src/hir_def/function.rs +++ b/crates/noirc_frontend/src/hir_def/function.rs @@ -6,7 +6,7 @@ use super::stmt::HirPattern; use crate::hir::def_map::ModuleId; use crate::node_interner::{ExprId, NodeInterner}; use crate::{token::Attribute, FunctionKind}; -use crate::{ContractFunctionType, Distinctness, FunctionReturnType, Type, Visibility}; +use crate::{ContractFunctionType, Distinctness, Type, UnresolvedType, Visibility}; /// A Hir function is a block expression /// with a list of statements @@ -116,7 +116,7 @@ pub struct FuncMeta { pub parameters: Parameters, - pub return_type: FunctionReturnType, + pub return_type: UnresolvedType, pub return_visibility: Visibility, diff --git a/crates/noirc_frontend/src/hir_def/types.rs b/crates/noirc_frontend/src/hir_def/types.rs index 8372f7a0355..228e30db884 100644 --- a/crates/noirc_frontend/src/hir_def/types.rs +++ b/crates/noirc_frontend/src/hir_def/types.rs @@ -130,7 +130,7 @@ pub enum TraitItemType { name: Ident, generics: Generics, arguments: Vec, - return_type: Option, + return_type: Type, span: Span, }, diff --git a/crates/noirc_frontend/src/parser/parser.rs b/crates/noirc_frontend/src/parser/parser.rs index 4a2de3850d2..cdd83619dc3 100644 --- a/crates/noirc_frontend/src/parser/parser.rs +++ b/crates/noirc_frontend/src/parser/parser.rs @@ -38,10 +38,10 @@ use crate::parser::{force, ignore_then_commit, statement_recovery}; use crate::token::{Attribute, Keyword, Token, TokenKind}; use crate::{ BinaryOp, BinaryOpKind, BlockExpression, ConstrainStatement, Distinctness, FunctionDefinition, - FunctionReturnType, Ident, IfExpression, InfixExpression, LValue, Lambda, Literal, - NoirFunction, NoirStruct, NoirTrait, NoirTypeAlias, Path, PathKind, Pattern, Recoverable, - TraitBound, TraitConstraint, TraitImpl, TraitImplItem, TraitItem, TypeImpl, UnaryOp, - UnresolvedTypeExpression, UseTree, UseTreeKind, Visibility, + Ident, IfExpression, InfixExpression, LValue, Lambda, Literal, NoirFunction, NoirStruct, + NoirTrait, NoirTypeAlias, Path, PathKind, Pattern, Recoverable, TraitBound, TraitConstraint, + TraitImpl, TraitImplItem, TraitItem, TypeImpl, UnaryOp, UnresolvedTypeExpression, UseTree, + UseTreeKind, Visibility, }; use chumsky::prelude::*; @@ -261,17 +261,17 @@ fn lambda_return_type() -> impl NoirParser { .map(|ret| ret.unwrap_or_else(UnresolvedType::unspecified)) } -fn function_return_type() -> impl NoirParser<((Distinctness, Visibility), FunctionReturnType)> { +fn function_return_type() -> impl NoirParser<((Distinctness, Visibility), UnresolvedType)> { just(Token::Arrow) .ignore_then(optional_distinctness()) .then(optional_visibility()) .then(spanned(parse_type())) .or_not() .map_with_span(|ret, span| match ret { - Some((head, (ty, span))) => (head, FunctionReturnType::Ty(ty, span)), + Some((head, (ty, _))) => (head, ty), None => ( (Distinctness::DuplicationAllowed, Visibility::Private), - FunctionReturnType::Default(span), + UnresolvedType { typ: UnresolvedTypeData::Unit, span: Some(span) }, ), }) }