From a76a51bcd7e105a37ce1457fbeed249c127da165 Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 2 Jul 2020 05:48:43 -0700 Subject: [PATCH] fix multiple import bug and update tests --- .../src/constraints/definitions/import.rs | 30 ++++++++--------- .../definitions/imported_symbols.rs | 25 +++++++-------- .../src/constraints/definitions/symbol.rs | 2 +- compiler/tests/import/many_import.leo | 6 ++-- compiler/tests/import/mod.rs | 32 +++++++++---------- 5 files changed, 44 insertions(+), 51 deletions(-) diff --git a/compiler/src/constraints/definitions/import.rs b/compiler/src/constraints/definitions/import.rs index cf917610f6..fdb8c1b1b5 100644 --- a/compiler/src/constraints/definitions/import.rs +++ b/compiler/src/constraints/definitions/import.rs @@ -13,26 +13,22 @@ impl> ConstrainedProgram { // get imported program name from import // get imported symbols from from import let imported_symbols = ImportedSymbols::from(import); - let program_name = imported_symbols.name.clone(); - // get imported program from hashmap - let program = imported_programs - .get(&program_name) - .ok_or(ImportError::unknown_package(import.package.name.clone()))?; + for (package, symbol) in imported_symbols.symbols { + // get imported program from hashmap + let program = imported_programs + .get(&package) + .ok_or(ImportError::unknown_package(import.package.name.clone()))?; - // resolve imported program's import statements - program - .imports - .iter() - .map(|import| self.store_import(program_name.clone(), import, imported_programs)) - .collect::, ImportError>>()?; + // resolve imported program's import statements + program + .imports + .iter() + .map(|import| self.store_import(package.clone(), import, imported_programs)) + .collect::, ImportError>>()?; - // store imported symbols in constrained program - imported_symbols - .symbols - .iter() - .map(|symbol| self.store_symbol(scope.clone(), program_name.clone(), symbol, program)) - .collect::, ImportError>>()?; + self.store_symbol(scope.clone(), package, &symbol, program)?; + } Ok(()) } diff --git a/compiler/src/constraints/definitions/imported_symbols.rs b/compiler/src/constraints/definitions/imported_symbols.rs index 9d292091b8..9926750265 100644 --- a/compiler/src/constraints/definitions/imported_symbols.rs +++ b/compiler/src/constraints/definitions/imported_symbols.rs @@ -2,38 +2,37 @@ use leo_types::{Import, ImportSymbol, Package, PackageAccess}; #[derive(Debug)] pub(crate) struct ImportedSymbols { - pub name: String, - pub symbols: Vec, + pub symbols: Vec<(String, ImportSymbol)>, } impl ImportedSymbols { - fn new(name: String) -> Self { - Self { name, symbols: vec![] } + fn new() -> Self { + Self { symbols: vec![] } } pub(crate) fn from(import: &Import) -> Self { - let mut symbols = Self::new(import.package.name.name.clone()); + let mut symbols = Self::new(); - symbols.from_package_access(&import.package.access); + symbols.from_package(&import.package); symbols } fn from_package(&mut self, package: &Package) { - self.name = package.name.name.clone(); - - self.from_package_access(&package.access); + self.from_package_access(package.name.name.clone(), &package.access); } - fn from_package_access(&mut self, access: &PackageAccess) { + fn from_package_access(&mut self, package: String, access: &PackageAccess) { match access { PackageAccess::SubPackage(package) => self.from_package(package), PackageAccess::Star(span) => { let star = ImportSymbol::star(span); - self.symbols.push(star); + self.symbols.push((package, star)); } - PackageAccess::Symbol(symbol) => self.symbols.push(symbol.clone()), - PackageAccess::Multiple(packages) => packages.iter().for_each(|access| self.from_package_access(access)), + PackageAccess::Symbol(symbol) => self.symbols.push((package, symbol.clone())), + PackageAccess::Multiple(packages) => packages + .iter() + .for_each(|access| self.from_package_access(package.clone(), access)), } } } diff --git a/compiler/src/constraints/definitions/symbol.rs b/compiler/src/constraints/definitions/symbol.rs index 79db3c990b..5fd7366ddc 100644 --- a/compiler/src/constraints/definitions/symbol.rs +++ b/compiler/src/constraints/definitions/symbol.rs @@ -57,7 +57,7 @@ impl> ConstrainedProgram { program_name.clone(), Box::new(ConstrainedValue::Function(None, function.clone())), ), - None => return Err(ImportError::unknown_symbol(symbol.to_owned(), scope)), + None => return Err(ImportError::unknown_symbol(symbol.to_owned(), program_name)), } } }; diff --git a/compiler/tests/import/many_import.leo b/compiler/tests/import/many_import.leo index d67b775cbe..8fad0137a9 100644 --- a/compiler/tests/import/many_import.leo +++ b/compiler/tests/import/many_import.leo @@ -12,12 +12,12 @@ import bar.( // imports directory import import car.Car; // imports directory import function main() -> u32 { - // const point = Point { x: 1u32, y: 1u32 }; - // const foo = foo(); + const point = Point { x: 1u32, y: 1u32 }; + const foo = foo(); const bar = Bar { r: 1u32 }; - const bat = Bat { t: 1u32 }; const baz = Baz { z: 1u32 }; + const bat = Bat { t: 1u32 }; const car = Car { c: 1u32 }; diff --git a/compiler/tests/import/mod.rs b/compiler/tests/import/mod.rs index 1feb2c02de..4f00d4259c 100644 --- a/compiler/tests/import/mod.rs +++ b/compiler/tests/import/mod.rs @@ -1,4 +1,4 @@ -use crate::{get_error, integers::u32::output_one, parse_program}; +use crate::{integers::u32::output_one, parse_program}; use std::env::{current_dir, set_current_dir}; @@ -16,55 +16,53 @@ fn set_local_dir() { #[test] #[ignore] fn test_basic() { + set_local_dir(); + let bytes = include_bytes!("basic.leo"); let program = parse_program(bytes).unwrap(); - set_local_dir(); - output_one(program); } #[test] #[ignore] fn test_multiple() { + set_local_dir(); + let bytes = include_bytes!("multiple.leo"); let program = parse_program(bytes).unwrap(); - set_local_dir(); - output_one(program); } #[test] #[ignore] fn test_star() { + set_local_dir(); + let bytes = include_bytes!("star.leo"); let program = parse_program(bytes).unwrap(); - set_local_dir(); - output_one(program); } #[test] #[ignore] fn test_star_fail() { - let bytes = include_bytes!("star_fail.leo"); - let program = parse_program(bytes).unwrap(); - set_local_dir(); - let _err = get_error(program); + let bytes = include_bytes!("star_fail.leo"); + assert!(parse_program(bytes).is_err()); } #[test] #[ignore] fn test_alias() { + set_local_dir(); + let bytes = include_bytes!("alias.leo"); let program = parse_program(bytes).unwrap(); - set_local_dir(); - output_one(program); } @@ -72,21 +70,21 @@ fn test_alias() { #[test] #[ignore] fn test_many_import() { + set_local_dir(); + let bytes = include_bytes!("many_import.leo"); let program = parse_program(bytes).unwrap(); - set_local_dir(); - output_one(program); } #[test] #[ignore] fn test_many_import_star() { + set_local_dir(); + let bytes = include_bytes!("many_import_star.leo"); let program = parse_program(bytes).unwrap(); - set_local_dir(); - output_one(program); }