[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 <lrn@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Michael Thomsen 2022-12-12 09:54:54 +00:00
parent 4028b9b0fe
commit b3304af17d
16 changed files with 70 additions and 214 deletions

View file

@ -5695,7 +5695,6 @@
"../../../tests/language_2/regress/regress40066_test.dart", "../../../tests/language_2/regress/regress40066_test.dart",
"../../../tests/language_2/regress/regress40765_test.dart", "../../../tests/language_2/regress/regress40765_test.dart",
"../../../tests/language_2/regress/regress4157508_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/regress41983_test.dart",
"../../../tests/language_2/regress/regress42946_test.dart", "../../../tests/language_2/regress/regress42946_test.dart",
"../../../tests/language_2/regress/regress4295001_test.dart", "../../../tests/language_2/regress/regress4295001_test.dart",

View file

@ -221,11 +221,11 @@ abstract class double extends num {
/// ///
/// Leading and trailing whitespace is ignored. /// Leading and trailing whitespace is ignored.
/// ///
/// If the [source] string is not a valid double literal, the [onError] /// Throws a [FormatException] if the [source] string is not
/// is called with the [source] as argument, and its return value is /// a valid double literal.
/// used instead. ///
/// Throws a [FormatException] if the [source] string is not valid /// Rather than throwing and immediately catching the [FormatException],
/// and no `onError` is provided. /// instead use [tryParse] to handle a potential parsing error.
/// ///
/// Examples of accepted strings: /// Examples of accepted strings:
/// ``` /// ```
@ -238,11 +238,7 @@ abstract class double extends num {
/// "+.12e-9" /// "+.12e-9"
/// "-NaN" /// "-NaN"
/// ``` /// ```
/// The [onError] parameter is deprecated and will be removed. external static double parse(String source);
/// Instead of `double.parse(string, (string) { ... })`,
/// you should use `double.tryParse(string) ?? (...)`.
external static double parse(String source,
[@deprecated double onError(String source)?]);
/// Parse [source] as a double literal and return its value. /// Parse [source] as a double literal and return its value.
/// ///

View file

@ -379,11 +379,10 @@ abstract class int extends num {
/// `n == int.parse(n.toRadixString(r), radix: r)`. /// `n == int.parse(n.toRadixString(r), radix: r)`.
/// ///
/// If the [source] string does not contain a valid integer literal, /// If the [source] string does not contain a valid integer literal,
/// optionally prefixed by a sign, a [FormatException] is thrown /// optionally prefixed by a sign, a [FormatException] is thrown.
/// (unless the deprecated [onError] parameter is used, see below).
/// ///
/// Instead of throwing and immediately catching the [FormatException], /// Rather than throwing and immediately catching the [FormatException],
/// instead use [tryParse] to handle a parsing error. /// instead use [tryParse] to handle a potential parsing error.
/// ///
/// Example: /// Example:
/// ```dart /// ```dart
@ -393,17 +392,7 @@ abstract class int extends num {
/// // ... /// // ...
/// } /// }
/// ``` /// ```
/// external static int parse(String source, {int? radix});
/// 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)?});
/// Parse [source] as a, possibly signed, integer literal. /// Parse [source] as a, possibly signed, integer literal.
/// ///

View file

@ -541,11 +541,10 @@ abstract class num implements Comparable<num> {
/// [int.parse] without a radix). /// [int.parse] without a radix).
/// If that fails, it tries to parse the [input] as a double (similar to /// If that fails, it tries to parse the [input] as a double (similar to
/// [double.parse]). /// [double.parse]).
/// If that fails, too, it invokes [onError] with [input], and the result /// If that fails, too, it throws a [FormatException].
/// of that invocation becomes the result of calling `parse`.
/// ///
/// If no [onError] is supplied, it defaults to a function that throws a /// Rather than throwing and immediately catching the [FormatException],
/// [FormatException]. /// instead use [tryParse] to handle a potential parsing error.
/// ///
/// For any number `n`, this function satisfies /// For any number `n`, this function satisfies
/// `identical(n, num.parse(n.toString()))` (except when `n` is a NaN `double` /// `identical(n, num.parse(n.toString()))` (except when `n` is a NaN `double`
@ -573,8 +572,7 @@ abstract class num implements Comparable<num> {
static num parse(String input, [@deprecated num onError(String input)?]) { static num parse(String input, [@deprecated num onError(String input)?]) {
num? result = tryParse(input); num? result = tryParse(input);
if (result != null) return result; if (result != null) return result;
if (onError == null) throw FormatException(input); throw FormatException(input);
return onError(input);
} }
/// Parses a string containing a number literal into a number. /// Parses a string containing a number literal into a number.

View file

@ -103,13 +103,10 @@ void testDouble(double value) {
void testFail(String source) { void testFail(String source) {
var object = new Object(); var object = new Object();
Expect.throws(() { Expect.throwsFormatException(() {
double.parse(source, (s) { double.parse(source);
Expect.equals(source, s); });
throw object; Expect.equals(1.5, double.tryParse(source) ?? 1.5);
});
}, (e) => identical(object, e), "Fail: '$source'");
Expect.equals(1.5, double.parse(source, (s) => 1.5));
} }
void main() { void main() {

View file

@ -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));
}

View file

@ -92,12 +92,9 @@ void main() {
Expect.equals(1, int.parse("+1", radix: 2)); Expect.equals(1, int.parse("+1", radix: 2));
void testFails(String source, int radix) { void testFails(String source, int radix) {
Expect.throws(() { Expect.throwsFormatException(
throw int.parse(source, radix: radix, onError: (s) { () => int.parse(source, radix: radix), "$source/$radix");
throw "FAIL"; Expect.equals(-999, int.tryParse(source, radix: radix) ?? -999);
});
}, isFail, "$source/$radix");
Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999));
} }
for (int i = 2; i < 36; i++) { for (int i = 2; i < 36; i++) {
@ -114,19 +111,10 @@ void main() {
testFails("0x10", i); 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) { testBadArguments(String source, int radix) {
// If the types match, it should be an ArgumentError of some sort. // If the types match, it should be an ArgumentError of some sort.
Expect.throwsArgumentError( Expect.throwsArgumentError(
() => int.parse(source, radix: radix, onError: (s) => 0)); () => int.parse(source, radix: radix));
} }
testBadArguments("0", -1); testBadArguments("0", -1);
@ -136,5 +124,3 @@ void main() {
// See also int_parse_radix_bad_handler_test.dart // See also int_parse_radix_bad_handler_test.dart
} }
bool isFail(e) => e == "FAIL";

View file

@ -7,9 +7,6 @@
import "package:expect/expect.dart"; import "package:expect/expect.dart";
main() { main() {
const int ERROR = 42;
int returnError(String s) => ERROR;
Expect.equals(0, int.parse("0")); Expect.equals(0, int.parse("0"));
Expect.equals(1, int.parse("1")); Expect.equals(1, int.parse("1"));
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, int.parse("-9223372036854775807"));
Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808")); Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808"));
Expect.equals(ERROR, int.parse("-0x8000000000000001", onError: returnError)); Expect.throws(() => int.parse("-0x8000000000000001"));
Expect.equals(ERROR, int.parse("9223372036854775808", onError: returnError)); Expect.throws(() => int.parse("9223372036854775808"));
Expect.equals(ERROR, int.parse("9223372036854775809", onError: returnError)); Expect.throws(() => int.parse("9223372036854775809"));
Expect.equals(ERROR, int.parse("-9223372036854775809", onError: returnError)); Expect.throws(() => int.parse("-9223372036854775809"));
Expect.equals(ERROR, int.parse("10000000000000000000", onError: returnError)); Expect.throws(() => int.parse("10000000000000000000"));
Expect.equals( Expect.equals(
0x7fffffffffffffff, 0x7fffffffffffffff,
@ -48,28 +45,16 @@ main() {
"-1000000000000000000000000000000000000000000000000000000000000000", "-1000000000000000000000000000000000000000000000000000000000000000",
radix: 2)); radix: 2));
Expect.equals( Expect.throws(() => int.parse(
ERROR, "1000000000000000000000000000000000000000000000000000000000000000",
int.parse( radix: 2));
"1000000000000000000000000000000000000000000000000000000000000000", Expect.throws(() => int.parse(
radix: 2, "1111111111111111111111111111111111111111111111111111111111111110",
onError: returnError)); radix: 2));
Expect.equals( Expect.throws(() => int.parse(
ERROR, "1111111111111111111111111111111111111111111111111111111111111111",
int.parse( radix: 2));
"1111111111111111111111111111111111111111111111111111111111111110", Expect.throws(() => int.parse(
radix: 2, "-1000000000000000000000000000000000000000000000000000000000000001",
onError: returnError)); radix: 2));
Expect.equals(
ERROR,
int.parse(
"1111111111111111111111111111111111111111111111111111111111111111",
radix: 2,
onError: returnError));
Expect.equals(
ERROR,
int.parse(
"-1000000000000000000000000000000000000000000000000000000000000001",
radix: 2,
onError: returnError));
} }

View file

@ -114,12 +114,9 @@ void testDouble(double value) {
void testFail(String source) { void testFail(String source) {
var object = new Object(); var object = new Object();
Expect.throws(() { Expect.throwsFormatException(() {
num.parse(source, (s) { num.parse(source);
Expect.equals(source, s); });
throw object;
});
}, (e) => identical(object, e), "Fail: '$source'");
} }
void main() { void main() {

View file

@ -105,13 +105,10 @@ void testDouble(double value) {
void testFail(String source) { void testFail(String source) {
var object = new Object(); var object = new Object();
Expect.throws(() { Expect.throwsFormatException(() {
double.parse(source, (s) { double.parse(source);
Expect.equals(source, s); });
throw object; Expect.equals(1.5, double.tryParse(source) ?? 1.5);
});
}, (e) => identical(object, e), "Fail: '$source'");
Expect.equals(1.5, double.parse(source, (s) => 1.5));
} }
void main() { void main() {

View file

@ -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));
}

View file

@ -94,12 +94,9 @@ void main() {
Expect.equals(1, int.parse("+1", radix: 2)); Expect.equals(1, int.parse("+1", radix: 2));
void testFails(String source, int radix) { void testFails(String source, int radix) {
Expect.throws(() { Expect.throwsFormatException(
throw int.parse(source, radix: radix, onError: (s) { () => int.parse(source, radix: radix), "$source/$radix");
throw "FAIL"; Expect.equals(-999, int.tryParse(source, radix: radix) ?? -999);
});
}, isFail, "$source/$radix");
Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999));
} }
for (int i = 2; i < 36; i++) { for (int i = 2; i < 36; i++) {
@ -117,19 +114,10 @@ void main() {
testFails("0x10", i); 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) { testBadArguments(String source, int radix) {
// If the types match, it should be an ArgumentError of some sort. // If the types match, it should be an ArgumentError of some sort.
Expect.throwsArgumentError( Expect.throwsArgumentError(
() => int.parse(source, radix: radix, onError: (s) => 0)); () => int.parse(source, radix: radix));
} }
testBadArguments("0", -1); testBadArguments("0", -1);
@ -139,5 +127,3 @@ void main() {
// See also int_parse_radix_bad_handler_test.dart // See also int_parse_radix_bad_handler_test.dart
} }
bool isFail(e) => e == "FAIL";

View file

@ -9,9 +9,6 @@
import "package:expect/expect.dart"; import "package:expect/expect.dart";
main() { main() {
const int ERROR = 42;
var returnError = (_) => ERROR;
Expect.equals(0, int.parse("0")); Expect.equals(0, int.parse("0"));
Expect.equals(1, int.parse("1")); Expect.equals(1, int.parse("1"));
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, int.parse("-9223372036854775807"));
Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808")); Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808"));
Expect.equals(ERROR, int.parse("-0x8000000000000001", onError: returnError)); Expect.throws(() => int.parse("-0x8000000000000001"));
Expect.equals(ERROR, int.parse("9223372036854775808", onError: returnError)); Expect.throws(() => int.parse("9223372036854775808"));
Expect.equals(ERROR, int.parse("9223372036854775809", onError: returnError)); Expect.throws(() => int.parse("9223372036854775809"));
Expect.equals(ERROR, int.parse("-9223372036854775809", onError: returnError)); Expect.throws(() => int.parse("-9223372036854775809"));
Expect.equals(ERROR, int.parse("10000000000000000000", onError: returnError)); Expect.throws(() => int.parse("10000000000000000000"));
Expect.equals( Expect.equals(
0x7fffffffffffffff, 0x7fffffffffffffff,
@ -50,28 +47,16 @@ main() {
"-1000000000000000000000000000000000000000000000000000000000000000", "-1000000000000000000000000000000000000000000000000000000000000000",
radix: 2)); radix: 2));
Expect.equals( Expect.throws(() => int.parse(
ERROR, "1000000000000000000000000000000000000000000000000000000000000000",
int.parse( radix: 2));
"1000000000000000000000000000000000000000000000000000000000000000", Expect.throws(() => int.parse(
radix: 2, "1111111111111111111111111111111111111111111111111111111111111110",
onError: returnError)); radix: 2));
Expect.equals( Expect.throws(() => int.parse(
ERROR, "1111111111111111111111111111111111111111111111111111111111111111",
int.parse( radix: 2));
"1111111111111111111111111111111111111111111111111111111111111110", Expect.throws(() => int.parse(
radix: 2, "-1000000000000000000000000000000000000000000000000000000000000001",
onError: returnError)); radix: 2));
Expect.equals(
ERROR,
int.parse(
"1111111111111111111111111111111111111111111111111111111111111111",
radix: 2,
onError: returnError));
Expect.equals(
ERROR,
int.parse(
"-1000000000000000000000000000000000000000000000000000000000000001",
radix: 2,
onError: returnError));
} }

View file

@ -116,12 +116,9 @@ void testDouble(double value) {
void testFail(String source) { void testFail(String source) {
var object = new Object(); var object = new Object();
Expect.throws(() { Expect.throwsFormatException(() {
num.parse(source, (s) { num.parse(source);
Expect.equals(source, s); });
throw object;
});
}, (e) => identical(object, e), "Fail: '$source'");
} }
void main() { void main() {

View file

@ -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));
}

View file

@ -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));
}