未验证 提交 b90b7920 编写于 作者: N Neal Gafter 提交者: GitHub

Restore flow graph produced for existing switch statements (#31129)

* Restore flow graph produced for existing switch statements
Fixes #28634
* Prevent type and constant value on a constant pattern
See also https://github.com/dotnet/roslyn/issues/31150
* Skip `AddResourceToModule` on Linux
Issue reported as #31279
上级 b4b406b3
......@@ -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)
......@@ -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);
......
......@@ -433,7 +433,7 @@ public void AddManagedResource()
c1 = null;
}
[ConditionalFact(typeof(DesktopOnly))]
[ConditionalFact(typeof(WindowsDesktopOnly))]
public void AddResourceToModule()
{
bool metadataOnly = false;
......
......@@ -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 = @"
......@@ -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 = @"
......@@ -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()
......@@ -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 = @"
......@@ -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 = @"
......
......@@ -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)
......
......@@ -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)
......
......@@ -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)
{
}
......@@ -6156,8 +6156,8 @@ public override void Accept(OperationVisitor visitor)
/// </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);
}
......@@ -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));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册