Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend)!: Explicit numeric generics and type kinds #5155

Merged
merged 101 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
b0a0bc9
initial work on explicit numeric generics and initial working tests
vezenovm May 30, 2024
6a6081f
error for bad numeric generic type
vezenovm May 30, 2024
49362cf
merge w/ master and working explicit numeric generics
vezenovm May 31, 2024
c9e84e4
fmt and clippy
vezenovm May 31, 2024
cef55da
fully working explciit num generics in resolver elaborator still has …
vezenovm May 31, 2024
84f4b2c
working numeric_generics test with elaborator
vezenovm Jun 3, 2024
0e56748
little cleanup
vezenovm Jun 3, 2024
3de41d6
error for numeric generic used as type
vezenovm Jun 4, 2024
8c2860a
fmt and clippy
vezenovm Jun 4, 2024
8809853
merge w/ master
vezenovm Jun 4, 2024
11655d5
add span method ot UnresolvedGeneric
vezenovm Jun 4, 2024
2a098bf
remove dbgs
vezenovm Jun 4, 2024
883dfa4
accurate checks against numeric generic used as a type
vezenovm Jun 4, 2024
fe28f92
remove bad errros
vezenovm Jun 4, 2024
e7dcd2b
improve err for numeric generic used in let type annotation
vezenovm Jun 4, 2024
214ed01
merge master
vezenovm Jun 4, 2024
6fff4c8
fix frontend/tests
vezenovm Jun 4, 2024
b7feb20
use type::error for bad numeric generic type
vezenovm Jun 4, 2024
8159a54
use numeric generic in for_loop_over_array test
vezenovm Jun 4, 2024
2154016
make separate numeric_generics_explicit test and ignored non elaborat…
vezenovm Jun 5, 2024
fe8f2ca
merge conflicts w/ master
vezenovm Jun 5, 2024
e5bfaea
cleanup with no numeric generics in the old resovler
vezenovm Jun 5, 2024
e98df52
cleanup
vezenovm Jun 5, 2024
6dc164a
add TypeKind for Numeric generics and also get them working with impl…
vezenovm Jun 7, 2024
265e385
fixed remaining errors in stdlib with elaborator and got warning for …
vezenovm Jun 11, 2024
c22732e
merge w/ master
vezenovm Jun 11, 2024
03d8c4d
clippy and fmt
vezenovm Jun 11, 2024
316cc47
cleanup and fix resolver add-generics from merge
vezenovm Jun 11, 2024
45cec2d
a little more comments and cleanup
vezenovm Jun 11, 2024
5f9536b
use push and pop scope
vezenovm Jun 11, 2024
6f387cb
fix panic in numeric_generic_binary_operation_type_mismatch
vezenovm Jun 11, 2024
3165bc7
use actual typ even if incorrect for accurate type checking when addi…
vezenovm Jun 11, 2024
b6f6813
Merge branch 'master' into mv/explicit-numeric-generics
vezenovm Jun 12, 2024
cf6e9a8
merge conflicts w/ master
vezenovm Jun 12, 2024
c880850
update comment
vezenovm Jun 12, 2024
89f51f3
additional comment
vezenovm Jun 12, 2024
901c970
resolved merge conflicts w/ master
vezenovm Jun 12, 2024
fe97064
more robust numeric generic parsing
vezenovm Jun 13, 2024
65aae29
update comments with issue referencing elaborator
vezenovm Jun 13, 2024
6d328cb
merge conflicts w/ master
vezenovm Jun 14, 2024
e5b12d5
Update compiler/noirc_frontend/src/hir_def/types.rs
vezenovm Jun 14, 2024
7b31c30
switch TypeKind -> Kind and Kind rather than bool in ResolvedGeneric
vezenovm Jun 14, 2024
63302d0
remove resolve_numeric_type as it is unused in favor of passing in th…
vezenovm Jun 14, 2024
1554149
Update compiler/noirc_frontend/src/parser/parser/function.rs
vezenovm Jun 14, 2024
8c5fd98
Update tooling/nargo_cli/build.rs
vezenovm Jun 14, 2024
aa53a05
Update tooling/nargo_fmt/tests/input/fn.nr
vezenovm Jun 14, 2024
09bcfe0
shorten numeric_generics_explicit test
vezenovm Jun 14, 2024
3db59b8
correctness of nuric generics test
vezenovm Jun 14, 2024
6fa665e
remove at_least(1) for generics
vezenovm Jun 14, 2024
759b7ec
Update compiler/noirc_frontend/src/parser/parser/structs.rs
vezenovm Jun 14, 2024
eff5960
remove unnecessary unwrap_or_default
vezenovm Jun 14, 2024
752e7f0
rename generic_variable -> generic_type in parser
vezenovm Jun 14, 2024
c0fb7ef
still destructure named generic in resolve_type_inner
vezenovm Jun 14, 2024
123256b
Update compiler/noirc_frontend/src/elaborator/mod.rs
vezenovm Jun 16, 2024
a021370
ident and into_ident instead of from
vezenovm Jun 17, 2024
1d05612
break in inner loop for finding implicit num generics in struct
vezenovm Jun 17, 2024
43cb4d0
remove generic_idents and switch collection of generics into dc_mod …
vezenovm Jun 17, 2024
6146793
merge conflicts w/ master
vezenovm Jun 17, 2024
0879ffa
clippy
vezenovm Jun 17, 2024
57e5bd5
get definition typ accurate and add a type to the numeric kind
vezenovm Jun 17, 2024
f14de96
resolve build.rs
vezenovm Jun 17, 2024
4a1f899
fix up frontend tests
vezenovm Jun 19, 2024
0bec263
merge conflicts w/ master
vezenovm Jun 19, 2024
31e79df
update fmt to handle more complex generics
vezenovm Jun 20, 2024
7ddae3e
ignore stdlib warnings for now until I update the stdlib
vezenovm Jun 20, 2024
bcaff2b
merge conflicts w/ master
vezenovm Jun 20, 2024
c58b7a6
fix NamedGeneric signature in is_valid_for_unconstrained_boundary
vezenovm Jun 20, 2024
4a7a994
fixup trailing in fmt item and build for nargo_cli
vezenovm Jun 20, 2024
36be839
expand comment in numeric_generic_in_trait_impl_with_extra_impl_gener…
vezenovm Jun 20, 2024
5a290d9
use resolve_generics inside of HirContext
vezenovm Jun 20, 2024
ebc38cb
fmt fix in resolve_trait-bound
vezenovm Jun 20, 2024
3ef133e
mvoe nested slice check
vezenovm Jun 20, 2024
818667e
remove ResolvedGeneric::dummy
vezenovm Jun 20, 2024
4001dfd
update GenericTypeVars comment
vezenovm Jun 20, 2024
154782d
check generic args for resolve_trait_as_type
vezenovm Jun 20, 2024
e3cf451
last cleanup in frontend tests and setting of implicit numeric struct…
vezenovm Jun 20, 2024
5ebd02d
Update compiler/noirc_frontend/src/elaborator/mod.rs
vezenovm Jun 20, 2024
12d44cd
merge w master
vezenovm Jun 20, 2024
d6ccab1
make resolve_numeric_kind_type inside of UnresolvedGeneric kind()
vezenovm Jun 20, 2024
25d640a
return result on kind() method of UnresolvedGeneric
vezenovm Jun 21, 2024
c0a375c
don't return generics from add_existing_generics now that they are re…
vezenovm Jun 21, 2024
05befe5
missed dbg
vezenovm Jun 21, 2024
1d8d65f
Merge branch 'master' into mv/explicit-numeric-generics
vezenovm Jun 21, 2024
13fad23
Update compiler/noirc_frontend/src/hir/def_collector/errors.rs
vezenovm Jun 21, 2024
9da3de6
Update compiler/noirc_frontend/src/elaborator/traits.rs
vezenovm Jun 21, 2024
98fd197
remove cmmented out struct generics update
vezenovm Jun 21, 2024
a6ec927
add back struct_numeric_generic_in_function test
vezenovm Jun 21, 2024
f87bc32
fix bad return type from context.resolve_generics
vezenovm Jun 21, 2024
8ba4cec
return DefCollectorErrorKind directly in resolve_numeric_kind_type
vezenovm Jun 21, 2024
2f7315a
cleanup kind method and error for kinds
vezenovm Jun 21, 2024
5dcd517
fix resolution of constant in turbofish:
vezenovm Jun 21, 2024
d3f36e7
fixup some tests
vezenovm Jun 21, 2024
1e5effa
use explicit num generics in regression_4124
vezenovm Jun 21, 2024
a46ba67
comment out resolved_type.kind() != kind check
vezenovm Jun 21, 2024
9bc16a4
clippy
vezenovm Jun 21, 2024
4ee9bc4
fixup test
vezenovm Jun 21, 2024
122c38b
merge conflicts w/ master
vezenovm Jun 24, 2024
af7e2f1
merge conflicts err
vezenovm Jun 24, 2024
dfc6345
fixup bad merge
vezenovm Jun 24, 2024
18cde4b
check type variable bindings and named generics in find_numeric_type_…
vezenovm Jun 24, 2024
f360c7d
Cargo fmt
vezenovm Jun 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aztec_macros/src/transforms/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn inject_context_in_storage(module: &mut SortedModule) -> Result<(), AztecM
r#struct.attributes.iter().any(|attr| is_custom_attribute(attr, "aztec(storage)"))
})
.unwrap();
storage_struct.generics.push(ident("Context"));
storage_struct.generics.push(ident("Context").into());
storage_struct
.fields
.iter_mut()
Expand Down Expand Up @@ -243,7 +243,7 @@ pub fn generate_storage_implementation(
span: Some(Span::default()),
},
type_span: Span::default(),
generics: vec![generic_context_ident],
generics: vec![generic_context_ident.into()],

