Skip to content

Commit

Permalink
Remove remaining artifacts of symbol-centric NonNullTypes context.
Browse files Browse the repository at this point in the history
Closes dotnet#30171.
Closes dotnet#29838.
  • Loading branch information
AlekseyTs committed Dec 7, 2018
1 parent 7a31585 commit 261872e
Show file tree
Hide file tree
Showing 69 changed files with 1,151 additions and 707 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ internal struct NamespaceOrTypeOrAliasSymbolWithAnnotations
{
private readonly TypeSymbolWithAnnotations _type;
private readonly Symbol _symbol;
private readonly bool _isNullableEnabled;

private NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations type, Symbol symbol)
private NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations type)
{
Debug.Assert(type.IsNull != (symbol is null));
Debug.Assert(!(symbol is TypeSymbol));
Debug.Assert(!type.IsNull);
_type = type;
_symbol = null;
_isNullableEnabled = false; // Not meaningful for a TypeSymbolWithAnnotations, it already baked the fact into its content.
}

private NamespaceOrTypeOrAliasSymbolWithAnnotations(Symbol symbol, bool isNullableEnabled)
{
Debug.Assert(!(symbol is TypeSymbol));
_type = default;
_symbol = symbol;
_isNullableEnabled = isNullableEnabled;
}

internal TypeSymbolWithAnnotations Type => _type;
Expand All @@ -27,21 +36,30 @@ private NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations ty
internal NamespaceOrTypeSymbol NamespaceOrTypeSymbol => Symbol as NamespaceOrTypeSymbol;
internal bool IsDefault => _type.IsNull && _symbol is null;

internal static NamespaceOrTypeOrAliasSymbolWithAnnotations CreateUnannotated(INonNullTypesContext nonNullTypesContext, Symbol symbol)
internal bool IsNullableEnabled
{
get
{
Debug.Assert(_symbol?.Kind == SymbolKind.Alias); // Not meaningful to use this property otherwise
return _isNullableEnabled;
}
}

internal static NamespaceOrTypeOrAliasSymbolWithAnnotations CreateUnannotated(bool isNullableEnabled, Symbol symbol)
{
if (symbol is null)
{
return default;
}
var type = symbol as TypeSymbol;
return type is null ?
new NamespaceOrTypeOrAliasSymbolWithAnnotations(default, symbol) :
new NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations.Create(nonNullTypesContext, type), null);
new NamespaceOrTypeOrAliasSymbolWithAnnotations(symbol, isNullableEnabled) :
new NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations.Create(isNullableEnabled, type));
}

public static implicit operator NamespaceOrTypeOrAliasSymbolWithAnnotations(TypeSymbolWithAnnotations type)
{
return new NamespaceOrTypeOrAliasSymbolWithAnnotations(type, null);
return new NamespaceOrTypeOrAliasSymbolWithAnnotations(type);
}
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,27 @@ internal virtual Symbol ContainingMemberOrLambda
/// <summary>
/// Are we in a context where un-annotated types should be interpreted as non-null?
/// </summary>
internal INonNullTypesContext NonNullTypesContext => (Flags & BinderFlags.InEEMethodBinder) == 0 ? ContainingMember().OriginalDefinition : NonNullTypesTrueContext.Instance;
internal bool IsNullableEnabled(SyntaxTree syntaxTree, int position)
{
bool? fromTree = ((CSharpSyntaxTree)syntaxTree).GetNullableDirectiveState(position);

if (fromTree != null)
{
return fromTree.GetValueOrDefault();
}

return IsNullableGloballyEnabled();
}

internal bool IsNullableEnabled(SyntaxToken token)
{
return IsNullableEnabled(token.SyntaxTree, token.SpanStart);
}

internal virtual bool IsNullableGloballyEnabled()
{
return Next.IsNullableGloballyEnabled();
}

/// <summary>
/// Is the contained code within a member method body?
Expand Down
17 changes: 14 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Constraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,24 @@ private TypeParameterConstraintClause BindTypeParameterConstraints(
diagnostics.Add(ErrorCode.ERR_RefValBoundMustBeFirst, syntax.GetFirstToken().GetLocation());
}

SyntaxToken questionToken = ((ClassOrStructConstraintSyntax)syntax).QuestionToken;
var constraintSyntax = (ClassOrStructConstraintSyntax)syntax;
SyntaxToken questionToken = constraintSyntax.QuestionToken;
if (questionToken.IsKind(SyntaxKind.QuestionToken))
{
constraints |= TypeParameterConstraintKind.NullableReferenceType;
diagnostics.Add(new LazyMissingNonNullTypesContextDiagnosticInfo(Compilation, NonNullTypesContext, type: default), questionToken.GetLocation());

DiagnosticInfo info = LazyMissingNonNullTypesContextDiagnosticInfo.ReportNullableReferenceTypesIfNeeded(Compilation, IsNullableEnabled(questionToken));

if (!(info is null))
{
diagnostics.Add(info, questionToken.GetLocation());
}
}
else if (IsNullableEnabled(constraintSyntax.ClassOrStructKeyword))
{
constraints |= TypeParameterConstraintKind.NotNullableReferenceType;
}
else
else
{
constraints |= TypeParameterConstraintKind.ReferenceType;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,13 @@ private BoundExpression SetInferredType(BoundExpression expression, TypeSymbol t
case BoundKind.DeconstructionVariablePendingInference:
{
var pending = (DeconstructionVariablePendingInference)expression;
return pending.SetInferredType(TypeSymbolWithAnnotations.Create(NonNullTypesContext, type), this, diagnostics);
return pending.SetInferredType(TypeSymbolWithAnnotations.Create(type), this, diagnostics);
}
case BoundKind.DiscardExpression:
{
var pending = (BoundDiscardExpression)expression;
Debug.Assert((object)pending.Type == null);
return pending.SetInferredType(TypeSymbolWithAnnotations.Create(NonNullTypesContext, type));
return pending.SetInferredType(TypeSymbolWithAnnotations.Create(type));
}
default:
throw ExceptionUtilities.UnexpectedValue(expression.Kind);
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ private BoundPattern BindDeclarationPattern(
TypeSymbolWithAnnotations declType = BindTypeOrVarKeyword(typeSyntax, diagnostics, out isVar, out aliasOpt);
if (isVar)
{
declType = TypeSymbolWithAnnotations.Create(NonNullTypesContext, operandType);
declType = TypeSymbolWithAnnotations.Create(operandType);
}

if (declType.IsNull)
{
Debug.Assert(hasErrors);
declType = TypeSymbolWithAnnotations.Create(NonNullTypesContext, this.CreateErrorType("var"));
declType = TypeSymbolWithAnnotations.Create(this.CreateErrorType("var"));
}

var boundDeclType = new BoundTypeExpression(typeSyntax, aliasOpt, inferredType: isVar, type: declType.TypeSymbol);
Expand Down
Loading

0 comments on commit 261872e

Please sign in to comment.