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

Create an unsafe method from a local function when necessary #49389

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 @@ -821,6 +821,102 @@ C LocalFunction(C c)
return null;
}
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertLocalFunctionToMethod)]
[WorkItem(32976, "https://github.com/dotnet/roslyn/issues/32976")]
public async Task TestUnsafeLocalFunction()
{
await TestInRegularAndScriptAsync(
@"class C
{
public unsafe void UnsafeFunction()
{
byte b = 1;
[|unsafe byte* GetPtr(byte* bytePt)
{
return bytePt;
}|]
var aReference = GetPtr(&b);
}
}",
@"class C
{
public unsafe void UnsafeFunction()
{
byte b = 1;
var aReference = GetPtr(&b);
}

private static unsafe byte* GetPtr(byte* bytePt)
{
return bytePt;
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertLocalFunctionToMethod)]
[WorkItem(32976, "https://github.com/dotnet/roslyn/issues/32976")]
public async Task TestUnsafeLocalFunctionInUnsafeMethod()
{
await TestInRegularAndScriptAsync(
@"class C
{
public unsafe void UnsafeFunction()
{
byte b = 1;
[|byte* GetPtr(byte* bytePt)
{
return bytePt;
}|]
var aReference = GetPtr(&b);
}
}",
@"class C
{
public unsafe void UnsafeFunction()
{
byte b = 1;
var aReference = GetPtr(&b);
}

private static unsafe byte* GetPtr(byte* bytePt)
{
return bytePt;
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertLocalFunctionToMethod)]
[WorkItem(32976, "https://github.com/dotnet/roslyn/issues/32976")]
public async Task TestLocalFunctionInUnsafeMethod()
{
await TestInRegularAndScriptAsync(
@"class C
{
public unsafe void UnsafeFunction()
{
byte b = 1;
[|byte GetPtr(byte bytePt)
{
return bytePt;
}|]
var aReference = GetPtr(b);
}
}",
@"class C
{
public unsafe void UnsafeFunction()
{
byte b = 1;
var aReference = GetPtr(b);
}

private static byte GetPtr(byte bytePt)
{
return bytePt;
}
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,18 @@ private static async Task<Document> UpdateDocumentAsync(
var containerSymbol = semanticModel.GetDeclaredSymbol(container, cancellationToken);
var isStatic = containerSymbol.IsStatic || captures.All(capture => !capture.IsThisParameter());

// GetSymbolModifiers actually checks if the local function needs to be unsafe, not whether
// it is declared as such, so this check we don't need to worry about whether the containing method
// is unsafe, this will just work regardless.
var needsUnsafe = declaredSymbol.GetSymbolModifiers().IsUnsafe;

var methodName = GenerateUniqueMethodName(declaredSymbol);
var parameters = declaredSymbol.Parameters;
var methodSymbol = CodeGenerationSymbolFactory.CreateMethodSymbol(
containingType: declaredSymbol.ContainingType,
attributes: default,
accessibility: Accessibility.Private,
modifiers: new DeclarationModifiers(isStatic, isAsync: declaredSymbol.IsAsync),
modifiers: new DeclarationModifiers(isStatic, isAsync: declaredSymbol.IsAsync, isUnsafe: needsUnsafe),
returnType: declaredSymbol.ReturnType,
refKind: default,
explicitInterfaceImplementations: default,
Expand Down