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

Do not suggest using implicit type for stackalloc initialization #22779

Merged
merged 3 commits into from
Nov 3, 2017
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 @@ -1749,5 +1749,216 @@ static void M()
}",
options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType()
{
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
{
unsafe public Span(void* pointer, int length) { }
}
}
class C
{
static void M()
{
[|Span<int>|] x = stackalloc int [10];
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType_NestedConditional()
{
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
{
unsafe public Span(void* pointer, int length) { }
}
}
class C
{
static void M(bool choice)
{
[|Span<int>|] x = choice ? stackalloc int [10] : stackalloc int [100];
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task DoNotSuggestVarOnStackAllocExpressions_SpanType_NestedCast()
{
await TestMissingInRegularAndScriptAsync(@"
using System;
namespace System
{
public readonly ref struct Span<T>
{
unsafe public Span(void* pointer, int length) { }
}
}
class C
{
static void M()
{
[|Span<int>|] x = (Span<int>)stackalloc int [100];
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnLambdasWithNestedStackAllocs()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
[|int|] x = new int[] { 1, 2, 3 }.First(i =>
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(i =>
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnAnonymousMethodsWithNestedStackAllocs()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
[|int|] x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
int* y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnStackAllocsNestedInLambdas()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(i =>
{
[|int*|] y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(i =>
{
var y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnStackAllocsNestedInAnonymousMethods()
{
await TestInRegularAndScriptAsync(@"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
[|int*|] y = stackalloc int[10];
return i == 1;
});
}
}", @"
using System.Linq;
class C
{
unsafe static void M()
{
var x = new int[] { 1, 2, 3 }.First(delegate (int i)
{
var y = stackalloc int[10];
return i == 1;
});
}
}", options: ImplicitTypeEverywhere());
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(22768, "https://github.com/dotnet/roslyn/issues/22768")]
public async Task SuggestVarOnStackAllocsInOuterMethodScope()
{
await TestInRegularAndScriptAsync(@"
class C
{
unsafe static void M()
{
[|int*|] x = stackalloc int [10];
Copy link
Member

@sharwell sharwell Oct 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ For the ImplicitTypeEverywhere() options, this should report a diagnostic. Span<T> is not a special case, the same way string interpolation with IFormattable is not a special case.

}
}", @"
class C
{
unsafe static void M()
{
var x = stackalloc int [10];
}
}", options: ImplicitTypeEverywhere());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,25 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
}

var variable = variableDeclaration.Variables.Single();
var initializer = variable.Initializer.Value;

// Do not suggest var replacement for stackalloc span expressions.
// This will change the bound type from a span to a pointer.
if (!variableDeclaration.Type.IsKind(SyntaxKind.PointerType))
{
var containsStackAlloc = initializer
.DescendantNodesAndSelf(descendIntoChildren: node => !node.IsAnyLambdaOrAnonymousMethod())
.Any(node => node.IsKind(SyntaxKind.StackAllocArrayCreationExpression));

if (containsStackAlloc)
{
issueSpan = default;
return false;
}
}

if (AssignmentSupportsStylePreference(
variable.Identifier, typeName, variable.Initializer.Value,
variable.Identifier, typeName, initializer,
semanticModel, optionSet, cancellationToken))
{
issueSpan = candidateIssueSpan;
Expand Down