Skip to content

Commit 4bf5599

Browse files
committed
fixed #190
1 parent bd3e764 commit 4bf5599

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,6 +2406,11 @@ fn simplify_lines(
24062406
let result = ConstExpression::MathExpr(*operation, const_args);
24072407
res.push(SimpleLine::equality(target_var, SimpleExpr::Constant(result)));
24082408
} else {
2409+
if !operation.supports_runtime() {
2410+
return Err(format!(
2411+
"Operation `{operation}` is compile-time only; all operands must be constants"
2412+
));
2413+
}
24092414
res.push(SimpleLine::Assignment {
24102415
var: target_var.into(),
24112416
operation: *operation,
@@ -2449,6 +2454,11 @@ fn simplify_lines(
24492454
let result = ConstExpression::MathExpr(*operation, const_args);
24502455
res.push(SimpleLine::equality(var, SimpleExpr::Constant(result)));
24512456
} else {
2457+
if !operation.supports_runtime() {
2458+
return Err(format!(
2459+
"Operation `{operation}` is compile-time only; all operands must be constants"
2460+
));
2461+
}
24522462
assert_eq!(simplified_args.len(), 2);
24532463
res.push(SimpleLine::Assignment {
24542464
var,
@@ -3062,6 +3072,11 @@ fn simplify_expr(
30623072
if let Some(const_args) = SimpleExpr::try_vec_as_constant(&simplified_args) {
30633073
return Ok(SimpleExpr::Constant(ConstExpression::MathExpr(*operation, const_args)));
30643074
}
3075+
if !operation.supports_runtime() {
3076+
return Err(format!(
3077+
"Operation `{operation}` is compile-time only; all operands must be constants"
3078+
));
3079+
}
30653080
let aux_var = state.counters.aux_var();
30663081
assert_eq!(simplified_args.len(), 2);
30673082
lines.push(SimpleLine::Assignment {

crates/lean_compiler/src/lang.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ impl MathOperation {
359359
pub fn is_unary(&self) -> bool {
360360
self.num_args() == 1
361361
}
362+
pub const fn supports_runtime(&self) -> bool {
363+
matches!(self, Self::Add | Self::Sub | Self::Mul | Self::Div)
364+
}
362365
pub fn num_args(&self) -> usize {
363366
match self {
364367
Self::Log2Ceil => 1,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Error: `%` is compile-time only; operands must be constants.
2+
def main():
3+
a = one()
4+
out = a % 2
5+
assert out == 1
6+
return
7+
8+
def one():
9+
return 1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Error: unary `log2_ceil` is compile-time only; operand must be a constant.
2+
def main():
3+
a = one()
4+
out = log2_ceil(a)
5+
assert out == 0
6+
return
7+
8+
def one():
9+
return 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Error: `%` is compile-time only; operands must be constants (array assignment path).
2+
def main():
3+
a = one()
4+
arr = Array(1)
5+
arr[0] = a % 2
6+
assert arr[0] == 1
7+
return
8+
9+
def one():
10+
return 1

0 commit comments

Comments
 (0)