methods: vec![(init, Span::default())],
};
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_driver/tests/stdlib_warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::Path;
use noirc_driver::{file_manager_with_stdlib, prepare_crate, ErrorsAndWarnings};
use noirc_frontend::hir::{def_map::parse_file, Context};

#[ignore = "Temporarily ignoring the test until the stdlib is updated to use explicit numeric generics"]
jfecher marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn stdlib_does_not_produce_constant_warnings() -> Result<(), ErrorsAndWarnings> {
// We use a minimal source file so that if stdlib produces warnings then we can expect these warnings to _always_
Expand Down
69 changes: 68 additions & 1 deletion compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::ast::{
Ident, ItemVisibility, Path, Pattern, Recoverable, Statement, StatementKind,
UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData, Visibility,
};
use crate::hir::def_collector::errors::DefCollectorErrorKind;
use crate::macros_api::StructId;
use crate::node_interner::ExprId;
use crate::token::{Attributes, Token, Tokens};
use crate::{Kind, Type};
use acvm::{acir::AcirField, FieldElement};
use iter_extended::vecmap;
use noirc_errors::{Span, Spanned};
Expand Down Expand Up @@ -46,7 +48,72 @@ pub enum ExpressionKind {

/// A Vec of unresolved names for type variables.
/// For `fn foo<A, B>(...)` this corresponds to vec!["A", "B"].
pub type UnresolvedGenerics = Vec<Ident>;
pub type UnresolvedGenerics = Vec<UnresolvedGeneric>;

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum UnresolvedGeneric {
Variable(Ident),
Numeric { ident: Ident, typ: UnresolvedType },
}

impl UnresolvedGeneric {
pub fn span(&self) -> Span {
match self {
UnresolvedGeneric::Variable(ident) => ident.0.span(),
UnresolvedGeneric::Numeric { ident, typ } => {
ident.0.span().merge(typ.span.unwrap_or_default())
}
}
}

pub fn kind(&self) -> Result<Kind, DefCollectorErrorKind> {
match self {
UnresolvedGeneric::Variable(_) => Ok(Kind::Normal),
UnresolvedGeneric::Numeric { typ, .. } => {
let typ = self.resolve_numeric_kind_type(typ)?;
Ok(Kind::Numeric(Box::new(typ)))
}
}
}

fn resolve_numeric_kind_type(
&self,
typ: &UnresolvedType,
) -> Result<Type, DefCollectorErrorKind> {
use crate::ast::UnresolvedTypeData::{FieldElement, Integer};

match typ.typ {
FieldElement => Ok(Type::FieldElement),
Integer(sign, bits) => Ok(Type::Integer(sign, bits)),
// Only fields and integers are supported for numeric kinds
_ => Err(DefCollectorErrorKind::UnsupportedNumericGenericType {
ident: self.ident().clone(),
typ: typ.typ.clone(),
}),
}
}

pub(crate) fn ident(&self) -> &Ident {
match self {
UnresolvedGeneric::Variable(ident) | UnresolvedGeneric::Numeric { ident, .. } => ident,
}
}
}

impl Display for UnresolvedGeneric {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UnresolvedGeneric::Variable(ident) => write!(f, "{ident}"),
UnresolvedGeneric::Numeric { ident, typ } => write!(f, "let {ident}: {typ}"),
}
}
}

