Skip to content

Commit

Permalink
Add nullability support to use local function
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmalinowski committed Jun 14, 2019
1 parent 9cdc1fb commit 54f4e4a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.CSharp.UseLocalFunction;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
Expand Down Expand Up @@ -3660,5 +3661,33 @@ void M()
}
}", parseOptions: CSharp8ParseOptions);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestWithNullableParameterAndReturn()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
using System;
class Program
{
static void Main(string[] args)
{
Func<string?, string?> [||]f = s => s;
}
}",
@"#nullable enable
using System;
class Program
{
static void Main(string[] args)
{
static string? f(string? s) => s;
}
}", parseOptions: TestOptions.Regular8WithNullableAnalysis);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ ParameterSyntax PromoteParameter(ParameterSyntax parameterNode, IParameterSymbol

if (parameterNode.Type == null)
{
parameterNode = parameterNode.WithType(delegateParameter?.Type.GenerateTypeSyntax() ?? s_objectType);
parameterNode = parameterNode.WithType(delegateParameter?.Type.WithNullability(delegateParameter.NullableAnnotation).GenerateTypeSyntax() ?? s_objectType);
}

if (delegateParameter?.HasExplicitDefaultValue == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,19 @@ public static bool IsTypeInferred(this TypeSyntax typeSyntax, SemanticModel sema

public static TypeSyntax GenerateReturnTypeSyntax(this IMethodSymbol method)
{
var returnType = method.ReturnType.WithNullability(method.ReturnNullableAnnotation);

if (method.ReturnsByRef)
{
return method.ReturnType.GenerateRefTypeSyntax();
return returnType.GenerateRefTypeSyntax();
}
else if (method.ReturnsByRefReadonly)
{
return method.ReturnType.GenerateRefReadOnlyTypeSyntax();
return returnType.GenerateRefReadOnlyTypeSyntax();
}
else
{
return method.ReturnType.GenerateTypeSyntax();
return returnType.GenerateTypeSyntax();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ protected CodeGenerationAbstractMethodSymbol(
public abstract IMethodSymbol PartialDefinitionPart { get; }
public abstract IMethodSymbol PartialImplementationPart { get; }

public NullableAnnotation ReceiverNullableAnnotation => throw new NotImplementedException();
public NullableAnnotation ReturnNullableAnnotation => throw new NotImplementedException();
public ImmutableArray<NullableAnnotation> TypeArgumentsNullableAnnotations => throw new NotImplementedException();
public NullableAnnotation ReceiverNullableAnnotation => ReceiverType.GetNullability();
public NullableAnnotation ReturnNullableAnnotation => ReturnType.GetNullability();
public ImmutableArray<NullableAnnotation> TypeArgumentsNullableAnnotations => TypeArguments.SelectAsArray(a => a.GetNullability());

public virtual ITypeSymbol ReceiverType
{
Expand Down

0 comments on commit 54f4e4a

Please sign in to comment.