mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 10:49:00 +00:00
Reland "Add an error when an integer literal is out of bounds"
Change-Id: Id6ef76b47bedabc96fc372f6aea455975dfcc213 Reviewed-on: https://dart-review.googlesource.com/37840 Commit-Queue: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
0a79c54cee
commit
1d9ac49dd9
11 changed files with 116 additions and 26 deletions
|
@ -147,6 +147,7 @@ const List<ErrorCode> errorCodeValues = const [
|
|||
CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY,
|
||||
CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC,
|
||||
CompileTimeErrorCode.INSTANTIATE_ENUM,
|
||||
CompileTimeErrorCode.INTEGER_LITERAL_OUT_OF_RANGE,
|
||||
CompileTimeErrorCode.INVALID_ANNOTATION,
|
||||
CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY,
|
||||
CompileTimeErrorCode.INVALID_CONSTANT,
|
||||
|
|
|
@ -1407,6 +1407,13 @@ class CompileTimeErrorCode extends ErrorCode {
|
|||
"Enums can't be instantiated.",
|
||||
"Try using one of the defined constants.");
|
||||
|
||||
static const CompileTimeErrorCode INTEGER_LITERAL_OUT_OF_RANGE =
|
||||
const CompileTimeErrorCode(
|
||||
'INTEGER_LITERAL_OUT_OF_RANGE',
|
||||
"The integer literal {0} can't be represented in 64 bits.",
|
||||
"Try using the BigInt class if you need an integer larger than "
|
||||
"9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808.");
|
||||
|
||||
/**
|
||||
* 15 Metadata: Metadata consists of a series of annotations, each of which
|
||||
* begin with the character @, followed by a constant expression that must be
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
// 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.
|
||||
|
||||
library analyzer.src.generated.error_verifier;
|
||||
|
||||
import 'dart:collection';
|
||||
import "dart:math" as math;
|
||||
|
||||
|
@ -917,6 +915,12 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
|
|||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Object visitIntegerLiteral(IntegerLiteral node) {
|
||||
_checkForOutOfRange(node);
|
||||
return super.visitIntegerLiteral(node);
|
||||
}
|
||||
|
||||
@override
|
||||
Object visitIsExpression(IsExpression node) {
|
||||
_checkForTypeAnnotationDeferredClass(node.type);
|
||||
|
@ -5368,6 +5372,20 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
|
|||
}
|
||||
}
|
||||
|
||||
void _checkForOutOfRange(IntegerLiteral node) {
|
||||
String lexeme = node.literal.lexeme;
|
||||
AstNode parent = node.parent;
|
||||
bool isNegated =
|
||||
parent is PrefixExpression && parent.operator.type == TokenType.MINUS;
|
||||
if (!IntegerLiteralImpl.isValidLiteral(lexeme, isNegated)) {
|
||||
if (isNegated) {
|
||||
lexeme = '-$lexeme';
|
||||
}
|
||||
_errorReporter.reportErrorForNode(
|
||||
CompileTimeErrorCode.INTEGER_LITERAL_OUT_OF_RANGE, node, [lexeme]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the given named optional [parameter] does not begin with '_'.
|
||||
*
|
||||
|
|
|
@ -1338,6 +1338,20 @@ class CompileTimeErrorCodeTest_Kernel extends CompileTimeErrorCodeTest_Driver {
|
|||
await super.test_instantiateEnum_new();
|
||||
}
|
||||
|
||||
@override
|
||||
@failingTest
|
||||
test_integerLiteralOutOfRange_negative() async {
|
||||
// Expected 1 errors of type CompileTimeErrorCode.INTEGER_LITERAL_OUT_OF_RANGE, found 0
|
||||
await super.test_integerLiteralOutOfRange_negative();
|
||||
}
|
||||
|
||||
@override
|
||||
@failingTest
|
||||
test_integerLiteralOutOfRange_positive() async {
|
||||
// Expected 1 errors of type CompileTimeErrorCode.INTEGER_LITERAL_OUT_OF_RANGE, found 0
|
||||
await super.test_integerLiteralOutOfRange_positive();
|
||||
}
|
||||
|
||||
@override
|
||||
@failingTest
|
||||
test_invalidAnnotation_getter() async {
|
||||
|
|
|
@ -3220,6 +3220,18 @@ E e(String name) {
|
|||
verify([source]);
|
||||
}
|
||||
|
||||
test_integerLiteralOutOfRange_negative() async {
|
||||
Source source = addSource('int x = -9223372036854775809;');
|
||||
await computeAnalysisResult(source);
|
||||
assertErrors(source, [CompileTimeErrorCode.INTEGER_LITERAL_OUT_OF_RANGE]);
|
||||
}
|
||||
|
||||
test_integerLiteralOutOfRange_positive() async {
|
||||
Source source = addSource('int x = 9223372036854775808;');
|
||||
await computeAnalysisResult(source);
|
||||
assertErrors(source, [CompileTimeErrorCode.INTEGER_LITERAL_OUT_OF_RANGE]);
|
||||
}
|
||||
|
||||
test_invalidAnnotation_getter() async {
|
||||
Source source = addSource(r'''
|
||||
get V => 0;
|
||||
|
|
|
@ -189,6 +189,13 @@ class NonErrorResolverTest_Kernel extends NonErrorResolverTest_Driver {
|
|||
return super.test_genericTypeAlias_invalidGenericFunctionType();
|
||||
}
|
||||
|
||||
@override
|
||||
@failingTest
|
||||
@potentialAnalyzerProblem
|
||||
test_integerLiteralOutOfRange_negative_valid() async {
|
||||
return super.test_integerLiteralOutOfRange_negative_valid();
|
||||
}
|
||||
|
||||
@override
|
||||
@failingTest
|
||||
@FastaProblem('https://github.com/dart-lang/sdk/issues/31641')
|
||||
|
|
|
@ -1169,18 +1169,6 @@ f() {
|
|||
verify([source]);
|
||||
}
|
||||
|
||||
test_forEach_genericFunctionType() async {
|
||||
Source source = addSource(r'''
|
||||
main() {
|
||||
for (Null Function<T>(T, Null) e in []) {
|
||||
e;
|
||||
}
|
||||
}''');
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
verify([source]);
|
||||
}
|
||||
|
||||
test_caseBlockNotTerminated() async {
|
||||
Source source = addSource(r'''
|
||||
f(int p) {
|
||||
|
@ -2410,6 +2398,18 @@ class A {
|
|||
verify([source]);
|
||||
}
|
||||
|
||||
test_forEach_genericFunctionType() async {
|
||||
Source source = addSource(r'''
|
||||
main() {
|
||||
for (Null Function<T>(T, Null) e in []) {
|
||||
e;
|
||||
}
|
||||
}''');
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
verify([source]);
|
||||
}
|
||||
|
||||
test_functionDeclaration_scope_returnType() async {
|
||||
Source source = addSource("int f(int) { return 0; }");
|
||||
await computeAnalysisResult(source);
|
||||
|
@ -3105,6 +3105,36 @@ class A {
|
|||
verify([source]);
|
||||
}
|
||||
|
||||
test_integerLiteralOutOfRange_negative_leadingZeros() async {
|
||||
Source source = addSource('int x = -000923372036854775809;');
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
}
|
||||
|
||||
test_integerLiteralOutOfRange_negative_valid() async {
|
||||
Source source = addSource('int x = -9223372036854775808;');
|
||||
await computeAnalysisResult(source);
|
||||
assertErrors(source);
|
||||
}
|
||||
|
||||
test_integerLiteralOutOfRange_positive_leadingZeros() async {
|
||||
Source source = addSource('int x = 000923372036854775808;');
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
}
|
||||
|
||||
test_integerLiteralOutOfRange_positive_valid() async {
|
||||
Source source = addSource('int x = 9223372036854775807;');
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
}
|
||||
|
||||
test_integerLiteralOutOfRange_positive_zero() async {
|
||||
Source source = addSource('int x = 0;');
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
}
|
||||
|
||||
test_invalidAnnotation_constantVariable_field() async {
|
||||
Source source = addSource(r'''
|
||||
@A.C
|
||||
|
@ -5413,6 +5443,13 @@ class B {
|
|||
verify([source]);
|
||||
}
|
||||
|
||||
test_typeArgument_boundToFunctionType() async {
|
||||
Source source = addSource("class A<T extends void Function<T>(T)>{}");
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
verify([source]);
|
||||
}
|
||||
|
||||
test_typeArgumentNotMatchingBounds_const() async {
|
||||
Source source = addSource(r'''
|
||||
class A {}
|
||||
|
@ -6399,13 +6436,6 @@ f() sync* {
|
|||
verify([source]);
|
||||
}
|
||||
|
||||
test_typeArgument_boundToFunctionType() async {
|
||||
Source source = addSource("class A<T extends void Function<T>(T)>{}");
|
||||
await computeAnalysisResult(source);
|
||||
assertNoErrors(source);
|
||||
verify([source]);
|
||||
}
|
||||
|
||||
Future<Null> _check_wrongNumberOfParametersForOperator(
|
||||
String name, String parameters) async {
|
||||
Source source = addSource("""
|
||||
|
|
|
@ -2231,9 +2231,8 @@ const Template<
|
|||
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
|
||||
const Code<Message Function(Token token)> codeIntegerLiteralIsOutOfRange =
|
||||
const Code<Message Function(Token token)>(
|
||||
"IntegerLiteralIsOutOfRange",
|
||||
templateIntegerLiteralIsOutOfRange,
|
||||
);
|
||||
"IntegerLiteralIsOutOfRange", templateIntegerLiteralIsOutOfRange,
|
||||
analyzerCode: "INTEGER_LITERAL_OUT_OF_RANGE", dart2jsCode: "*fatal*");
|
||||
|
||||
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
|
||||
Message _withArgumentsIntegerLiteralIsOutOfRange(Token token) {
|
||||
|
|
|
@ -1675,6 +1675,8 @@ InterpolationInUri:
|
|||
IntegerLiteralIsOutOfRange:
|
||||
template: "The integer literal #lexeme can't be represented in 64 bits."
|
||||
tip: "Try using the BigInt class if you need an integer larger than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808."
|
||||
analyzerCode: INTEGER_LITERAL_OUT_OF_RANGE
|
||||
dart2jsCode: "*fatal*"
|
||||
|
||||
ColonInPlaceOfIn:
|
||||
template: "For-in loops use 'in' rather than a colon."
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
# BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
[ $compiler == dart2analyzer ]
|
||||
compare_to2_test: CompileTimeError # invalid test
|
||||
int_parse_radix_bad_handler_test: MissingCompileTimeError
|
||||
iterable_element_at_test/static: Pass
|
||||
num_sign_test: Crash, Pass # Issue 31768
|
||||
|
@ -383,6 +384,7 @@ map_keys2_test: RuntimeError # needs Dart 2 is checks
|
|||
iterable_mapping_test/01: MissingCompileTimeError
|
||||
|
||||
[ $compiler == dartdevc && $runtime != none ]
|
||||
compare_to2_test: CompileTimeError # invalid test
|
||||
symbol_operator_test: RuntimeError # Issue 29921
|
||||
|
||||
[ $compiler != dartdevc && $compiler != dartdevk && $checked && !$strong ]
|
||||
|
|
|
@ -1251,8 +1251,6 @@ generic_test: CompileTimeError
|
|||
generics_test: CompileTimeError
|
||||
import_core_prefix_test: CompileTimeError # "dynamic" should be defined in core.
|
||||
instantiate_type_variable_test/01: CompileTimeError
|
||||
int64_literal_test/03: MissingCompileTimeError # http://dartbug.com/31479
|
||||
int64_literal_test/30: MissingCompileTimeError # http://dartbug.com/31479
|
||||
interceptor6_test: CompileTimeError
|
||||
issue13673_test: StaticWarning # Issue 31925
|
||||
issue31596_implement_covariant_test: CompileTimeError
|
||||
|
|
Loading…
Reference in a new issue