Skip to content

Fix bug in const evaluator #2296

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

Merged
merged 2 commits into from
Apr 11, 2025
Merged
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
2 changes: 1 addition & 1 deletion compiler/qsc_qasm3/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub enum ErrorKind {
#[error("missing switch statement case labels")]
#[diagnostic(code("Qasm3.Parse.MissingSwitchCaseLabels"))]
MissingSwitchCaseLabels(#[label] Span),
#[error("missing switch statement case labels")]
#[error("missing gate call operands")]
#[diagnostic(code("Qasm3.Parse.MissingGateCallOperands"))]
MissingGateCallOperands(#[label] Span),
#[error("expected an item or closing brace, found {0}")]
Expand Down
3 changes: 2 additions & 1 deletion compiler/qsc_qasm3/src/semantic/ast/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ impl BinaryOpExpr {
fn const_eval(&self, ctx: &mut Lowerer) -> Option<LiteralKind> {
use LiteralKind::{Angle, Bit, Bitstring, Bool, Float, Int};

assert_binary_op_ty_invariant(self.op, &self.lhs.ty, &self.rhs.ty);
let lhs = self.lhs.const_eval(ctx);
let rhs = self.rhs.const_eval(ctx);
let (lhs, rhs) = (lhs?, rhs?);
let lhs_ty = &self.lhs.ty;

assert_binary_op_ty_invariant(self.op, &self.lhs.ty, &self.rhs.ty);

match &self.op {
// Bit Shifts
BinOp::Shl => {
Expand Down
148 changes: 148 additions & 0 deletions compiler/qsc_qasm3/src/tests/statement/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1982,3 +1982,151 @@ fn cast_to_bit() -> miette::Result<(), Vec<Report>> {
.assert_eq(&qsharp);
Ok(())
}

#[test]
fn binary_op_err_type_fails() {
let source = r#"
int[a + b] x = 2;
"#;

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#"
Qsc.Qasm3.Lowerer.UndefinedSymbol
x undefined symbol: a
,-[Test.qasm:2:13]
1 |
2 | int[a + b] x = 2;
: ^
3 |
`----
Qsc.Qasm3.Lowerer.UndefinedSymbol
x undefined symbol: b
,-[Test.qasm:2:17]
1 |
2 | int[a + b] x = 2;
: ^
3 |
`----
Qsc.Qasm3.Lowerer.CannotCast
x cannot cast expression of type Err to type UInt(None, true)
,-[Test.qasm:2:13]
1 |
2 | int[a + b] x = 2;
: ^^^^^
3 |
`----
Qsc.Qasm3.Compile.ExprMustBeConst
x expression must be const
,-[Test.qasm:2:13]
1 |
2 | int[a + b] x = 2;
: ^^^^^
3 |
`----
Qsc.Qasm3.Lowerer.ExprMustBeConst
x designator must be a const expression
,-[Test.qasm:2:13]
1 |
2 | int[a + b] x = 2;
: ^^^^^
3 |
`----
Qsc.Qasm3.Lowerer.CannotCastLiteral
x cannot cast literal expression of type Int(None, true) to type Err
,-[Test.qasm:2:9]
1 |
2 | int[a + b] x = 2;
: ^^^^^^^^^^^^^^^^^
3 |
`----
"#]]
.assert_eq(&errs_string);
}

#[test]
fn fuzzer_issue_2294() {
let source = r#"
ctrl(5/_)@l
"#;

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#"
Qasm3.Parse.Token
x expected `;`, found EOF
,-[Test.qasm:3:5]
2 | ctrl(5/_)@l
3 |
`----
Qasm3.Parse.MissingGateCallOperands
x missing gate call operands
,-[Test.qasm:2:9]
1 |
2 | ctrl(5/_)@l
: ^^^^^^^^^^^
3 |
`----
Qsc.Qasm3.Lowerer.UndefinedSymbol
x undefined symbol: _
,-[Test.qasm:2:16]
1 |
2 | ctrl(5/_)@l
: ^
3 |
`----
Qsc.Qasm3.Lowerer.CannotCast
x cannot cast expression of type Err to type Float(None, true)
,-[Test.qasm:2:16]
1 |
2 | ctrl(5/_)@l
: ^
3 |
`----
Qsc.Qasm3.Compile.ExprMustBeConst
x expression must be const
,-[Test.qasm:2:16]
1 |
2 | ctrl(5/_)@l
: ^
3 |
`----
Qsc.Qasm3.Lowerer.ExprMustBeConst
x ctrl modifier argument must be a const expression
,-[Test.qasm:2:14]
1 |
2 | ctrl(5/_)@l
: ^^^
3 |
`----
"#]]
.assert_eq(&errs_string);
}
Loading