From b3304af17dd18dd2a7598ab7b3a898db97b18d67 Mon Sep 17 00:00:00 2001 From: Michael Thomsen Date: Mon, 12 Dec 2022 09:54:54 +0000 Subject: [PATCH] [3.0 alpha] Remove deprecated onError API on int.parse, double.parse, and num.parse Change-Id: I9ed24c380bf716cb893b99ba228e34f4671cc8fd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/259042 Reviewed-by: Lasse Nielsen Reviewed-by: Martin Kustermann --- .../tests/concurrency/stress_test_list.json | 1 - sdk/lib/core/double.dart | 16 +++--- sdk/lib/core/int.dart | 19 ++----- sdk/lib/core/num.dart | 10 ++-- tests/corelib/double_parse_test.dart | 11 ++--- .../int_parse_radix_bad_handler_test.dart | 12 ----- tests/corelib/int_parse_radix_test.dart | 22 ++------- .../int_parse_with_limited_ints_test.dart | 49 +++++++------------ tests/corelib/num_parse_test.dart | 9 ++-- tests/corelib_2/double_parse_test.dart | 11 ++--- .../int_parse_radix_bad_handler_test.dart | 14 ------ tests/corelib_2/int_parse_radix_test.dart | 22 ++------- .../int_parse_with_limited_ints_test.dart | 49 +++++++------------ tests/corelib_2/num_parse_test.dart | 9 ++-- tests/language/regress/regress41613_test.dart | 16 ------ .../language_2/regress/regress41613_test.dart | 14 ------ 16 files changed, 70 insertions(+), 214 deletions(-) delete mode 100644 tests/corelib/int_parse_radix_bad_handler_test.dart delete mode 100644 tests/corelib_2/int_parse_radix_bad_handler_test.dart delete mode 100644 tests/language/regress/regress41613_test.dart delete mode 100644 tests/language_2/regress/regress41613_test.dart diff --git a/runtime/tests/concurrency/stress_test_list.json b/runtime/tests/concurrency/stress_test_list.json index 26c77570751..68370089aa7 100644 --- a/runtime/tests/concurrency/stress_test_list.json +++ b/runtime/tests/concurrency/stress_test_list.json @@ -5695,7 +5695,6 @@ "../../../tests/language_2/regress/regress40066_test.dart", "../../../tests/language_2/regress/regress40765_test.dart", "../../../tests/language_2/regress/regress4157508_test.dart", - "../../../tests/language_2/regress/regress41613_test.dart", "../../../tests/language_2/regress/regress41983_test.dart", "../../../tests/language_2/regress/regress42946_test.dart", "../../../tests/language_2/regress/regress4295001_test.dart", diff --git a/sdk/lib/core/double.dart b/sdk/lib/core/double.dart index 7c04f3582a7..7124a58f04e 100644 --- a/sdk/lib/core/double.dart +++ b/sdk/lib/core/double.dart @@ -221,11 +221,11 @@ abstract class double extends num { /// /// Leading and trailing whitespace is ignored. /// - /// If the [source] string is not a valid double literal, the [onError] - /// is called with the [source] as argument, and its return value is - /// used instead. - /// Throws a [FormatException] if the [source] string is not valid - /// and no `onError` is provided. + /// Throws a [FormatException] if the [source] string is not + /// a valid double literal. + /// + /// Rather than throwing and immediately catching the [FormatException], + /// instead use [tryParse] to handle a potential parsing error. /// /// Examples of accepted strings: /// ``` @@ -238,11 +238,7 @@ abstract class double extends num { /// "+.12e-9" /// "-NaN" /// ``` - /// The [onError] parameter is deprecated and will be removed. - /// Instead of `double.parse(string, (string) { ... })`, - /// you should use `double.tryParse(string) ?? (...)`. - external static double parse(String source, - [@deprecated double onError(String source)?]); + external static double parse(String source); /// Parse [source] as a double literal and return its value. /// diff --git a/sdk/lib/core/int.dart b/sdk/lib/core/int.dart index e2465cc979b..d463215a741 100644 --- a/sdk/lib/core/int.dart +++ b/sdk/lib/core/int.dart @@ -379,11 +379,10 @@ abstract class int extends num { /// `n == int.parse(n.toRadixString(r), radix: r)`. /// /// If the [source] string does not contain a valid integer literal, - /// optionally prefixed by a sign, a [FormatException] is thrown - /// (unless the deprecated [onError] parameter is used, see below). + /// optionally prefixed by a sign, a [FormatException] is thrown. /// - /// Instead of throwing and immediately catching the [FormatException], - /// instead use [tryParse] to handle a parsing error. + /// Rather than throwing and immediately catching the [FormatException], + /// instead use [tryParse] to handle a potential parsing error. /// /// Example: /// ```dart @@ -393,17 +392,7 @@ abstract class int extends num { /// // ... /// } /// ``` - /// - /// The [onError] parameter is deprecated and will be removed. - /// Instead of `int.parse(string, onError: (string) => ...)`, - /// you should use `int.tryParse(string) ?? (...)`. - /// - /// When the source string is not valid and [onError] is provided, - /// whenever a [FormatException] would be thrown, - /// [onError] is instead called with [source] as argument, - /// and the result of that call is returned by [parse]. - external static int parse(String source, - {int? radix, @deprecated int onError(String source)?}); + external static int parse(String source, {int? radix}); /// Parse [source] as a, possibly signed, integer literal. /// diff --git a/sdk/lib/core/num.dart b/sdk/lib/core/num.dart index b6a2a6d21a8..0b70f636692 100644 --- a/sdk/lib/core/num.dart +++ b/sdk/lib/core/num.dart @@ -541,11 +541,10 @@ abstract class num implements Comparable { /// [int.parse] without a radix). /// If that fails, it tries to parse the [input] as a double (similar to /// [double.parse]). - /// If that fails, too, it invokes [onError] with [input], and the result - /// of that invocation becomes the result of calling `parse`. + /// If that fails, too, it throws a [FormatException]. /// - /// If no [onError] is supplied, it defaults to a function that throws a - /// [FormatException]. + /// Rather than throwing and immediately catching the [FormatException], + /// instead use [tryParse] to handle a potential parsing error. /// /// For any number `n`, this function satisfies /// `identical(n, num.parse(n.toString()))` (except when `n` is a NaN `double` @@ -573,8 +572,7 @@ abstract class num implements Comparable { static num parse(String input, [@deprecated num onError(String input)?]) { num? result = tryParse(input); if (result != null) return result; - if (onError == null) throw FormatException(input); - return onError(input); + throw FormatException(input); } /// Parses a string containing a number literal into a number. diff --git a/tests/corelib/double_parse_test.dart b/tests/corelib/double_parse_test.dart index 97e5c00fbcc..a4a639f3655 100644 --- a/tests/corelib/double_parse_test.dart +++ b/tests/corelib/double_parse_test.dart @@ -103,13 +103,10 @@ void testDouble(double value) { void testFail(String source) { var object = new Object(); - Expect.throws(() { - double.parse(source, (s) { - Expect.equals(source, s); - throw object; - }); - }, (e) => identical(object, e), "Fail: '$source'"); - Expect.equals(1.5, double.parse(source, (s) => 1.5)); + Expect.throwsFormatException(() { + double.parse(source); + }); + Expect.equals(1.5, double.tryParse(source) ?? 1.5); } void main() { diff --git a/tests/corelib/int_parse_radix_bad_handler_test.dart b/tests/corelib/int_parse_radix_bad_handler_test.dart deleted file mode 100644 index 2c3b2be0d01..00000000000 --- a/tests/corelib/int_parse_radix_bad_handler_test.dart +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) 2012, 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() { - // These are compile time errors for Dart 2.0. - Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: "not a function")); - Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: () => 42)); - Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: (v1, v2) => 42)); -} diff --git a/tests/corelib/int_parse_radix_test.dart b/tests/corelib/int_parse_radix_test.dart index 215f737e069..3bbf7696a8a 100644 --- a/tests/corelib/int_parse_radix_test.dart +++ b/tests/corelib/int_parse_radix_test.dart @@ -92,12 +92,9 @@ void main() { Expect.equals(1, int.parse("+1", radix: 2)); void testFails(String source, int radix) { - Expect.throws(() { - throw int.parse(source, radix: radix, onError: (s) { - throw "FAIL"; - }); - }, isFail, "$source/$radix"); - Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999)); + Expect.throwsFormatException( + () => int.parse(source, radix: radix), "$source/$radix"); + Expect.equals(-999, int.tryParse(source, radix: radix) ?? -999); } for (int i = 2; i < 36; i++) { @@ -114,19 +111,10 @@ void main() { testFails("0x10", i); } - testBadTypes(var source, var radix) { - Expect.throwsTypeError(() => int.parse(source, radix: radix, onError: (s) => 0)); //# badTypes: ok - } - - testBadTypes(9, 10); - testBadTypes(true, 10); - testBadTypes("0", true); - testBadTypes("0", "10"); - testBadArguments(String source, int radix) { // If the types match, it should be an ArgumentError of some sort. Expect.throwsArgumentError( - () => int.parse(source, radix: radix, onError: (s) => 0)); + () => int.parse(source, radix: radix)); } testBadArguments("0", -1); @@ -136,5 +124,3 @@ void main() { // See also int_parse_radix_bad_handler_test.dart } - -bool isFail(e) => e == "FAIL"; diff --git a/tests/corelib/int_parse_with_limited_ints_test.dart b/tests/corelib/int_parse_with_limited_ints_test.dart index 018bcd2d19e..6a99f02a4ef 100644 --- a/tests/corelib/int_parse_with_limited_ints_test.dart +++ b/tests/corelib/int_parse_with_limited_ints_test.dart @@ -7,9 +7,6 @@ import "package:expect/expect.dart"; main() { - const int ERROR = 42; - int returnError(String s) => ERROR; - Expect.equals(0, int.parse("0")); Expect.equals(1, int.parse("1")); Expect.equals(-1, int.parse("-1")); @@ -26,11 +23,11 @@ main() { Expect.equals(-9223372036854775807, int.parse("-9223372036854775807")); Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808")); - Expect.equals(ERROR, int.parse("-0x8000000000000001", onError: returnError)); - Expect.equals(ERROR, int.parse("9223372036854775808", onError: returnError)); - Expect.equals(ERROR, int.parse("9223372036854775809", onError: returnError)); - Expect.equals(ERROR, int.parse("-9223372036854775809", onError: returnError)); - Expect.equals(ERROR, int.parse("10000000000000000000", onError: returnError)); + Expect.throws(() => int.parse("-0x8000000000000001")); + Expect.throws(() => int.parse("9223372036854775808")); + Expect.throws(() => int.parse("9223372036854775809")); + Expect.throws(() => int.parse("-9223372036854775809")); + Expect.throws(() => int.parse("10000000000000000000")); Expect.equals( 0x7fffffffffffffff, @@ -48,28 +45,16 @@ main() { "-1000000000000000000000000000000000000000000000000000000000000000", radix: 2)); - Expect.equals( - ERROR, - int.parse( - "1000000000000000000000000000000000000000000000000000000000000000", - radix: 2, - onError: returnError)); - Expect.equals( - ERROR, - int.parse( - "1111111111111111111111111111111111111111111111111111111111111110", - radix: 2, - onError: returnError)); - Expect.equals( - ERROR, - int.parse( - "1111111111111111111111111111111111111111111111111111111111111111", - radix: 2, - onError: returnError)); - Expect.equals( - ERROR, - int.parse( - "-1000000000000000000000000000000000000000000000000000000000000001", - radix: 2, - onError: returnError)); + Expect.throws(() => int.parse( + "1000000000000000000000000000000000000000000000000000000000000000", + radix: 2)); + Expect.throws(() => int.parse( + "1111111111111111111111111111111111111111111111111111111111111110", + radix: 2)); + Expect.throws(() => int.parse( + "1111111111111111111111111111111111111111111111111111111111111111", + radix: 2)); + Expect.throws(() => int.parse( + "-1000000000000000000000000000000000000000000000000000000000000001", + radix: 2)); } diff --git a/tests/corelib/num_parse_test.dart b/tests/corelib/num_parse_test.dart index 848a52e1d4c..b961c0826a3 100644 --- a/tests/corelib/num_parse_test.dart +++ b/tests/corelib/num_parse_test.dart @@ -114,12 +114,9 @@ void testDouble(double value) { void testFail(String source) { var object = new Object(); - Expect.throws(() { - num.parse(source, (s) { - Expect.equals(source, s); - throw object; - }); - }, (e) => identical(object, e), "Fail: '$source'"); + Expect.throwsFormatException(() { + num.parse(source); + }); } void main() { diff --git a/tests/corelib_2/double_parse_test.dart b/tests/corelib_2/double_parse_test.dart index 991540ad14f..7f5c305e987 100644 --- a/tests/corelib_2/double_parse_test.dart +++ b/tests/corelib_2/double_parse_test.dart @@ -105,13 +105,10 @@ void testDouble(double value) { void testFail(String source) { var object = new Object(); - Expect.throws(() { - double.parse(source, (s) { - Expect.equals(source, s); - throw object; - }); - }, (e) => identical(object, e), "Fail: '$source'"); - Expect.equals(1.5, double.parse(source, (s) => 1.5)); + Expect.throwsFormatException(() { + double.parse(source); + }); + Expect.equals(1.5, double.tryParse(source) ?? 1.5); } void main() { diff --git a/tests/corelib_2/int_parse_radix_bad_handler_test.dart b/tests/corelib_2/int_parse_radix_bad_handler_test.dart deleted file mode 100644 index c5de1eac712..00000000000 --- a/tests/corelib_2/int_parse_radix_bad_handler_test.dart +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2012, 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. - -// @dart = 2.9 - -import "package:expect/expect.dart"; - -void main() { - // These are compile time errors for Dart 2.0. - Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: "not a function")); - Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: () => 42)); - Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: (v1, v2) => 42)); -} diff --git a/tests/corelib_2/int_parse_radix_test.dart b/tests/corelib_2/int_parse_radix_test.dart index f53f834e932..7ed7b2e4202 100644 --- a/tests/corelib_2/int_parse_radix_test.dart +++ b/tests/corelib_2/int_parse_radix_test.dart @@ -94,12 +94,9 @@ void main() { Expect.equals(1, int.parse("+1", radix: 2)); void testFails(String source, int radix) { - Expect.throws(() { - throw int.parse(source, radix: radix, onError: (s) { - throw "FAIL"; - }); - }, isFail, "$source/$radix"); - Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999)); + Expect.throwsFormatException( + () => int.parse(source, radix: radix), "$source/$radix"); + Expect.equals(-999, int.tryParse(source, radix: radix) ?? -999); } for (int i = 2; i < 36; i++) { @@ -117,19 +114,10 @@ void main() { testFails("0x10", i); } - testBadTypes(var source, var radix) { - Expect.throwsTypeError(() => int.parse(source, radix: radix, onError: (s) => 0)); //# badTypes: ok - } - - testBadTypes(9, 10); - testBadTypes(true, 10); - testBadTypes("0", true); - testBadTypes("0", "10"); - testBadArguments(String source, int radix) { // If the types match, it should be an ArgumentError of some sort. Expect.throwsArgumentError( - () => int.parse(source, radix: radix, onError: (s) => 0)); + () => int.parse(source, radix: radix)); } testBadArguments("0", -1); @@ -139,5 +127,3 @@ void main() { // See also int_parse_radix_bad_handler_test.dart } - -bool isFail(e) => e == "FAIL"; diff --git a/tests/corelib_2/int_parse_with_limited_ints_test.dart b/tests/corelib_2/int_parse_with_limited_ints_test.dart index 48246d347dd..3cd9528ce1b 100644 --- a/tests/corelib_2/int_parse_with_limited_ints_test.dart +++ b/tests/corelib_2/int_parse_with_limited_ints_test.dart @@ -9,9 +9,6 @@ import "package:expect/expect.dart"; main() { - const int ERROR = 42; - var returnError = (_) => ERROR; - Expect.equals(0, int.parse("0")); Expect.equals(1, int.parse("1")); Expect.equals(-1, int.parse("-1")); @@ -28,11 +25,11 @@ main() { Expect.equals(-9223372036854775807, int.parse("-9223372036854775807")); Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808")); - Expect.equals(ERROR, int.parse("-0x8000000000000001", onError: returnError)); - Expect.equals(ERROR, int.parse("9223372036854775808", onError: returnError)); - Expect.equals(ERROR, int.parse("9223372036854775809", onError: returnError)); - Expect.equals(ERROR, int.parse("-9223372036854775809", onError: returnError)); - Expect.equals(ERROR, int.parse("10000000000000000000", onError: returnError)); + Expect.throws(() => int.parse("-0x8000000000000001")); + Expect.throws(() => int.parse("9223372036854775808")); + Expect.throws(() => int.parse("9223372036854775809")); + Expect.throws(() => int.parse("-9223372036854775809")); + Expect.throws(() => int.parse("10000000000000000000")); Expect.equals( 0x7fffffffffffffff, @@ -50,28 +47,16 @@ main() { "-1000000000000000000000000000000000000000000000000000000000000000", radix: 2)); - Expect.equals( - ERROR, - int.parse( - "1000000000000000000000000000000000000000000000000000000000000000", - radix: 2, - onError: returnError)); - Expect.equals( - ERROR, - int.parse( - "1111111111111111111111111111111111111111111111111111111111111110", - radix: 2, - onError: returnError)); - Expect.equals( - ERROR, - int.parse( - "1111111111111111111111111111111111111111111111111111111111111111", - radix: 2, - onError: returnError)); - Expect.equals( - ERROR, - int.parse( - "-1000000000000000000000000000000000000000000000000000000000000001", - radix: 2, - onError: returnError)); + Expect.throws(() => int.parse( + "1000000000000000000000000000000000000000000000000000000000000000", + radix: 2)); + Expect.throws(() => int.parse( + "1111111111111111111111111111111111111111111111111111111111111110", + radix: 2)); + Expect.throws(() => int.parse( + "1111111111111111111111111111111111111111111111111111111111111111", + radix: 2)); + Expect.throws(() => int.parse( + "-1000000000000000000000000000000000000000000000000000000000000001", + radix: 2)); } diff --git a/tests/corelib_2/num_parse_test.dart b/tests/corelib_2/num_parse_test.dart index 3d6567ef78f..4e0370981e6 100644 --- a/tests/corelib_2/num_parse_test.dart +++ b/tests/corelib_2/num_parse_test.dart @@ -116,12 +116,9 @@ void testDouble(double value) { void testFail(String source) { var object = new Object(); - Expect.throws(() { - num.parse(source, (s) { - Expect.equals(source, s); - throw object; - }); - }, (e) => identical(object, e), "Fail: '$source'"); + Expect.throwsFormatException(() { + num.parse(source); + }); } void main() { diff --git a/tests/language/regress/regress41613_test.dart b/tests/language/regress/regress41613_test.dart deleted file mode 100644 index 0ea1de06036..00000000000 --- a/tests/language/regress/regress41613_test.dart +++ /dev/null @@ -1,16 +0,0 @@ -// 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. - -// @dart=2.7 - -// Requirements=nnbd-weak - -import "package:expect/expect.dart"; - -main() { - // In legacy Dart 2 and null safety weak mode the error handlers can - // return null. - Expect.equals(null, int.parse("foo", onError: (_) => null)); - Expect.equals(null, double.parse("foo", (_) => null)); -} diff --git a/tests/language_2/regress/regress41613_test.dart b/tests/language_2/regress/regress41613_test.dart deleted file mode 100644 index 91dbfd4f0e1..00000000000 --- a/tests/language_2/regress/regress41613_test.dart +++ /dev/null @@ -1,14 +0,0 @@ -// 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. - -// @dart = 2.9 - -import "package:expect/expect.dart"; - -main() { - // In legacy Dart 2 and null safety weak mode the error handlers can - // return null. - Expect.equals(null, int.parse("foo", onError: (_) => null)); - Expect.equals(null, double.parse("foo", (_) => null)); -}