-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SwitchExpressionBinder.cs
240 lines (212 loc) · 10.8 KB
/
SwitchExpressionBinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp
{
internal class SwitchExpressionBinder : Binder
{
private readonly SwitchExpressionSyntax SwitchExpressionSyntax;
private BoundExpression _inputExpression;
private DiagnosticBag _inputExpressionDiagnostics;
internal SwitchExpressionBinder(SwitchExpressionSyntax switchExpressionSyntax, Binder next)
: base(next)
{
SwitchExpressionSyntax = switchExpressionSyntax;
}
internal override BoundExpression BindSwitchExpressionCore(SwitchExpressionSyntax node, Binder originalBinder, DiagnosticBag diagnostics)
{
Debug.Assert(node == SwitchExpressionSyntax);
// Bind switch expression and set the switch governing type.
var boundInputExpression = InputExpression;
diagnostics.AddRange(InputExpressionDiagnostics);
ImmutableArray<BoundSwitchExpressionArm> switchArms = BindSwitchExpressionArms(node, originalBinder, diagnostics);
TypeSymbol naturalType = InferResultType(switchArms, diagnostics);
bool reportedNotExhaustive = CheckSwitchExpressionExhaustive(node, boundInputExpression, switchArms, out BoundDecisionDag decisionDag, out LabelSymbol defaultLabel, diagnostics);
// When the input is constant, we use that to reshape the decision dag that is returned
// so that flow analysis will see that some of the cases may be unreachable.
decisionDag = decisionDag.SimplifyDecisionDagIfConstantInput(boundInputExpression);
return new BoundUnconvertedSwitchExpression(
node, boundInputExpression, switchArms, decisionDag,
defaultLabel: defaultLabel, reportedNotExhaustive: reportedNotExhaustive, type: naturalType);
}
/// <summary>
/// Build the decision dag, giving an error if some cases are subsumed and a warning if the switch expression is not exhaustive.
/// </summary>
/// <param name="node"></param>
/// <param name="boundInputExpression"></param>
/// <param name="switchArms"></param>
/// <param name="decisionDag"></param>
/// <param name="diagnostics"></param>
/// <returns>true if there was a non-exhaustive warning reported</returns>
private bool CheckSwitchExpressionExhaustive(
SwitchExpressionSyntax node,
BoundExpression boundInputExpression,
ImmutableArray<BoundSwitchExpressionArm> switchArms,
out BoundDecisionDag decisionDag,
out LabelSymbol defaultLabel,
DiagnosticBag diagnostics)
{
defaultLabel = new GeneratedLabelSymbol("default");
decisionDag = DecisionDagBuilder.CreateDecisionDagForSwitchExpression(this.Compilation, node, boundInputExpression, switchArms, defaultLabel, diagnostics);
var reachableLabels = decisionDag.ReachableLabels;
bool hasErrors = false;
foreach (BoundSwitchExpressionArm arm in switchArms)
{
hasErrors |= arm.HasErrors;
if (!hasErrors && !reachableLabels.Contains(arm.Label))
{
diagnostics.Add(ErrorCode.ERR_SwitchArmSubsumed, arm.Pattern.Syntax.Location);
}
}
if (!reachableLabels.Contains(defaultLabel))
{
// switch expression is exhaustive; no default label needed.
defaultLabel = null;
return false;
}
if (hasErrors)
return true;
// We only report exhaustive warnings when the default label is reachable through some series of
// tests that do not include a test in which the value is known to be null. Handling paths with
// nulls is the job of the nullable walker.
bool wasAcyclic = TopologicalSort.TryIterativeSort<BoundDecisionDagNode>(new[] { decisionDag.RootNode }, nonNullSuccessors, out var nodes);
// Since decisionDag.RootNode is acyclic by construction, its subset of nodes sorted here cannot be cyclic
Debug.Assert(wasAcyclic);
foreach (var n in nodes)
{
if (n is BoundLeafDecisionDagNode leaf && leaf.Label == defaultLabel)
{
var samplePattern = PatternExplainer.SamplePatternForPathToDagNode(
BoundDagTemp.ForOriginalInput(boundInputExpression), nodes, n, nullPaths: false, out bool requiresFalseWhenClause, out bool unnamedEnumValue);
ErrorCode warningCode =
requiresFalseWhenClause ? ErrorCode.WRN_SwitchExpressionNotExhaustiveWithWhen :
unnamedEnumValue ? ErrorCode.WRN_SwitchExpressionNotExhaustiveWithUnnamedEnumValue :
ErrorCode.WRN_SwitchExpressionNotExhaustive;
diagnostics.Add(
warningCode,
node.SwitchKeyword.GetLocation(),
samplePattern);
return true;
}
}
return false;
ImmutableArray<BoundDecisionDagNode> nonNullSuccessors(BoundDecisionDagNode n)
{
switch (n)
{
case BoundTestDecisionDagNode p:
switch (p.Test)
{
case BoundDagNonNullTest t: // checks that the input is not null
return ImmutableArray.Create(p.WhenTrue);
case BoundDagExplicitNullTest t: // checks that the input is null
return ImmutableArray.Create(p.WhenFalse);
default:
return BoundDecisionDag.Successors(n);
}
default:
return BoundDecisionDag.Successors(n);
}
}
}
/// <summary>
/// Infer the result type of the switch expression by looking for a common type
/// to which every arm's expression can be converted.
/// </summary>
private TypeSymbol InferResultType(ImmutableArray<BoundSwitchExpressionArm> switchCases, DiagnosticBag diagnostics)
{
var seenTypes = Symbols.SpecializedSymbolCollections.GetPooledSymbolHashSetInstance<TypeSymbol>();
var typesInOrder = ArrayBuilder<TypeSymbol>.GetInstance();
foreach (var @case in switchCases)
{
var type = @case.Value.Type;
if (type is object && seenTypes.Add(type))
{
typesInOrder.Add(type);
}
}
seenTypes.Free();
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
var commonType = BestTypeInferrer.GetBestType(typesInOrder, Conversions, ref useSiteDiagnostics);
typesInOrder.Free();
// We've found a candidate common type among those arms that have a type. Also check that every arm's
// expression (even those without a type) can be converted to that type.
if (commonType is object)
{
foreach (var @case in switchCases)
{
if (!this.Conversions.ClassifyImplicitConversionFromExpression(@case.Value, commonType, ref useSiteDiagnostics).Exists)
{
commonType = null;
break;
}
}
}
diagnostics.Add(SwitchExpressionSyntax, useSiteDiagnostics);
return commonType;
}
private ImmutableArray<BoundSwitchExpressionArm> BindSwitchExpressionArms(SwitchExpressionSyntax node, Binder originalBinder, DiagnosticBag diagnostics)
{
bool hasErrors = InputExpression.HasErrors;
var builder = ArrayBuilder<BoundSwitchExpressionArm>.GetInstance();
foreach (var arm in node.Arms)
{
var armBinder = originalBinder.GetBinder(arm);
var boundArm = armBinder.BindSwitchExpressionArm(arm, diagnostics);
builder.Add(boundArm);
}
return builder.ToImmutableAndFree();
}
internal BoundExpression InputExpression
{
get
{
EnsureSwitchGoverningExpressionAndDiagnosticsBound();
Debug.Assert(_inputExpression != null);
return _inputExpression;
}
}
internal TypeSymbol SwitchGoverningType => InputExpression.Type;
internal uint SwitchGoverningValEscape => GetValEscape(InputExpression, LocalScopeDepth);
protected DiagnosticBag InputExpressionDiagnostics
{
get
{
EnsureSwitchGoverningExpressionAndDiagnosticsBound();
Debug.Assert(_inputExpressionDiagnostics != null);
return _inputExpressionDiagnostics;
}
}
private void EnsureSwitchGoverningExpressionAndDiagnosticsBound()
{
if (_inputExpression == null)
{
var switchGoverningDiagnostics = new DiagnosticBag();
var boundSwitchGoverningExpression = BindSwitchGoverningExpression(switchGoverningDiagnostics);
_inputExpressionDiagnostics = switchGoverningDiagnostics;
Interlocked.CompareExchange(ref _inputExpression, boundSwitchGoverningExpression, null);
}
}
private BoundExpression BindSwitchGoverningExpression(DiagnosticBag diagnostics)
{
var switchGoverningExpression = BindRValueWithoutTargetType(SwitchExpressionSyntax.GoverningExpression, diagnostics);
if (switchGoverningExpression.Type == (object)null || switchGoverningExpression.Type.IsVoidType())
{
diagnostics.Add(ErrorCode.ERR_BadPatternExpression, SwitchExpressionSyntax.GoverningExpression.Location, switchGoverningExpression.Display);
switchGoverningExpression = this.GenerateConversionForAssignment(CreateErrorType(), switchGoverningExpression, diagnostics);
}
return switchGoverningExpression;
}
}
}