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: merge and sort imports #6322

Merged
merged 15 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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: 3 additions & 1 deletion compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ impl UseTree {

match self.kind {
UseTreeKind::Path(name, alias) => {
vec![ImportStatement { visibility, path: prefix.join(name), alias }]
// Desugar `use foo::{self}` to `use foo`
let path = if name.0.contents == "self" { prefix } else { prefix.join(name) };
vec![ImportStatement { visibility, path, alias }]
}
UseTreeKind::List(trees) => {
let trees = trees.into_iter();
Expand Down
15 changes: 15 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/use_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ impl<'a> Parser<'a> {
fn parse_use_tree_in_list(&mut self) -> Option<UseTree> {
let start_span = self.current_token_span;

// Special case: "self" cannot be followed by anything else
if self.eat_self() {
return Some(UseTree {
prefix: Path { segments: Vec::new(), kind: PathKind::Plain, span: start_span },
kind: UseTreeKind::Path(Ident::new("self".to_string(), start_span), None),
});
}

let use_tree = self.parse_use_tree_without_kind(
start_span,
PathKind::Plain,
Expand Down Expand Up @@ -250,4 +258,11 @@ mod tests {
let (_, errors) = parse_program(src);
assert!(!errors.is_empty());
}

#[test]
fn errors_on_double_colon_after_self() {
let src = "use foo::{self::bar};";
let (_, errors) = parse_program(src);
assert!(!errors.is_empty());
}
}
22 changes: 22 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@
}

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 1977 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
// (originally from https://github.com/noir-lang/noir/issues/6125)
#[test]
fn numeric_generic_field_larger_than_u32() {
Expand All @@ -1991,7 +1991,7 @@
assert_eq!(errors.len(), 2);
assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),

Check warning on line 1994 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
));
assert!(matches!(
errors[1].0,
Expand All @@ -2000,7 +2000,7 @@
}

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 2003 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
// (originally from https://github.com/noir-lang/noir/issues/6126)
#[test]
fn numeric_generic_field_arithmetic_larger_than_u32() {
Expand Down Expand Up @@ -2029,7 +2029,7 @@

assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),

Check warning on line 2032 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
));

assert!(matches!(
Expand Down Expand Up @@ -2165,7 +2165,7 @@
assert_eq!(errors.len(), 3);

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 2168 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),
Expand Down Expand Up @@ -3185,7 +3185,7 @@
}

#[test]
fn arithmetic_generics_canonicalization_deduplication_regression() {

Check warning on line 3188 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (canonicalization)
let source = r#"
struct ArrData<let N: u32> {
a: [Field; N],
Expand Down Expand Up @@ -3389,3 +3389,25 @@
let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);
}

#[test]
fn uses_self_in_import() {
let src = r#"
mod moo {
pub mod bar {
pub fn foo() -> i32 {
1
}
}
}

use moo::bar::{self};

pub fn baz() -> i32 {
bar::foo()
}

fn main() {}
"#;
assert_no_errors(src);
}
2 changes: 1 addition & 1 deletion noir_stdlib/src/array/check_shuffle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ where
}

