mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 07:48:04 +03:00
Merge pull request #536 from AleoHQ/fix/return-type-check
Fix/return type check
This commit is contained in:
commit
d3fcd19506
@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_ast::{Error as FormattedError, Span};
|
||||
use crate::errors::ValueError;
|
||||
use leo_ast::{Error as FormattedError, Span, Type};
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
@ -22,12 +23,16 @@ use std::path::Path;
|
||||
pub enum OutputBytesError {
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ValueError(#[from] ValueError),
|
||||
}
|
||||
|
||||
impl OutputBytesError {
|
||||
pub fn set_path(&mut self, path: &Path) {
|
||||
match self {
|
||||
OutputBytesError::Error(error) => error.set_path(path),
|
||||
OutputBytesError::ValueError(error) => error.set_path(path),
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,4 +45,13 @@ impl OutputBytesError {
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
|
||||
pub fn mismatched_output_types(left: Type, right: Type, span: Span) -> Self {
|
||||
let message = format!(
|
||||
"Mismatched types. Expected register output type `{}`, found type `{}`.",
|
||||
left, right
|
||||
);
|
||||
|
||||
Self::new_from_span(message, span)
|
||||
}
|
||||
}
|
||||
|
@ -63,10 +63,22 @@ impl OutputBytes {
|
||||
// format: "token_id: u64 = 1u64;"
|
||||
for (parameter, value) in register_values.into_iter().zip(return_values.into_iter()) {
|
||||
let name = parameter.variable.name;
|
||||
let type_ = parameter.type_;
|
||||
|
||||
// Check register type == return value type.
|
||||
let register_type = parameter.type_;
|
||||
let return_value_type = value.to_type(&span)?;
|
||||
|
||||
if !register_type.eq_flat(&return_value_type) {
|
||||
return Err(OutputBytesError::mismatched_output_types(
|
||||
register_type,
|
||||
return_value_type,
|
||||
span,
|
||||
));
|
||||
}
|
||||
|
||||
let value = value.to_string();
|
||||
|
||||
let format = format!("{}: {} = {};\n", name, type_, value,);
|
||||
let format = format!("{}: {} = {};\n", name, register_type, value,);
|
||||
|
||||
string.push_str(&format);
|
||||
}
|
||||
|
@ -16,4 +16,5 @@
|
||||
|
||||
mod program_input;
|
||||
mod program_input_and_program_state;
|
||||
mod program_registers;
|
||||
mod program_state;
|
||||
|
@ -0,0 +1,2 @@
|
||||
[registers]
|
||||
r2: [[u8; 4]; 2] = [[0u64, 0u64, 0u64, 0u64], [0u64, 0u64, 0u64, 0u64]];
|
@ -0,0 +1,4 @@
|
||||
[main]
|
||||
|
||||
[registers]
|
||||
r: u8 = 0u8;
|
53
compiler/tests/input_files/program_registers/mod.rs
Normal file
53
compiler/tests/input_files/program_registers/mod.rs
Normal file
@ -0,0 +1,53 @@
|
||||
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{expect_compiler_error, get_output, parse_program_with_input};
|
||||
|
||||
#[test]
|
||||
fn test_registers_pass() {
|
||||
let program_string = include_str!("registers_pass.leo");
|
||||
let input_string = include_str!("input/main.in");
|
||||
let expected = include_bytes!("output/registers_pass.out");
|
||||
|
||||
let program = parse_program_with_input(program_string, input_string).unwrap();
|
||||
|
||||
let actual = get_output(program);
|
||||
|
||||
assert!(expected.eq(actual.bytes().as_slice()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_registers_fail() {
|
||||
let program_string = include_str!("registers_fail.leo");
|
||||
let input_string = include_str!("input/main.in");
|
||||
|
||||
let program = parse_program_with_input(program_string, input_string).unwrap();
|
||||
|
||||
expect_compiler_error(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_registers_array() {
|
||||
let program_string = include_str!("registers_array.leo");
|
||||
let input_string = include_str!("input/array.in");
|
||||
let expected = include_bytes!("output/registers_array.out");
|
||||
|
||||
let program = parse_program_with_input(program_string, input_string).unwrap();
|
||||
|
||||
let actual = get_output(program);
|
||||
|
||||
assert!(expected.eq(actual.bytes().as_slice()));
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
[registers]
|
||||
r2: [[u8; 4]; 2] = [[1, 2, 3, 4], [5, 6, 7, 8]];
|
@ -0,0 +1,2 @@
|
||||
[registers]
|
||||
r: u8 = 1;
|
@ -0,0 +1,3 @@
|
||||
function main () -> [[u8; 4]; 2] {
|
||||
return [[1u8, 2u8, 3u8, 4u8], [5u8, 6u8, 7u8, 8u8]]
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() -> bool {
|
||||
return false
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() -> u8 {
|
||||
return 1u8
|
||||
}
|
Loading…
Reference in New Issue
Block a user