Merge pull request #2146 from AleoHQ/fix/tyc-for-loop-bounds

Fix/tyc for loop bounds
This commit is contained in:
d0cd 2022-10-31 17:11:19 -07:00 committed by GitHub
commit c4279e2a83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 0 deletions

View File

@ -322,6 +322,8 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> {
// If `input.start` is a literal, instantiate it as a value.
if let Expression::Literal(literal) = &input.start {
input.start_value.replace(Some(Value::from(literal)));
} else {
self.emit_err(TypeCheckerError::loop_bound_must_be_a_literal(input.start.span()));
}
self.visit_expression(&input.stop, iter_type);
@ -329,6 +331,8 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> {
// If `input.stop` is a literal, instantiate it as a value.
if let Expression::Literal(literal) = &input.stop {
input.stop_value.replace(Some(Value::from(literal)));
} else {
self.emit_err(TypeCheckerError::loop_bound_must_be_a_literal(input.stop.span()));
}
}

View File

@ -423,4 +423,11 @@ create_messages!(
msg: format!("Cannot call a local transition function from a transition function."),
help: None,
}
@formatted
loop_bound_must_be_a_literal {
args: (),
msg: format!("Loop bound must be a literal."),
help: None,
}
);

View File

@ -0,0 +1,24 @@
/*
namespace: Compile
expectation: Fail
*/
program test.aleo {
record Token {
owner: address,
gates: u64,
amount: u64,
}
transition main(owner: address, amount: u64) -> Token {
let max: u64 = 0u64;
for i:u64 in 0u64..amount {
max = i;
}
return Token {
owner: owner,
gates: 0u64,
amount: amount,
};
}
}

View File

@ -0,0 +1,5 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Error [ETYC0372049]: Loop bound must be a literal.\n --> compiler-test:12:28\n |\n 12 | for i:u64 in 0u64..amount {\n | ^^^^^^\n"