Skip to content

Commit

Permalink
DRAFT prototype for 16195
Browse files Browse the repository at this point in the history
Possible approach for fixing dotnet#16195
  • Loading branch information
gafter committed Jan 5, 2017
1 parent da840c5 commit 8a7cd48
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/ExpressionVariableFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal abstract class ExpressionVariableFinder<TFieldOrLocalSymbol> : CSharpSy
case SyntaxKind.IfStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.VariableDeclarator:
case SyntaxKind.LocalFunctionStatement:
break;
case SyntaxKind.ArgumentList:
Debug.Assert(node.Parent is ConstructorInitializerSyntax);
Expand Down Expand Up @@ -179,6 +180,18 @@ public override void VisitSwitchStatement(SwitchStatementSyntax node)
VisitNodeToBind(node.Expression);
}

public override void VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
{
// Expression variables may not be declared in a local function's
// parameter initializer, because the initializer is required to be a kind
// of constant. Although it is an error, we must handle such code.
// We give such variables the scope of the enclosing block.
foreach (var parameter in node.ParameterList.Parameters)
{
VisitNodeToBind(parameter.Default?.Value);
}
}

public override void VisitDeclarationPattern(DeclarationPatternSyntax node)
{
var variable = MakePatternVariable(node, _nodeToBind);
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Binder/LocalScopeBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ protected ImmutableArray<LocalSymbol> BuildLocals(SyntaxList<StatementSyntax> st
}
break;

case SyntaxKind.LocalFunctionStatement:
case SyntaxKind.ExpressionStatement:
case SyntaxKind.IfStatement:
case SyntaxKind.YieldReturnStatement:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="FlowAnalysis\RegionAnalysisTests.cs" />
<Compile Include="FlowAnalysis\StructTests.cs" />
<Compile Include="FlowAnalysis\TryLockUsingStatementTests.cs" />
<Compile Include="Semantics\FuzzTests.cs" />
<Compile Include="Semantics\PatternMatchingTests_Global.cs" />
<Compile Include="Semantics\BindingAsyncTasklikeMoreTests.cs" />
<Compile Include="Semantics\BindingAsyncTasklikeTests.cs" />
Expand Down
77 changes: 77 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/FuzzTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics
{
public class FuzzTests : CompilingTestBase
{
[Fact, WorkItem(16167, "https://github.com/dotnet/roslyn/issues/16167")]
public void CompileXmlAsSource()
{
var text = @"
using System;
using System.Collections.Generic;
using System.Linq;
class C
{
public static void Main()
{
<summary>Copies a range of elements from an <see cref=""T:System.Array"" /> starting at the first element and pastes them into another <see cref=""T:System.Array"" /> starting at the first element. The length is specified as a 32-bit integer.</summary>
<param name=""sourceArray"">The <see cref=""T:System.Array"" /> that contains the data to copy.</param>
<param name=""destinationArray"">The <see cref=""T:System.Array"" /> that receives the data.</param>
<param name=""length"">A 32-bit integer that represents the number of elements to copy.</param>
<exception cref=""T:System.ArgumentNullException"">
<paramref name=""sourceArray"" /> is null.-or-<paramref name=""destinationArray"" /> is null.</exception>
<exception cref=""T:System.RankException"">
<paramref name=""sourceArray"" /> and <paramref name=""destinationArray"" /> have different ranks.</exception>
<exception cref=""T:System.ArrayTypeMismatchException"">
<paramref name=""sourceArray"" /> and <paramref name=""destinationArray"" /> are of incompatible types.</exception>
<exception cref=""T:System.InvalidCastException"">At least one element in <paramref name=""sourceArray"" /> cannot be cast to the type of <paramref name=""destinationArray"" />.</exception>
<exception cref=""T:System.ArgumentOutOfRangeException"">
<paramref name=""length"" /> is less than zero.</exception>
<exception cref=""T:System.ArgumentException"">
<paramref name=""length"" /> is greater than the number of elements in <paramref name=""sourceArray"" />.-or-<paramref name=""length"" /> is greater than the number of elements in <paramref name=""destinationArray"" />.</exception>
}
}
";
var compilation = CreateCompilationWithMscorlib45(text);
var tree = compilation.SyntaxTrees[0];
var model = compilation.GetSemanticModel(tree);
foreach (var node in tree.GetRoot().DescendantNodes())
{
var _ = model.GetSymbolInfo(node);
}
}

[Fact, WorkItem(16167, "https://github.com/dotnet/roslyn/issues/16167")]
public void Bug16167Root()
{
var text = @"
using System;
using System.Collections.Generic;
using System.Linq;
class C
{
public static void Main(string[] args)
{
void Local1(bool b = args is string[] z1) {}
void Local2(bool b = M(out int z2)) {}
void Local3((int, int) ii = ((int a1, int a2) = (1, 2)) {}
}
}
";
var compilation = CreateCompilationWithMscorlib45(text);
var tree = compilation.SyntaxTrees[0];
var model = compilation.GetSemanticModel(tree);
foreach (var node in tree.GetRoot().DescendantNodes())
{
var _ = model.GetSymbolInfo(node);
}
}
}
}

0 comments on commit 8a7cd48

Please sign in to comment.