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

Handle cycles in IsValueType and IsReferenceType binding constraints #47968

Merged
merged 11 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 4 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ internal bool AreNullableAnnotationsEnabled(SyntaxTree syntaxTree, int position)
internal bool AreNullableAnnotationsEnabled(SyntaxToken token)
{
RoslynDebug.Assert(token.SyntaxTree is object);
if ((Flags & BinderFlags.IgnoreNullableContext) != 0)
{
return false;
}
return AreNullableAnnotationsEnabled(token.SyntaxTree, token.SpanStart);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/BinderFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ internal enum BinderFlags : uint
/// </summary>
InEEMethodBinder = 1 << 30,

/// <summary>
/// Assume '#nullable disabled'.
/// </summary>
IgnoreNullableContext = 1u << 31,

// Groups

AllClearedAtExecutableCodeBoundary = InLockBody | InCatchBlock | InCatchFilter | InFinallyBlock | InTryBlockOfTryCatch | InNestedFinallyBlock,
Expand Down
11 changes: 7 additions & 4 deletions src/Compilers/CSharp/Portable/Binder/Binder_Constraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal ImmutableArray<TypeParameterConstraintClause> BindTypeParameterConstrai
ImmutableArray<TypeParameterSymbol> typeParameters,
TypeParameterListSyntax typeParameterList,
SyntaxList<TypeParameterConstraintClauseSyntax> clauses,
bool canIgnoreNullableContext,
ref IReadOnlyDictionary<TypeParameterSymbol, bool> isValueTypeOverride,
DiagnosticBag diagnostics,
bool isForOverride = false)
Expand Down Expand Up @@ -66,7 +67,8 @@ internal ImmutableArray<TypeParameterConstraintClause> BindTypeParameterConstrai
Debug.Assert(ordinal >= 0);
Debug.Assert(ordinal < n);

