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

Issue:
[Bug][Compiler-v2] undeclared type error if enum is only defined but not used
# 🐛 Bug

When compiling the following code with enum type:

```
module hello::enum_types {

    use std::string;

    enum MessageHolder has key, drop {
        Empty,
        Message{
           message: string::String,
        }
        NewMessage{
           message: string::String,
        }
    }

}
```

The following error will be generated:

```
Diagnostics:
error: undeclared `0x1::string::String`
   ┌─ tests/checking/unused/no_use_enum.move:11:14
   │
11 │     message: string::String,
   │              ^^^^^^^^^^^^^^

error: undeclared `0x1::string::String`
   ┌─ tests/checking/unused/no_use_enum.move:14:14
   │
14 │     message: string::String,
   │              ^^^^^^^^^^^^^^

```



[Bug][Compiler-v2] undeclared type error if enum is only defined but not used
# 🐛 Bug

When compiling the following code with enum type:

```
module hello::enum_types {

    use std::string;

    enum MessageHolder has key, drop {
        Empty,
        Message{
           message: string::String,
        }
        NewMessage{
           message: string::String,
        }
    }

}
```

The following error will be generated:

```
Diagnostics:
error: undeclared `0x1::string::String`
   ┌─ tests/checking/unused/no_use_enum.move:11:14
   │
11 │     message: string::String,
   │              ^^^^^^^^^^^^^^

error: undeclared `0x1::string::String`
   ┌─ tests/checking/unused/no_use_enum.move:14:14
   │
14 │     message: string::String,
   │              ^^^^^^^^^^^^^^

```





Discussion / hints:

Fix patch:
diff --git a/third_party/move/move-compiler/src/expansion/dependency_ordering.rs b/third_party/move/move-compiler/src/expansion/dependency_ordering.rs
index f83289520377d..92cb3ed239168 100644
--- a/third_party/move/move-compiler/src/expansion/dependency_ordering.rs
+++ b/third_party/move/move-compiler/src/expansion/dependency_ordering.rs
@@ -337,6 +337,13 @@ fn function_acquires(context: &mut Context, acqs: &[E::ModuleAccess]) {
 fn struct_def(context: &mut Context, sdef: &E::StructDefinition) {
     if let E::StructLayout::Singleton(fields, _) = &sdef.layout {
         fields.iter().for_each(|(_, _, (_, bt))| type_(context, bt));
+    } else if let E::StructLayout::Variants(variants) = &sdef.layout {
+        for variant in variants {
+            variant
+                .fields
+                .iter()
+                .for_each(|(_, _, (_, bt))| type_(context, bt));
+        }
     }
 }
 


Test patch:
diff --git a/third_party/move/move-compiler-v2/tests/checking/unused/unused_enum.exp b/third_party/move/move-compiler-v2/tests/checking/unused/unused_enum.exp
new file mode 100644
index 0000000000000..118953e1f7238
--- /dev/null
+++ b/third_party/move/move-compiler-v2/tests/checking/unused/unused_enum.exp
@@ -0,0 +1,13 @@
+// -- Model dump before bytecode pipeline
+module 0x42::enum_types {
+    use std::string;
+    enum MessageHolder {
+        Empty,
+        Message {
+            message: string::String,
+        }
+        NewMessage {
+            message: string::String,
+        }
+    }
+} // end 0x42::enum_types
diff --git a/third_party/move/move-compiler-v2/tests/checking/unused/unused_enum.move b/third_party/move/move-compiler-v2/tests/checking/unused/unused_enum.move
new file mode 100644
index 0000000000000..834cbe3f8b0e8
--- /dev/null
+++ b/third_party/move/move-compiler-v2/tests/checking/unused/unused_enum.move
@@ -0,0 +1,14 @@
+module 0x42::enum_types {
+
+    use std::string;
+
+    enum MessageHolder has key, drop {
+        Empty,
+        Message{
+            message: string::String,
+        }
+        NewMessage{
+            message: string::String,
+        }
+    }
+}