Skip to content
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

Fix to RCA panic when original tuple binding is dynamic #1900

Merged
merged 3 commits into from
Sep 5, 2024
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
29 changes: 13 additions & 16 deletions compiler/qsc_rca/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,13 +1335,14 @@ impl<'a> Analyzer<'a> {
let pat = self.get_pat(pat_id);
match &pat.kind {
PatKind::Bind(ident) => {
let application_instance = self.get_current_application_instance();
let compute_kind = *application_instance.get_expr_compute_kind(expr_id);
let local_kind = match mutability {
Mutability::Immutable => LocalKind::Immutable(expr_id),
Mutability::Mutable => LocalKind::Mutable,
};
self.bind_compute_kind_to_ident(pat, ident, local_kind, compute_kind);
let application_instance = self.get_current_application_instance();
let expr_compute_kind = *application_instance.get_expr_compute_kind(expr_id);
let bound_compute_kind = ComputeKind::map_to_type(expr_compute_kind, &pat.ty);
self.bind_compute_kind_to_ident(pat, ident, local_kind, bound_compute_kind);
}
PatKind::Tuple(pats) => match &expr.kind {
ExprKind::Tuple(exprs) => {
Expand All @@ -1368,13 +1369,14 @@ impl<'a> Analyzer<'a> {
let pat = self.get_pat(pat_id);
match &pat.kind {
PatKind::Bind(ident) => {
let application_instance = self.get_current_application_instance();
let compute_kind = *application_instance.get_expr_compute_kind(expr_id);
let local_kind = match mutability {
Mutability::Immutable => LocalKind::Immutable(expr_id),
Mutability::Mutable => LocalKind::Mutable,
};
self.bind_compute_kind_to_ident(pat, ident, local_kind, compute_kind);
let application_instance = self.get_current_application_instance();
let expr_compute_kind = *application_instance.get_expr_compute_kind(expr_id);
let bound_compute_kind = ComputeKind::map_to_type(expr_compute_kind, &pat.ty);
self.bind_compute_kind_to_ident(pat, ident, local_kind, bound_compute_kind);
}
PatKind::Tuple(pats) => {
for pat_id in pats {
Expand Down Expand Up @@ -1552,16 +1554,11 @@ impl<'a> Analyzer<'a> {
// a UDT variable field since we do not track individual UDT fields).
let value_expr_compute_kind =
*application_instance.get_expr_compute_kind(value_expr_id);
let mut value_kind =
ValueKind::new_static_from_type(&local_var_compute_kind.local.ty);
if let ComputeKind::Quantum(value_expr_quantum_properties) = value_expr_compute_kind
{
value_expr_quantum_properties
.value_kind
.project_onto_variant(&mut value_kind);
}
updated_compute_kind = updated_compute_kind
.aggregate_runtime_features(value_expr_compute_kind, value_kind);
let assigned_compute_kind = ComputeKind::map_to_type(
value_expr_compute_kind,
&local_var_compute_kind.local.ty,
);
updated_compute_kind = updated_compute_kind.aggregate(assigned_compute_kind);

// If a local is updated within a dynamic scope, the updated value of the local variable should be
// dynamic and additional runtime features may apply.
Expand Down
17 changes: 17 additions & 0 deletions compiler/qsc_rca/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,23 @@ impl Display for ComputeKind {
}

impl ComputeKind {
pub(crate) fn map_to_type(compute_kind: Self, ty: &Ty) -> Self {
match compute_kind {
ComputeKind::Classical => ComputeKind::Classical,
ComputeKind::Quantum(quantum_properties) => {
let runtime_features = quantum_properties.runtime_features;
let mut value_kind = ValueKind::new_static_from_type(ty);
quantum_properties
.value_kind
.project_onto_variant(&mut value_kind);
ComputeKind::Quantum(QuantumProperties {
runtime_features,
value_kind,
})
}
}
}

pub(crate) fn new_with_runtime_features(
runtime_features: RuntimeFeatureFlags,
value_kind: ValueKind,
Expand Down
39 changes: 39 additions & 0 deletions compiler/qsc_rca/src/tests/assigns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,45 @@ fn check_rca_for_assign_dynamic_call_result_to_tuple_of_vars() {
);
}

#[test]
fn check_rca_for_assign_dynamic_static_mix_call_result_to_tuple_of_vars() {
let mut compilation_context = CompilationContext::default();
compilation_context.update(
r#"
operation Foo(q : Qubit) : (Int[], Result[]) {
([1, 2], [MResetZ(q)])
}
use q = Qubit();
mutable (a, b) = Foo(q);
set (a, b) = Foo(q);
a
"#,
);
check_last_statement_compute_properties(
compilation_context.get_compute_properties(),
&expect![[r#"
ApplicationsGeneratorSet:
inherent: Quantum: QuantumProperties:
runtime_features: RuntimeFeatureFlags(UseOfDynamicInt)
value_kind: Array(Content: Dynamic, Size: Static)
dynamic_param_applications: <empty>"#]],
);
compilation_context.update(
r#"
b
"#,
);
check_last_statement_compute_properties(
compilation_context.get_compute_properties(),
&expect![[r#"
ApplicationsGeneratorSet:
inherent: Quantum: QuantumProperties:
runtime_features: RuntimeFeatureFlags(UseOfDynamicInt)
value_kind: Array(Content: Dynamic, Size: Static)
dynamic_param_applications: <empty>"#]],
);
}

#[test]
fn check_rca_for_mutable_classical_integer_assigned_updated_with_classical_integer() {
let mut compilation_context = CompilationContext::default();
Expand Down
Loading