Update usage of int/String fromEnvironment

Breaking change 40678 requires the constructors int.fromEnvironment and
String.fromEnvironment to get new default values for the named
parameter `defaultValue`. This CL changes usages of these constructors
such that they do not depend on the default value, such that it
becomes a non-breaking change for code in the SDK repo to perform the
change in sdk and in sdk_nnbd.

Change-Id: I82af0e1f92d6cd3618b65c0c50d754ae8c39eb0a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140284
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
Commit-Queue: Erik Ernst <eernst@google.com>
diff --git a/pkg/analysis_server/test/utils/package_root.dart b/pkg/analysis_server/test/utils/package_root.dart
index 1957a61..ab5cdac 100644
--- a/pkg/analysis_server/test/utils/package_root.dart
+++ b/pkg/analysis_server/test/utils/package_root.dart
@@ -11,7 +11,8 @@
 String get packageRoot {
   // If the package root directory is specified on the command line using
   // -DpkgRoot=..., use it.
-  String pkgRootVar = const String.fromEnvironment('pkgRoot');
+  const String pkgRootVar =
+      bool.hasEnvironment('pkgRoot') ? String.fromEnvironment('pkgRoot') : null;
   if (pkgRootVar != null) {
     String path = pathos.join(Directory.current.path, pkgRootVar);
     if (!path.endsWith(pathos.separator)) path += pathos.separator;
diff --git a/pkg/analyzer/test/utils/package_root.dart b/pkg/analyzer/test/utils/package_root.dart
index 1957a61..8055970 100644
--- a/pkg/analyzer/test/utils/package_root.dart
+++ b/pkg/analyzer/test/utils/package_root.dart
@@ -11,7 +11,9 @@
 String get packageRoot {
   // If the package root directory is specified on the command line using
   // -DpkgRoot=..., use it.
-  String pkgRootVar = const String.fromEnvironment('pkgRoot');
+  String pkgRootVar = const bool.hasEnvironment('pkgRoot')
+      ? const String.fromEnvironment('pkgRoot')
+      : null;
   if (pkgRootVar != null) {
     String path = pathos.join(Directory.current.path, pkgRootVar);
     if (!path.endsWith(pathos.separator)) path += pathos.separator;
diff --git a/pkg/analyzer_plugin/test/utils/package_root.dart b/pkg/analyzer_plugin/test/utils/package_root.dart
index 47d0e31..98c24ba 100644
--- a/pkg/analyzer_plugin/test/utils/package_root.dart
+++ b/pkg/analyzer_plugin/test/utils/package_root.dart
@@ -11,7 +11,9 @@
 String get packageRoot {
   // If the package root directory is specified on the command line using
   // -DpkgRoot=..., use it.
-  String pkgRootVar = const String.fromEnvironment('pkgRoot');
+  String pkgRootVar = bool.hasEnvironment('pkgRoot')
+      ? const String.fromEnvironment('pkgRoot')
+      : null;
   if (pkgRootVar != null) {
     String path = pathos.join(Directory.current.path, pkgRootVar);
     if (!path.endsWith(pathos.separator)) path += pathos.separator;
diff --git a/pkg/compiler/lib/src/tracer.dart b/pkg/compiler/lib/src/tracer.dart
index e42352b..66564af 100644
--- a/pkg/compiler/lib/src/tracer.dart
+++ b/pkg/compiler/lib/src/tracer.dart
@@ -17,8 +17,7 @@
     : TRACE_FILTER_PATTERN_FOR_TEST;
 
 const String TRACE_FILTER_PATTERN_FROM_ENVIRONMENT =
-    // TODO(sigmund): remove `?? ''` once #40678 is backported.
-    const String.fromEnvironment("DUMP_IR") ?? '';
+    String.fromEnvironment("DUMP_IR", defaultValue: "");
 String TRACE_FILTER_PATTERN_FOR_TEST;
 
 /// Dumps the intermediate representation after each phase in a format
diff --git a/pkg/front_end/tool/_fasta/abcompile.dart b/pkg/front_end/tool/_fasta/abcompile.dart
index 4ccde2a..63ea80b 100644
--- a/pkg/front_end/tool/_fasta/abcompile.dart
+++ b/pkg/front_end/tool/_fasta/abcompile.dart
@@ -9,11 +9,10 @@
 
 import 'standard_deviation.dart';
 
-const String bRootPath = const String.fromEnvironment("bRoot");
-const int abIterations =
-    const int.fromEnvironment("abIterations", defaultValue: 15);
-const int iterations =
-    const int.fromEnvironment("iterations", defaultValue: 15);
+const String bRootPath =
+    bool.hasEnvironment("bRoot") ? String.fromEnvironment("bRoot") : null;
+const int abIterations = int.fromEnvironment("abIterations", defaultValue: 15);
+const int iterations = int.fromEnvironment("iterations", defaultValue: 15);
 
 /// Compare the performance of two different fast implementations
 /// by alternately launching the compile application in this directory
diff --git a/pkg/testing/lib/src/discover.dart b/pkg/testing/lib/src/discover.dart
index 9b82e8a..3fd09e9 100644
--- a/pkg/testing/lib/src/discover.dart
+++ b/pkg/testing/lib/src/discover.dart
@@ -64,9 +64,15 @@
   return Uri.base.resolve(".packages");
 }
 
+// TODO(eernst): Use `bool.hasEnvironment` below when possible;
+// for now we use a dual `defaultValue` rewrite.
+const _dartSdk = (String.fromEnvironment("DART_SDK", defaultValue: "1") ==
+        String.fromEnvironment("DART_SDK", defaultValue: "2"))
+    ? String.fromEnvironment("DART_SDK")
+    : null;
+
 Uri computeDartSdk() {
-  String dartSdkPath = Platform.environment["DART_SDK"] ??
-      const String.fromEnvironment("DART_SDK");
+  String dartSdkPath = Platform.environment["DART_SDK"] ?? _dartSdk;
   if (dartSdkPath != null) {
     return Uri.base.resolveUri(new Uri.file(dartSdkPath));
   } else {
diff --git a/pkg/vm/lib/transformations/type_flow/utils.dart b/pkg/vm/lib/transformations/type_flow/utils.dart
index 164a6e9..379c1ed 100644
--- a/pkg/vm/lib/transformations/type_flow/utils.dart
+++ b/pkg/vm/lib/transformations/type_flow/utils.dart
@@ -32,7 +32,7 @@
     const bool.fromEnvironment('global.type.flow.scope.trace');
 
 const int kScopeIndent =
-    const int.fromEnvironment('global.type.flow.scope.indent');
+    const int.fromEnvironment('global.type.flow.scope.indent', defaultValue: 1);
 
 /// Extended 'assert': always checks condition.
 assertx(bool cond, {details}) {
@@ -57,7 +57,7 @@
     "\u001b[35m", // magenta
     "\u001b[36m", // cyan
   ];
-  static const int _scopeIndent = kScopeIndent ?? 1;
+  static const int _scopeIndent = kScopeIndent;
 
   int _scope = 0;
   List<String> _scopePrefixes = <String>[""];
diff --git a/runtime/observatory/lib/src/app/analytics.dart b/runtime/observatory/lib/src/app/analytics.dart
index 484f7da..a34c2da 100644
--- a/runtime/observatory/lib/src/app/analytics.dart
+++ b/runtime/observatory/lib/src/app/analytics.dart
@@ -4,10 +4,17 @@
 
 part of app;
 
+// TODO(eernst): Use 'bool.fromEnvironment' below when possible;
+// for now we use a dual `defaultValue` rewrite.
+const _obsVer = (String.fromEnvironment('OBS_VER', defaultValue: '1') ==
+        String.fromEnvironment('OBS_VER', defaultValue: '2'))
+    ? String.fromEnvironment('OBS_VER')
+    : null;
+
 class Analytics {
   static final _UA = 'UA-26406144-17';
   static final _name = 'Observatory';
-  static final _version = const String.fromEnvironment('OBS_VER');
+  static final _version = _obsVer;
   static final _googleAnalytics = new AnalyticsHtml(_UA, _name, _version);
 
   static initialize() {
diff --git a/tests/compiler/dart2js_extra/new_from_env_test.dart b/tests/compiler/dart2js_extra/new_from_env_test.dart
index cec8a78..7138c6b 100644
--- a/tests/compiler/dart2js_extra/new_from_env_test.dart
+++ b/tests/compiler/dart2js_extra/new_from_env_test.dart
@@ -11,7 +11,7 @@
 /// `new` instead of `const`.
 main() {
   Expect.isFalse(const bool.fromEnvironment('X'));
-  Expect.isNull(const String.fromEnvironment('X'));
+  Expect.equals('', const String.fromEnvironment('X', defaultValue: ''));
   Expect.equals(const int.fromEnvironment('X', defaultValue: 0), 0);
 
   Expect.throws(() => new bool.fromEnvironment('X'));
diff --git a/tests/corelib_2/from_environment_const_type_undefined_test.dart b/tests/corelib_2/from_environment_const_type_undefined_test.dart
index c2e90ef..d63d75f 100644
--- a/tests/corelib_2/from_environment_const_type_undefined_test.dart
+++ b/tests/corelib_2/from_environment_const_type_undefined_test.dart
@@ -25,14 +25,14 @@
     int //   //# 10: ok
     String //# 11: compile-time error
     Foo //   //# 12: compile-time error
-    c = const int.fromEnvironment('c');
+    c = const int.fromEnvironment('c', defaultValue: 0);
 
 const
     bool //  //# 13: compile-time error
     int //   //# 14: compile-time error
     String //# 15: ok
     Foo //   //# 16: compile-time error
-    d = const String.fromEnvironment('d');
+    d = const String.fromEnvironment('d', defaultValue: '');
 
 main() {
   Expect.equals(false, a);
diff --git a/tests/language/bool/has_environment_not_new_test.dart b/tests/language/bool/has_environment_not_new_test.dart
new file mode 100644
index 0000000..947c6be
--- /dev/null
+++ b/tests/language/bool/has_environment_not_new_test.dart
@@ -0,0 +1,9 @@
+// Copyright (c) 2020, 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";
+
+main() {
+  Expect.throws(() => new bool.hasEnvironment("Anything"));
+}
diff --git a/tests/language/bool/has_environment_test.dart b/tests/language/bool/has_environment_test.dart
new file mode 100644
index 0000000..df593f1
--- /dev/null
+++ b/tests/language/bool/has_environment_test.dart
@@ -0,0 +1,15 @@
+// Copyright (c) 2020, 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.
+
+// SharedOptions=-Da= -Db=b -Dc=Something
+
+import 'package:expect/expect.dart';
+
+main() {
+  Expect.isTrue(const bool.hasEnvironment('dart.library.core'));
+  Expect.isTrue(const bool.hasEnvironment('a'));
+  Expect.isTrue(const bool.hasEnvironment('b'));
+  Expect.isTrue(const bool.hasEnvironment('c'));
+  Expect.isFalse(const bool.hasEnvironment('d'));
+}
diff --git a/tests/language_2/string/const_interpolation2_test.dart b/tests/language_2/string/const_interpolation2_test.dart
index 968d2c2..c9c373c 100644
--- a/tests/language_2/string/const_interpolation2_test.dart
+++ b/tests/language_2/string/const_interpolation2_test.dart
@@ -9,9 +9,15 @@
 const u1 = null;
 const int u2 = null;
 const List u3 = null;
-const u4 = const String.fromEnvironment("XXXXX");
-const u5 = const int.fromEnvironment("XXXXX");
-const u6 = const bool.fromEnvironment("XXXXX", defaultValue: null);
+const u4 = const bool.hasEnvironment("XXXXX")
+    ? const String.fromEnvironment("XXXXX")
+    : null;
+const u5 = const bool.hasEnvironment("XXXXX")
+    ? const int.fromEnvironment("XXXXX")
+    : null;
+const u6 = bool.hasEnvironment("XXXXX")
+    ? const bool.fromEnvironment("XXXXX")
+    : null;
 const n1 = 42;
 const n2 = 3.1415;
 const int n3 = 37;
@@ -88,7 +94,7 @@
   Expect.equals(b2.toString(), sb2);
   Expect.equals(b3.toString(), sb3);
   Expect.equals(b4.toString(), sb4);
-  var expect = "null null null  0 null 42 3.1415 37 4.6692 2.71828 87 "
+  var expect = "null null null null null null 42 3.1415 37 4.6692 2.71828 87 "
       "s1 s2 s1s2 s4 true false false true";
   Expect.equals(expect, interpolation1);
   Expect.equals(expect, interpolation2);
diff --git a/tests/language_2/unsorted/unevaluated_field.dart b/tests/language_2/unsorted/unevaluated_field.dart
index 3567a7c..5c701db 100644
--- a/tests/language_2/unsorted/unevaluated_field.dart
+++ b/tests/language_2/unsorted/unevaluated_field.dart
@@ -6,11 +6,13 @@
 
 import "package:expect/expect.dart";
 
-const int gx = const int.fromEnvironment("x");
+const int gx =
+    const bool.hasEnvironment("x") ? const int.fromEnvironment("x") : null;
 
 class A {
   final int x = gx;
-  final int y = const int.fromEnvironment("y");
+  final int y =
+      const bool.hasEnvironment("y") ? const int.fromEnvironment("y") : null;
   const A();
 }
 
diff --git a/tests/lib/isolate/string_from_environment_default_value_test.dart b/tests/lib/isolate/string_from_environment_default_value_test.dart
index 153ab6e..78b2feb 100644
--- a/tests/lib/isolate/string_from_environment_default_value_test.dart
+++ b/tests/lib/isolate/string_from_environment_default_value_test.dart
@@ -10,7 +10,7 @@
 import "package:expect/expect.dart";
 
 void test(port) {
-  Expect.identical(const String.fromEnvironment('NOT_FOUND'), "");
+  Expect.equals('', const String.fromEnvironment('NOT_FOUND'));
   Expect.equals(
       'x', const String.fromEnvironment('NOT_FOUND', defaultValue: 'x'));
   if (port != null) port.send(null);