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
Fix generate method type parameter nullability #39079
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -306,7 +306,18 @@ public override TypeSyntax VisitPointerType(IPointerTypeSymbol symbol) | |
|
||
public override TypeSyntax VisitTypeParameter(ITypeParameterSymbol symbol) | ||
{ | ||
return AddInformationTo(symbol.Name.ToIdentifierName(), symbol); | ||
TypeSyntax typeSyntax = AddInformationTo(symbol.Name.ToIdentifierName(), symbol); | ||
|
||
// Add a ? if the type parameter is annotated as nullable, and the constraints allow it. It's possible to end up with | ||
// a T? where T is constrained to class? if we combined a type parameter with some flow state -- we'll ignore the ? here | ||
// at this stage. We should consider moving that check earlier when we actually wrap the symbol. | ||
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. I recognize this check isn't right -- I'm just getting things off the ground to see what else might be breaking here. 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. Can you elaborate when this would be incorrect? Is it the case where flow state shows that we don't need to mark as nullable only because null will never be passed in? 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. The check for "HasReferenceTypeConstraint" is probably insufficient, because you can be constrainted to reference types if you have something like "T : SomeClass". And there's probably other cases too. |
||
if (symbol.GetNullability() == NullableAnnotation.Annotated && | ||
!(symbol.HasReferenceTypeConstraint && symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated)) | ||
{ | ||
typeSyntax = AddInformationTo(SyntaxFactory.NullableType(typeSyntax), symbol); | ||
} | ||
|
||
return typeSyntax; | ||
} | ||
} | ||
} | ||
|
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.
Can we make sure to test nullable constraint with not-null flow analysis?
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.
Isn't that the TestInvocationWithNullableClassConstrainedTypeArgumentAndNullableTypes test below?
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.
oof, mondays...