-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Const enums #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Const enums #970
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0d171ca
initial implementation of constant folding
vladima 97460f5
handle non-qualified names, add 'propagateEnumConstants' command line…
vladima ce336bc
added folding for references to enum members in enum member initializ…
vladima 365587f
addressed CR feedback, added support for indexed access
vladima cb472eb
move code around to consolidate checks in one place
vladima 03cb645
dropped redundand type assertion, added mising check
vladima 329d6e2
merge with master
vladima 2dd9511
'const enum' iteration 0. TODO: allow and track const enums in import…
vladima 6f4ea86
merge with master
vladima e949eda
const enums, iteration 1: const enums can be used in imports, const e…
vladima 4aa4ea7
allow arithmetic operations in constant expressions, handle infinity\…
vladima 270d187
addressed CR feedback
vladima dd57c6c
added .d.ts generation tests
vladima ac54fbf
set 'earlyError' bit to 'non-constant expression in constant enum ini…
vladima 7d80b71
do not treat module that contains only const enums as instantiated
vladima 8662c68
add test for 'preserveConstEnums' command line argument
vladima 0b738e8
merge with master
vladima 2d94030
inline enum constant values for indexed access when index is string l…
vladima 4d354c0
addressed CR feedback: adjusted text of error messages, added descrip…
vladima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
tests/baselines/reference/constantsInEnumMembers.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
tests/cases/compiler/constantsInEnumMembers.ts(38,9): error TS4079: In 'const' enum declarations member initializer must be constant expression. | ||
tests/cases/compiler/constantsInEnumMembers.ts(40,9): error TS4079: In 'const' enum declarations member initializer must be constant expression. | ||
tests/cases/compiler/constantsInEnumMembers.ts(41,10): error TS4079: In 'const' enum declarations member initializer must be constant expression. | ||
|
||
|
||
==== tests/cases/compiler/constantsInEnumMembers.ts (3 errors) ==== | ||
const enum Enum1 { | ||
A0 = 100, | ||
} | ||
|
||
const enum Enum1 { | ||
// correct cases | ||
A, | ||
B, | ||
C = 10, | ||
D = A + B, | ||
E = A + 1, | ||
F = 1 + A, | ||
G = 1 + 1, | ||
H = A - B, | ||
I = A - 1, | ||
J = 1 - A, | ||
K = 1 - 1, | ||
L = ~D, | ||
M = E << B, | ||
N = E << 1, | ||
O = E >> B, | ||
P = E >> 1, | ||
Q = -D, | ||
R = C & 5, | ||
S = 5 & C, | ||
T = C | D, | ||
U = C | 1, | ||
V = 10 | D, | ||
W = Enum1.V, | ||
|
||
// correct cases: reference to the enum member from different enum declaration | ||
W1 = A0, | ||
W2 = Enum1.A0, | ||
W3 = Enum1["A0"], | ||
W4 = Enum1["W"], | ||
// illegal case | ||
// forward reference to the element of the same enum | ||
X = Y, | ||
~ | ||
!!! error TS4079: In 'const' enum declarations member initializer must be constant expression. | ||
// forward reference to the element of the same enum | ||
Y = Enum1.Z, | ||
~~~~~~~ | ||
!!! error TS4079: In 'const' enum declarations member initializer must be constant expression. | ||
Y1 = Enum1["Z"], | ||
~~~~~~~~~~ | ||
!!! error TS4079: In 'const' enum declarations member initializer must be constant expression. | ||
Z = 100, | ||
} | ||
|
||
|
||
module A { | ||
export module B { | ||
export module C { | ||
export const enum E { | ||
V1 = 1, | ||
V2 = A.B.C.E.V1 + 100 | ||
} | ||
} | ||
} | ||
} | ||
|
||
module A { | ||
export module B { | ||
export module C { | ||
export const enum E { | ||
V3 = A.B.C.E["V2"] + 200, | ||
} | ||
} | ||
} | ||
} | ||
|
||
function foo(x: Enum1) { | ||
switch (x) { | ||
case Enum1.A: | ||
case Enum1.B: | ||
case Enum1.C: | ||
case Enum1.D: | ||
case Enum1.E: | ||
case Enum1.F: | ||
case Enum1.G: | ||
case Enum1.H: | ||
case Enum1.I: | ||
case Enum1.J: | ||
case Enum1.K: | ||
case Enum1.L: | ||
case Enum1.M: | ||
case Enum1.N: | ||
case Enum1.O: | ||
case Enum1.P: | ||
case Enum1.Q: | ||
case Enum1.R: | ||
case Enum1.S: | ||
case Enum1.T: | ||
case Enum1.U: | ||
case Enum1.V: | ||
case Enum1.W: | ||
case Enum1.W1: | ||
case Enum1.W2: | ||
case Enum1.W3: | ||
case Enum1.W4: | ||
case Enum1.X: | ||
case Enum1.Y: | ||
case Enum1.Y1: | ||
case Enum1.Z: | ||
break; | ||
} | ||
} | ||
|
||
function bar(e: A.B.C.E): number { | ||
switch (e) { | ||
case A.B.C.E.V1: return 1; | ||
case A.B.C.E.V2: return 1; | ||
case A.B.C.E.V3: return 1; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting that for non-const ambient enums, we check that the initializers are integer literals, but for const, we do not check anything until typecheck. It seems like we might want to do syntactic validation that the const initializers are of the syntactic form of a constant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should discuss it: definitely we can try to validate that initializer syntactically includes only operations that can be performed in compile time however this won't remove need in check in typechecker (because parser cannot validate that
A.B
can indeed be constant expression).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, you would need to check in both places. It would just be more consistent with the integer literal check for ambients.