Skip to content

Commit

Permalink
Merge b8ea132 into 16ee14b
Browse files Browse the repository at this point in the history
  • Loading branch information
idavis committed May 22, 2024
2 parents 16ee14b + b8ea132 commit 9359082
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
23 changes: 22 additions & 1 deletion compiler/qsc/src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ impl Interpreter {

let source_package_id = compiler.source_package_id();
let package_id = compiler.package_id();

let package = map_hir_package_to_fir(package_id);
if capabilities != TargetCapabilityFlags::all() {
let _ = PassContext::run_fir_passes_on_fir(
&fir_store,
map_hir_package_to_fir(source_package_id),
capabilities,
)
.map_err(|caps_errors| {
let source_package = compiler
.package_store()
.get(source_package_id)
.expect("package should exist in the package store");

caps_errors
.into_iter()
.map(|error| Error::Pass(WithSource::from_map(&source_package.sources, error)))
.collect::<Vec<_>>()
})?;
}

Ok(Self {
compiler,
lines: 0,
Expand All @@ -215,7 +236,7 @@ impl Interpreter {
sim: sim_circuit_backend(),
quantum_seed: None,
classical_seed: None,
package: map_hir_package_to_fir(package_id),
package,
source_package: map_hir_package_to_fir(source_package_id),
})
}
Expand Down
34 changes: 34 additions & 0 deletions compiler/qsc/src/interpret/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,40 @@ mod given_interpreter {
is_unit_with_output_eval_entry(&result, &output, "hello there...");
}

#[test]
fn errors_returned_if_sources_do_not_match_profile() {
let source = indoc! { r#"
namespace A { operation Test() : Double { use q = Qubit(); mutable x = 1.0; if MResetZ(q) == One { set x = 2.0; } x } }"#};

let sources = SourceMap::new([("test".into(), source.into())], Some("A.Test()".into()));
let result = Interpreter::new(
true,
sources,
PackageType::Exe,
TargetCapabilityFlags::Adaptive
| TargetCapabilityFlags::IntegerComputations
| TargetCapabilityFlags::QubitReset,
LanguageFeatures::default(),
);

match result {
Ok(_) => panic!("Expected error, got interpreter."),
Err(errors) => is_error(
&errors,
&expect![[r#"
cannot use a dynamic double value
[<entry>] [A.Test()]
cannot use a double value as an output
[<entry>] [A.Test()]
cannot use a dynamic double value
[test] [set x = 2.0]
cannot use a dynamic double value
[test] [x]
"#]],
),
}
}

#[test]
fn stdlib_members_can_be_accessed_from_sources() {
let source = indoc! { r#"
Expand Down
20 changes: 20 additions & 0 deletions npm/qsharp/test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,26 @@ test("debug service loading source with bad entry expr fails - web worker", asyn
}
});

test("debug service loading source that doesn't match profile fails - web worker", async () => {
const debugService = getDebugServiceWorker();
try {
const result = await debugService.loadSource(
[
[
"test.qs",
`namespace A { operation Test() : Double { use q = Qubit(); mutable x = 1.0; if MResetZ(q) == One { set x = 2.0; } x } }`,
],
],
"adaptive_ri",
"A.Test()",
[],
);
assert.ok(typeof result === "string" && result.trim().length > 0);
} finally {
debugService.terminate();
}
});

test("debug service loading source with good entry expr succeeds - web worker", async () => {
const debugService = getDebugServiceWorker();
try {
Expand Down

0 comments on commit 9359082

Please sign in to comment.