Skip to content

Commit 16c2c0d

Browse files
committed
Do not pass target typed expressions to BestTypeInferrer
1 parent 260a25d commit 16c2c0d

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/Compilers/CSharp/Portable/Binder/Semantics/BestTypeInferrer.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ private static bool IsTargetTypedExpression(BoundExpression node)
8383
HashSet<TypeSymbol> candidateTypes = new HashSet<TypeSymbol>(comparer);
8484
foreach (BoundExpression expr in exprs)
8585
{
86-
if (IsTargetTypedExpression(expr))
87-
{
88-
continue;
89-
}
90-
86+
Debug.Assert(!IsTargetTypedExpression(expr));
9187
TypeSymbol? type = expr.GetTypeOrFunctionType();
9288

9389
if (type is { })
@@ -152,7 +148,8 @@ private static bool IsTargetTypedExpression(BoundExpression node)
152148
{
153149
var conversionsWithoutNullability = conversions.WithNullability(false);
154150

155-
if (!IsTargetTypedExpression(expr1) && expr1.Type is { } type1)
151+
Debug.Assert(!IsTargetTypedExpression(expr1));
152+
if (expr1.Type is { } type1)
156153
{
157154
if (type1.IsErrorType())
158155
{
@@ -166,7 +163,8 @@ private static bool IsTargetTypedExpression(BoundExpression node)
166163
}
167164
}
168165

169-
if (!IsTargetTypedExpression(expr2) && expr2.Type is { } type2)
166+
Debug.Assert(!IsTargetTypedExpression(expr1));
167+
if (expr2.Type is { } type2)
170168
{
171169
if (type2.IsErrorType())
172170
{

src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4613,7 +4613,11 @@ internal static TypeWithAnnotations BestTypeForLambdaReturns(
46134613
{
46144614
var (returnExpr, resultType, _) = returns[i];
46154615
resultTypes.Add(resultType);
4616-
placeholdersBuilder.Add(CreatePlaceholderIfNecessary(returnExpr, resultType));
4616+
4617+
if (!IsTargetTypedExpression(returnExpr))
4618+
{
4619+
placeholdersBuilder.Add(CreatePlaceholderIfNecessary(returnExpr, resultType));
4620+
}
46174621
}
46184622

46194623
var discardedUseSiteInfo = CompoundUseSiteInfo<AssemblySymbol>.Discarded;
@@ -5817,6 +5821,14 @@ void makeAndAdjustReceiverSlot(BoundExpression receiver)
58175821
{
58185822
resultType = null;
58195823
}
5824+
else if (IsTargetTypedExpression(consequence))
5825+
{
5826+
resultType = alternativeRValue.Type;
5827+
}
5828+
else if (IsTargetTypedExpression(alternative))
5829+
{
5830+
resultType = consequenceRValue.Type;
5831+
}
58205832
else
58215833
{
58225834
// Determine nested nullability using BestTypeInferrer.

src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,11 @@ private void VisitSwitchExpressionCore(BoundSwitchExpression node, bool inferTyp
898898
resultTypes.Add(armType);
899899
Join(ref endState, ref this.State);
900900

901-
// Build placeholders for inference in order to preserve annotations.
902-
placeholderBuilder.Add(CreatePlaceholderIfNecessary(expression, armType.ToTypeWithAnnotations(compilation)));
901+
if (!IsTargetTypedExpression(expression))
902+
{
903+
// Build placeholders for inference in order to preserve annotations.
904+
placeholderBuilder.Add(CreatePlaceholderIfNecessary(expression, armType.ToTypeWithAnnotations(compilation)));
905+
}
903906
}
904907

905908
SetState(endState);

src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5385,5 +5385,7 @@ public string[] M2(bool b)
53855385
Assert.Equal(PublicNullableAnnotation.NotAnnotated, type.ElementNullableAnnotation);
53865386
}
53875387
}
5388+
5389+
// TODO2: test collection-exprs in lambda returns to exercise all BestTypeInferrer usages in NullableWalker
53885390
}
53895391
}

0 commit comments

Comments
 (0)