1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

fix tests of compile issues, change struct handling to make tests pass

This commit is contained in:
schaeff 2020-10-08 10:43:14 +02:00
parent b561614f4a
commit 2b3183a6d3
14 changed files with 57 additions and 45 deletions

View file

@ -0,0 +1,2 @@
def main():
return 1

View file

@ -1,2 +0,0 @@
def main(field a) -> field:
return a*2**3

View file

@ -2,6 +2,6 @@
// /!\ should be called with a = 0
def main(field a) -> bool:
field p = 21888242871839275222246405745257275088548364400416034343698204186575808495617 + a
field p = 21888242871839275222246405745257275088548364400416034343698204186575808495616 + a
// we added a = 0 to prevent the condition to be evaluated at compile time
return 0 < p - 1
return 0 < p

View file

@ -994,7 +994,7 @@ mod tests {
use super::*;
#[test]
fn examples() {
fn compile_examples() {
for p in glob("./examples/**/*").expect("Failed to read glob pattern") {
let path = match p {
Ok(x) => x,
@ -1007,9 +1007,7 @@ mod tests {
assert!(path.extension().expect("extension expected") == "zok");
if path.to_str().unwrap().contains("error") {
continue;
}
let should_error = path.to_str().unwrap().contains("compile_errors");
println!("Testing {:?}", path);
@ -1022,13 +1020,14 @@ mod tests {
let stdlib = std::fs::canonicalize("../zokrates_stdlib/stdlib").unwrap();
let resolver = FileSystemResolver::with_stdlib_root(stdlib.to_str().unwrap());
let _: CompilationArtifacts<Bn128Field> =
compile(source, path, Some(&resolver)).unwrap();
let res = compile::<Bn128Field, _>(source, path, Some(&resolver));
assert_eq!(res.is_err(), should_error);
}
}
#[test]
fn examples_with_input_success() {
fn execute_examples_ok() {
//these examples should compile and run
for p in glob("./examples/test*").expect("Failed to read glob pattern") {
let path = match p {
@ -1059,7 +1058,7 @@ mod tests {
#[test]
#[should_panic]
fn examples_with_input_failure() {
fn execute_examples_err() {
//these examples should compile but not run
for p in glob("./examples/runtime_errors/*").expect("Failed to read glob pattern") {
let path = match p {

View file

@ -292,7 +292,7 @@ mod test {
#[test]
fn use_struct_declaration_types() {
// when importing types and renaming them, we use the top-most renaming in the ABI
// when importing types and renaming them, we use the canonical struct names in the ABI
// // main.zok
// from foo import Foo as FooMain
@ -305,7 +305,7 @@ mod test {
// struct Bar { field a }
// Expected resolved type for FooMain:
// FooMain { BarFoo b }
// Foo { Bar b }
let main = r#"
from "foo" import Foo as FooMain
@ -370,21 +370,21 @@ struct Bar { field a }
inputs: vec![AbiInput {
name: "f".into(),
public: true,
ty: Type::Struct(StructType {
module: "main".into(),
name: "FooMain".into(),
members: vec![StructMember {
ty: Type::Struct(StructType::new(
"foo".into(),
"Foo".into(),
vec![StructMember {
id: "b".into(),
ty: box Type::Struct(StructType {
module: "foo".into(),
name: "BarFoo".into(),
members: vec![StructMember {
ty: box Type::Struct(StructType::new(
"bar".into(),
"Bar".into(),
vec![StructMember {
id: "a".into(),
ty: box Type::FieldElement
}]
})
))
}]
})
))
}],
outputs: vec![]
}

View file

@ -16,7 +16,7 @@ use zokrates_field::Field;
use crate::parser::Position;
use crate::absy::types::{UnresolvedSignature, UnresolvedType, UserTypeId};
use crate::typed_absy::types::{FunctionKey, Signature, Type};
use crate::typed_absy::types::{FunctionKey, Signature, StructLocation, Type};
use std::hash::{Hash, Hasher};
use typed_absy::types::{ArrayType, StructMember};
@ -124,6 +124,7 @@ impl fmt::Display for ErrorInner {
}
/// A function query in the current module.
#[derive(Debug)]
struct FunctionQuery<'ast> {
id: Identifier<'ast>,
inputs: Vec<Type>,
@ -439,8 +440,10 @@ impl<'ast> Checker<'ast> {
// rename the type to the declared symbol
let t = match t {
Type::Struct(t) => Type::Struct(StructType {
module: module_id.clone(),
name: declaration.id.into(),
location: Some(StructLocation {
name: declaration.id.into(),
module: module_id.clone()
}),
..t
}),
_ => unreachable!()

View file

@ -1,5 +1,5 @@
use std::fmt;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
pub type Identifier<'ast> = &'ast str;
@ -20,17 +20,25 @@ pub struct ArrayType {
pub ty: Box<Type>,
}
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct StructType {
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialOrd, Ord, Eq, PartialEq)]
pub struct StructLocation {
#[serde(skip)]
pub module: PathBuf,
pub name: String,
}
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct StructType {
#[serde(flatten)]
pub canonical_location: StructLocation,
#[serde(skip)]
pub location: Option<StructLocation>,
pub members: Vec<StructMember>,
}
impl PartialEq for StructType {
fn eq(&self, other: &Self) -> bool {
self.members.eq(&other.members)
self.canonical_location.eq(&other.canonical_location) && self.members.eq(&other.members)
}
}
@ -39,8 +47,8 @@ impl Eq for StructType {}
impl StructType {
pub fn new(module: PathBuf, name: String, members: Vec<StructMember>) -> Self {
StructType {
module,
name,
canonical_location: StructLocation { module, name },
location: None,
members,
}
}
@ -52,6 +60,18 @@ impl StructType {
pub fn iter(&self) -> std::slice::Iter<StructMember> {
self.members.iter()
}
fn location(&self) -> &StructLocation {
&self.location.as_ref().unwrap_or(&self.canonical_location)
}
pub fn name(&self) -> &str {
&self.location().name
}
pub fn module(&self) -> &Path {
&self.location().module
}
}
impl IntoIterator for StructType {
@ -230,7 +250,7 @@ impl fmt::Display for Type {
Type::Struct(ref struct_type) => write!(
f,
"{} {{{}}}",
struct_type.name,
struct_type.name(),
struct_type
.members
.iter()
@ -249,17 +269,7 @@ impl fmt::Debug for Type {
Type::Boolean => write!(f, "bool"),
Type::Uint(ref bitwidth) => write!(f, "u{}", bitwidth),
Type::Array(ref array_type) => write!(f, "{}[{}]", array_type.ty, array_type.size),
Type::Struct(ref struct_type) => write!(
f,
"{} {{{}}}",
struct_type.name,
struct_type
.members
.iter()
.map(|member| format!("{}: {}", member.id, member.ty))
.collect::<Vec<_>>()
.join(", ")
),
Type::Struct(ref struct_type) => write!(f, "{:?}", struct_type),
}
}
}