Skip to content

Allow user-specified underlying type for enums - #8329

Merged
Travis Plunk (TravisEz13) merged 36 commits into
PowerShell:masterfrom
IISResetMe:feature/enum-with-underlying-type
Jan 17, 2019
Merged

Travis Plunk (TravisEz13) merged 36 commits into
PowerShell:masterfrom
IISResetMe:feature/enum-with-underlying-type

Conversation

@IISResetMe

@IISResetMe Mathias R. Jessen (IISResetMe) commented Nov 21, 2018 •

Copy link
Copy Markdown
Collaborator

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
Ilya (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
@TravisEz13 Travis Plunk (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 Travis Plunk (TravisEz13) added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Nov 30, 2018
@TravisEz13

Copy link
Copy Markdown
Member

Can we get committee review for the new syntax?

@SteveL-MSFT Steve Lee (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
Copy Markdown
Member

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

@adityapatwardhan

Copy link
Copy Markdown
Member

Travis Plunk (@TravisEz13) Please update your review.

@stale

stale Bot commented Jan 10, 2019

Copy link
Copy Markdown

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
Copy Markdown
Collaborator

Travis Plunk (@TravisEz13) Mathias R. Jessen (@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
Copy Markdown
Member

Rain Sallow (/u/ta11ow) (@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
Copy Markdown
Collaborator Author

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

@iSazonov

Copy link
Copy Markdown
Collaborator

Mathias R. Jessen (@IISResetMe) Please resolve merge conflicts.

//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;

@iSazonov Ilya (iSazonov) Jan 13, 2019 •

Copy link
Copy Markdown
Collaborator

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

@iSazonov Ilya (iSazonov) Jan 13, 2019 •

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Collaborator Author

Ilya (@iSazonov) done!

@IISResetMe

Copy link
Copy Markdown
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
Copy Markdown
Collaborator

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

@TravisEz13
Travis Plunk (TravisEz13) merged commit 59a3696 into PowerShell:master Jan 17, 2019
@iSazonov

Copy link
Copy Markdown
Collaborator

Mathias R. Jessen (@IISResetMe) Thanks for your contribution!

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Fix PowerShell#8028 

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

```powershell
enum MyEnum : long 
{
  A = 0x0FFFFFFFFFFFFFFF
  B
}
# or
enum MyByte : byte 
{
  A = 0x01
  B = 0x02
  C = 0x03
  D
}
```
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