Skip to content

Commit 54f4e4a

Browse files
Add nullability support to use local function
Fixes #30322
1 parent 9cdc1fb commit 54f4e4a

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/EditorFeatures/CSharpTest/UseLocalFunction/UseLocalFunctionTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using Microsoft.CodeAnalysis.CodeFixes;
55
using Microsoft.CodeAnalysis.CSharp;
6+
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
67
using Microsoft.CodeAnalysis.CSharp.UseLocalFunction;
78
using Microsoft.CodeAnalysis.Diagnostics;
89
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
@@ -3660,5 +3661,33 @@ void M()
36603661
}
36613662
}", parseOptions: CSharp8ParseOptions);
36623663
}
3664+
3665+
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
3666+
public async Task TestWithNullableParameterAndReturn()
3667+
{
3668+
await TestInRegularAndScriptAsync(
3669+
@"#nullable enable
3670+
3671+
using System;
3672+
3673+
class Program
3674+
{
3675+
static void Main(string[] args)
3676+
{
3677+
Func<string?, string?> [||]f = s => s;
3678+
}
3679+
}",
3680+
@"#nullable enable
3681+
3682+
using System;
3683+
3684+
class Program
3685+
{
3686+
static void Main(string[] args)
3687+
{
3688+
static string? f(string? s) => s;
3689+
}
3690+
}", parseOptions: TestOptions.Regular8WithNullableAnalysis);
3691+
}
36633692
}
36643693
}

src/Features/CSharp/Portable/UseLocalFunction/CSharpUseLocalFunctionCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ ParameterSyntax PromoteParameter(ParameterSyntax parameterNode, IParameterSymbol
245245

246246
if (parameterNode.Type == null)
247247
{
248-
parameterNode = parameterNode.WithType(delegateParameter?.Type.GenerateTypeSyntax() ?? s_objectType);
248+
parameterNode = parameterNode.WithType(delegateParameter?.Type.WithNullability(delegateParameter.NullableAnnotation).GenerateTypeSyntax() ?? s_objectType);
249249
}
250250

251251
if (delegateParameter?.HasExplicitDefaultValue == true)

src/Workspaces/CSharp/Portable/Extensions/TypeSyntaxExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,19 @@ public static bool IsTypeInferred(this TypeSyntax typeSyntax, SemanticModel sema
9393

9494
public static TypeSyntax GenerateReturnTypeSyntax(this IMethodSymbol method)
9595
{
96+
var returnType = method.ReturnType.WithNullability(method.ReturnNullableAnnotation);
97+
9698
if (method.ReturnsByRef)
9799
{
98-
return method.ReturnType.GenerateRefTypeSyntax();
100+
return returnType.GenerateRefTypeSyntax();
99101
}
100102
else if (method.ReturnsByRefReadonly)
101103
{
102-
return method.ReturnType.GenerateRefReadOnlyTypeSyntax();
104+
return returnType.GenerateRefReadOnlyTypeSyntax();
103105
}
104106
else
105107
{
106-
return method.ReturnType.GenerateTypeSyntax();
108+
return returnType.GenerateTypeSyntax();
107109
}
108110
}
109111

src/Workspaces/Core/Portable/CodeGeneration/Symbols/CodeGenerationAbstractMethodSymbol.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ protected CodeGenerationAbstractMethodSymbol(
4848
public abstract IMethodSymbol PartialDefinitionPart { get; }
4949
public abstract IMethodSymbol PartialImplementationPart { get; }
5050

51-
public NullableAnnotation ReceiverNullableAnnotation => throw new NotImplementedException();
52-
public NullableAnnotation ReturnNullableAnnotation => throw new NotImplementedException();
53-
public ImmutableArray<NullableAnnotation> TypeArgumentsNullableAnnotations => throw new NotImplementedException();
51+
public NullableAnnotation ReceiverNullableAnnotation => ReceiverType.GetNullability();
52+
public NullableAnnotation ReturnNullableAnnotation => ReturnType.GetNullability();
53+
public ImmutableArray<NullableAnnotation> TypeArgumentsNullableAnnotations => TypeArguments.SelectAsArray(a => a.GetNullability());
5454

5555
public virtual ITypeSymbol ReceiverType
5656
{

0 commit comments

Comments
 (0)