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

Issue:
[compiler-v2] Emit no warning when `self` is unused
When `self` is used as the first parameter of a function and unused, do not emit a warning.


Discussion / hints:

Fix patch:
diff --git a/third_party/move/move-compiler/src/hlir/ast.rs b/third_party/move/move-compiler/src/hlir/ast.rs
index c2d7b6eac0793..086fbfb660a50 100644
--- a/third_party/move/move-compiler/src/hlir/ast.rs
+++ b/third_party/move/move-compiler/src/hlir/ast.rs
@@ -371,6 +371,13 @@ impl FunctionSignature {
             .iter()
             .any(|(parameter_name, _)| parameter_name == v)
     }
+
+    pub fn is_first_parameter(&self, v: &Var) -> bool {
+        self.parameters
+            .first()
+            .map(|(parameter_name, _)| parameter_name == v)
+            .unwrap_or(false)
+    }
 }
 
 impl Command_ {
diff --git a/third_party/move/move-compiler/src/hlir/translate.rs b/third_party/move/move-compiler/src/hlir/translate.rs
index ed37b4fc265e9..0e94e6a06f52e 100644
--- a/third_party/move/move-compiler/src/hlir/translate.rs
+++ b/third_party/move/move-compiler/src/hlir/translate.rs
@@ -1972,6 +1972,11 @@ fn check_unused_locals(
             DisplayVar::Tmp => panic!("ICE unused tmp"),
             DisplayVar::Orig(vstr) => vstr,
         };
+        if signature.is_first_parameter(&v) && vstr == "self" {
+            // Special treatment for `self` Move 2 parameter: do not warn if unused
+            // for the case v1 compiles v2 code
+            continue;
+        }
         let loc = v.loc();
         let msg = if signature.is_parameter(&v) {
             format!(


Test patch:
diff --git a/third_party/move/move-compiler-v2/tests/checking/receiver/dont_warn_unused_self.exp b/third_party/move/move-compiler-v2/tests/checking/receiver/dont_warn_unused_self.exp
new file mode 100644
index 0000000000000..135e3d8795172
--- /dev/null
+++ b/third_party/move/move-compiler-v2/tests/checking/receiver/dont_warn_unused_self.exp
@@ -0,0 +1,21 @@
+
+Diagnostics:
+warning: Unused parameter `y`. Consider removing or prefixing with an underscore: `_y`
+  ┌─ tests/checking/receiver/dont_warn_unused_self.move:5:27
+  │
+5 │     fun receiver(self: S, y: u64) {
+  │                           ^
+
+// -- Model dump before bytecode pipeline
+module 0x42::m {
+    struct S {
+        x: u64,
+    }
+    private fun receiver(self: m::S,y: u64) {
+        Tuple()
+    }
+    spec {
+      requires true;
+    }
+
+} // end 0x42::m
diff --git a/third_party/move/move-compiler-v2/tests/checking/receiver/dont_warn_unused_self.move b/third_party/move/move-compiler-v2/tests/checking/receiver/dont_warn_unused_self.move
new file mode 100644
index 0000000000000..8c63ab010d99e
--- /dev/null
+++ b/third_party/move/move-compiler-v2/tests/checking/receiver/dont_warn_unused_self.move
@@ -0,0 +1,11 @@
+module 0x42::m {
+
+    struct S has drop { x: u64 }
+
+    fun receiver(self: S, y: u64) {
+    }
+
+    spec receiver(self: S, y: u64) {
+        requires true;
+    }
+}
diff --git a/third_party/move/move-compiler/tests/move_check/locals/dont_warn_unused_self.exp b/third_party/move/move-compiler/tests/move_check/locals/dont_warn_unused_self.exp
new file mode 100644
index 0000000000000..cfd2a222de0a5
--- /dev/null
+++ b/third_party/move/move-compiler/tests/move_check/locals/dont_warn_unused_self.exp
@@ -0,0 +1,6 @@
+warning[W09002]: unused variable
+  ┌─ tests/move_check/locals/dont_warn_unused_self.move:5:27
+  │
+5 │     fun receiver(self: S, y: u64) {
+  │                           ^ Unused parameter 'y'. Consider removing or prefixing with an underscore: '_y'
+
diff --git a/third_party/move/move-compiler/tests/move_check/locals/dont_warn_unused_self.move b/third_party/move/move-compiler/tests/move_check/locals/dont_warn_unused_self.move
new file mode 100644
index 0000000000000..8c63ab010d99e
--- /dev/null
+++ b/third_party/move/move-compiler/tests/move_check/locals/dont_warn_unused_self.move
@@ -0,0 +1,11 @@
+module 0x42::m {
+
+    struct S has drop { x: u64 }
+
+    fun receiver(self: S, y: u64) {
+    }
+
+    spec receiver(self: S, y: u64) {
+        requires true;
+    }
+}