allow arithmetic operations in constant expressions, handle infinity\NaN results

This commit is contained in:
Vladimir Matveev
2014-10-29 23:21:30 -07:00
parent e949eda583
commit 4aa4ea75d1
6 changed files with 70 additions and 4 deletions
+14
View File
@@ -7686,6 +7686,15 @@ module ts {
checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined);
}
}
else if (enumIsConst) {
if (isNaN(autoValue)) {
error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_NaN);
}
else if (!isFinite(autoValue)) {
error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_number);
}
}
}
else if (ambient && !enumIsConst) {
autoValue = undefined;
@@ -7735,6 +7744,11 @@ module ts {
case SyntaxKind.GreaterThanGreaterThanGreaterThanToken: return left >>> right;
case SyntaxKind.LessThanLessThanToken: return left << right;
case SyntaxKind.CaretToken: return left ^ right;
case SyntaxKind.AsteriskToken: return left * right;
case SyntaxKind.SlashToken: return left / right;
case SyntaxKind.PlusToken: return left + right;
case SyntaxKind.MinusToken: return left - right;
case SyntaxKind.PercentToken: return left % right;
}
return undefined;
case SyntaxKind.NumericLiteral:
@@ -354,6 +354,8 @@ module ts {
In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 4083, category: DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression." },
const_enums_can_only_be_used_in_property_access_expressions: { code: 4084, category: DiagnosticCategory.Error, key: "'const' enums can only be used in property access expressions." },
Index_expression_arguments_in_const_enums_must_be_of_type_string: { code: 4085, category: DiagnosticCategory.Error, key: "Index expression arguments in 'const' enums must be of type 'string'." },
const_enum_member_initializer_was_evaluated_to_a_non_finite_number: { code: 4086, category: DiagnosticCategory.Error, key: "'const' enum member initializer was evaluated to a non-finite number." },
const_enum_member_initializer_was_evaluated_to_NaN: { code: 4087, category: DiagnosticCategory.Error, key: "'const' enum member initializer was evaluated to NaN." },
The_current_host_does_not_support_the_0_option: { code: 5001, category: DiagnosticCategory.Error, key: "The current host does not support the '{0}' option." },
Cannot_find_the_common_subdirectory_path_for_the_input_files: { code: 5009, category: DiagnosticCategory.Error, key: "Cannot find the common subdirectory path for the input files." },
Cannot_read_file_0_Colon_1: { code: 5012, category: DiagnosticCategory.Error, key: "Cannot read file '{0}': {1}" },
+8
View File
@@ -1417,6 +1417,14 @@
"category": "Error",
"code": 4085
},
"'const' enum member initializer was evaluated to a non-finite number.": {
"category": "Error",
"code": 4086
},
"'const' enum member initializer was evaluated to NaN.": {
"category": "Error",
"code": 4087
},
"The current host does not support the '{0}' option.": {
"category": "Error",
"code": 5001
@@ -8,9 +8,12 @@ tests/cases/compiler/constEnumErrors.ts(24,13): error TS4085: Index expression a
tests/cases/compiler/constEnumErrors.ts(26,9): error TS4084: 'const' enums can only be used in property access expressions.
tests/cases/compiler/constEnumErrors.ts(27,10): error TS4084: 'const' enums can only be used in property access expressions.
tests/cases/compiler/constEnumErrors.ts(32,5): error TS4084: 'const' enums can only be used in property access expressions.
tests/cases/compiler/constEnumErrors.ts(40,9): error TS4086: 'const' enum member initializer was evaluated to a non-finite number.
tests/cases/compiler/constEnumErrors.ts(41,9): error TS4086: 'const' enum member initializer was evaluated to a non-finite number.
tests/cases/compiler/constEnumErrors.ts(42,9): error TS4087: 'const' enum member initializer was evaluated to NaN.
==== tests/cases/compiler/constEnumErrors.ts (10 errors) ====
==== tests/cases/compiler/constEnumErrors.ts (13 errors) ====
const enum E {
~
!!! error TS2300: Duplicate identifier 'E'.
@@ -62,4 +65,21 @@ tests/cases/compiler/constEnumErrors.ts(32,5): error TS4084: 'const' enums can o
foo(E2);
~~
!!! error TS4084: 'const' enums can only be used in property access expressions.
!!! error TS4084: 'const' enums can only be used in property access expressions.
const enum NaNOrInfinity {
A = 9007199254740992,
B = A * A,
C = B * B,
D = C * C,
E = D * D,
F = E * E, // overflow
~~~~~
!!! error TS4086: 'const' enum member initializer was evaluated to a non-finite number.
G = 1 / 0, // overflow
~~~~~
!!! error TS4086: 'const' enum member initializer was evaluated to a non-finite number.
H = 0 / 0 // NaN
~~~~~
!!! error TS4087: 'const' enum member initializer was evaluated to NaN.
}
+12 -1
View File
@@ -30,7 +30,18 @@ var y = [E2];
function foo(t: any): void {
}
foo(E2);
foo(E2);
const enum NaNOrInfinity {
A = 9007199254740992,
B = A * A,
C = B * B,
D = C * C,
E = D * D,
F = E * E, // overflow
G = 1 / 0, // overflow
H = 0 / 0 // NaN
}
//// [constEnumErrors.js]
var E;
+12 -1
View File
@@ -29,4 +29,15 @@ var y = [E2];
function foo(t: any): void {
}
foo(E2);
foo(E2);
const enum NaNOrInfinity {
A = 9007199254740992,
B = A * A,
C = B * B,
D = C * C,
E = D * D,
F = E * E, // overflow
G = 1 / 0, // overflow
H = 0 / 0 // NaN
}