/// <summary> /// Infer the result type of the switch expression by looking for a common type. /// </summary> private TypeSymbol InferResultType(ImmutableArray <BoundSwitchExpressionArm> switchCases, DiagnosticBag diagnostics) { var seenTypes = PooledHashSet <TypeSymbol> .GetInstance(); var typesInOrder = ArrayBuilder <TypeSymbol> .GetInstance(); foreach (var @case in switchCases) { var type = @case.Value.Type; if (!(type is null) && seenTypes.Add(type)) { typesInOrder.Add(type); } } HashSet <DiagnosticInfo> useSiteDiagnostics = null; var commonType = BestTypeInferrer.GetBestType(typesInOrder, Conversions, ref useSiteDiagnostics); diagnostics.Add(SwitchExpressionSyntax, useSiteDiagnostics); if (commonType is null) { diagnostics.Add(ErrorCode.ERR_SwitchExpressionNoBestType, SwitchExpressionSyntax.Location); commonType = CreateErrorType(); } seenTypes.Free(); return(commonType); }
/// <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, BindingDiagnosticBag 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(); CompoundUseSiteInfo <AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo( diagnostics ); var commonType = BestTypeInferrer.GetBestType( typesInOrder, Conversions, ref useSiteInfo ); 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 useSiteInfo ).Exists ) { commonType = null; break; } } } diagnostics.Add(SwitchExpressionSyntax, useSiteInfo); return(commonType); }
/// <summary> /// Infer the result type of the switch expression by looking for a common type. /// </summary> private TypeSymbol InferResultType(ImmutableArray <BoundSwitchExpressionArm> switchCases, DiagnosticBag diagnostics) { var seenTypes = SpecializedCollections.GetPooledSymbolHashSetInstance <TypeSymbol>(); var typesInOrder = ArrayBuilder <TypeSymbol> .GetInstance(); foreach (var @case in switchCases) { var type = @case.Value.Type; if (!(type is null) && seenTypes.Add(type)) { typesInOrder.Add(type); } } HashSet <DiagnosticInfo> useSiteDiagnostics = null; var commonType = BestTypeInferrer.GetBestType(typesInOrder, Conversions, ref useSiteDiagnostics); diagnostics.Add(SwitchExpressionSyntax, useSiteDiagnostics); seenTypes.Free(); return(commonType); }
public static TypeSymbol InferBestType(ImmutableArray<TypeSymbol> types, ConversionsBase conversions, ref HashSet<DiagnosticInfo> useSiteDiagnostics) { var inferrer = new BestTypeInferrer(conversions); return inferrer.GetBestType(types, ref useSiteDiagnostics); }
public static TypeSymbol InferBestType(ImmutableArray <TypeSymbol> types, Conversions conversions, ref HashSet <DiagnosticInfo> useSiteDiagnostics) { var inferrer = new BestTypeInferrer(conversions); return(inferrer.GetBestType(types, ref useSiteDiagnostics)); }