impl From<Ident> for UnresolvedGeneric {
fn from(value: Ident) -> Self {
UnresolvedGeneric::Variable(value)
}
}

impl ExpressionKind {
pub fn into_path(self) -> Option<Path> {
Expand Down
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub struct UnaryRhsMethodCall {
}

/// The precursor to TypeExpression, this is the type that the parser allows
/// to be used in the length position of an array type. Only constants, variables,
/// to be used in the length position of an array type. Only constant integers, variables,
/// and numeric binary operators are allowed here.
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum UnresolvedTypeExpression {
Expand Down Expand Up @@ -259,6 +259,10 @@ impl UnresolvedType {
pub fn unspecified() -> UnresolvedType {
UnresolvedType { typ: UnresolvedTypeData::Unspecified, span: None }
}

pub(crate) fn is_type_expression(&self) -> bool {
matches!(&self.typ, UnresolvedTypeData::Expression(_))
}
}

impl UnresolvedTypeData {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/ast/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl NoirStruct {
pub fn new(
name: Ident,
attributes: Vec<SecondaryAttribute>,
generics: Vec<Ident>,
generics: UnresolvedGenerics,
fields: Vec<(Ident, UnresolvedType)>,
span: Span,
) -> NoirStruct {
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/ast/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::node_interner::TraitId;
#[derive(Clone, Debug)]
pub struct NoirTrait {
pub name: Ident,
pub generics: Vec<Ident>,
pub generics: UnresolvedGenerics,
pub where_clause: Vec<UnresolvedTraitConstraint>,
pub span: Span,
pub items: Vec<TraitItem>,
Expand All @@ -26,7 +26,7 @@ pub struct NoirTrait {
pub enum TraitItem {
Function {
name: Ident,
generics: Vec<Ident>,
generics: UnresolvedGenerics,
parameters: Vec<(Ident, UnresolvedType)>,
return_type: FunctionReturnType,
where_clause: Vec<UnresolvedTraitConstraint>,
Expand Down
17 changes: 15 additions & 2 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
},
node_interner::{DefinitionKind, ExprId, FuncId},
token::Tokens,
QuotedType, Shared, StructType, Type,
Kind, QuotedType, Shared, StructType, Type,
};

use super::Elaborator;
Expand All @@ -52,7 +52,20 @@ impl<'context> Elaborator<'context> {
ExpressionKind::If(if_) => self.elaborate_if(*if_),
ExpressionKind::Variable(variable, generics) => {
let generics = generics.map(|option_inner| {
option_inner.into_iter().map(|generic| self.resolve_type(generic)).collect()
option_inner
.into_iter()
.map(|generic| {
// All type expressions should resolve to a `Type::Constant`
if generic.is_type_expression() {
self.resolve_type_inner(
generic,
&Kind::Numeric(Box::new(Type::default_int_type())),
)
} else {
self.resolve_type(generic)
}
})
.collect()
});
return self.elaborate_variable(variable, generics);
}
Expand Down
Loading
Loading