Skip to content

Commit

Permalink
Use struct for TypeSymbolWithAnnotations (dotnet#28943)
Browse files Browse the repository at this point in the history
  • Loading branch information
cston authored Aug 2, 2018
1 parent 7f19150 commit 14efbde
Show file tree
Hide file tree
Showing 74 changed files with 797 additions and 630 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ internal partial class Binder
{
internal struct NamespaceOrTypeOrAliasSymbolWithAnnotations
{
private readonly object _symbolOrTypeSymbolWithAnnotations;
private readonly TypeSymbolWithAnnotations _type;
private readonly Symbol _symbol;

private NamespaceOrTypeOrAliasSymbolWithAnnotations(object symbolOrTypeSymbolWithAnnotations)
private NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations type, Symbol symbol)
{
Debug.Assert(symbolOrTypeSymbolWithAnnotations != null);
Debug.Assert(!(symbolOrTypeSymbolWithAnnotations is TypeSymbol));
_symbolOrTypeSymbolWithAnnotations = symbolOrTypeSymbolWithAnnotations;
Debug.Assert(type.IsNull != (symbol is null));
Debug.Assert(!(symbol is TypeSymbol));
_type = type;
_symbol = symbol;
}

internal TypeSymbolWithAnnotations Type => _symbolOrTypeSymbolWithAnnotations as TypeSymbolWithAnnotations;
internal Symbol Symbol => _symbolOrTypeSymbolWithAnnotations as Symbol ?? Type?.TypeSymbol;
internal bool IsType => !(Type is null);
internal bool IsAlias => (_symbolOrTypeSymbolWithAnnotations as Symbol)?.Kind == SymbolKind.Alias;
internal TypeSymbolWithAnnotations Type => _type;
internal Symbol Symbol => _symbol ?? Type.TypeSymbol;
internal bool IsType => !_type.IsNull;
internal bool IsAlias => _symbol?.Kind == SymbolKind.Alias;
internal NamespaceOrTypeSymbol NamespaceOrTypeSymbol => Symbol as NamespaceOrTypeSymbol;
internal bool IsDefault => _symbolOrTypeSymbolWithAnnotations is null;
internal bool IsDefault => _type.IsNull && _symbol is null;

internal static NamespaceOrTypeOrAliasSymbolWithAnnotations CreateUnannotated(INonNullTypesContext nonNullTypesContext, Symbol symbol)
{
Expand All @@ -32,16 +34,14 @@ internal static NamespaceOrTypeOrAliasSymbolWithAnnotations CreateUnannotated(IN
return default;
}
var type = symbol as TypeSymbol;
if (type is null)
{
return new NamespaceOrTypeOrAliasSymbolWithAnnotations(symbol);
}
return new NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations.CreateUnannotated(nonNullTypesContext, type));
return type is null ?
new NamespaceOrTypeOrAliasSymbolWithAnnotations(default, symbol) :
new NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations.CreateUnannotated(nonNullTypesContext, type), null);
}

