Skip to content

Handle division by zero error in const evaluator #2333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions compiler/qsc_qasm/src/semantic/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use thiserror::Error;

#[derive(Clone, Debug, Diagnostic, Eq, Error, PartialEq)]
pub enum ConstEvalError {
#[error("division by error during const evaluation")]
#[diagnostic(code("Qasm.Lowerer.DivisionByZero"))]
DivisionByZero(#[label] Span),
#[error("expression must be const")]
#[diagnostic(code("Qasm.Lowerer.ExprMustBeConst"))]
ExprMustBeConst(#[label] Span),
Expand Down Expand Up @@ -457,25 +460,45 @@ impl BinaryOpExpr {
},
BinOp::Div => match lhs_ty {
Type::Int(..) | Type::UInt(..) => {
rewrap_lit!((lhs, rhs), (Int(lhs), Int(rhs)), Int(lhs / rhs))
rewrap_lit!((lhs, rhs), (Int(lhs), Int(rhs)), {
if rhs == 0 {
ctx.push_const_eval_error(ConstEvalError::DivisionByZero(self.span()));
return None;
}
Int(lhs / rhs)
})
}
Type::Float(..) => {
rewrap_lit!((lhs, rhs), (Float(lhs), Float(rhs)), Float(lhs / rhs))
rewrap_lit!((lhs, rhs), (Float(lhs), Float(rhs)), {
if rhs == 0. {
ctx.push_const_eval_error(ConstEvalError::DivisionByZero(self.span()));
return None;
}
Float(lhs / rhs)
})
}
Type::Angle(..) => match &self.rhs.ty {
Type::UInt(..) => {
rewrap_lit!(
(lhs, rhs),
(Angle(lhs), Int(rhs)),
rewrap_lit!((lhs, rhs), (Angle(lhs), Int(rhs)), {
if rhs == 0 {
ctx.push_const_eval_error(ConstEvalError::DivisionByZero(
self.span(),
));
return None;
}
Angle(lhs / u64::try_from(rhs).ok()?)
)
})
}
Type::Angle(..) => {
rewrap_lit!(
(lhs, rhs),
(Angle(lhs), Angle(rhs)),
rewrap_lit!((lhs, rhs), (Angle(lhs), Angle(rhs)), {
if rhs.value == 0 {
ctx.push_const_eval_error(ConstEvalError::DivisionByZero(
self.span(),
));
return None;
}
Int((lhs / rhs).try_into().ok()?)
)
})
}
_ => None,
},
Expand Down
7 changes: 7 additions & 0 deletions compiler/qsc_qasm/src/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ fn fuzz_2313() {
super::compare_qasm_and_qasharp_asts(source);
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2332() {
let source = r#"ctrl(0/0)@s"#;
super::compare_qasm_and_qasharp_asts(source);
compile_qasm_best_effort(source, Profile::Unrestricted);
}
147 changes: 147 additions & 0 deletions compiler/qsc_qasm/src/tests/statement/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,3 +2186,150 @@ fn binary_op_with_non_supported_types_fails() {
"#]]
.assert_eq(&errs_string);
}

#[test]
fn division_of_int_by_zero_int_errors() {
let source = r#"
const int a = 2 / 0;
def f() { a; }
"#;

let Err(errs) = compile_qasm_to_qsharp(source) else {
panic!("should have generated an error");
};
let errs: Vec<_> = errs.iter().map(|e| format!("{e:?}")).collect();
let errs_string = errs.join("\n");
expect![[r#"
Qasm.Lowerer.DivisionByZero

x division by error during const evaluation
,-[Test.qasm:2:23]
1 |
2 | const int a = 2 / 0;
: ^^^^^
3 | def f() { a; }
`----

Qasm.Lowerer.ExprMustBeConst

x a captured variable must be a const expression
,-[Test.qasm:3:19]
2 | const int a = 2 / 0;
3 | def f() { a; }
: ^
4 |
`----
"#]]
.assert_eq(&errs_string);
}

#[test]
fn division_of_angle_by_zero_int_errors() {
let source = r#"
const angle a = 2.0;
const angle b = a / 0;
def f() { b; }
"#;

let Err(errs) = compile_qasm_to_qsharp(source) else {
panic!("should have generated an error");
};
let errs: Vec<_> = errs.iter().map(|e| format!("{e:?}")).collect();
let errs_string = errs.join("\n");
expect![[r#"
Qasm.Lowerer.DivisionByZero

x division by error during const evaluation
,-[Test.qasm:3:25]
2 | const angle a = 2.0;
3 | const angle b = a / 0;
: ^^^^^
4 | def f() { b; }
`----

Qasm.Lowerer.ExprMustBeConst

x a captured variable must be a const expression
,-[Test.qasm:4:19]
3 | const angle b = a / 0;
4 | def f() { b; }
: ^
5 |
`----
"#]]
.assert_eq(&errs_string);
}

#[test]
fn division_by_zero_float_errors() {
let source = r#"
const float a = 2.0 / 0.0;
def f() { a; }
"#;

let Err(errs) = compile_qasm_to_qsharp(source) else {
panic!("should have generated an error");
};
let errs: Vec<_> = errs.iter().map(|e| format!("{e:?}")).collect();
let errs_string = errs.join("\n");
expect![[r#"
Qasm.Lowerer.DivisionByZero

x division by error during const evaluation
,-[Test.qasm:2:25]
1 |
2 | const float a = 2.0 / 0.0;
: ^^^^^^^^^
3 | def f() { a; }
`----

Qasm.Lowerer.ExprMustBeConst

x a captured variable must be a const expression
,-[Test.qasm:3:19]
2 | const float a = 2.0 / 0.0;
3 | def f() { a; }
: ^
4 |
`----
"#]]
.assert_eq(&errs_string);
}

#[test]
fn division_by_zero_angle_errors() {
let source = r#"
const angle a = 2.0;
const angle b = 0.0;
const uint c = a / b;
def f() { c; }
"#;

let Err(errs) = compile_qasm_to_qsharp(source) else {
panic!("should have generated an error");
};
let errs: Vec<_> = errs.iter().map(|e| format!("{e:?}")).collect();
let errs_string = errs.join("\n");
expect![[r#"
Qasm.Lowerer.DivisionByZero

x division by error during const evaluation
,-[Test.qasm:4:24]
3 | const angle b = 0.0;
4 | const uint c = a / b;
: ^^^^^
5 | def f() { c; }
`----

Qasm.Lowerer.ExprMustBeConst

x a captured variable must be a const expression
,-[Test.qasm:5:19]
4 | const uint c = a / b;
5 | def f() { c; }
: ^
6 |
`----
"#]]
.assert_eq(&errs_string);
}
Loading