Skip to content

Commit

Permalink
chore: remove unneeded type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvitkov committed Sep 4, 2023
1 parent f06fbdb commit 30e1338
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 81 deletions.
32 changes: 3 additions & 29 deletions crates/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -367,19 +367,11 @@ pub struct FunctionDefinition {
pub body: BlockExpression,
pub span: Span,
pub where_clause: Vec<TraitConstraint>,
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)]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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}"),
}
}
}
11 changes: 3 additions & 8 deletions crates/noirc_frontend/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions crates/noirc_frontend/src/ast/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<generics> { ... items ... }`
Expand All @@ -27,7 +24,7 @@ pub enum TraitItem {
name: Ident,
generics: Vec<Ident>,
parameters: Vec<(Ident, UnresolvedType)>,
return_type: FunctionReturnType,
return_type: UnresolvedType,
where_clause: Vec<TraitConstraint>,
body: Option<BlockExpression>,
},
Expand Down
14 changes: 5 additions & 9 deletions crates/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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![];
Expand Down
9 changes: 4 additions & 5 deletions crates/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions crates/noirc_frontend/src/hir/def_map/aztec_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions crates/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,7 +27,7 @@ pub enum Source {
#[error("BinOp")]
BinOp,
#[error("Return")]
Return(FunctionReturnType, Span),
Return(UnresolvedType, Span),
}

#[derive(Error, Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -205,13 +206,11 @@ impl From<TypeCheckError> 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}`"));
}

Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_frontend/src/hir/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_frontend/src/hir_def/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -116,7 +116,7 @@ pub struct FuncMeta {

pub parameters: Parameters,

pub return_type: FunctionReturnType,
pub return_type: UnresolvedType,

pub return_visibility: Visibility,

Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub enum TraitItemType {
name: Ident,
generics: Generics,
arguments: Vec<Type>,
return_type: Option<Type>,
return_type: Type,
span: Span,
},

Expand Down
14 changes: 7 additions & 7 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -261,17 +261,17 @@ fn lambda_return_type() -> impl NoirParser<UnresolvedType> {
.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) },
),
})
}
Expand Down

0 comments on commit 30e1338

Please sign in to comment.