提交 54f4e4a2 编写于 作者: J Jason Malinowski

Add nullability support to use local function

Fixes https://github.com/dotnet/roslyn/issues/30322
上级 9cdc1fb5
......@@ -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;
......@@ -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);
}
}
}
......@@ -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)
......
......@@ -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();
}
}
......
......@@ -48,9 +48,9 @@ public virtual ImmutableArray<AttributeData> GetReturnTypeAttributes()
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
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册