-
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
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
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.
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. |
@@ -971,7 +971,7 @@ public override object VisitField(FieldSymbol symbol, TypeCompilationState argum | |||
} | |||
|
|||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -971,7 +971,7 @@ public override object VisitField(FieldSymbol symbol, TypeCompilationState argum | |||
} | |||
|
|||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(); |
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.
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.
Removed, thanks.
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.
LGTM (commit 3), assuming CI is passing.
@dotnet/roslyn-compiler, please review, thanks. |
3611015
to
7782494
Compare
fd7ea66
to
3363d91
Compare
[Fact] | ||
public void WellKnownTypeAsStruct_DefaultConstructor_IsReadOnlyAttribute_02() | ||
{ | ||
var sourceAttribute = | ||
@"#pragma warning disable 414 | ||
namespace System.Runtime.CompilerServices | ||
{ | ||
public struct IsReadOnlyAttribute | ||
{ | ||
private int F = 1; // requires synthesized parameterless .ctor |
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.
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.
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