mod test {
use super::check_shuffle;
use crate::cmp::Eq;
use super::check_shuffle;

struct CompoundStruct {
a: bool,
Expand Down
4 changes: 1 addition & 3 deletions noir_stdlib/src/array/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::cmp::{Eq, Ord};
use crate::convert::From;
use crate::runtime::is_unconstrained;
use crate::{cmp::{Eq, Ord}, convert::From, runtime::is_unconstrained};

mod check_shuffle;
mod quicksort;
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/bigint.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ops::{Add, Sub, Mul, Div};
use crate::cmp::Eq;
use crate::{cmp::Eq, ops::{Add, Div, Mul, Sub}};

global bn254_fq = &[
0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/cmp.nr
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ where
}

mod cmp_tests {
use crate::cmp::{min, max};
use crate::cmp::{max, min};

#[test]
fn sanity_check_min() {
Expand Down
12 changes: 7 additions & 5 deletions noir_stdlib/src/collections/map.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::cmp::Eq;
use crate::option::Option;
use crate::default::Default;
use crate::hash::{Hash, Hasher, BuildHasher};
use crate::collections::bounded_vec::BoundedVec;
use crate::{
cmp::Eq,
collections::bounded_vec::BoundedVec,
default::Default,
hash::{BuildHasher, Hash, Hasher},
option::Option,
};

// We use load factor alpha_max = 0.75.
// Upon exceeding it, assert will fail in order to inform the user
Expand Down
5 changes: 1 addition & 4 deletions noir_stdlib/src/collections/umap.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::cmp::Eq;
use crate::option::Option;
use crate::default::Default;
use crate::hash::{Hash, Hasher, BuildHasher};
use crate::{cmp::Eq, default::Default, hash::{BuildHasher, Hash, Hasher}, option::Option};

// An unconstrained hash table with open addressing and quadratic probing.
// Note that "unconstrained" here means that almost all operations on this
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/ec/consts/te.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ec::tecurve::affine::Point as TEPoint;
use crate::ec::tecurve::affine::Curve as TECurve;
use crate::ec::tecurve::affine::{Curve as TECurve, Point as TEPoint};

pub struct BabyJubjub {
pub curve: TECurve,
Expand Down
36 changes: 20 additions & 16 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ pub mod affine {
// Points are represented by two-dimensional Cartesian coordinates.
// All group operations are induced by those of the corresponding Twisted Edwards curve.
// See e.g. <https://eprint.iacr.org/2017/212.pdf> for details on the correspondences.
use crate::ec::montcurve::curvegroup;
use crate::ec::swcurve::affine::Curve as SWCurve;
use crate::ec::swcurve::affine::Point as SWPoint;
use crate::ec::tecurve::affine::Curve as TECurve;
use crate::ec::tecurve::affine::Point as TEPoint;
use crate::ec::is_square;
use crate::ec::safe_inverse;
use crate::ec::sqrt;
use crate::ec::ZETA;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
is_square,
montcurve::curvegroup,
safe_inverse,
sqrt,
swcurve::affine::{Curve as SWCurve, Point as SWPoint},
tecurve::affine::{Curve as TECurve, Point as TEPoint},
ZETA,
},
};

// Curve specification
pub struct Curve { // Montgomery Curve configuration (ky^2 = x^3 + j*x^2 + x)
Expand Down Expand Up @@ -222,12 +224,14 @@ pub mod curvegroup {
// Points are represented by three-dimensional projective (homogeneous) coordinates.
// All group operations are induced by those of the corresponding Twisted Edwards curve.
// See e.g. <https://eprint.iacr.org/2017/212.pdf> for details on the correspondences.
use crate::ec::montcurve::affine;
use crate::ec::swcurve::curvegroup::Curve as SWCurve;
use crate::ec::swcurve::curvegroup::Point as SWPoint;
use crate::ec::tecurve::curvegroup::Curve as TECurve;
use crate::ec::tecurve::curvegroup::Point as TEPoint;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
montcurve::affine,
swcurve::curvegroup::{Curve as SWCurve, Point as SWPoint},
tecurve::curvegroup::{Curve as TECurve, Point as TEPoint},
},
};

pub struct Curve { // Montgomery Curve configuration (ky^2 z = x*(x^2 + j*x*z + z*z))
pub j: Field,
Expand Down
9 changes: 2 additions & 7 deletions noir_stdlib/src/ec/swcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ pub mod affine {
// Points are represented by two-dimensional Cartesian coordinates.
// Group operations are implemented in terms of those in CurveGroup (in this case, extended Twisted Edwards) coordinates
// for reasons of efficiency, cf. <https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates>.
use crate::ec::swcurve::curvegroup;
use crate::ec::safe_inverse;
use crate::ec::is_square;
use crate::ec::sqrt;
use crate::cmp::Eq;
use crate::{cmp::Eq, ec::{is_square, safe_inverse, sqrt, swcurve::curvegroup}};

// Curve specification
pub struct Curve { // Short Weierstrass curve
Expand Down Expand Up @@ -190,8 +186,7 @@ pub mod curvegroup {
// CurveGroup representation of Weierstrass curves
// Points are represented by three-dimensional Jacobian coordinates.
// See <https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates> for details.
use crate::ec::swcurve::affine;
use crate::cmp::Eq;
use crate::{cmp::Eq, ec::swcurve::affine};

// Curve specification
pub struct Curve { // Short Weierstrass curve
Expand Down
28 changes: 16 additions & 12 deletions noir_stdlib/src/ec/tecurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ pub mod affine {
// Group operations are implemented in terms of those in CurveGroup (in this case, extended Twisted Edwards) coordinates
// for reasons of efficiency.
// See <https://eprint.iacr.org/2008/522.pdf> for details.
use crate::ec::tecurve::curvegroup;
use crate::ec::montcurve::affine::Curve as MCurve;
use crate::ec::montcurve::affine::Point as MPoint;
use crate::ec::swcurve::affine::Curve as SWCurve;
use crate::ec::swcurve::affine::Point as SWPoint;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
montcurve::affine::{Curve as MCurve, Point as MPoint},
swcurve::affine::{Curve as SWCurve, Point as SWPoint},
tecurve::curvegroup,
},
};

// Curve specification
pub struct Curve { // Twisted Edwards curve
Expand Down Expand Up @@ -197,12 +199,14 @@ pub mod curvegroup {
// CurveGroup coordinate representation of Twisted Edwards curves
// Points are represented by four-dimensional projective coordinates, viz. extended Twisted Edwards coordinates.
// See section 3 of <https://eprint.iacr.org/2008/522.pdf> for details.
use crate::ec::tecurve::affine;
use crate::ec::montcurve::curvegroup::Curve as MCurve;
use crate::ec::montcurve::curvegroup::Point as MPoint;
use crate::ec::swcurve::curvegroup::Curve as SWCurve;
use crate::ec::swcurve::curvegroup::Point as SWPoint;
use crate::cmp::Eq;
use crate::{
cmp::Eq,
ec::{
montcurve::curvegroup::{Curve as MCurve, Point as MPoint},
swcurve::curvegroup::{Curve as SWCurve, Point as SWPoint},
tecurve::affine,
},
};

// Curve specification
pub struct Curve { // Twisted Edwards curve
Expand Down
10 changes: 5 additions & 5 deletions noir_stdlib/src/eddsa.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ec::consts::te::baby_jubjub;
use crate::ec::tecurve::affine::Point as TEPoint;
use crate::hash::Hasher;
use crate::hash::poseidon::PoseidonHasher;
use crate::default::Default;
use crate::{
default::Default,
ec::{consts::te::baby_jubjub, tecurve::affine::Point as TEPoint},
hash::{Hasher, poseidon::PoseidonHasher},
};

// Returns true if signature is valid
pub fn eddsa_poseidon_verify(
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/embedded_curve_ops.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ops::arith::{Add, Sub, Neg};
use crate::cmp::Eq;
use crate::{cmp::Eq, ops::arith::{Add, Neg, Sub}};

/// A point on the embedded elliptic curve
/// By definition, the base field of the embedded curve is the scalar field of the proof system curve, i.e the Noir Field.
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/field/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn lt(a: Field, b: Field) -> bool {
mod tests {
// TODO: Allow imports from "super"
use crate::field::bn254::{
decompose, compute_lt, assert_gt, gt, TWO_POW_128, compute_lte, PLO, PHI,
assert_gt, compute_lt, compute_lte, decompose, gt, PHI, PLO, TWO_POW_128,
};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/field/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod bn254;
use bn254::lt as bn254_lt;
use crate::runtime::is_unconstrained;
use bn254::lt as bn254_lt;

impl Field {
/// Asserts that `self` can be represented in `bit_size` bits.
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/keccak.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::collections::vec::Vec;
use crate::runtime::is_unconstrained;
use crate::{collections::vec::Vec, runtime::is_unconstrained};

global BLOCK_SIZE_IN_BYTES: u32 = 136; //(1600 - BITS * 2) / WORD_SIZE;
global WORD_SIZE: u32 = 8; // Limbs are made up of u64s so 8 bytes each.
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/mimc.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::Hasher;
use crate::default::Default;
use crate::{default::Default, hash::Hasher};

// mimc-p/p implementation
// constants are (publicly generated) random numbers, for instance using keccak as a ROM.
Expand Down
12 changes: 7 additions & 5 deletions noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod keccak;
pub mod sha256;
pub mod sha512;

use crate::default::Default;
use crate::uint128::U128;
use crate::embedded_curve_ops::{
EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return,
use crate::{
default::Default,
embedded_curve_ops::{
EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return,
},
meta::derive_via,
uint128::U128,
};
use crate::meta::derive_via;

// Kept for backwards compatibility
pub use sha256::{digest, sha256, sha256_compression, sha256_var};
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon/bn254/consts.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Used like so: sage generate_parameters_grain.sage 1 0 254 2 8 56 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
// Constants for various Poseidon instances in the case of the prime field of the same order as BN254.
// Consistent with https://github.com/iden3/circomlib/blob/master/circuits/poseidon.circom and https://github.com/iden3/circomlib/blob/master/circuits/poseidon_constants.circom
use crate::hash::poseidon::PoseidonConfig;
use crate::hash::poseidon::config;
use crate::hash::poseidon::{config, PoseidonConfig};
// S-box power
fn alpha() -> Field {
5
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon/bn254/perm.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Instantiations of Poseidon permutation for the prime field of the same order as BN254
use crate::hash::poseidon::bn254::consts;
use crate::hash::poseidon::permute;
use crate::hash::poseidon::{bn254::consts, permute};

#[field(bn254)]
pub fn x5_2(mut state: [Field; 2]) -> [Field; 2] {
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod bn254; // Instantiations of Poseidon for prime field of the same order as BN254
use crate::hash::Hasher;
use crate::default::Default;
use crate::{default::Default, hash::Hasher};

// A config struct defining the parameters of the Poseidon instance to use.
//
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/hash/poseidon2.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::Hasher;
use crate::default::Default;
use crate::{default::Default, hash::Hasher};

comptime global RATE: u32 = 3;

Expand Down
4 changes: 1 addition & 3 deletions noir_stdlib/src/meta/expr.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Contains methods on the built-in `Expr` type for quoted, syntactically valid expressions.

use crate::option::Option;
use crate::meta::op::UnaryOp;
use crate::meta::op::BinaryOp;
use crate::{meta::op::{BinaryOp, UnaryOp}, option::Option};

impl Expr {
/// If this expression is an array literal `[elem1, ..., elemN]`, this returns a slice of each element in the array.
Expand Down
4 changes: 1 addition & 3 deletions noir_stdlib/src/meta/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ pub comptime fn type_of<T>(x: T) -> Type {}

// docs:start:derive_example
// These are needed for the unconstrained hashmap we're using to store derive functions
use crate::collections::umap::UHashMap;
use crate::hash::BuildHasherDefault;
use crate::hash::poseidon2::Poseidon2Hasher;
use crate::{collections::umap::UHashMap, hash::{BuildHasherDefault, poseidon2::Poseidon2Hasher}};

// A derive function is one that given a struct definition can
// create us a quoted trait impl from it.
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/meta/quoted.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::cmp::Eq;
use crate::option::Option;
use crate::{cmp::Eq, option::Option};

impl Quoted {
#[builtin(quoted_as_expr)]
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/meta/trait_constraint.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::{Hash, Hasher};
use crate::cmp::Eq;
use crate::{cmp::Eq, hash::{Hash, Hasher}};

impl Eq for TraitConstraint {
comptime fn eq(self, other: Self) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions noir_stdlib/src/meta/trait_def.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::hash::{Hash, Hasher};
use crate::cmp::Eq;
use crate::{cmp::Eq, hash::{Hash, Hasher}};

impl TraitDefinition {
#[builtin(trait_def_as_trait_constraint)]
Expand Down
Loading
Loading