Skip to content
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
83 changes: 82 additions & 1 deletion source/compiler/qsc/src/interpret/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ mod given_interpreter {
}

#[test]
fn invalid_statements_and_unbound_vars_return_error() {
fn invalid_statements_and_unbound_vars_return_error_on_immutable_usage() {
let mut interpreter = get_interpreter();

let (result, output) = line(&mut interpreter, "let y = x;");
Expand All @@ -193,6 +193,87 @@ mod given_interpreter {
);
}

#[test]
fn invalid_statements_and_unbound_vars_return_error_on_mutable_update() {
let mut interpreter = get_interpreter();

let (result, output) = line(&mut interpreter, "mutable y = x;");
is_only_error(
&result,
&output,
&expect![[r#"
name error: `x` not found
[line_0] [x]
type error: insufficient type information to infer type
[line_0] [y]
"#]],
);

let (result, output) = line(&mut interpreter, "y = 3");
is_only_error(
&result,
&output,
&expect![[r#"
cannot update immutable variable
[line_1] [y]
"#]],
);
}

#[test]
fn invalid_statements_and_unbound_vars_return_error_on_immutable_usage_with_rca() {
let mut interpreter = get_interpreter_with_capabilities(TargetCapabilityFlags::empty());

let (result, output) = line(&mut interpreter, "let y = x;");
is_only_error(
&result,
&output,
&expect![[r#"
name error: `x` not found
[line_0] [x]
type error: insufficient type information to infer type
[line_0] [y]
"#]],
);

let (result, output) = line(&mut interpreter, "y");
is_only_error(
&result,
&output,
&expect![[r#"
runtime error: name is not bound
[line_1] [y]
"#]],
);
}

#[test]
fn invalid_statements_and_unbound_vars_return_error_on_mutable_update_with_rca() {
let mut interpreter = get_interpreter_with_capabilities(TargetCapabilityFlags::empty());

let (result, output) = line(&mut interpreter, "mutable y = x;");
is_only_error(
&result,
&output,
&expect![[r#"
name error: `x` not found
[line_0] [x]
type error: insufficient type information to infer type
[line_0] [y]
"#]],
);

let (result, output) = line(&mut interpreter, "y = 3");
is_only_error(
&result,
&output,
&expect![[r#"
cannot update immutable variable
[line_1] [y]
"#]],
);
}

#[test]
fn failing_statements_return_early_error() {
let mut interpreter = get_interpreter();
Expand Down
24 changes: 21 additions & 3 deletions source/compiler/qsc_rca/src/applications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,27 @@ impl LocalsComputeKindMap {
self.0.get(local_var_id)
}

pub fn get_local_compute_kind(&self, local_var_id: LocalVarId) -> &LocalComputeKind {
self.find_local_compute_kind(local_var_id)
.expect("local compute kind does not exist")
pub fn get_or_init_local_compute_kind(
&mut self,
local_var_id: LocalVarId,
local_kind: LocalKind,
compute_kind: ComputeKind,
) -> &LocalComputeKind {
if self.0.contains_key(local_var_id) {
self.0.get(local_var_id).expect("local should exist")
} else {
self.0.insert(
local_var_id,
LocalComputeKind {
local: Local {
var: local_var_id,
kind: local_kind,
},
compute_kind,
},
);
self.0.get(local_var_id).expect("local should exist")
}
}

pub fn insert(&mut self, local_var_id: LocalVarId, value: LocalComputeKind) {
Expand Down
2 changes: 0 additions & 2 deletions source/compiler/qsc_rca/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::{
#[derive(Clone, Debug)]
pub struct Local {
pub var: LocalVarId,
pub ty: Ty,
pub kind: LocalKind,
}

Expand Down Expand Up @@ -57,7 +56,6 @@ pub fn initialize_locals_map(input_params: &Vec<InputParam>) -> FxHashMap<LocalV
id,
Local {
var: id,
ty: param.ty.clone(),
kind: LocalKind::InputParam(param.index),
},
);
Expand Down
34 changes: 16 additions & 18 deletions source/compiler/qsc_rca/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ impl<'a> Analyzer<'a> {
self.get_current_application_instance()
.locals_map
.find_local_compute_kind(local_var_id)
.expect("local should have been processed before this")
.compute_kind
.map_or(ComputeKind::Classical, |v| v.compute_kind)
.value_kind_or_default(ValueKind::Element(RuntimeKind::Static))
})
.chain(self.derive_arg_value_kinds(&arg_exprs))
Expand Down Expand Up @@ -1091,10 +1090,10 @@ impl<'a> Analyzer<'a> {
// Gather the current compute kind of the local.
Res::Local(local_var_id) => {
let application_instance = self.get_current_application_instance();
let local_compute_kind = application_instance
application_instance
.locals_map
.get_local_compute_kind(*local_var_id);
local_compute_kind.compute_kind
.find_local_compute_kind(*local_var_id)
.map_or(ComputeKind::Classical, |v| v.compute_kind)
}
Res::Err => panic!("unexpected error resolution"),
}
Expand Down Expand Up @@ -1332,15 +1331,13 @@ impl<'a> Analyzer<'a> {

fn bind_compute_kind_to_ident(
&mut self,
pat: &Pat,
ident: &Ident,
local_kind: LocalKind,
compute_kind: ComputeKind,
) {
let application_instance = self.get_current_application_instance_mut();
let local = Local {
var: ident.id,
ty: pat.ty.clone(),
kind: local_kind,
};
let local_compute_kind = LocalComputeKind {
Expand Down Expand Up @@ -1369,7 +1366,7 @@ impl<'a> Analyzer<'a> {
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);
self.bind_compute_kind_to_ident(ident, local_kind, bound_compute_kind);
}
PatKind::Tuple(pats) => match &expr.kind {
ExprKind::Tuple(exprs) => {
Expand Down Expand Up @@ -1403,7 +1400,7 @@ impl<'a> Analyzer<'a> {
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);
self.bind_compute_kind_to_ident(ident, local_kind, bound_compute_kind);
}
PatKind::Tuple(pats) => {
for pat_id in pats {
Expand Down Expand Up @@ -1569,10 +1566,14 @@ impl<'a> Analyzer<'a> {
// The updated compute kind is the aggregation of the compute kind of the local variable and the
// assigned value.
// Start by initializing the updated compute kind with the compute kind of the local variable.
let application_instance = self.get_current_application_instance();
let application_instance = self.get_current_application_instance_mut();
let local_var_compute_kind = application_instance
.locals_map
.get_local_compute_kind(*local_var_id);
.get_or_init_local_compute_kind(
*local_var_id,
LocalKind::Mutable,
ComputeKind::Classical,
);
let mut updated_compute_kind = local_var_compute_kind.compute_kind;

// Since the local variable compute kind is what will be updated, the value kind must match the local
Expand All @@ -1582,16 +1583,14 @@ 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 assigned_compute_kind = ComputeKind::map_to_type(
value_expr_compute_kind,
&local_var_compute_kind.local.ty,
);
let assigned_compute_kind =
ComputeKind::map_to_type(value_expr_compute_kind, &assignee_expr.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.
if !application_instance.active_dynamic_scopes.is_empty() {
let local_type = &local_var_compute_kind.local.ty;
let local_type = &assignee_expr.ty;
let mut dynamic_value_kind = ValueKind::new_dynamic_from_type(local_type);
let mut dynamic_runtime_features =
derive_runtime_features_for_value_kind_associated_to_type(
Expand Down Expand Up @@ -1621,7 +1620,7 @@ impl<'a> Analyzer<'a> {
updated_quantum_properties.runtime_features |=
derive_runtime_features_for_value_kind_associated_to_type(
value_kind,
&local_var_compute_kind.local.ty,
&assignee_expr.ty,
);
}

Expand Down Expand Up @@ -2551,7 +2550,6 @@ fn derive_specialization_controls(
match &pat.kind {
PatKind::Bind(ident) => Some(Local {
var: ident.id,
ty: pat.ty.clone(),
kind: LocalKind::SpecInput,
}),
PatKind::Discard => None, // Nothing to bind to.
Expand Down
1 change: 0 additions & 1 deletion source/compiler/qsc_rca/src/cycle_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl<'a> CycleDetector<'a> {
ident.id,
Local {
var: ident.id,
ty: pat.ty.clone(),
kind,
},
);
Expand Down
Loading