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

Allow user-specified underlying type for enums #8329

Merged

Conversation

IISResetMe
Copy link
Collaborator

@IISResetMe IISResetMe commented Nov 21, 2018

PR Summary

Fix #8028

This change adds support for specifiying the underlying type for an enum:

enum MyEnum : long 
{
  A = 0x0FFFFFFFFFFFFFFF
  B
}
# or
enum MyByte : byte 
{
  A = 0x01
  B = 0x02
  C = 0x03
  D
}

TODO:

  • re-introduce range validation (fixed in f417453)
    • currently breaks on negative value literals (fixed in 19b2770)
  • better parsing errors for invalid underlying types (fixed in fc5a608)
  • value assignment is currently broken from non-int types (fixed in f58eba8)
  • negative parsing tests

PR Checklist

This commit changes the behavior of the DefineEnumHelper, specifically the way the underlying type of the Enum type is specified.
06ff8a3 makes the parser recognize a base type token, this changes the underlying type of the generated EnumBuilder if the base type token corresponds to a builtin CLS value type as specified in ECMA-335 §I.8.5.2 (CLS Rule 7), otherwise it defaults to [int]

TODO:
 - re-introduce range validation
 - better parsing errors for invalid underlying types
I'm thinking there must be an easier way of abstracting this away, but I can't think of anything
Forgot to reset the tokenizer in a previous commit, causing parsing errors.
Added new context for testing behavior when Flags() is applied.
Increased test coverage for all underlying types, added type constraint test
enums can only be based off builtin integral types, parser should report an error if any other kind of type name is passed in
iSazonov and others added 4 commits November 22, 2018 13:29
Co-Authored-By: IISResetMe <IISResetMe@users.noreply.github.com>
Given that valid enum underlying types are all resolvable at parse-time (and unlikely to change), we might as well have the Parser error when an invalid underlying type is passed in as a type constraint.
ErrorId updated in @548b6cd3
Since (any valid) input value and maxValue are bound to be of the same type, we may as well mark maxValue dynamic as well - they will be comparable at runtime, and this avoids incorrectly erring when the user specifies a negative integer value for a member
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 29, 2018
@TravisEz13 TravisEz13 added CL-Engine Indicates that a PR should be marked as an engine change in the Change Log and removed CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log labels Nov 30, 2018
@TravisEz13 TravisEz13 added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Nov 30, 2018
@TravisEz13
Copy link
Member

Can we get committee review for the new syntax?

@SteveL-MSFT SteveL-MSFT added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Dec 5, 2018
@SteveL-MSFT
Copy link
Member

@PowerShell/powershell-committee reviewed this and is fine with the syntax

@adityapatwardhan
Copy link
Member

@TravisEz13 Please update your review.

@stale
Copy link

stale bot commented Jan 10, 2019

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale bot added the Stale label Jan 10, 2019
@vexx32
Copy link
Collaborator

vexx32 commented Jan 10, 2019

@TravisEz13 @IISResetMe apart from the merge conflict, was there anything else pending here? 💖

@stale stale bot removed the Stale label Jan 10, 2019
@TravisEz13
Copy link
Member

@vexx32 My comments have not been addressed. #8329 (review)

The contexts in which this error string is used covers not only literals that are too large, but also sometimes too small
Renamed to EnumeratorValueOutOfBounds
@IISResetMe
Copy link
Collaborator Author

@TravisEz13 I believe the last two commits address your open comments :)

@iSazonov
Copy link
Collaborator

@IISResetMe Please resolve merge conflicts.

//G enum-member-list:
//G enum-member new-lines:opt
//G enum-member-list enum-member

const TypeCode validUnderlyingTypeCodes = TypeCode.Byte | TypeCode.Int16 | TypeCode.Int32 | TypeCode.Int64 | TypeCode.SByte | TypeCode.UInt16 | TypeCode.UInt32 | TypeCode.UInt64;
Copy link
Collaborator

@iSazonov iSazonov Jan 13, 2019

Choose a reason for hiding this comment

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

Const should be upper case:

            const TypeCode ValidUnderlyingTypeCodes

var enumDefn = new TypeDefinitionAst(extent, name.Value, customAttributes == null ? null : customAttributes.OfType<AttributeAst>(), members, TypeAttributes.Enum, underlyingTypeConstraint == null ? null : new[] { underlyingTypeConstraint });
if (customAttributes != null && customAttributes.OfType<TypeConstraintAst>().Any())
{
//no need to report error since there is error reported in method StatementRule
Copy link
Collaborator

@iSazonov iSazonov Jan 13, 2019

Choose a reason for hiding this comment

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

Please add space after //:

Suggested change
//no need to report error since there is error reported in method StatementRule
// No need to report error since there is error reported in method StatementRule.

@IISResetMe
Copy link
Collaborator Author

@iSazonov done!

@IISResetMe
Copy link
Collaborator Author

The Codacy issue currently failing is not really relevant.

While the compiler can't prove it, that path is unreachable since case TypeCode.Int32 will always be chosen if typeConstraintAst == null

@iSazonov
Copy link
Collaborator

Yes, I see. I see that Codacy has many false-positives.

@TravisEz13 TravisEz13 merged commit 59a3696 into PowerShell:master Jan 17, 2019
@iSazonov
Copy link
Collaborator

@IISResetMe Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-Engine Indicates that a PR should be marked as an engine change in the Change Log Committee-Reviewed PS-Committee has reviewed this and made a decision
Projects
None yet
Development

Successfully merging this pull request may close these issues.

enum: Underlying type
7 participants