Skip to content

Commit

Permalink
Restore flow graph produced for existing switch statements (#31129)
Browse files Browse the repository at this point in the history
* Restore flow graph produced for existing switch statements
Fixes #28634
* Prevent type and constant value on a constant pattern
See also #31150
* Skip `AddResourceToModule` on Linux
Issue reported as #31279
  • Loading branch information
Neal Gafter authored Nov 28, 2018
1 parent b4b406b commit b90b792
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1964,10 +1964,8 @@ private IConstantPatternOperation CreateBoundConstantPatternOperation(BoundConst
{
Lazy<IOperation> value = new Lazy<IOperation>(() => Create(boundConstantPattern.Value));
SyntaxNode syntax = boundConstantPattern.Syntax;
ITypeSymbol type = null;
Optional<object> constantValue = default(Optional<object>);
bool isImplicit = boundConstantPattern.WasCompilerGenerated;
return new LazyConstantPattern(value, _semanticModel, syntax, type, constantValue, isImplicit);
return new LazyConstantPattern(value, _semanticModel, syntax, isImplicit);
}

private IDeclarationPatternOperation CreateBoundDeclarationPatternOperation(BoundDeclarationPattern boundDeclarationPattern)
Expand Down Expand Up @@ -2024,7 +2022,7 @@ private ICaseClauseOperation CreateBoundPatternSwitchLabelOperation(BoundPattern
else if (boundPatternSwitchLabel.WhenClause == null &&
boundPatternSwitchLabel.Pattern.Kind == BoundKind.ConstantPattern &&
boundPatternSwitchLabel.Pattern is BoundConstantPattern cp &&
cp.Value.Type.IsValidV6SwitchGoverningType())
cp.InputType.IsValidV6SwitchGoverningType())
{
Lazy<IOperation> value = new Lazy<IOperation>(() => Create(cp.Value));
return new LazySingleValueCaseClause(label, value, _semanticModel, syntax, type, constantValue, isImplicit);
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Test/Emit/Emit/ResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public void AddManagedResource()
c1 = null;
}

[ConditionalFact(typeof(DesktopOnly))]
[ConditionalFact(typeof(WindowsDesktopOnly))]
public void AddResourceToModule()
{
bool metadataOnly = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ public override int GetHashCode()
}

[CompilerTrait(CompilerFeature.IOperation, CompilerFeature.Dataflow)]
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/28634")]
[Fact]
public void SwitchFlow_15()
{
string source = @"
Expand Down Expand Up @@ -1936,7 +1936,7 @@ void M(bool result, int input, int? other)
}

[CompilerTrait(CompilerFeature.IOperation, CompilerFeature.Dataflow)]
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/28634")]
[Fact]
public void SwitchFlow_22()
{
string source = @"
Expand Down Expand Up @@ -2004,6 +2004,52 @@ void M(bool result, dynamic input)
VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>(source, expectedFlowGraph, expectedDiagnostics);
}

[CompilerTrait(CompilerFeature.IOperation, CompilerFeature.Dataflow)]
[Fact]
public void SwitchIOperation_022()
{
string source = @"
public sealed class MyClass
{
void M(bool result, dynamic input)
{
/*<bind>*/switch (input)
{
case 1:
result = false;
break;
}/*</bind>*/
}
}
";
var expectedDiagnostics = DiagnosticDescription.None;
var expectedOperationTree =
@"ISwitchOperation (1 cases, Exit Label Id: 0) (OperationKind.Switch, Type: null) (Syntax: 'switch (inp ... }')
Switch expression:
IParameterReferenceOperation: input (OperationKind.ParameterReference, Type: dynamic) (Syntax: 'input')
Sections:
ISwitchCaseOperation (1 case clauses, 2 statements) (OperationKind.SwitchCase, Type: null) (Syntax: 'case 1: ... break;')
Clauses:
IPatternCaseClauseOperation (Label Id: 1) (CaseKind.Pattern) (OperationKind.CaseClause, Type: null) (Syntax: 'case 1:')
Pattern:
IConstantPatternOperation (OperationKind.ConstantPattern, Type: null, IsImplicit) (Syntax: 'case 1:')
Value:
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 1) (Syntax: '1')
Guard Expression:
null
Body:
IExpressionStatementOperation (OperationKind.ExpressionStatement, Type: null) (Syntax: 'result = false;')
Expression:
ISimpleAssignmentOperation (OperationKind.SimpleAssignment, Type: System.Boolean) (Syntax: 'result = false')
Left:
IParameterReferenceOperation: result (OperationKind.ParameterReference, Type: System.Boolean) (Syntax: 'result')
Right:
ILiteralOperation (OperationKind.Literal, Type: System.Boolean, Constant: False) (Syntax: 'false')
IBranchOperation (BranchKind.Break, Label Id: 0) (OperationKind.Branch, Type: null) (Syntax: 'break;')
";
VerifyOperationTreeAndDiagnosticsForTest<SwitchStatementSyntax>(source, expectedOperationTree, expectedDiagnostics);
}

[CompilerTrait(CompilerFeature.IOperation, CompilerFeature.Dataflow)]
[Fact]
public void SwitchFlow_23()
Expand Down Expand Up @@ -2388,7 +2434,7 @@ void M(int input1, MyClass input2)
}

