-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Allow user-specified underlying type for enums #8329
Conversation
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
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.
…m/IISResetMe/PowerShell into feature/enum-with-underlying-type
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
Can we get committee review for the new syntax? |
@PowerShell/powershell-committee reviewed this and is fine with the syntax |
@TravisEz13 Please update your review. |
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. |
@TravisEz13 @IISResetMe apart from the merge conflict, was there anything else pending here? 💖 |
@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
@TravisEz13 I believe the last two commits address your open comments :) |
@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; |
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.
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 |
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.
Please add space after //:
//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. |
@iSazonov done! |
The Codacy issue currently failing is not really relevant. While the compiler can't prove it, that path is unreachable since |
Yes, I see. I see that Codacy has many false-positives. |
@IISResetMe Thanks for your contribution! |
PR Summary
Fix #8028
This change adds support for specifiying the underlying type for an enum:
TODO:
currently breaks on negative value literals(fixed in 19b2770)value assignment is currently broken from non-int types(fixed in f58eba8)PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
to the beginning of the title and remove the prefix when the PR is ready.[feature]
if the change is significant or affects feature tests