Produce a specific error for 'enum' as constraint#59612
Conversation
|
❔ Should we handle |
Sure. Added |
| <value>Line contains different whitespace than the closing line of the raw string literal: '{0}' versus '{1}'</value> | ||
| </data> | ||
| <data name="ERR_NoEnumConstraint" xml:space="preserve"> | ||
| <value>Keyword 'enum' cannot be used as a constraint. Did you mean 'System.Enum'?</value> |
There was a problem hiding this comment.
| <value>Keyword 'enum' cannot be used as a constraint. Did you mean 'System.Enum'?</value> | |
| <value>Keyword 'enum' cannot be used as a constraint. Did you mean 'where T : struct, System.Enum'?</value> |
There was a problem hiding this comment.
I lean against this change (the original text is a better match to the variety of scenarios supported)
There was a problem hiding this comment.
I genuinely disagree. The original text isn't really the recommendation (it would allow System.Enum itself, which most people do not want, and can lead to confusing scenarios since it doesn't actually contrain just to actual enums). Since we know their intent (when they write enum) i think we should suggest the best lang consturct to match that intent.
There was a problem hiding this comment.
I agree with Cyrus here. It feels like the struct, System.Enum version is the intent being expressed by the user.
| { | ||
| var missingType = this.AddError(this.CreateMissingIdentifierName(), ErrorCode.ERR_NoEnumConstraint); | ||
| missingType = AddTrailingSkippedSyntax(missingType, this.EatToken()); | ||
| return _syntaxFactory.TypeConstraint(missingType); |
There was a problem hiding this comment.
i still think we should make this a struct-constraint (to help with later stuff). but it's nbd.
There was a problem hiding this comment.
It's tempting, but I'd rather not give too much meaning to a bad token. It could be a legitimate misspelling (from Anum?).
There was a problem hiding this comment.
i think that seems highly ulikely. enum is a strong c# keyword, and i personally think it's totally clear what hte user is trying to say when they add that constraint. Indeed, it's a constraint we've even considered, we just didn't do it since it falls out from where T : struct, System.Enum :)
|
@dotnet/roslyn-compiler for review. |
| N(SyntaxKind.EndOfFileToken); | ||
| } | ||
| EOF(); | ||
| } |
|
@dotnet/roslyn-compiler for second review. Thanks |
|
@333fred @dotnet/roslyn-compiler for second review. Small parser error recover improvement. Thanks |
| <value>Line contains different whitespace than the closing line of the raw string literal: '{0}' versus '{1}'</value> | ||
| </data> | ||
| <data name="ERR_NoEnumConstraint" xml:space="preserve"> | ||
| <value>Keyword 'enum' cannot be used as a constraint. Did you mean 'System.Enum'?</value> |
There was a problem hiding this comment.
I agree with Cyrus here. It feels like the struct, System.Enum version is the intent being expressed by the user.
Fixes #59495