public static implicit operator NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations type)
{
return new NamespaceOrTypeOrAliasSymbolWithAnnotations(type);
return new NamespaceOrTypeOrAliasSymbolWithAnnotations(type, null);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ private bool CheckPropertyValueKind(SyntaxNode node, BoundExpression expr, BindV
// change from Dev10 which reports this error for struct types only,
// not for type parameters constrained to "struct".

Debug.Assert((object)propertySymbol.Type != null);
Debug.Assert(!propertySymbol.Type.IsNull);
Error(diagnostics, ErrorCode.ERR_ReturnNotLValue, expr.Syntax, propertySymbol);
}
else
Expand Down Expand Up @@ -1622,7 +1622,7 @@ private static void ReportReadOnlyFieldError(FieldSymbol field, SyntaxNode node,
{
Debug.Assert((object)field != null);
Debug.Assert(RequiresAssignableVariable(kind));
Debug.Assert((object)field.Type != null);
Debug.Assert(!field.Type.IsNull);

// It's clearer to say that the address can't be taken than to say that the field can't be modified
// (even though the latter message gives more explanation of why).
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void ValidateTypeForAttributeParameters(ImmutableArray<ParameterSymbol>
foreach (var parameter in parameters)
{
var paramType = parameter.Type;
Debug.Assert((object)paramType != null);
Debug.Assert(!paramType.IsNull);

if (!paramType.TypeSymbol.IsValidAttributeParameterType(Compilation))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Crefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ private static ImmutableArray<Symbol> PerformCrefOverloadResolution(ArrayBuilder
containingType: null,
name: null,
refKind: RefKind.None,
returnType: null,
returnType: default,
refCustomModifiers: ImmutableArray<CustomModifier>.Empty,
explicitInterfaceImplementations: ImmutableArray<MethodSymbol>.Empty);
break;
Expand All @@ -755,7 +755,7 @@ private static ImmutableArray<Symbol> PerformCrefOverloadResolution(ArrayBuilder
containingType: null,
name: null,
refKind: RefKind.None,
type: null,
type: default,
refCustomModifiers: ImmutableArray<CustomModifier>.Empty,
isStatic: false,
explicitInterfaceImplementations: ImmutableArray<PropertySymbol>.Empty);
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ private DeconstructionVariable BindDeconstructionVariables(
bool isConst = false;
AliasSymbol alias;
var declType = BindVariableType(component.Designation, diagnostics, component.Type, ref isConst, out isVar, out alias);
Debug.Assert(isVar == ((object)declType == null));
Debug.Assert(isVar == declType.IsNull);
if (component.Designation.Kind() == SyntaxKind.ParenthesizedVariableDesignation && !isVar)
{
// An explicit is not allowed with a parenthesized designation
Expand Down Expand Up @@ -800,7 +800,7 @@ private static BoundDiscardExpression BindDiscardExpression(
SyntaxNode syntax,
TypeSymbolWithAnnotations declType)
{
return new BoundDiscardExpression(syntax, declType?.TypeSymbol);
return new BoundDiscardExpression(syntax, declType.TypeSymbol);
}

/// <summary>
Expand All @@ -824,7 +824,7 @@ private BoundExpression BindDeconstructionVariable(
// might own nested scope.
var hasErrors = localSymbol.ScopeBinder.ValidateDeclarationNameConflictsInScope(localSymbol, diagnostics);

if ((object)declType != null)
if (!declType.IsNull)
{
return new BoundLocal(syntax, localSymbol, BoundLocalDeclarationKind.WithExplicitType, constantValueOpt: null, isNullableUnknown: false, type: declType.TypeSymbol, hasErrors: hasErrors);
}
Expand All @@ -844,7 +844,7 @@ private BoundExpression BindDeconstructionVariable(
BoundThisReference receiver = ThisReference(designation, this.ContainingType, hasErrors: false,
wasCompilerGenerated: true);

if ((object)declType != null)
if (!declType.IsNull)
{
var fieldType = field.GetFieldType(this.FieldsBeingBound);
Debug.Assert(declType.TypeSymbol == fieldType.TypeSymbol);
Expand Down
16 changes: 8 additions & 8 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ private BoundExpression BindDeclarationExpression(DeclarationExpressionSyntax no
/// </summary>
private BoundExpression BindDeclarationVariables(TypeSymbolWithAnnotations declType, VariableDesignationSyntax node, CSharpSyntaxNode syntax, DiagnosticBag diagnostics)
{
declType = declType ?? TypeSymbolWithAnnotations.Create(CreateErrorType("var"));
declType = declType.IsNull ? TypeSymbolWithAnnotations.Create(CreateErrorType("var")) : declType;
switch (node.Kind())
{
case SyntaxKind.SingleVariableDesignation:
Expand Down Expand Up @@ -783,7 +783,7 @@ private BoundExpression BindTupleExpression(TupleExpressionSyntax node, Diagnost
var elementType = boundArgument.GetTypeAndNullability(includeNullability);
elementTypes.Add(elementType);

if ((object)elementType == null)
if (elementType.IsNull)
{
hasNaturalType = false;
}
Expand Down Expand Up @@ -2056,7 +2056,7 @@ private void GenerateExplicitConversionErrorsForTupleLiteralArguments(
/// </summary>
private BoundExpression BindExplicitNullableCastFromNonNullable(ExpressionSyntax node, BoundExpression operand, TypeSymbolWithAnnotations targetType, DiagnosticBag diagnostics)
{
Debug.Assert((object)targetType != null && targetType.IsNullableType());
Debug.Assert(!targetType.IsNull && targetType.IsNullableType());
Debug.Assert(operand.Type != null && !operand.Type.IsNullableType());

// Section 6.2.3 of the spec only applies when the non-null version of the types involved have a
Expand Down Expand Up @@ -2310,9 +2310,9 @@ private BoundExpression BindOutDeclarationArgument(DeclarationExpressionSyntax d
bool isConst = false;
AliasSymbol alias;
var declType = BindVariableType(designation, diagnostics, typeSyntax, ref isConst, out isVar, out alias);
Debug.Assert(isVar == ((object)declType == null));
Debug.Assert(isVar == declType.IsNull);

return new BoundDiscardExpression(declarationExpression, declType?.TypeSymbol);
return new BoundDiscardExpression(declarationExpression, declType.TypeSymbol);
}
case SyntaxKind.SingleVariableDesignation:
return BindOutVariableDeclarationArgument(declarationExpression, diagnostics);
Expand Down Expand Up @@ -2563,7 +2563,7 @@ private void CoerceArguments<TMember>(
else if (argument.Kind == BoundKind.DiscardExpression && !argument.HasExpressionType())
{
TypeSymbolWithAnnotations parameterType = GetCorrespondingParameterType(ref result, parameters, arg);
Debug.Assert((object)parameterType != null);
Debug.Assert(!parameterType.IsNull);
arguments[arg] = ((BoundDiscardExpression)argument).SetInferredType(parameterType);
}
}
Expand Down Expand Up @@ -2711,7 +2711,7 @@ private BoundExpression BindImplicitArrayCreationExpression(ImplicitArrayCreatio
useSiteDiagnostics: ref useSiteDiagnostics);
diagnostics.Add(node, useSiteDiagnostics);

if ((object)bestType == null || bestType.SpecialType == SpecialType.System_Void) // Dev10 also reports ERR_ImplicitlyTypedArrayNoBestType for void.
if (bestType.IsNull || bestType.SpecialType == SpecialType.System_Void) // Dev10 also reports ERR_ImplicitlyTypedArrayNoBestType for void.
{
Error(diagnostics, ErrorCode.ERR_ImplicitlyTypedArrayNoBestType, node);
bestType = TypeSymbolWithAnnotations.Create(CreateErrorType());
Expand All @@ -2737,7 +2737,7 @@ private BoundExpression BindImplicitStackAllocArrayCreationExpression(ImplicitSt
TypeSymbolWithAnnotations bestType = BestTypeInferrer.InferBestType(boundInitializerExpressions, this.Conversions, out bool hadMultipleCandidates, ref useSiteDiagnostics);
diagnostics.Add(node, useSiteDiagnostics);

if ((object)bestType == null || bestType.SpecialType == SpecialType.System_Void)
if (bestType.IsNull || bestType.SpecialType == SpecialType.System_Void)
{
Error(diagnostics, ErrorCode.ERR_ImplicitlyTypedArrayNoBestType, node);
bestType = TypeSymbolWithAnnotations.Create(CreateErrorType());
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private Tuple<ImmutableArray<RefKind>, ImmutableArray<TypeSymbolWithAnnotations>
}

var typeSyntax = p.Type;
TypeSymbolWithAnnotations type = null;
TypeSymbolWithAnnotations type = default;
var refKind = RefKind.None;

if (typeSyntax == null)
Expand Down Expand Up @@ -228,7 +228,7 @@ private UnboundLambda BindAnonymousFunction(CSharpSyntaxNode syntax, DiagnosticB
foreach (var type in types)
{
// UNDONE: Where do we report improper use of pointer types?
if ((object)type != null && type.IsStatic)
if (!type.IsNull && type.IsStatic)
{
Error(diagnostics, ErrorCode.ERR_ParameterIsStaticClass, syntax, type.TypeSymbol);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3645,7 +3645,7 @@ private BoundExpression BindConditionalOperator(ConditionalExpressionSyntax node
useSiteDiagnostics: ref useSiteDiagnostics);
diagnostics.Add(node, useSiteDiagnostics);

if ((object)bestType == null)
if (bestType.IsNull)
{
// CONSIDER: Dev10 suppresses ERR_InvalidQM unless the following is true for both trueType and falseType
// (!T->type->IsErrorType() || T->type->AsErrorType()->HasTypeParent() || T->type->AsErrorType()->HasNSParent())
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private BoundPattern BindDeclarationPattern(
declType = TypeSymbolWithAnnotations.CreateUnannotated(NonNullTypesContext, operandType);
}

if (declType == (object)null)
if (declType.IsNull)
{
Debug.Assert(hasErrors);
declType = TypeSymbolWithAnnotations.CreateUnannotated(NonNullTypesContext, this.CreateErrorType("var"));
Expand Down
10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ declarationNode is VariableDesignationSyntax ||
// we want to treat the declaration as an explicitly typed declaration.

TypeSymbolWithAnnotations declType = BindTypeOrVarKeyword(typeSyntax.SkipRef(out _), diagnostics, out isVar, out alias);
Debug.Assert((object)declType != null || isVar);
Debug.Assert(!declType.IsNull || isVar);

if (isVar)
{
Expand Down Expand Up @@ -851,7 +851,7 @@ protected BoundLocalDeclaration BindVariableDeclaration(
CSharpSyntaxNode associatedSyntaxNode = null)
{
Debug.Assert(declarator != null);
Debug.Assert((object)declTypeOpt != null || isVar);
Debug.Assert(!declTypeOpt.IsNull || isVar);
Debug.Assert(typeSyntax != null);

var localDiagnostics = DiagnosticBag.GetInstance();
Expand Down Expand Up @@ -934,7 +934,7 @@ protected BoundLocalDeclaration BindVariableDeclaration(
}
}

Debug.Assert((object)declTypeOpt != null);
Debug.Assert(!declTypeOpt.IsNull);

if (kind == LocalDeclarationKind.FixedVariable)
{
Expand Down Expand Up @@ -2336,7 +2336,7 @@ internal BoundStatement BindForOrUsingOrFixedDeclarations(VariableDeclarationSyn
bool isVar;
TypeSymbolWithAnnotations declType = BindTypeOrVarKeyword(typeSyntax, diagnostics, out isVar, out alias);

Debug.Assert((object)declType != null || isVar);
Debug.Assert(!declType.IsNull || isVar);

var variables = nodeOpt.Variables;
int count = variables.Count;
Expand Down Expand Up @@ -2475,7 +2475,7 @@ protected virtual TypeSymbol GetCurrentReturnType(out RefKind refKind)

TypeSymbolWithAnnotations returnType = symbol.ReturnType;

if ((object)returnType == null || (object)returnType == LambdaSymbol.ReturnTypeIsBeingInferred)
if (returnType.IsNull || (object)returnType.TypeSymbol == LambdaSymbol.ReturnTypeIsBeingInferred)
{
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal TypeSymbolWithAnnotations BindTypeOrVarKeyword(TypeSyntax syntax, Diagn
{
var symbol = BindTypeOrAliasOrVarKeyword(syntax, diagnostics, out isVar);
Debug.Assert(isVar == symbol.IsDefault);
return isVar ? null : UnwrapAlias(symbol, diagnostics, syntax).Type;
return isVar ? default : UnwrapAlias(symbol, diagnostics, syntax).Type;
}

/// <summary>
Expand All @@ -52,7 +52,7 @@ internal TypeSymbolWithAnnotations BindTypeOrUnmanagedKeyword(TypeSyntax syntax,
{
var symbol = BindTypeOrAliasOrUnmanagedKeyword(syntax, diagnostics, out isUnmanaged);
Debug.Assert(isUnmanaged == symbol.IsDefault);
return isUnmanaged ? null : UnwrapAlias(symbol, diagnostics, syntax).Type;
return isUnmanaged ? default : UnwrapAlias(symbol, diagnostics, syntax).Type;
}

/// <summary>
Expand All @@ -76,7 +76,7 @@ internal TypeSymbolWithAnnotations BindTypeOrVarKeyword(TypeSyntax syntax, Diagn
if (isVar)
{
alias = null;
return null;
return default;
}
else
{
Expand Down Expand Up @@ -327,7 +327,7 @@ internal NamespaceOrTypeOrAliasSymbolWithAnnotations BindNamespaceOrTypeSymbol(E
internal NamespaceOrTypeOrAliasSymbolWithAnnotations BindNamespaceOrTypeSymbol(ExpressionSyntax syntax, DiagnosticBag diagnostics, ConsList<Symbol> basesBeingResolved, bool suppressUseSiteDiagnostics)
{
var result = BindNamespaceOrTypeOrAliasSymbol(syntax, diagnostics, basesBeingResolved, suppressUseSiteDiagnostics);
Debug.Assert((object)result != null);
Debug.Assert(!result.IsDefault);

return UnwrapAlias(result, diagnostics, syntax, basesBeingResolved);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private static (ImmutableArray<BoundExpression> Elements, ImmutableArray<string>
// placeholder bound nodes with the proper types are sufficient to bind the element-wise binary operators
TypeSymbol tupleType = expr.Type.StrippedType();
ImmutableArray<BoundExpression> placeholders = tupleType.TupleElementTypes
.SelectAsArray((t, s) => (BoundExpression)new BoundTupleOperandPlaceholder(s, t?.TypeSymbol), expr.Syntax);
.SelectAsArray((t, s) => (BoundExpression)new BoundTupleOperandPlaceholder(s, t.TypeSymbol), expr.Syntax);

return (placeholders, tupleType.TupleElementNames);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private ForEachEnumeratorInfo(
BinderFlags location)
{
Debug.Assert((object)collectionType != null, "Field 'collectionType' cannot be null");
Debug.Assert((object)elementType != null, "Field 'elementType' cannot be null");
Debug.Assert(!elementType.IsNull, "Field 'elementType' cannot be null");
Debug.Assert((object)getEnumeratorMethod != null, "Field 'getEnumeratorMethod' cannot be null");
Debug.Assert((object)currentPropertyGetter != null, "Field 'currentPropertyGetter' cannot be null");
Debug.Assert((object)moveNextMethod != null, "Field 'moveNextMethod' cannot be null");
Expand Down
Loading

0 comments on commit 14efbde

Please sign in to comment.