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

Run RCA on interpreter creation and propagate errors #1560

Merged
merged 1 commit into from
May 23, 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
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 @@ -1411,6 +1411,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
Loading