Skip to content

Commit

Permalink
Generate class? if the constraint has a type annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmalinowski committed Oct 4, 2019
1 parent aa7faa1 commit 21584df
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Expand Up @@ -4686,6 +4686,7 @@ internal static void Test(global::Outer.Inner inner)

[Theory]
[InlineData("class")]
[InlineData("class?")]
[InlineData("struct")]
[InlineData("new()")]
[InlineData("unmanaged")]
Expand Down
Expand Up @@ -42,7 +42,11 @@ internal static class ITypeParameterSymbolExtensions

if (typeParameter.HasReferenceTypeConstraint)
{
constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.ClassConstraint));
var questionMarkToken = typeParameter.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated
? SyntaxFactory.Token(SyntaxKind.QuestionToken)
: default;

constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.ClassConstraint, SyntaxFactory.Token(SyntaxKind.ClassKeyword), questionMarkToken));
}
else if (typeParameter.HasUnmanagedTypeConstraint)
{
Expand Down
Expand Up @@ -308,7 +308,11 @@ public override TypeSyntax VisitTypeParameter(ITypeParameterSymbol symbol)
{
TypeSyntax typeSyntax = AddInformationTo(symbol.Name.ToIdentifierName(), symbol);

if (symbol.GetNullability() == NullableAnnotation.Annotated)
// 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.
if (symbol.GetNullability() == NullableAnnotation.Annotated &&
!(symbol.HasReferenceTypeConstraint && symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated))
{
typeSyntax = AddInformationTo(SyntaxFactory.NullableType(typeSyntax), symbol);
}
Expand Down

0 comments on commit 21584df

Please sign in to comment.