(TypeParameterConstraintClause constraintClause, ArrayBuilder<TypeConstraintSyntax>? typeConstraintNodes) = this.BindTypeParameterConstraints(typeParameterList.Parameters[ordinal], clause, isForOverride, diagnostics);
(TypeParameterConstraintClause constraintClause, ArrayBuilder<TypeConstraintSyntax>? typeConstraintNodes) =
this.BindTypeParameterConstraints(typeParameterList.Parameters[ordinal], clause, isForOverride, canIgnoreNullableContext: canIgnoreNullableContext, diagnostics);
if (results[ordinal] == null)
{
results[ordinal] = constraintClause;
Expand Down Expand Up @@ -117,7 +119,8 @@ internal ImmutableArray<TypeParameterConstraintClause> BindTypeParameterConstrai
/// <summary>
/// Bind and return a single type parameter constraint clause along with syntax nodes corresponding to type constraints.
/// </summary>
private (TypeParameterConstraintClause, ArrayBuilder<TypeConstraintSyntax>?) BindTypeParameterConstraints(TypeParameterSyntax typeParameterSyntax, TypeParameterConstraintClauseSyntax constraintClauseSyntax, bool isForOverride, DiagnosticBag diagnostics)
private (TypeParameterConstraintClause, ArrayBuilder<TypeConstraintSyntax>?) BindTypeParameterConstraints(
TypeParameterSyntax typeParameterSyntax, TypeParameterConstraintClauseSyntax constraintClauseSyntax, bool isForOverride, bool canIgnoreNullableContext, DiagnosticBag diagnostics)
{
var constraints = TypeParameterConstraintKind.None;
ArrayBuilder<TypeWithAnnotations>? constraintTypes = null;
Expand Down Expand Up @@ -307,7 +310,7 @@ internal ImmutableArray<TypeParameterConstraintClause> BindTypeParameterConstrai
Debug.Assert(!isForOverride ||
(constraints & (TypeParameterConstraintKind.ReferenceType | TypeParameterConstraintKind.ValueType)) != (TypeParameterConstraintKind.ReferenceType | TypeParameterConstraintKind.ValueType));

return (TypeParameterConstraintClause.Create(constraints, constraintTypes?.ToImmutableAndFree() ?? ImmutableArray<TypeWithAnnotations>.Empty), syntaxBuilder);
return (TypeParameterConstraintClause.Create(constraints, constraintTypes?.ToImmutableAndFree() ?? ImmutableArray<TypeWithAnnotations>.Empty, canIgnoreNullableContext), syntaxBuilder);

static void reportOverrideWithConstraints(ref bool reportedOverrideWithConstraints, TypeParameterConstraintSyntax syntax, DiagnosticBag diagnostics)
{
Expand Down Expand Up @@ -390,7 +393,7 @@ private static TypeParameterConstraintClause RemoveInvalidConstraints(

if (constraintTypeBuilder.Count < n)
{
return TypeParameterConstraintClause.Create(constraintClause.Constraints, constraintTypeBuilder.ToImmutableAndFree());
return TypeParameterConstraintClause.Create(constraintClause.Constraints, constraintTypeBuilder.ToImmutableAndFree(), constraintClause.IgnoresNullableContext);
}

constraintTypeBuilder.Free();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private static bool IsNamedTypeAccessible(NamedTypeSymbol type, Symbol within, r
{
// type parameters are always accessible, so don't check those (so common it's
// worth optimizing this).
if (typeArg.Type.Kind != SymbolKind.TypeParameter && !IsSymbolAccessibleCore(typeArg.Type, within, null, out unused, compilation, ref useSiteDiagnostics, basesBeingResolved))
Copy link
Contributor

@AlekseyTs AlekseyTs Sep 23, 2020

Choose a reason for hiding this comment

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

Type [](start = 32, length = 4)

Why not simply access DefaultType here? #Closed

if (!typeArg.IsAnnotatedOrUnannotatedTypeParameter && !IsSymbolAccessibleCore(typeArg.Type, within, null, out unused, compilation, ref useSiteDiagnostics, basesBeingResolved))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public override VarianceKind Variance
get { return VarianceKind.None; }
}

internal override void EnsureAllConstraintsAreResolved()
internal override void EnsureAllConstraintsAreResolved(bool canIgnoreNullableContext)
{
}

internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress, bool canIgnoreNullableContext)
{
return ImmutableArray<TypeWithAnnotations>.Empty;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Compilers/CSharp/Portable/Symbols/ConstraintsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ public static TypeParameterBounds ResolveBounds(
ConsList<TypeParameterSymbol> inProgress,
ImmutableArray<TypeWithAnnotations> constraintTypes,
bool inherited,
bool ignoresNullableContext,
CSharpCompilation currentCompilation,
DiagnosticBag diagnostics)
{
var diagnosticsBuilder = ArrayBuilder<TypeParameterDiagnosticInfo>.GetInstance();
ArrayBuilder<TypeParameterDiagnosticInfo> useSiteDiagnosticsBuilder = null;
var bounds = typeParameter.ResolveBounds(corLibrary, inProgress, constraintTypes, inherited, currentCompilation, diagnosticsBuilder, ref useSiteDiagnosticsBuilder);
var bounds = typeParameter.ResolveBounds(corLibrary, inProgress, constraintTypes, inherited, ignoresNullableContext: ignoresNullableContext, currentCompilation, diagnosticsBuilder, ref useSiteDiagnosticsBuilder);

if (useSiteDiagnosticsBuilder != null)
{
Expand All @@ -97,6 +98,7 @@ public static TypeParameterBounds ResolveBounds(
ConsList<TypeParameterSymbol> inProgress,
ImmutableArray<TypeWithAnnotations> constraintTypes,
bool inherited,
bool ignoresNullableContext,
CSharpCompilation currentCompilation,
ArrayBuilder<TypeParameterDiagnosticInfo> diagnosticsBuilder,
ref ArrayBuilder<TypeParameterDiagnosticInfo> useSiteDiagnosticsBuilder)
Expand Down Expand Up @@ -295,7 +297,7 @@ public static TypeParameterBounds ResolveBounds(
return null;
}

var bounds = new TypeParameterBounds(constraintTypes, interfaces, effectiveBaseClass, deducedBaseType);
var bounds = new TypeParameterBounds(constraintTypes, interfaces, effectiveBaseClass, deducedBaseType, ignoresNullableContext);
Copy link
Contributor

@AlekseyTs AlekseyTs Sep 24, 2020

Choose a reason for hiding this comment

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

ignoresNullableContext [](start = 115, length = 22)

It doesn't look like this value actually affects other content in the TypeParameterBounds instance. Why not always use false and and getting rid of the parameter. #Closed


// Additional constraint checks for overrides.
if (inherited)
Expand All @@ -312,7 +314,6 @@ internal static ImmutableArray<TypeParameterConstraintClause> MakeTypeParameterC
ImmutableArray<TypeParameterSymbol> typeParameters,
TypeParameterListSyntax typeParameterList,
SyntaxList<TypeParameterConstraintClauseSyntax> constraintClauses,
Location location,
DiagnosticBag diagnostics)
{
if (typeParameters.Length == 0)
Expand All @@ -334,6 +335,7 @@ internal static ImmutableArray<TypeParameterConstraintClause> MakeTypeParameterC

IReadOnlyDictionary<TypeParameterSymbol, bool> isValueTypeOverride = null;
return binder.BindTypeParameterConstraintClauses(containingSymbol, typeParameters, typeParameterList, constraintClauses,
canIgnoreNullableContext: false,
ref isValueTypeOverride,
diagnostics);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ public override bool IsImplicitlyDeclared
}
}

internal override void EnsureAllConstraintsAreResolved()
internal override void EnsureAllConstraintsAreResolved(bool canIgnoreNullableContext)
{
}

internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress, bool canIgnoreNullableContext)
{
return ImmutableArray<TypeWithAnnotations>.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,18 +568,19 @@ public override VarianceKind Variance
}
}

internal override void EnsureAllConstraintsAreResolved()
internal override void EnsureAllConstraintsAreResolved(bool canIgnoreNullableContext)
{
if (!_lazyBounds.IsSet())
canIgnoreNullableContext = false; // Resolve bounds eagerly.
if (!_lazyBounds.HasValue(canIgnoreNullableContext))
{
var typeParameters = (_containingSymbol.Kind == SymbolKind.Method) ?
((PEMethodSymbol)_containingSymbol).TypeParameters :
((PENamedTypeSymbol)_containingSymbol).TypeParameters;
EnsureAllConstraintsAreResolved(typeParameters);
EnsureAllConstraintsAreResolved(typeParameters, canIgnoreNullableContext);
}
}

internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress, bool canIgnoreNullableContext)
{
var bounds = this.GetBounds(inProgress);
return (bounds != null) ? bounds.ConstraintTypes : ImmutableArray<TypeWithAnnotations>.Empty;
Expand Down Expand Up @@ -634,7 +635,7 @@ private TypeParameterBounds GetBounds(ConsList<TypeParameterSymbol> inProgress)
var diagnostics = ArrayBuilder<TypeParameterDiagnosticInfo>.GetInstance();
ArrayBuilder<TypeParameterDiagnosticInfo> useSiteDiagnosticsBuilder = null;
bool inherited = (_containingSymbol.Kind == SymbolKind.Method) && ((MethodSymbol)_containingSymbol).IsOverride;
var bounds = this.ResolveBounds(this.ContainingAssembly.CorLibrary, inProgress.Prepend(this), constraintTypes, inherited, currentCompilation: null,
var bounds = this.ResolveBounds(this.ContainingAssembly.CorLibrary, inProgress.Prepend(this), constraintTypes, inherited, ignoresNullableContext: false, currentCompilation: null,
diagnosticsBuilder: diagnostics, useSiteDiagnosticsBuilder: ref useSiteDiagnosticsBuilder);
DiagnosticInfo errorInfo = null;

Expand Down Expand Up @@ -670,7 +671,7 @@ private TypeParameterBounds GetBounds(ConsList<TypeParameterSymbol> inProgress)

internal override DiagnosticInfo GetConstraintsUseSiteErrorInfo()
{
EnsureAllConstraintsAreResolved();
EnsureAllConstraintsAreResolved(canIgnoreNullableContext: false);
Debug.Assert(!ReferenceEquals(_lazyConstraintsUseSiteErrorInfo, CSDiagnosticInfo.EmptyErrorInfo));
return _lazyConstraintsUseSiteErrorInfo;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Symbols/NamedTypeSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal ImmutableArray<TypeWithAnnotations> TypeArgumentsWithDefinitionUseSiteD

foreach (var typeArgument in result)
{
typeArgument.Type.OriginalDefinition.AddUseSiteDiagnostics(ref useSiteDiagnostics);
Copy link
Contributor

@AlekseyTs AlekseyTs Sep 23, 2020

Choose a reason for hiding this comment

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

Type [](start = 29, length = 4)

Why not simply access DefaultType here? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be good to avoid repeating type.Default.OriginalDefinition.AddUseSiteDiagnostics(). Moved the helper method here.


In reply to: 493955260 [](ancestors = 493955260)

typeArgument.AddUseSiteDiagnostics(ref useSiteDiagnostics);
}

return result;
Expand All @@ -72,7 +72,7 @@ internal ImmutableArray<TypeWithAnnotations> TypeArgumentsWithDefinitionUseSiteD
internal TypeWithAnnotations TypeArgumentWithDefinitionUseSiteDiagnostics(int index, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
{
var result = TypeArgumentsWithAnnotationsNoUseSiteDiagnostics[index];
result.Type.OriginalDefinition.AddUseSiteDiagnostics(ref useSiteDiagnostics);
Copy link
Contributor

@AlekseyTs AlekseyTs Sep 23, 2020

Choose a reason for hiding this comment

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

Type [](start = 19, length = 4)

Why not simply access DefaultType here? #Closed

result.AddUseSiteDiagnostics(ref useSiteDiagnostics);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ internal override ModuleSymbol ContainingModule
}
}

internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress, bool canIgnoreNullableContext)
{
return this.RetargetingTranslator.Retarget(_underlyingTypeParameter.GetConstraintTypes(inProgress));
return this.RetargetingTranslator.Retarget(_underlyingTypeParameter.GetConstraintTypes(inProgress, canIgnoreNullableContext));
}

internal override bool? IsNotNullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
}
}

internal override void EnsureAllConstraintsAreResolved()
internal override void EnsureAllConstraintsAreResolved(bool canIgnoreNullableContext)
{
}

internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress, bool canIgnoreNullableContext)
{
return ImmutableArray<TypeWithAnnotations>.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
}
}

