Repository: aptos-labs/aptos-core (language: python)

Issue:
[bug][compiler-v2] Thread 'main' panicked
Second time I've run into this with compiler-v2. I'm getting this error in various scenarios:
```
thread 'main' panicked at third_party/move/move-compiler/src/expansion/translate.rs:2606:61:
bind list always present
```

The first time I saw it was with this code:
```
match (entity_data) {
            Entity::V1 { tools, .. } => {
                vector::push_back(tools, tool_def);
            }
        };
```
I had failed to include `Entity` in a use statement.  When I added the use statement the error went away.  Today I'm seeing it with this code:
```
public fun init_registration(
		creator: &signer,
		tool_type: ToolType
	): Object<Registration> {
		let registration: Registration;
		match(tool_type) {
			ToolType::Http { uri } => {
				registration =	init_http_registration(uri);
			},
			ToolType::Aptos { module_address, module_name, function_name, max_transaction_amount } => {
				registration = init_aptos_registration(creator, module_name, function_name, max_transaction_amount);
			}
		}
		
	}
```
Clearly this code is not finished as it's not returning a value (along with some other problems). But rather than the compiler picking this up, it's throwing this same error:
```
(base) culbrethw@MBA beacon % aptos move compile --move-2
Compiling, may take a little while to download git dependencies...
UPDATING GIT DEPENDENCY https://github.com/aptos-labs/aptos-core.git
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING Beacon
thread 'main' panicked at third_party/move/move-compiler/src/expansion/translate.rs:2606:61:
bind list always present
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

When I add a return value to the code, even if it's wrong, the compiler functions as expected and identifies errors without panic. And to make it even more confusing, if I change two or three more things and then go back to the exact same code that was causing the panic, the compiler works as expected and doesn't throw the panic error. Perplixing and frustraiting as there's some sequence of events happening to get the compiler to this state that I haven't been able to replicate deliberately.




Discussion / hints:
Thanks for the report, @magnum6actual, we will investigate this issue and prepare a fix for it.

Fix patch:
diff --git a/third_party/move/move-compiler/src/expansion/translate.rs b/third_party/move/move-compiler/src/expansion/translate.rs
index ca954085198f3..b8ea685e454d7 100644
--- a/third_party/move/move-compiler/src/expansion/translate.rs
+++ b/third_party/move/move-compiler/src/expansion/translate.rs
@@ -2600,13 +2600,22 @@ fn exp_(context: &mut Context, sp!(loc, pe_): P::Exp) -> E::Exp {
             let discriminator = exp(context, *pd);
             let match_arms = parms
                 .into_iter()
-                .map(|parm| {
+                .filter_map(|parm| {
                     let loc = parm.loc;
                     let (pbl, pc, pb) = parm.value;
-                    let bind_list = bind_list(context, pbl).expect("bind list always present");
-                    let opt_cond = pc.map(|e| *exp(context, e));
-                    let body = *exp(context, pb);
-                    sp(loc, (bind_list, opt_cond, body))
+                    if let Some(bind_list) = bind_list(context, pbl) {
+                        let opt_cond = pc.map(|e| *exp(context, e));
+                        let body = *exp(context, pb);
+                        Some(sp(loc, (bind_list, opt_cond, body)))
+                    } else {
+                        if !context.env.has_errors() {
+                            context.env.add_diag(diag!(
+                                Syntax::InvalidLValue,
+                                (loc, "bind list cannot be constructed")
+                            ));
+                        }
+                        None
+                    }
                 })
                 .collect::<Vec<_>>();
             EE::Match(discriminator, match_arms)


Test patch:
diff --git a/third_party/move/move-compiler-v2/tests/checking/variants/bug_15073.exp b/third_party/move/move-compiler-v2/tests/checking/variants/bug_15073.exp
new file mode 100644
index 0000000000000..7abba59d663f5
--- /dev/null
+++ b/third_party/move/move-compiler-v2/tests/checking/variants/bug_15073.exp
@@ -0,0 +1,7 @@
+
+Diagnostics:
+error: unbound module
+  ┌─ tests/checking/variants/bug_15073.move:8:13
+  │
+8 │             ToolType::Http { uri } => {
+  │             ^^^^^^^^ Unbound module or type alias 'ToolType'
diff --git a/third_party/move/move-compiler-v2/tests/checking/variants/bug_15073.move b/third_party/move/move-compiler-v2/tests/checking/variants/bug_15073.move
new file mode 100644
index 0000000000000..da326a52c95a8
--- /dev/null
+++ b/third_party/move/move-compiler-v2/tests/checking/variants/bug_15073.move
@@ -0,0 +1,14 @@
+module 0x815::m {
+
+    public fun init_registration(
+        creator: &signer,
+        tool_type: ToolType
+    ) {
+        match(tool_type) {
+            ToolType::Http { uri } => {
+
+            },
+        }
+
+    }
+}