mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-03 01:16:16 +03:00
Merge pull request #1027 from AleoHQ/bug/1025-assigning-slice-to-string
fix slice assignment for strings
This commit is contained in:
commit
04c809c0dc
@ -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()),
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
@ -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");
|
||||
@ -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.");
|
||||
@ -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.");
|
||||
@ -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.");
|
||||
|
@ -31,16 +31,7 @@
|
||||
}
|
||||
}
|
||||
],
|
||||
"type_": {
|
||||
"Array": [
|
||||
"Char",
|
||||
[
|
||||
{
|
||||
"value": "13"
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"type_": null,
|
||||
"value": {
|
||||
"ArrayInline": {
|
||||
"elements": [
|
||||
@ -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": "",
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
function main() {
|
||||
let s = "Hello, World!";
|
||||
s[..2] = "ab";
|
||||
}
|
12
tests/compiler/string/replace.leo
Normal file
12
tests/compiler/string/replace.leo
Normal file
@ -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
tests/expectations/compiler/compiler/string/replace.leo.out
Normal file
24
tests/expectations/compiler/compiler/string/replace.leo.out
Normal file
@ -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"
|
Loading…
Reference in New Issue
Block a user