nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: microsoft/qsharp
Pull request #642 (2023-08-31 21:57:15)

Items in a cell should be usable before definition
Missed case in the interpreter. I believe #628 originally intended to address this.


Patch:
diff --git a/compiler/qsc/src/interpret/stateful.rs b/compiler/qsc/src/interpret/stateful.rs
index 5b3adc09..2154a47f 100644
--- a/compiler/qsc/src/interpret/stateful.rs
+++ b/compiler/qsc/src/interpret/stateful.rs
@@ -392,6 +392,13 @@ impl Interpreter {
                 .collect());
         }
 
+        // Items must be processed before top-level statements, so sort the fragments.
+        // Note that stable sorting is used here to preserve the order of top-level statements.
+        fragments.sort_by_key(|f| match f {
+            Fragment::Item(_) => 0,
+            Fragment::Stmt(_) => 1,
+        });
+
         for fragment in fragments {
             match fragment {
                 Fragment::Item(item) => match item.kind {


Test patch:
diff --git a/compiler/qsc/src/interpret/stateful/tests.rs b/compiler/qsc/src/interpret/stateful/tests.rs
index fffa9ff1..b30d3b05 100644
--- a/compiler/qsc/src/interpret/stateful/tests.rs
+++ b/compiler/qsc/src/interpret/stateful/tests.rs
@@ -468,6 +468,19 @@ mod given_interpreter {
             is_only_value(&result, &output, &Value::unit());
         }
 
+        #[test]
+        fn items_usable_before_definition_top_level() {
+            let mut interpreter = get_interpreter();
+            let (result, output) = line(
+                &mut interpreter,
+                indoc! {r#"
+                    B();
+                    function B() : Unit {}
+                "#},
+            );
+            is_only_value(&result, &output, &Value::unit());
+        }
+
         #[test]
         fn namespace_usable_before_definition() {
             let mut interpreter = get_interpreter();