Skip to content
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

Add nullability support to use local function #36458

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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