-
Notifications
You must be signed in to change notification settings - Fork 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
Disallow declaration of variables within constructor/field initializers. #13510
Conversation
@@ -5012,11 +5193,22 @@ public C(object b) | |||
} | |||
"; | |||
var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); | |||
#if ALLOW_IN_CONSTRUCTOR_INITIALIZER |
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.
I didn't understand the purpose of the ALLOW_IN_CONSTRUCTOR_INITIALIZER
pragma. Why not just update the test code?
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.
The purpose is to keep expectations, we are planning to allow declarations in these contexts in the future.
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.
Makes sense. Thanks
LGTM but I would not introduce #if blocks. |
Ping. @dotnet/roslyn-compiler, @gafter Please review. |
@@ -4947,4 +4947,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ | |||
<data name="ERR_OutVarDeconstructionIsNotSupported" xml:space="preserve"> | |||
<value>Deconstruction is not supported for an 'out' argument.</value> | |||
</data> | |||
<data name="ERR_ExpressionVariableInConstructorOrFieldInitializer" xml:space="preserve"> | |||
<value>Out variable or pattern variable declarations are not allowed within constructor/field/auto-implemented property initializers.</value> |
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.
Consider separating cases with comma for consistency with other errors. And remove auto-implemented
if not necessary. Perhaps: ... not allowed within constructors, field initializers, or property initializers
.
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.
Also, I believe the first "or" should be "and" (they are both forbidden), so I suggest
Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers.
This is long-winded enough that you could consider specializing it.
In reply to: 77212251 [](ancestors = 77212251)
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.
Opened #13528.
Other than a slight wording change on the diagnostic, LGTM |
Related to #12597.
@dotnet/roslyn-compiler, @gafter Please review.