internal override void EnsureAllConstraintsAreResolved()
internal override void EnsureAllConstraintsAreResolved(bool canIgnoreNullableContext)
{
}

internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress, bool canIgnoreNullableContext)
{
return ImmutableArray<TypeWithAnnotations>.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ public override ImmutableArray<TypeParameterConstraintClause> GetTypeParameterCo
TypeParameters,
syntax.TypeParameterList,
syntax.ConstraintClauses,
syntax.Identifier.GetLocation(),
diagnostics);
lock (_declarationDiagnostics)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,18 @@ private ImmutableArray<TypeParameterSymbol> MakeTypeParameters(DiagnosticBag dia
/// <summary>
/// Returns the constraint clause for the given type parameter.
/// </summary>
internal TypeParameterConstraintClause GetTypeParameterConstraintClause(int ordinal)
internal TypeParameterConstraintClause GetTypeParameterConstraintClause(bool canIgnoreNullableContext, int ordinal)
{
var clauses = _lazyTypeParameterConstraints;
if (clauses.IsDefault)
if (!clauses.HasValue(canIgnoreNullableContext))
{
var diagnostics = DiagnosticBag.GetInstance();
if (ImmutableInterlocked.InterlockedInitialize(ref _lazyTypeParameterConstraints, MakeTypeParameterConstraints(diagnostics)))
if (ImmutableInterlocked.InterlockedCompareExchange(ref _lazyTypeParameterConstraints, MakeTypeParameterConstraints(canIgnoreNullableContext, diagnostics), clauses) == clauses)
Copy link
Contributor

@AlekseyTs AlekseyTs Sep 23, 2020

Choose a reason for hiding this comment

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

InterlockedCompareExchange [](start = 41, length = 26)

Is there a possibility of a race here? If a thread with canIgnoreNullableContext==true completes first, is the thread with canIgnoreNullableContext==false going to return expected value? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks.


In reply to: 493779200 [](ancestors = 493779200)

{
this.AddDeclarationDiagnostics(diagnostics);
if (_lazyTypeParameterConstraints.HasValue(canIgnoreNullableContext: false))
{
this.AddDeclarationDiagnostics(diagnostics);
}
}
diagnostics.Free();
clauses = _lazyTypeParameterConstraints;
Expand All @@ -268,7 +271,7 @@ internal TypeParameterConstraintClause GetTypeParameterConstraintClause(int ordi
return (clauses.Length > 0) ? clauses[ordinal] : TypeParameterConstraintClause.Empty;
}

private ImmutableArray<TypeParameterConstraintClause> MakeTypeParameterConstraints(DiagnosticBag diagnostics)
private ImmutableArray<TypeParameterConstraintClause> MakeTypeParameterConstraints(bool canIgnoreNullableContext, DiagnosticBag diagnostics)
{
var typeParameters = this.TypeParameters;
var results = ImmutableArray<TypeParameterConstraintClause>.Empty;
Expand Down Expand Up @@ -317,9 +320,10 @@ private ImmutableArray<TypeParameterConstraintClause> MakeTypeParameterConstrain
// Wrap binder from factory in a generic constraints specific binder
// to avoid checking constraints when binding type names.
Debug.Assert(!binder.Flags.Includes(BinderFlags.GenericConstraintsClause));
binder = binder.WithContainingMemberOrLambda(this).WithAdditionalFlags(BinderFlags.GenericConstraintsClause | BinderFlags.SuppressConstraintChecks);
binder = binder.WithContainingMemberOrLambda(this).WithAdditionalFlags(
BinderFlags.GenericConstraintsClause | BinderFlags.SuppressConstraintChecks | (canIgnoreNullableContext ? BinderFlags.IgnoreNullableContext : 0));

constraints = binder.BindTypeParameterConstraintClauses(this, typeParameters, typeParameterList, constraintClauses, ref isValueTypeOverride, diagnostics);
constraints = binder.BindTypeParameterConstraintClauses(this, typeParameters, typeParameterList, constraintClauses, canIgnoreNullableContext, ref isValueTypeOverride, diagnostics);
}

Debug.Assert(constraints.Length == arity);
Expand Down Expand Up @@ -438,7 +442,7 @@ private ImmutableArray<TypeParameterConstraintClause> MergeConstraintsForPartial
}

builder[i] = TypeParameterConstraintClause.Create(mergedKind,
mergedConstraintTypes?.ToImmutableAndFree() ?? originalConstraintTypes);
mergedConstraintTypes?.ToImmutableAndFree() ?? originalConstraintTypes, ignoresNullableContext: constraint.IgnoresNullableContext);
}
}

Expand Down
Loading