[dart2wasm] Simplify watchPoints field types

There's no difference between a module watchpoints field being `null` or
`[]`, so the type `List<int>? watchPoints` can be simplified as
`List<int> watchPoints` and by treating empty list the same way as
previous `null`.

Change-Id: I9be8c872c4336b1c0bf5157af5e8587136cf2088
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/325261
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
diff --git a/pkg/dart2wasm/lib/translator.dart b/pkg/dart2wasm/lib/translator.dart
index e936472..de40f28 100644
--- a/pkg/dart2wasm/lib/translator.dart
+++ b/pkg/dart2wasm/lib/translator.dart
@@ -43,7 +43,7 @@
   bool jsCompatibility = false;
   int inliningLimit = 0;
   int? sharedMemoryMaxPages;
-  List<int>? watchPoints = null;
+  List<int> watchPoints = [];
 }
 
 /// The main entry point for the translation from kernel to Wasm and the hub for
diff --git a/pkg/wasm_builder/lib/src/builder/instructions.dart b/pkg/wasm_builder/lib/src/builder/instructions.dart
index 853acf1..61b4d3e 100644
--- a/pkg/wasm_builder/lib/src/builder/instructions.dart
+++ b/pkg/wasm_builder/lib/src/builder/instructions.dart
@@ -145,7 +145,7 @@
   /// Create a new instruction sequence.
   InstructionsBuilder(this.module, List<ir.ValueType> outputs,
       {this.isGlobalInitializer = false})
-      : _stackTraces = module.watchPoints != null ? {} : null {
+      : _stackTraces = module.watchPoints.isNotEmpty ? {} : null {
     _labelStack.add(Expression(const [], outputs));
   }
 
@@ -164,7 +164,7 @@
 
   void _add(ir.Instruction i) {
     _instructions.add(i);
-    if (module.watchPoints != null) {
+    if (module.watchPoints.isNotEmpty) {
       _stackTraces![i] = StackTrace.current;
     }
   }
diff --git a/pkg/wasm_builder/lib/src/builder/module.dart b/pkg/wasm_builder/lib/src/builder/module.dart
index 66288af..0cd8666 100644
--- a/pkg/wasm_builder/lib/src/builder/module.dart
+++ b/pkg/wasm_builder/lib/src/builder/module.dart
@@ -8,7 +8,7 @@
 // TODO(joshualitt): Get rid of cycles in the builder graph.
 /// A Wasm module builder.
 class ModuleBuilder with Builder<ir.Module> {
-  final List<int>? watchPoints;
+  final List<int> watchPoints;
   final types = TypesBuilder();
   late final functions = FunctionsBuilder(this);
   final tables = TablesBuilder();
@@ -22,10 +22,10 @@
   /// Create a new, initially empty, module.
   ///
   /// The [watchPoints] is a list of byte offsets within the final module of
-  /// bytes to watch. When the module is serialized, the stack traces leading to
-  /// the production of all watched bytes are printed. This can be used to debug
-  /// runtime errors happening at specific offsets within the module.
-  ModuleBuilder({this.watchPoints});
+  /// bytes to watch. When the module is serialized, the stack traces leading
+  /// to the production of all watched bytes are printed. This can be used to
+  /// debug runtime errors happening at specific offsets within the module.
+  ModuleBuilder({this.watchPoints = const []});
 
   @override
   ir.Module forceBuild() {
diff --git a/pkg/wasm_builder/lib/src/ir/module.dart b/pkg/wasm_builder/lib/src/ir/module.dart
index db3cf32..60f3558 100644
--- a/pkg/wasm_builder/lib/src/ir/module.dart
+++ b/pkg/wasm_builder/lib/src/ir/module.dart
@@ -16,7 +16,7 @@
   final Types types;
   final DataSegments dataSegments;
   final List<Import> imports;
-  final List<int>? watchPoints;
+  final List<int> watchPoints;
   final bool dataReferencedFromGlobalInitializer;
 
   Module(
@@ -35,7 +35,7 @@
   /// Serialize a module to its binary representation.
   @override
   void serialize(Serializer s) {
-    if (watchPoints != null) {
+    if (watchPoints.isNotEmpty) {
       Serializer.traceEnabled = true;
     }
     // Wasm module preamble: magic number, version 1.
diff --git a/pkg/wasm_builder/lib/src/serialize/sections.dart b/pkg/wasm_builder/lib/src/serialize/sections.dart
index 178eb8e..860d53c 100644
--- a/pkg/wasm_builder/lib/src/serialize/sections.dart
+++ b/pkg/wasm_builder/lib/src/serialize/sections.dart
@@ -6,7 +6,7 @@
 import 'serializer.dart';
 
 abstract class Section implements Serializable {
-  final List<int>? watchPoints;
+  final List<int> watchPoints;
 
   Section(this.watchPoints);
 
diff --git a/pkg/wasm_builder/lib/src/serialize/serializer.dart b/pkg/wasm_builder/lib/src/serialize/serializer.dart
index 2404ff242..ae2fc7b 100644
--- a/pkg/wasm_builder/lib/src/serialize/serializer.dart
+++ b/pkg/wasm_builder/lib/src/serialize/serializer.dart
@@ -101,24 +101,22 @@
     }
   }
 
-  void writeData(Serializer chunk, [List<int>? watchPoints]) {
+  void writeData(Serializer chunk, [List<int> watchPoints = const []]) {
     if (traceEnabled) debugTrace(chunk);
-    if (watchPoints != null) {
-      for (int watchPoint in watchPoints) {
-        if (_index <= watchPoint && watchPoint < _index + chunk.data.length) {
-          int byteValue = chunk.data[watchPoint - _index];
-          Object trace = this;
-          int offset = watchPoint;
-          while (trace is Serializer) {
-            int keyOffset = trace._traces.containsKey(offset)
-                ? offset
-                : trace._traces.lastKeyBefore(offset)!;
-            trace = trace._traces[keyOffset]!;
-            offset -= keyOffset;
-          }
-          String byte = byteValue.toRadixString(16).padLeft(2, '0');
-          print("Watch $watchPoint: 0x$byte\n$trace");
+    for (int watchPoint in watchPoints) {
+      if (_index <= watchPoint && watchPoint < _index + chunk.data.length) {
+        int byteValue = chunk.data[watchPoint - _index];
+        Object trace = this;
+        int offset = watchPoint;
+        while (trace is Serializer) {
+          int keyOffset = trace._traces.containsKey(offset)
+              ? offset
+              : trace._traces.lastKeyBefore(offset)!;
+          trace = trace._traces[keyOffset]!;
+          offset -= keyOffset;
         }
+        String byte = byteValue.toRadixString(16).padLeft(2, '0');
+        print("Watch $watchPoint: 0x$byte\n$trace");
       }
     }
     writeBytes(chunk.data);