[dart2js] Automatically set dart.web.assertions_enabled=true.
When (user) assertions are enabled in dart2js, we automatically update
the environment as if -Ddart.web.assertions_enabled=true were
passed. This allows dev-only code to be gated behind
`const bool.fromEnvironment('dart.web.assertions_enabled')` without
having to supply a redundant option manually.
Change-Id: I40d670b6fe87d5e27b2b8d50599261cca8a79a42
Bug: b/254543452
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/270301
Reviewed-by: Sigmund Cherem <sigmund@google.com>
diff --git a/pkg/compiler/lib/src/compiler.dart b/pkg/compiler/lib/src/compiler.dart
index 0d3d0ac..099cf30 100644
--- a/pkg/compiler/lib/src/compiler.dart
+++ b/pkg/compiler/lib/src/compiler.dart
@@ -105,7 +105,7 @@
DiagnosticReporter get reporter => _reporter;
Map<Entity, WorldImpact> get impactCache => _impactCache;
- final Environment environment;
+ late final Environment environment;
late final List<CompilerTask> tasks;
late final GenericTask loadKernelTask;
@@ -147,10 +147,10 @@
this.options)
// NOTE: allocating measurer is done upfront to ensure the wallclock is
// started before other computations.
- : measurer = Measurer(enableTaskMeasurements: options.verbose),
- this.environment = Environment(options.environment) {
+ : measurer = Measurer(enableTaskMeasurements: options.verbose) {
options.deriveOptions();
options.validate();
+ environment = Environment(options.environment);
abstractValueStrategy = options.experimentalInferrer
? (options.useTrivialAbstractValueDomain
diff --git a/pkg/compiler/lib/src/options.dart b/pkg/compiler/lib/src/options.dart
index 36e3def..ff50a75 100644
--- a/pkg/compiler/lib/src/options.dart
+++ b/pkg/compiler/lib/src/options.dart
@@ -834,6 +834,8 @@
if (_mergeFragmentsThreshold != null) {
mergeFragmentsThreshold = _mergeFragmentsThreshold;
}
+
+ environment['dart.web.assertions_enabled'] = '$enableUserAssertions';
}
/// Returns `true` if warnings and hints are shown for all packages.
diff --git a/tests/web/assertions_disabled_test.dart b/tests/web/assertions_disabled_test.dart
new file mode 100644
index 0000000..fee23f4
--- /dev/null
+++ b/tests/web/assertions_disabled_test.dart
@@ -0,0 +1,9 @@
+// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "package:expect/expect.dart";
+
+void main() {
+ Expect.isFalse(const bool.fromEnvironment('dart.web.assertions_enabled'));
+}
diff --git a/tests/web/assertions_enabled_test.dart b/tests/web/assertions_enabled_test.dart
new file mode 100644
index 0000000..c6037fb
--- /dev/null
+++ b/tests/web/assertions_enabled_test.dart
@@ -0,0 +1,11 @@
+// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// dart2jsOptions=--enable-asserts
+
+import "package:expect/expect.dart";
+
+void main() {
+ Expect.isTrue(const bool.fromEnvironment('dart.web.assertions_enabled'));
+}
diff --git a/tests/web_2/assertions_disabled_test.dart b/tests/web_2/assertions_disabled_test.dart
new file mode 100644
index 0000000..fee23f4
--- /dev/null
+++ b/tests/web_2/assertions_disabled_test.dart
@@ -0,0 +1,9 @@
+// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "package:expect/expect.dart";
+
+void main() {
+ Expect.isFalse(const bool.fromEnvironment('dart.web.assertions_enabled'));
+}
diff --git a/tests/web_2/assertions_enabled_test.dart b/tests/web_2/assertions_enabled_test.dart
new file mode 100644
index 0000000..c6037fb
--- /dev/null
+++ b/tests/web_2/assertions_enabled_test.dart
@@ -0,0 +1,11 @@
+// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// dart2jsOptions=--enable-asserts
+
+import "package:expect/expect.dart";
+
+void main() {
+ Expect.isTrue(const bool.fromEnvironment('dart.web.assertions_enabled'));
+}