Skip to content

Commit

Permalink
Merge pull request #1027 from AleoHQ/bug/1025-assigning-slice-to-string
Browse files Browse the repository at this point in the history
fix slice assignment for strings
  • Loading branch information
acoglio authored Jun 11, 2021
2 parents 0dbbf74 + 2be23c4 commit 04c809c
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 27 deletions.
6 changes: 6 additions & 0 deletions ast/src/reducer/canonicalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,12 @@ impl ReconstructingReducer for Canonicalizer {
span: assign.span.clone(),
})
}
Expression::ArrayInline(_) => Ok(AssignStatement {
operation: AssignOperation::Assign,
assignee,
value,
span: assign.span.clone(),
}),
_ => Ok(assign.clone()),
}
}
Expand Down
15 changes: 1 addition & 14 deletions compiler/tests/canonicalization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// 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::{assert_satisfied, parse_program, parse_program_with_input};
use crate::parse_program;
use leo_ast::Ast;
use leo_parser::parser;

Expand All @@ -35,9 +35,6 @@ pub fn parse_program_ast(file_string: &str) -> Ast {
fn test_big_self_in_circuit_replacement() {
// Check program is valid.
let program_string = include_str!("big_self_in_circuit_replacement.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);

// Check we get expected ast.
let ast = parse_program_ast(program_string);
let expected_json = include_str!("big_self_in_circuit_replacement.json");
Expand All @@ -57,10 +54,6 @@ fn test_big_self_outside_circuit_fail() {
#[test]
fn test_array_expansion() {
let program_string = include_str!("array_expansion.leo");
let input_string = include_str!("input/array_expansion.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);

let ast = parse_program_ast(program_string);
let expected_json = include_str!("array_expansion.json");
let expected_ast: Ast = Ast::from_json_string(expected_json).expect("Unable to parse json.");
Expand All @@ -78,9 +71,6 @@ fn test_array_size_zero_fail() {
#[test]
fn test_compound_assignment() {
let program_string = include_str!("compound_assignment.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);

let ast = parse_program_ast(program_string);
let expected_json = include_str!("compound_assignment.json");
let expected_ast: Ast = Ast::from_json_string(expected_json).expect("Unable to parse json.");
Expand All @@ -99,9 +89,6 @@ fn test_illegal_array_range_fail() {
#[test]
fn test_string_transformation() {
let program_string = include_str!("string_transformation.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);

let ast = parse_program_ast(program_string);
let expected_json = include_str!("string_transformation.json");
let expected_ast: Ast = Ast::from_json_string(expected_json).expect("Unable to parse json.");
Expand Down
111 changes: 98 additions & 13 deletions compiler/tests/canonicalization/string_transformation.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@
}
}
],
"type_": {
"Array": [
"Char",
[
{
"value": "13"
}
]
]
},
"type_": null,
"value": {
"ArrayInline": {
"elements": [
Expand Down Expand Up @@ -285,11 +276,105 @@
"content": " let s = \"Hello, World!\";"
}
}
},
{
"Assign": {
"operation": "Assign",
"assignee": {
"identifier": "{\"name\":\"s\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":5,\\\"col_stop\\\":6,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\" s[..2] = \\\\\\\"ab\\\\\\\";\\\"}\"}",
"accesses": [
{
"ArrayRange": [
null,
{
"Value": {
"Implicit": [
"2",
{
"line_start": 3,
"line_stop": 3,
"col_start": 9,
"col_stop": 10,
"path": "",
"content": " s[..2] = \"ab\";"
}
]
}
}
]
}
],
"span": {
"line_start": 3,
"line_stop": 3,
"col_start": 5,
"col_stop": 11,
"path": "",
"content": " s[..2] = \"ab\";"
}
},
"value": {
"ArrayInline": {
"elements": [
{
"Expression": {
"Value": {
"Char": [
"a",
{
"line_start": 3,
"line_stop": 3,
"col_start": 14,
"col_stop": 18,
"path": "",
"content": " s[..2] = \"ab\";"
}
]
}
}
},
{
"Expression": {
"Value": {
"Char": [
"b",
{
"line_start": 3,
"line_stop": 3,
"col_start": 14,
"col_stop": 18,
"path": "",
"content": " s[..2] = \"ab\";"
}
]
}
}
}
],
"span": {
"line_start": 3,
"line_stop": 3,
"col_start": 14,
"col_stop": 18,
"path": "",
"content": " s[..2] = \"ab\";"
}
}
},
"span": {
"line_start": 3,
"line_stop": 3,
"col_start": 5,
"col_stop": 18,
"path": "",
"content": " s[..2] = \"ab\";"
}
}
}
],
"span": {
"line_start": 1,
"line_stop": 3,
"line_stop": 4,
"col_start": 17,
"col_stop": 2,
"path": "",
Expand All @@ -298,11 +383,11 @@
},
"span": {
"line_start": 1,
"line_stop": 3,
"line_stop": 4,
"col_start": 1,
"col_stop": 2,
"path": "",
"content": "function main() {\n...\n}"
"content": "function main() {\n...\n}\n"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/tests/canonicalization/string_transformation.leo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
function main() {
let s = "Hello, World!";
s[..2] = "ab";
}
12 changes: 12 additions & 0 deletions tests/compiler/string/replace.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
namespace: Compile
expectation: Pass
input_file:
- inputs/string.in
- inputs/weird.in
*/

function main(s1: [char; 13], s2: [char; 4]) -> bool {
s1[..4] = s2;
return s1 != "Hello, World!";
}
24 changes: 24 additions & 0 deletions tests/expectations/compiler/compiler/string/replace.leo.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
namespace: Compile
expectation: Pass
outputs:
- circuit:
num_public_variables: 0
num_private_variables: 68
num_constraints: 51
at: 45fa182371664010950da6ec3fa6360de34dda5cb814b3c8bd119a09e86b5c77
bt: 4da3499799890924c2910625e052c6757e7cfd857ebbd18afd3d16b5bd0e9a73
ct: 183c15a7bf0d7c109b082a55e696874797e7ce685096881ce803f6bfa453a875
output:
- input_file: inputs/string.in
output:
registers:
out:
type: bool
value: "true"
- input_file: inputs/weird.in
output:
registers:
out:
type: bool
value: "true"

0 comments on commit 04c809c

Please sign in to comment.