[CompilerTrait(CompilerFeature.IOperation, CompilerFeature.Dataflow)]
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/28634")]
[Fact]
public void SwitchFlow_27()
{
string source = @"
Expand Down Expand Up @@ -2762,7 +2808,7 @@ void M(bool result, object input1, bool other2, bool other3, bool other4)
}

[CompilerTrait(CompilerFeature.IOperation, CompilerFeature.Dataflow)]
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/28634")]
[Fact]
public void SwitchFlow_31()
{
string source = @"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6547,7 +6547,7 @@ public override IOperation VisitTranslatedQuery(ITranslatedQueryOperation operat
public override IOperation VisitConstantPattern(IConstantPatternOperation operation, int? captureIdForResult)
{
return new ConstantPattern(Visit(operation.Value), semanticModel: null,
operation.Syntax, operation.Type, operation.ConstantValue, IsImplicit(operation));
operation.Syntax, IsImplicit(operation));
}

public override IOperation VisitDeclarationPattern(IDeclarationPatternOperation operation, int? captureIdForResult)
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/Core/Portable/Operations/OperationCloner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public override IOperation VisitIsPattern(IIsPatternOperation operation, object

public override IOperation VisitConstantPattern(IConstantPatternOperation operation, object argument)
{
return new ConstantPattern(Visit(operation.Value), ((Operation)operation).OwningSemanticModel, operation.Syntax, operation.Type, operation.ConstantValue, operation.IsImplicit);
return new ConstantPattern(Visit(operation.Value), ((Operation)operation).OwningSemanticModel, operation.Syntax, operation.IsImplicit);
}

public override IOperation VisitDeclarationPattern(IDeclarationPatternOperation operation, object argument)
Expand Down
12 changes: 6 additions & 6 deletions src/Compilers/Core/Portable/Operations/OperationNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6122,8 +6122,8 @@ public LazyLocalFunctionStatement(IMethodSymbol symbol, Lazy<IBlockOperation> bo
/// </summary>
internal abstract partial class BaseConstantPattern : Operation, IConstantPatternOperation
{
protected BaseConstantPattern(SemanticModel semanticModel, SyntaxNode syntax, ITypeSymbol type, Optional<object> constantValue, bool isImplicit) :
base(OperationKind.ConstantPattern, semanticModel, syntax, type, constantValue, isImplicit)
protected BaseConstantPattern(SemanticModel semanticModel, SyntaxNode syntax, bool isImplicit) :
base(OperationKind.ConstantPattern, semanticModel, syntax, type: null, constantValue: default, isImplicit)
{
}

Expand Down Expand Up @@ -6156,8 +6156,8 @@ public override TResult Accept<TArgument, TResult>(OperationVisitor<TArgument, T
/// </summary>
internal sealed partial class ConstantPattern : BaseConstantPattern, IConstantPatternOperation
{
public ConstantPattern(IOperation value, SemanticModel semanticModel, SyntaxNode syntax, ITypeSymbol type, Optional<object> constantValue, bool isImplicit) :
base(semanticModel, syntax, type, constantValue, isImplicit)
public ConstantPattern(IOperation value, SemanticModel semanticModel, SyntaxNode syntax, bool isImplicit) :
base(semanticModel, syntax, isImplicit)
{
Value = SetParentOperation(value, this);
}
Expand All @@ -6172,8 +6172,8 @@ internal sealed partial class LazyConstantPattern : BaseConstantPattern, IConsta
{
private readonly Lazy<IOperation> _lazyValue;

public LazyConstantPattern(Lazy<IOperation> value, SemanticModel semanticModel, SyntaxNode syntax, ITypeSymbol type, Optional<object> constantValue, bool isImplicit) :
base(semanticModel, syntax, type, constantValue, isImplicit)
public LazyConstantPattern(Lazy<IOperation> value, SemanticModel semanticModel, SyntaxNode syntax, bool isImplicit) :
base(semanticModel, syntax, isImplicit)
{
_lazyValue = value ?? throw new System.ArgumentNullException(nameof(value));
}
Expand Down

0 comments on commit b90b792

Please sign in to comment.