Skip to content

Commit 4d9fb93

Browse files
authored
Prohibit return statements in apply block (#2839)
Fixes #2837
1 parent 9c33427 commit 4d9fb93

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

katas/content/qec_shor/Common.qs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Kata.Verification {
1313
for _ in 1..10 {
1414
use qs = Qubit[n];
1515
let theta = DrawRandomDouble(0.0, 1.0);
16-
within {
16+
let correct = within {
1717
// Prepare logical state on first qubit
1818
Ry(2.0 * theta * PI(), qs[0]);
1919
// Encode the state in multiple qubits
@@ -30,9 +30,14 @@ namespace Kata.Verification {
3030
Message("Incorrect.");
3131
let actual = err_ind == -1 ? "No error happened" | $"Error happened on qubit {err_ind}";
3232
Message($"{actual}, but solution returned {detected}");
33-
ResetAll(qs);
34-
return false;
33+
false
34+
} else {
35+
true
3536
}
37+
};
38+
if not correct {
39+
ResetAll(qs);
40+
return false;
3641
}
3742
// Check that the state was not modified by the solution
3843
if not CheckAllZero(qs) {

source/compiler/qsc_passes/src/conjugate_invert.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ impl<'a> Visitor<'a> for ReturnCheck {
241241
fn visit_expr(&mut self, expr: &'a Expr) {
242242
if matches!(&expr.kind, ExprKind::Return(..)) {
243243
self.errors.push(Error::ReturnForbidden(expr.span));
244+
} else {
245+
visit::walk_expr(self, expr);
244246
}
245247
}
246248
}

source/compiler/qsc_passes/src/conjugate_invert/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,39 @@ fn conjugate_return_in_apply_fail() {
537537
);
538538
}
539539

540+
#[test]
541+
fn conjugate_return_nested_in_apply_fail() {
542+
check(
543+
indoc! {"
544+
namespace Test {
545+
operation B(i : Int) : Unit is Adj {}
546+
operation A() : Unit {
547+
mutable a = 1;
548+
within {
549+
let x = a;
550+
B(2);
551+
}
552+
apply {
553+
if true {
554+
return ();
555+
}
556+
}
557+
}
558+
}
559+
"},
560+
&expect![[r#"
561+
[
562+
ReturnForbidden(
563+
Span {
564+
lo: 231,
565+
hi: 240,
566+
},
567+
),
568+
]
569+
"#]],
570+
);
571+
}
572+
540573
#[test]
541574
fn conjugate_mutable_correct_use_succeeds() {
542575
check(

0 commit comments

Comments
 (0)