Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store nested conversion in the target-typed switch expression conversion #40107

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Symbols;
Expand Down Expand Up @@ -104,7 +105,7 @@ protected BoundExpression CreateConversion(

if (conversion.Kind == ConversionKind.SwitchExpression)
{
return ConvertSwitchExpression((BoundUnconvertedSwitchExpression)source, destination, targetTyped: true, diagnostics);
return ConvertSwitchExpression((BoundUnconvertedSwitchExpression)source, destination, conversionIfTargetTyped: conversion, diagnostics);
}

if (source.Kind == BoundKind.UnconvertedSwitchExpression)
Expand All @@ -117,7 +118,7 @@ protected BoundExpression CreateConversion(
hasErrors = true;
}

source = ConvertSwitchExpression((BoundUnconvertedSwitchExpression)source, type, targetTyped: false, diagnostics, hasErrors);
source = ConvertSwitchExpression((BoundUnconvertedSwitchExpression)source, type, conversionIfTargetTyped: null, diagnostics, hasErrors);
if (destination.Equals(type, TypeCompareKind.ConsiderEverything) && wasCompilerGenerated)
{
return source;
Expand Down Expand Up @@ -151,17 +152,24 @@ protected BoundExpression CreateConversion(
{ WasCompilerGenerated = wasCompilerGenerated };
}

#nullable enable
/// <summary>
/// Rewrite the expressions in the switch expression arms to add a conversion to the destination type.
/// </summary>
private BoundExpression ConvertSwitchExpression(BoundUnconvertedSwitchExpression source, TypeSymbol destination, bool targetTyped, DiagnosticBag diagnostics, bool hasErrors = false)
private BoundExpression ConvertSwitchExpression(BoundUnconvertedSwitchExpression source, TypeSymbol destination, Conversion? conversionIfTargetTyped, DiagnosticBag diagnostics, bool hasErrors = false)
{
bool targetTyped = conversionIfTargetTyped != null;
Debug.Assert(targetTyped || destination.IsErrorType() || destination.Equals(source.Type, TypeCompareKind.ConsiderEverything));
ImmutableArray<Conversion> underlyingConversions = conversionIfTargetTyped.GetValueOrDefault().UnderlyingConversions;
var builder = ArrayBuilder<BoundSwitchExpressionArm>.GetInstance(source.SwitchArms.Length);
foreach (var oldCase in source.SwitchArms)
for (int i = 0, n = source.SwitchArms.Length; i < n; i++)
{
var oldCase = source.SwitchArms[i];
var oldValue = oldCase.Value;
var newValue = GenerateConversionForAssignment(destination, oldValue, diagnostics);
var newValue =
targetTyped
? CreateConversion(oldValue.Syntax, oldValue, underlyingConversions[i], isCast: false, conversionGroupOpt: null, destination, diagnostics)
: GenerateConversionForAssignment(destination, oldValue, diagnostics);
var newCase = (oldValue == newValue) ? oldCase :
new BoundSwitchExpressionArm(oldCase.Syntax, oldCase.Locals, oldCase.Pattern, oldCase.WhenClause, newValue, oldCase.Label, oldCase.HasErrors);
builder.Add(newCase);
Expand All @@ -172,6 +180,7 @@ private BoundExpression ConvertSwitchExpression(BoundUnconvertedSwitchExpression
source.Syntax, source.Type, targetTyped, source.Expression, newSwitchArms, source.DecisionDag,
source.DefaultLabel, source.ReportedNotExhaustive, destination, hasErrors || source.HasErrors);
}
#nullable restore

private BoundExpression CreateUserDefinedConversion(
SyntaxNode syntax,
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ internal BoundExpression BindToNaturalType(BoundExpression expression, Diagnosti
commonType = CreateErrorType();
hasErrors = true;
}
result = ConvertSwitchExpression(expr, commonType, targetTyped: false, diagnostics, hasErrors);
result = ConvertSwitchExpression(expr, commonType, conversionIfTargetTyped: null, diagnostics, hasErrors);
}
break;
case BoundTupleLiteral sourceTuple:
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ private BoundCall BindInvocationExpressionContinued(
_ = BindToNaturalType(argument, diagnostics);
break;
case BoundUnconvertedSwitchExpression { Type: { } naturalType } switchExpr:
_ = ConvertSwitchExpression(switchExpr, naturalType ?? CreateErrorType(), targetTyped: false, diagnostics);
_ = ConvertSwitchExpression(switchExpr, naturalType ?? CreateErrorType(), conversionIfTargetTyped: null, diagnostics);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ internal static Conversion GetTrivialConversion(ConversionKind kind)
internal static Conversion ImplicitDynamic => new Conversion(ConversionKind.ImplicitDynamic);
internal static Conversion ExplicitDynamic => new Conversion(ConversionKind.ExplicitDynamic);
internal static Conversion InterpolatedString => new Conversion(ConversionKind.InterpolatedString);
internal static Conversion SwitchExpression => new Conversion(ConversionKind.SwitchExpression);
internal static Conversion Deconstruction => new Conversion(ConversionKind.Deconstruction);
internal static Conversion PinnedObjectToPointer => new Conversion(ConversionKind.PinnedObjectToPointer);

Expand Down Expand Up @@ -306,6 +305,11 @@ internal static Conversion MakeNullableConversion(ConversionKind kind, Conversio
return new Conversion(kind, nested);
}

internal static Conversion MakeSwitchExpression(ImmutableArray<Conversion> innerConversions)
{
return new Conversion(ConversionKind.SwitchExpression, innerConversions);
}

internal ConversionKind Kind
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,15 +935,20 @@ private Conversion GetSwitchExpressionConversion(BoundExpression source, TypeSym
// It has already been subjected to a switch expression conversion.
return Conversion.NoConversion;
case BoundUnconvertedSwitchExpression switchExpression:
var innerConversions = ArrayBuilder<Conversion>.GetInstance(switchExpression.SwitchArms.Length);
foreach (var arm in switchExpression.SwitchArms)
{
if (!this.ClassifyConversionFromExpression(arm.Value, destination, ref useSiteDiagnostics).IsImplicit)
var nestedConversion = this.ClassifyImplicitConversionFromExpression(arm.Value, destination, ref useSiteDiagnostics);
if (!nestedConversion.Exists)
{
innerConversions.Free();
return Conversion.NoConversion;
}

innerConversions.Add(nestedConversion);
}

return Conversion.SwitchExpression;
return Conversion.MakeSwitchExpression(innerConversions.ToImmutableAndFree());
default:
return Conversion.NoConversion;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@
<!-- Converted switch expression must have a type, even if that's `void` for a statement. -->
<Field Name="Type" Type="TypeSymbol" Override="true" Null="disallow"/>
<!-- Natural type is preserved for the purpose of semantic model. Can be `null`. -->
<Field Name="NaturalTypeOpt" Type="TypeSymbol" Null="allow"/>
<Field Name="NaturalTypeOpt" Type="TypeSymbol?" Null="allow"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like a nullable annotation should be used for any reference type field with Null="allow". Do we have a tracking issue for updating the generator/BoundNodes.xml to do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know if.

<Field Name="WasTargetTyped" Type="bool" />
</Node>

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.