Skip to content
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

Const enums #970

Merged
merged 19 commits into from
Nov 3, 2014
Merged

Const enums #970

merged 19 commits into from
Nov 3, 2014

Conversation

vladima
Copy link
Contributor

@vladima vladima commented Oct 26, 2014

No description provided.

case SyntaxKind.AmpersandToken: return left & right;
case SyntaxKind.PlusToken: return left + right;
case SyntaxKind.MinusToken: return left - right;
case SyntaxKind.GreaterThanGreaterThanToken: return left >> right;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should support >>> as well probably

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not include *, /, %, and ^ as well? Seems hard to define a principle that would exclude them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to Anders' suggestion. Might be arguable as to what you want to do when the RHS for / and % are 0 though.

@JsonFreeman
Copy link
Contributor

👍

@ahejlsberg
Copy link
Member

Is there a particular reason these optimizations need to be under a flag. Could we just always do them?

Also, an even more impactful optimization (that would need to be under a flag) is to completely erase enums from the generated code, i.e. require that all declared values can be evaluated to constants at compile time and that there are no references to the enum object itself anywhere in the code such that we can omit it in the generated code.

@CyrusNajmabadi
Copy link
Contributor

@ahejlsberg We could always do them. We'd simply have to add that to the spec. Right now these are an extension to the language, and so they should not be happening without the user opting-in.

Also, note that the complete erasure would also be something we'd need to spec, (and likely make into a switch that someone could opt-in/out of). Even if there are no references in the code at runtime, the enum may still be used during debugging, and we'd want some way for it to be there.

@vladima
Copy link
Contributor Author

vladima commented Oct 28, 2014

I've tried to convert enums in core compiler to const enums. Size of tsc.js before conversion: ~930k, after conversion 887k

@@ -5158,8 +5164,8 @@ module ts {
}

function inferTypeArguments(signature: Signature, args: Expression[], excludeArgument?: boolean[]): InferenceContext {
var typeParameters = signature.typeParameters;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge artefact, this part was not changed

@vladima
Copy link
Contributor Author

vladima commented Oct 28, 2014

It is on my TODO list

isRhsOfImportStatement(node);

if (!ok) {
error(node, Diagnostics.const_enums_can_only_be_used_in_property_access_expressions);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message is updated in the latest bits and will be checked in shortly

@vladima vladima changed the title Constant folding in computed enum members Const enums Oct 30, 2014
@vladima vladima mentioned this pull request Nov 2, 2014
@vladima
Copy link
Contributor Author

vladima commented Nov 2, 2014

2d94030 is technically a breaking change since we didn't used to inline indexed access expressions for regular enums. However spec defined property access by using either dot or bracket notations so I'd say current version is more consistent and aligned with a spec - property access (in any notation) for enum members is always inlined if member was initialized with a constant value (for regular enums) or with constant expression (for const enums)

@CyrusNajmabadi
Copy link
Contributor

The only piece missing for me is the ability to have a command line switch to emit the reverse-enum-map. We definitely need that, or else debugging will be excessively painful.

@vladima
Copy link
Contributor Author

vladima commented Nov 3, 2014

actually this functionality is already checked in - you need to specify 'preserveConstEnums' command line argument

…tion to 'preserveConstEnums' command line argument
vladima added a commit that referenced this pull request Nov 3, 2014
@vladima vladima merged commit f801ec1 into master Nov 3, 2014
@vladima vladima deleted the fold_computed_enum_members branch November 19, 2014 06:32
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants