From 0bd580e3d138338df434c5f71742f11391404856 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Thu, 8 Jul 2021 04:05:00 -0700 Subject: [PATCH] fmt fixes, realized they have to be in that format for outputs --- .gitignore | 5 +++ Cargo.lock | 4 +-- Cargo.toml | 2 +- compiler/src/errors/value/group.rs | 7 ++++ compiler/src/errors/value/integer.rs | 4 +-- .../src/function/input/main_function_input.rs | 18 +++++++--- compiler/src/output/mod.rs | 2 ++ compiler/src/value/group/group_type.rs | 2 ++ .../src/value/group/targets/edwards_bls12.rs | 10 ++++++ compiler/src/value/integer/integer.rs | 6 +++- compiler/src/value/value.rs | 10 ++++-- leo/logger.rs | 36 +++++++++++++------ leo/main.rs | 12 +++---- ...n => different_types_const_signed_fail.in} | 0 ...=> different_types_const_unsigned_fail.in} | 0 15 files changed, 89 insertions(+), 29 deletions(-) rename tests/compiler/input_files/program_input/input/{different_types_fail_const_signedin => different_types_const_signed_fail.in} (100%) rename tests/compiler/input_files/program_input/input/{different_types_unsigned_const_fail.in => different_types_const_unsigned_fail.in} (100%) diff --git a/.gitignore b/.gitignore index f9e224d6a5..cabf0b1b00 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,8 @@ **/.crates.toml **/.crates2.json **/bin/ + +# Emacs gitignore files +*~ +\#*\# +.\#* diff --git a/Cargo.lock b/Cargo.lock index 46338d8749..a5dd7f1faa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2800,9 +2800,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa5553bf0883ba7c9cbe493b085c29926bd41b66afc31ff72cf17ff4fb60dcd5" +checksum = "ab69019741fca4d98be3c62d2b75254528b5432233fd8a4d2739fec20278de48" dependencies = [ "ansi_term 0.12.1", "chrono", diff --git a/Cargo.toml b/Cargo.toml index db5ac5af9b..bdd7707bed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -145,7 +145,7 @@ version = "0.5" version = "0.1" [dependencies.tracing-subscriber] -version = "0.2" +version = "0.2.19" features = [ "fmt" ] [dependencies.zip] diff --git a/compiler/src/errors/value/group.rs b/compiler/src/errors/value/group.rs index ae3bd107d0..65ddcd7698 100644 --- a/compiler/src/errors/value/group.rs +++ b/compiler/src/errors/value/group.rs @@ -21,6 +21,9 @@ use snarkvm_r1cs::SynthesisError; pub enum GroupError { #[error("{}", _0)] Error(#[from] FormattedError), + + #[error("Failed to represent group as log string.")] + LogError, } impl LeoError for GroupError {} @@ -30,6 +33,10 @@ impl GroupError { GroupError::Error(FormattedError::new_from_span(message, span)) } + pub fn failed_to_create_log_string() -> Self { + GroupError::LogError + } + pub fn negate_operation(error: SynthesisError, span: &Span) -> Self { let message = format!("group negation failed due to the synthesis error `{:?}`", error,); diff --git a/compiler/src/errors/value/integer.rs b/compiler/src/errors/value/integer.rs index 535162f07b..f27ce2f071 100644 --- a/compiler/src/errors/value/integer.rs +++ b/compiler/src/errors/value/integer.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use leo_ast::{FormattedError, IntegerType, LeoError, Span}; +use leo_ast::{FormattedError, LeoError, Span}; use snarkvm_gadgets::errors::{SignedIntegerError, UnsignedIntegerError}; use snarkvm_r1cs::SynthesisError; @@ -68,7 +68,7 @@ impl IntegerError { Self::new_from_span(message, span) } - pub fn integer_type_mismatch(expected: &IntegerType, received: IntegerType, span: &Span) -> Self { + pub fn integer_type_mismatch(expected: String, received: String, span: &Span) -> Self { let message = format!("expected data type `{}`, found `{}`", expected, received); Self::new_from_span(message, span) diff --git a/compiler/src/function/input/main_function_input.rs b/compiler/src/function/input/main_function_input.rs index 2cfee8e105..75379676fe 100644 --- a/compiler/src/function/input/main_function_input.rs +++ b/compiler/src/function/input/main_function_input.rs @@ -18,7 +18,7 @@ use crate::{ address::Address, - errors::FunctionError, + errors::{FunctionError, IntegerError}, program::ConstrainedProgram, value::{ boolean::input::bool_from_input, @@ -101,9 +101,19 @@ impl<'a, F: PrimeField, G: GroupType> ConstrainedProgram<'a, F, G> { Ok(ConstrainedValue::Field(FieldType::constant(cs, value, span)?)) } (Type::Group, InputValue::Group(value)) => Ok(ConstrainedValue::Group(G::constant(&value.into(), span)?)), - (Type::Integer(integer_type), InputValue::Integer(_, value)) => Ok(ConstrainedValue::Integer( - Integer::new(&ConstInt::parse(integer_type, &value, span)?), - )), + (Type::Integer(integer_type), InputValue::Integer(input_type, value)) => { + let parsed = ConstInt::parse(integer_type, &value, span)?; + let parsed_type = parsed.get_int_type(); + let input_type = input_type.into(); + if std::mem::discriminant(&parsed_type) != std::mem::discriminant(&input_type) { + return Err(FunctionError::from(IntegerError::integer_type_mismatch( + input_type.to_string(), + parsed_type.to_string(), + span, + ))); + } + Ok(ConstrainedValue::Integer(Integer::new(&parsed))) + } (Type::Array(type_, arr_len), InputValue::Array(values)) => { if *arr_len != values.len() { return Err(FunctionError::invalid_input_array_dimensions( diff --git a/compiler/src/output/mod.rs b/compiler/src/output/mod.rs index 89d09e6823..ff2aa193e2 100644 --- a/compiler/src/output/mod.rs +++ b/compiler/src/output/mod.rs @@ -155,6 +155,8 @@ impl Output { format!("({})", values) } + ConstrainedValue::Field(field) => format!("{:?}", field), + ConstrainedValue::Group(group) => format!("{:?}", group), _ => value.to_string(), }; diff --git a/compiler/src/value/group/group_type.rs b/compiler/src/value/group/group_type.rs index 6a94d040ea..7efa212ca9 100644 --- a/compiler/src/value/group/group_type.rs +++ b/compiler/src/value/group/group_type.rs @@ -45,6 +45,8 @@ pub trait GroupType: + ToBitsBEGadget + ToBytesGadget { + fn log_string(&self) -> Result; + fn constant(value: &GroupValue, span: &Span) -> Result; fn to_allocated>(&self, cs: CS, span: &Span) -> Result; diff --git a/compiler/src/value/group/targets/edwards_bls12.rs b/compiler/src/value/group/targets/edwards_bls12.rs index 0713542d07..7ef921eaf7 100644 --- a/compiler/src/value/group/targets/edwards_bls12.rs +++ b/compiler/src/value/group/targets/edwards_bls12.rs @@ -52,6 +52,16 @@ pub enum EdwardsGroupType { } impl GroupType for EdwardsGroupType { + fn log_string(&self) -> Result { + match self { + EdwardsGroupType::Constant(constant) => Ok(format!("({}, {})group", constant.x, constant.y)), + EdwardsGroupType::Allocated(allocated) => match (allocated.x.get_value(), allocated.y.get_value()) { + (Some(x), Some(y)) => Ok(format!("({}, {})group", x, y)), + _ => Err(GroupError::failed_to_create_log_string()), + }, + } + } + fn constant(group: &GroupValue, span: &Span) -> Result { let value = Self::edwards_affine_from_value(group, span)?; diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index f98e0e75d2..eacc71f064 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -160,7 +160,11 @@ impl Integer { if let InputValue::Integer(type_, number) = input { let asg_type = IntegerType::from(type_); if std::mem::discriminant(&asg_type) != std::mem::discriminant(integer_type) { - return Err(IntegerError::integer_type_mismatch(integer_type, asg_type, span)); + return Err(IntegerError::integer_type_mismatch( + integer_type.to_string(), + asg_type.to_string(), + span, + )); } Some(number) } else { diff --git a/compiler/src/value/value.rs b/compiler/src/value/value.rs index 4e93ec2cbc..7030087148 100644 --- a/compiler/src/value/value.rs +++ b/compiler/src/value/value.rs @@ -96,8 +96,14 @@ impl<'a, F: PrimeField, G: GroupType> fmt::Display for ConstrainedValue<'a, F .unwrap_or_else(|| "[allocated]".to_string()) ), ConstrainedValue::Char(ref value) => write!(f, "{}", value), - ConstrainedValue::Field(ref value) => write!(f, "{:?}", value), - ConstrainedValue::Group(ref value) => write!(f, "{:?}", value), + ConstrainedValue::Field(ref value) => { + if let Some(value) = value.get_value() { + write!(f, "{}", value) + } else { + Err(std::fmt::Error) + } + } + ConstrainedValue::Group(ref value) => write!(f, "{}", value.log_string().unwrap_or_else(|e| e.to_string())), ConstrainedValue::Integer(ref value) => write!(f, "{}", value), // Data type wrappers diff --git a/leo/logger.rs b/leo/logger.rs index 0445f39002..eb9a590796 100644 --- a/leo/logger.rs +++ b/leo/logger.rs @@ -16,6 +16,7 @@ use std::{fmt, sync::Once}; +use anyhow::{anyhow, Result}; use colored::Colorize; use tracing::{event::Event, subscriber::Subscriber}; use tracing_subscriber::{ @@ -181,17 +182,23 @@ where let mut message = "".to_string(); - let scope = context.scope(); - for span in scope { - message += span.metadata().name(); + match context.lookup_current() { + Some(span_ref) => { + let scope = span_ref.scope(); - let ext = span.extensions(); - let fields = &ext - .get::>() - .expect("Unable to find FormattedFields in extensions; this is a bug"); - if !fields.is_empty() { - message += &format!("{{{}}}", fields); + for span in scope { + message += span.metadata().name(); + + let ext = span.extensions(); + let fields = &ext + .get::>() + .expect("Unable to find FormattedFields in extensions; this is a bug"); + if !fields.is_empty() { + message += &format!("{{{}}}", fields); + } + } } + None => return Err(std::fmt::Error), } write!(writer, "{:>10} ", colored_string(meta.level(), &message)).expect("Error writing event"); @@ -203,10 +210,16 @@ where } /// Initialize logger with custom format and verbosity. -pub fn init_logger(_app_name: &'static str, verbosity: usize) { +pub fn init_logger(_app_name: &'static str, verbosity: usize) -> Result<()> { // This line enables Windows 10 ANSI coloring API. #[cfg(target_family = "windows")] - ansi_term::enable_ansi_support(); + match ansi_term::enable_ansi_support() { + Ok(_) => {} + Err(_) => { + println!("YOTE"); + return Err(anyhow!("Error: Failedto enable ansi_support")); + } + }; let subscriber = FmtSubscriber::builder() // all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.) @@ -226,4 +239,5 @@ pub fn init_logger(_app_name: &'static str, verbosity: usize) { START.call_once(|| { tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); }); + Ok(()) } diff --git a/leo/main.rs b/leo/main.rs index 2629c52202..d20347160f 100644 --- a/leo/main.rs +++ b/leo/main.rs @@ -38,7 +38,7 @@ use commands::{ Watch, }; -use anyhow::Error; +use anyhow::Result; use std::{path::PathBuf, process::exit}; use structopt::{clap::AppSettings, StructOpt}; @@ -185,13 +185,13 @@ fn main() { } /// Run command with custom build arguments. -fn run_with_args(opt: Opt) -> Result<(), Error> { +fn run_with_args(opt: Opt) -> Result<()> { if !opt.quiet { // Init logger with optional debug flag. logger::init_logger("leo", match opt.debug { false => 1, true => 2, - }); + })?; } // Get custom root folder and create context for it. @@ -225,7 +225,7 @@ fn run_with_args(opt: Opt) -> Result<(), Error> { } } -fn handle_error(res: Result) -> T { +fn handle_error(res: Result) -> T { match res { Ok(t) => t, Err(err) => { @@ -239,13 +239,13 @@ fn handle_error(res: Result) -> T { mod cli_tests { use crate::{run_with_args, Opt}; - use anyhow::Error; + use anyhow::Result; use std::path::PathBuf; use structopt::StructOpt; use test_dir::{DirBuilder, FileType, TestDir}; // Runs Command from cmd-like argument "leo run --arg1 --arg2". - fn run_cmd(args: &str, path: &Option) -> Result<(), Error> { + fn run_cmd(args: &str, path: &Option) -> Result<()> { let args = args.split(' ').collect::>(); let mut opts = Opt::from_iter_safe(args)?; diff --git a/tests/compiler/input_files/program_input/input/different_types_fail_const_signedin b/tests/compiler/input_files/program_input/input/different_types_const_signed_fail.in similarity index 100% rename from tests/compiler/input_files/program_input/input/different_types_fail_const_signedin rename to tests/compiler/input_files/program_input/input/different_types_const_signed_fail.in diff --git a/tests/compiler/input_files/program_input/input/different_types_unsigned_const_fail.in b/tests/compiler/input_files/program_input/input/different_types_const_unsigned_fail.in similarity index 100% rename from tests/compiler/input_files/program_input/input/different_types_unsigned_const_fail.in rename to tests/compiler/input_files/program_input/input/different_types_const_unsigned_fail.in