未验证 提交 4f600e6a 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #47844 from genlu/GenerateName

Allow genearted local function parameter name to shadow variables
...@@ -280,11 +280,15 @@ public class C ...@@ -280,11 +280,15 @@ public class C
await VerifyItemExistsAsync(markup, "cancellationToken1", glyph: (int)Glyph.Parameter); await VerifyItemExistsAsync(markup, "cancellationToken1", glyph: (int)Glyph.Parameter);
} }
[Fact, Trait(Traits.Feature, Traits.Features.Completion)] [InlineData(LanguageVersion.CSharp7)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.Latest)]
[Theory, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(42049, "https://github.com/dotnet/roslyn/issues/42049")]
[WorkItem(45492, "https://github.com/dotnet/roslyn/issues/45492")] [WorkItem(45492, "https://github.com/dotnet/roslyn/issues/45492")]
public async Task Parameter10() public async Task Parameter10(LanguageVersion languageVersion)
{ {
var markup = @" var source = @"
public class DbContext { } public class DbContext { }
public class C public class C
{ {
...@@ -293,16 +297,29 @@ public class C ...@@ -293,16 +297,29 @@ public class C
} }
} }
"; ";
var markup = GetMarkup(source, languageVersion);
await VerifyItemExistsAsync(markup, "dbContext", glyph: (int)Glyph.Parameter); await VerifyItemExistsAsync(markup, "dbContext", glyph: (int)Glyph.Parameter);
await VerifyItemExistsAsync(markup, "db", glyph: (int)Glyph.Parameter); await VerifyItemExistsAsync(markup, "db", glyph: (int)Glyph.Parameter);
await VerifyItemExistsAsync(markup, "context1", glyph: (int)Glyph.Parameter);
if (languageVersion.MapSpecifiedToEffectiveVersion() >= LanguageVersion.CSharp8)
{
await VerifyItemExistsAsync(markup, "context", glyph: (int)Glyph.Parameter);
}
else
{
await VerifyItemExistsAsync(markup, "context1", glyph: (int)Glyph.Parameter);
}
} }
[Fact, Trait(Traits.Feature, Traits.Features.Completion)] [InlineData(LanguageVersion.CSharp7)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.Latest)]
[Theory, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(42049, "https://github.com/dotnet/roslyn/issues/42049")]
[WorkItem(45492, "https://github.com/dotnet/roslyn/issues/45492")] [WorkItem(45492, "https://github.com/dotnet/roslyn/issues/45492")]
public async Task Parameter11() public async Task Parameter11(LanguageVersion languageVersion)
{ {
var markup = @" var source = @"
public class DbContext { } public class DbContext { }
public class C public class C
{ {
...@@ -312,16 +329,29 @@ public class C ...@@ -312,16 +329,29 @@ public class C
} }
} }
"; ";
var markup = GetMarkup(source, languageVersion);
await VerifyItemExistsAsync(markup, "dbContext", glyph: (int)Glyph.Parameter); await VerifyItemExistsAsync(markup, "dbContext", glyph: (int)Glyph.Parameter);
await VerifyItemExistsAsync(markup, "db", glyph: (int)Glyph.Parameter); await VerifyItemExistsAsync(markup, "db", glyph: (int)Glyph.Parameter);
await VerifyItemExistsAsync(markup, "context1", glyph: (int)Glyph.Parameter);
if (languageVersion.MapSpecifiedToEffectiveVersion() >= LanguageVersion.CSharp8)
{
await VerifyItemExistsAsync(markup, "context", glyph: (int)Glyph.Parameter);
}
else
{
await VerifyItemExistsAsync(markup, "context1", glyph: (int)Glyph.Parameter);
}
} }
[Fact, Trait(Traits.Feature, Traits.Features.Completion)] [InlineData(LanguageVersion.CSharp7)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.Latest)]
[Theory, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(42049, "https://github.com/dotnet/roslyn/issues/42049")]
[WorkItem(45492, "https://github.com/dotnet/roslyn/issues/45492")] [WorkItem(45492, "https://github.com/dotnet/roslyn/issues/45492")]
public async Task Parameter12() public async Task Parameter12(LanguageVersion languageVersion)
{ {
var markup = @" var source = @"
public class DbContext { } public class DbContext { }
public class C public class C
{ {
...@@ -331,9 +361,18 @@ public class C ...@@ -331,9 +361,18 @@ public class C
} }
} }
"; ";
var markup = GetMarkup(source, languageVersion);
await VerifyItemExistsAsync(markup, "dbContext", glyph: (int)Glyph.Parameter); await VerifyItemExistsAsync(markup, "dbContext", glyph: (int)Glyph.Parameter);
await VerifyItemExistsAsync(markup, "db", glyph: (int)Glyph.Parameter); await VerifyItemExistsAsync(markup, "db", glyph: (int)Glyph.Parameter);
await VerifyItemExistsAsync(markup, "context1", glyph: (int)Glyph.Parameter);
if (languageVersion.MapSpecifiedToEffectiveVersion() >= LanguageVersion.CSharp8)
{
await VerifyItemExistsAsync(markup, "context", glyph: (int)Glyph.Parameter);
}
else
{
await VerifyItemExistsAsync(markup, "context1", glyph: (int)Glyph.Parameter);
}
} }
[WorkItem(19260, "https://github.com/dotnet/roslyn/issues/19260")] [WorkItem(19260, "https://github.com/dotnet/roslyn/issues/19260")]
...@@ -1828,11 +1867,15 @@ void M2() ...@@ -1828,11 +1867,15 @@ void M2()
expectedDescriptionOrNull: CSharpFeaturesResources.Suggested_name); expectedDescriptionOrNull: CSharpFeaturesResources.Suggested_name);
} }
[InlineData(LanguageVersion.CSharp7)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.Latest)]
[Theory, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(35891, "https://github.com/dotnet/roslyn/issues/35891")] [WorkItem(35891, "https://github.com/dotnet/roslyn/issues/35891")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)] [WorkItem(42049, "https://github.com/dotnet/roslyn/issues/42049")]
public async Task TestCompletionDoesNotUseLocalAsLocalFunctionParameter() public async Task TestUseLocalAsLocalFunctionParameter(LanguageVersion languageVersion)
{ {
var markup = @" var source = @"
class ClassA class ClassA
{ {
class ClassB { } class ClassB { }
...@@ -1843,14 +1886,28 @@ void M() ...@@ -1843,14 +1886,28 @@ void M()
} }
} }
"; ";
await VerifyItemIsAbsentAsync(markup, "classB"); var markup = GetMarkup(source, languageVersion);
if (languageVersion.MapSpecifiedToEffectiveVersion() >= LanguageVersion.CSharp8)
{
await VerifyItemExistsAsync(markup, "classB", glyph: (int)Glyph.Parameter,
expectedDescriptionOrNull: CSharpFeaturesResources.Suggested_name);
}
else
{
await VerifyItemIsAbsentAsync(markup, "classB");
}
} }
[InlineData(LanguageVersion.CSharp7)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.Latest)]
[Theory, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(35891, "https://github.com/dotnet/roslyn/issues/35891")] [WorkItem(35891, "https://github.com/dotnet/roslyn/issues/35891")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)] [WorkItem(42049, "https://github.com/dotnet/roslyn/issues/42049")]
public async Task TestCompletionDoesNotUseLocalAsLocalFunctionVariable() public async Task TestCompletionDoesNotUseLocalAsLocalFunctionVariable(LanguageVersion languageVersion)
{ {
var markup = @" var source = @"
class ClassA class ClassA
{ {
class ClassB { } class ClassB { }
...@@ -1864,6 +1921,7 @@ void LocalM1() ...@@ -1864,6 +1921,7 @@ void LocalM1()
} }
} }
"; ";
var markup = GetMarkup(source, languageVersion);
await VerifyItemIsAbsentAsync(markup, "classB"); await VerifyItemIsAbsentAsync(markup, "classB");
} }
...@@ -2103,5 +2161,14 @@ private static SerializableNamingRule CreateRule(SymbolSpecification specificati ...@@ -2103,5 +2161,14 @@ private static SerializableNamingRule CreateRule(SymbolSpecification specificati
EnforcementLevel = ReportDiagnostic.Error EnforcementLevel = ReportDiagnostic.Error
}; };
} }
private static string GetMarkup(string source, LanguageVersion languageVersion)
=> $@"<Workspace>
<Project Language=""C#"" AssemblyName=""Assembly1"" CommonReferences=""true"" LanguageVersion=""{languageVersion.ToDisplayString()}"">
<Document FilePath=""Test2.cs"">
{source}
</Document>
</Project>
</Workspace>";
} }
} }
...@@ -34,6 +34,15 @@ protected override IEnumerable<ISymbol> GetCollidableSymbols(SemanticModel seman ...@@ -34,6 +34,15 @@ protected override IEnumerable<ISymbol> GetCollidableSymbols(SemanticModel seman
// Get all the symbols visible to the current location. // Get all the symbols visible to the current location.
var visibleSymbols = semanticModel.LookupSymbols(location.SpanStart); var visibleSymbols = semanticModel.LookupSymbols(location.SpanStart);
// Local function parameter is allowed to shadow variables since C# 8.
if (((CSharpCompilation)semanticModel.Compilation).LanguageVersion.MapSpecifiedToEffectiveVersion() >= LanguageVersion.CSharp8)
{
if (SyntaxFacts.IsParameterList(container) && SyntaxFacts.IsLocalFunctionStatement(container.Parent))
{
visibleSymbols = visibleSymbols.WhereAsArray(s => !s.MatchesKind(SymbolKind.Local, SymbolKind.Parameter));
}
}
// Some symbols in the enclosing block could cause conflicts even if they are not available at the location. // Some symbols in the enclosing block could cause conflicts even if they are not available at the location.
// E.g. symbols inside if statements / try catch statements. // E.g. symbols inside if statements / try catch statements.
var symbolsInBlock = semanticModel.GetExistingSymbols(container, cancellationToken, var symbolsInBlock = semanticModel.GetExistingSymbols(container, cancellationToken,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册