Do not synthesize a parameterless constructor for struct types; report error if struct has field initializers but no constructors#58581
Conversation
…t error if struct has field initializers but no constructors
3d1c93d to
ed9f37c
Compare
Youssef1313
left a comment
There was a problem hiding this comment.
Should this be documented as a breaking change in https://github.com/dotnet/roslyn/blob/main/docs/compilers/CSharp/Compiler%20Breaking%20Changes%20-%20DotNet%207.md?
|
@dotnet/roslyn-compiler, please review, thanks. |
|
|
||
| // no need to emit the default ctor, we are not emitting those | ||
| if (methodSymbol.IsDefaultValueTypeConstructor(requireZeroInit: true)) | ||
| if (methodSymbol.IsDefaultValueTypeConstructor(requireZeroInit: false)) |
|
|
||
| // no need to emit the default ctor, we are not emitting those | ||
| if (methodSymbol.IsDefaultValueTypeConstructor(requireZeroInit: true)) | ||
| if (methodSymbol.IsDefaultValueTypeConstructor(requireZeroInit: false)) |
There was a problem hiding this comment.
The logic here looks confusing.
I agree, it is confusing. We're ignoring implicitly-declared parameterless constructors only though, not explicitly-declared constructors.
There was a problem hiding this comment.
We're ignoring implicitly-declared parameterless constructors only though, not explicitly-declared constructors.
In that case the parameter must be true.
There was a problem hiding this comment.
It looks like either the parameter is no longer meaningful, or the logic here is incorrect.
…aultValueTypeConstructor()
| // If there are field initializers and an explicit constructor with parameters | ||
| // (that is, more than one constructor), the implicit parameterless constructor | ||
| // is treated as the zero-init constructor and does not execute field initializers. | ||
| return constructors.Length > 1 || !containingType.HasFieldInitializers(); |
AlekseyTs
left a comment
There was a problem hiding this comment.
LGTM (commit 3), assuming CI is passing.
|
@dotnet/roslyn-compiler, please review, thanks. |
3611015 to
7782494
Compare
fd7ea66 to
3363d91
Compare
| { | ||
| public struct IsReadOnlyAttribute | ||
| { | ||
| private int F = 1; // requires synthesized parameterless .ctor |
333fred
left a comment
There was a problem hiding this comment.
LGTM (commit 6), other than the comment that should be updated.
Update volatile process program code to avoid CS8983 (A 'struct' with field initializers must include an explicitly declared constructor) After the changes in C# discussed at dotnet/csharplang#5552 For the upstream changes, see dotnet/roslyn#58581 For discussion of the change, also see dotnet/sdk#23971
See dotnet/csharplang#5552