-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Report nullable ctor warnings for struct primary constructors #74844
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1735,12 +1735,7 @@ private static void GetStateMachineSlotDebugInfo( | |
|
||
initializersBody ??= GetSynthesizedEmptyBody(method); | ||
|
||
if (method is SynthesizedPrimaryConstructor primaryCtor && method.ContainingType.IsStructType()) | ||
{ | ||
body = BoundBlock.SynthesizedNoLocals(primaryCtor.GetSyntax()); | ||
nullableInitialState = getInitializerState(body); | ||
} | ||
else if (method is SourceMemberMethodSymbol sourceMethod) | ||
Comment on lines
-1738
to
-1743
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This path was causing us to miss nullable analyzing the struct primary constructor. |
||
if (method is SourceMemberMethodSymbol sourceMethod) | ||
{ | ||
CSharpSyntaxNode syntaxNode = sourceMethod.SyntaxNode; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -639,8 +639,7 @@ void enforceMemberNotNull(SyntaxNode? syntaxOpt, LocalState state) | |
Debug.Assert(thisParameter is object); | ||
thisSlot = GetOrCreateSlot(thisParameter); | ||
} | ||
// https://github.com/dotnet/roslyn/issues/46718: give diagnostics on return points, not constructor signature | ||
var exitLocation = method.DeclaringSyntaxReferences.IsEmpty ? null : method.TryGetFirstLocation(); | ||
var exitLocation = method is SynthesizedPrimaryConstructor || method.DeclaringSyntaxReferences.IsEmpty ? null : method.TryGetFirstLocation(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a type has a primary constructor, all other instance constructors will be required to chain to it (directly or indirectly), so it will be the only constructor which will get warnings for uninitialized non-nullable instance fields. That means the field/property declaration is really the best place to report diagnostics in such cases. |
||
bool constructorEnforcesRequiredMembers = method.ShouldCheckRequiredMembers(); | ||
|
||
// Required properties can be attributed MemberNotNull, indicating that if the property is set, the field will be set as well. | ||
|
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.
What replicates this logic now? What body is created for the primary constructor?
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 same logic which was used for class primary constructors is now also used for struct primary constructors. You can see that where the assertions were adjusted.
I reviewed the code around this area and didn't spot anything which would be problematic for structs, but, it's quite possible I missed something.