Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal override bool? ReferenceTypeConstraintIsNullable

public override bool HasNotNullConstraint => false;

internal override bool? IsNotNullableIfReferenceType => null;
internal override bool? IsNotNullable => null;

public override bool HasValueTypeConstraint
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Symbols/ConstraintsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ private static bool CheckConstraints(
{
if (!SatisfiesConstraintType(conversions.WithNullability(true), typeArgument, constraintType, ref useSiteDiagnostics) ||
(typeArgument.GetValueNullableAnnotation().IsAnnotated() && !typeArgument.Type.IsNonNullableValueType() &&
TypeParameterSymbol.IsNotNullableIfReferenceTypeFromConstraintType(constraintType, out _) == true))
TypeParameterSymbol.IsNotNullableFromConstraintType(constraintType, out _) == true))
{
var diagnostic = new CSDiagnosticInfo(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, containingSymbol.ConstructedFrom(), constraintType, typeParameter, typeArgument);
nullabilityDiagnosticsBuilderOpt.Add(new TypeParameterDiagnosticInfo(typeParameter, diagnostic));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal override bool? ReferenceTypeConstraintIsNullable

public override bool HasNotNullConstraint => false;

internal override bool? IsNotNullableIfReferenceType => null;
internal override bool? IsNotNullable => null;

public override bool HasValueTypeConstraint
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,10 @@ public static bool HaveSameNullabilityInConstraints(TypeParameterSymbol typePara
{
if (!typeParameter1.IsValueType)
{
bool? isNotNullableIfReferenceType1 = typeParameter1.IsNotNullableIfReferenceType;
bool? isNotNullableIfReferenceType2 = typeParameter2.IsNotNullableIfReferenceType;
if (isNotNullableIfReferenceType1.HasValue && isNotNullableIfReferenceType2.HasValue &&
isNotNullableIfReferenceType1.GetValueOrDefault() != isNotNullableIfReferenceType2.GetValueOrDefault())
bool? isNotNullable1 = typeParameter1.IsNotNullable;
bool? isNotNullable2 = typeParameter2.IsNotNullable;
if (isNotNullable1.HasValue && isNotNullable2.HasValue &&
isNotNullable1.GetValueOrDefault() != isNotNullable2.GetValueOrDefault())
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private ImmutableArray<TypeWithAnnotations> GetDeclaredConstraintTypes(ConsList<
inProgress = inProgress.Prepend(this);
foreach (TypeWithAnnotations constraintType in symbolsBuilder)
{
if (!ConstraintsHelper.IsObjectConstraintSignificant(IsNotNullableIfReferenceTypeFromConstraintType(constraintType, inProgress, out _), bestObjectConstraint))
if (!ConstraintsHelper.IsObjectConstraintSignificant(IsNotNullableFromConstraintType(constraintType, inProgress, out _), bestObjectConstraint))
{
bestObjectConstraint = default;
break;
Expand Down Expand Up @@ -277,36 +277,36 @@ private TypeWithAnnotations GetConstraintTypeOrDefault(PEModuleSymbol moduleSymb
return type;
}

private static bool? IsNotNullableIfReferenceTypeFromConstraintType(TypeWithAnnotations constraintType, ConsList<PETypeParameterSymbol> inProgress, out bool isNonNullableValueType)
private static bool? IsNotNullableFromConstraintType(TypeWithAnnotations constraintType, ConsList<PETypeParameterSymbol> inProgress, out bool isNonNullableValueType)
{
if (!(constraintType.Type is PETypeParameterSymbol typeParameter) ||
(object)typeParameter.ContainingSymbol != inProgress.Head.ContainingSymbol ||
typeParameter.GetConstraintHandleCollection().Count == 0)
{
return IsNotNullableIfReferenceTypeFromConstraintType(constraintType, out isNonNullableValueType);
return IsNotNullableFromConstraintType(constraintType, out isNonNullableValueType);
}

bool? isNotNullableIfReferenceType = typeParameter.CalculateIsNotNullableIfReferenceType(inProgress, out isNonNullableValueType);
bool? isNotNullable = typeParameter.CalculateIsNotNullable(inProgress, out isNonNullableValueType);

if (isNonNullableValueType)
{
Debug.Assert(isNotNullableIfReferenceType == true);
Debug.Assert(isNotNullable == true);
return true;
}

if (constraintType.NullableAnnotation.IsAnnotated() || isNotNullableIfReferenceType == false)
if (constraintType.NullableAnnotation.IsAnnotated() || isNotNullable == false)
{
return false;
}
else if (constraintType.NullableAnnotation.IsOblivious() || isNotNullableIfReferenceType == null)
else if (constraintType.NullableAnnotation.IsOblivious() || isNotNullable == null)
{
return null;
}

return true;
}

private bool? CalculateIsNotNullableIfReferenceType(ConsList<PETypeParameterSymbol> inProgress, out bool isNonNullableValueType)
private bool? CalculateIsNotNullable(ConsList<PETypeParameterSymbol> inProgress, out bool isNonNullableValueType)
{
if (inProgress.ContainsReference(this))
{
Expand All @@ -330,7 +330,7 @@ private TypeWithAnnotations GetConstraintTypeOrDefault(PEModuleSymbol moduleSymb
return fromNonTypeConstraints;
}

bool? fromTypes = IsNotNullableIfReferenceTypeFromConstraintTypes(constraintTypes, inProgress, out isNonNullableValueType);
bool? fromTypes = IsNotNullableFromConstraintTypes(constraintTypes, inProgress, out isNonNullableValueType);

if (isNonNullableValueType)
{
Expand All @@ -348,15 +348,15 @@ private TypeWithAnnotations GetConstraintTypeOrDefault(PEModuleSymbol moduleSymb
return fromNonTypeConstraints;
}

private static bool? IsNotNullableIfReferenceTypeFromConstraintTypes(ImmutableArray<TypeWithAnnotations> constraintTypes, ConsList<PETypeParameterSymbol> inProgress, out bool isNonNullableValueType)
private static bool? IsNotNullableFromConstraintTypes(ImmutableArray<TypeWithAnnotations> constraintTypes, ConsList<PETypeParameterSymbol> inProgress, out bool isNonNullableValueType)
{
Debug.Assert(!constraintTypes.IsDefaultOrEmpty);

isNonNullableValueType = false;
bool? result = false;
foreach (TypeWithAnnotations constraintType in constraintTypes)
{
bool? fromType = IsNotNullableIfReferenceTypeFromConstraintType(constraintType, inProgress, out isNonNullableValueType);
bool? fromType = IsNotNullableFromConstraintType(constraintType, inProgress, out isNonNullableValueType);

if (isNonNullableValueType)
{
Expand Down Expand Up @@ -466,7 +466,7 @@ public override bool HasNotNullConstraint
}
}

internal override bool? IsNotNullableIfReferenceType
internal override bool? IsNotNullable
{
get
{
Expand Down Expand Up @@ -507,11 +507,11 @@ internal override bool? IsNotNullableIfReferenceType
symbolsBuilder.Add(type);
}

return IsNotNullableIfReferenceTypeFromConstraintTypes(symbolsBuilder.ToImmutableAndFree());
return IsNotNullableFromConstraintTypes(symbolsBuilder.ToImmutableAndFree());
}
}

return CalculateIsNotNullableIfReferenceType();
return CalculateIsNotNullable();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsLis
return this.RetargetingTranslator.Retarget(_underlyingTypeParameter.GetConstraintTypes(inProgress));
}

internal override bool? IsNotNullableIfReferenceType
internal override bool? IsNotNullable
{
get
{
return _underlyingTypeParameter.IsNotNullableIfReferenceType;
return _underlyingTypeParameter.IsNotNullable;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ internal override bool? ReferenceTypeConstraintIsNullable

public override bool HasNotNullConstraint => false;

internal override bool? IsNotNullableIfReferenceType => null;
internal override bool? IsNotNullable => null;

public override bool HasUnmanagedTypeConstraint
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal override bool? ReferenceTypeConstraintIsNullable

public override bool HasNotNullConstraint => false;

internal override bool? IsNotNullableIfReferenceType => null;
internal override bool? IsNotNullable => null;

public override bool HasUnmanagedTypeConstraint
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ internal bool ConstraintsNeedNullableAttribute()
return !this.HasReferenceTypeConstraint &&
!this.HasValueTypeConstraint &&
this.ConstraintTypesNoUseSiteDiagnostics.IsEmpty &&
this.IsNotNullableIfReferenceType == false;
this.IsNotNullable == false;
}

private NamedTypeSymbol GetDefaultBaseType()
Expand Down Expand Up @@ -414,7 +414,7 @@ private byte GetSynthesizedNullableAttributeValue()
{
return NullableAnnotationExtensions.NotAnnotatedAttributeValue;
}
else if (!this.HasValueTypeConstraint && this.ConstraintTypesNoUseSiteDiagnostics.IsEmpty && this.IsNotNullableIfReferenceType == false)
else if (!this.HasValueTypeConstraint && this.ConstraintTypesNoUseSiteDiagnostics.IsEmpty && this.IsNotNullable == false)
{
return NullableAnnotationExtensions.AnnotatedAttributeValue;
}
Expand Down Expand Up @@ -531,7 +531,7 @@ public override bool HasNotNullConstraint
}
}

internal override bool? IsNotNullableIfReferenceType
internal override bool? IsNotNullable
{
get
{
Expand All @@ -540,7 +540,7 @@ internal override bool? IsNotNullableIfReferenceType
return null;
}

return CalculateIsNotNullableIfReferenceType();
return CalculateIsNotNullable();
}
}

Expand Down Expand Up @@ -652,7 +652,7 @@ internal override bool? ReferenceTypeConstraintIsNullable
}
}

internal override bool? IsNotNullableIfReferenceType
internal override bool? IsNotNullable
{
get
{
Expand All @@ -661,7 +661,7 @@ internal override bool? IsNotNullableIfReferenceType
return null;
}

return CalculateIsNotNullableIfReferenceType();
return CalculateIsNotNullable();
}
}

Expand Down Expand Up @@ -892,11 +892,11 @@ public override bool HasNotNullConstraint
}
}

internal override bool? IsNotNullableIfReferenceType
internal override bool? IsNotNullable
{
get
{
return this.OverriddenTypeParameter?.IsNotNullableIfReferenceType;
return this.OverriddenTypeParameter?.IsNotNullable;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsLis
{
foreach (TypeWithAnnotations constraintType in constraintTypes)
{
if (!ConstraintsHelper.IsObjectConstraintSignificant(IsNotNullableIfReferenceTypeFromConstraintType(constraintType, out _), bestObjectConstraint))
if (!ConstraintsHelper.IsObjectConstraintSignificant(IsNotNullableFromConstraintType(constraintType, out _), bestObjectConstraint))
{
bestObjectConstraint = default;
break;
Expand All @@ -149,22 +149,22 @@ internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsLis
return constraintTypes.ToImmutableAndFree();
}

internal override bool? IsNotNullableIfReferenceType
internal override bool? IsNotNullable
{
get
{
if (_underlyingTypeParameter.ConstraintTypesNoUseSiteDiagnostics.IsEmpty)
{
return _underlyingTypeParameter.IsNotNullableIfReferenceType;
return _underlyingTypeParameter.IsNotNullable;
}
else if (!HasNotNullConstraint && !HasValueTypeConstraint && !HasReferenceTypeConstraint)
{
var constraintTypes = ArrayBuilder<TypeWithAnnotations>.GetInstance();
_map.SubstituteConstraintTypesDistinctWithoutModifiers(_underlyingTypeParameter, _underlyingTypeParameter.GetConstraintTypes(ConsList<TypeParameterSymbol>.Empty), constraintTypes, null);
return IsNotNullableIfReferenceTypeFromConstraintTypes(constraintTypes.ToImmutableAndFree());
return IsNotNullableFromConstraintTypes(constraintTypes.ToImmutableAndFree());
}

return CalculateIsNotNullableIfReferenceType();
return CalculateIsNotNullable();
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/Compilers/CSharp/Portable/Symbols/TypeParameterSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,14 @@ internal static bool IsReferenceTypeFromConstraintTypes(ImmutableArray<TypeWithA
return false;
}

internal static bool? IsNotNullableIfReferenceTypeFromConstraintTypes(ImmutableArray<TypeWithAnnotations> constraintTypes)
internal static bool? IsNotNullableFromConstraintTypes(ImmutableArray<TypeWithAnnotations> constraintTypes)
{
Debug.Assert(!constraintTypes.IsDefaultOrEmpty);

bool? result = false;
foreach (TypeWithAnnotations constraintType in constraintTypes)
{
bool? fromType = IsNotNullableIfReferenceTypeFromConstraintType(constraintType, out _);
bool? fromType = IsNotNullableFromConstraintType(constraintType, out _);
if (fromType == true)
{
return true;
Expand All @@ -451,7 +451,7 @@ internal static bool IsReferenceTypeFromConstraintTypes(ImmutableArray<TypeWithA
return result;
}

internal static bool? IsNotNullableIfReferenceTypeFromConstraintType(TypeWithAnnotations constraintType, out bool isNonNullableValueType)
internal static bool? IsNotNullableFromConstraintType(TypeWithAnnotations constraintType, out bool isNonNullableValueType)
{
if (constraintType.Type.IsNonNullableValueType())
{
Expand All @@ -468,13 +468,13 @@ internal static bool IsReferenceTypeFromConstraintTypes(ImmutableArray<TypeWithA

if (constraintType.TypeKind == TypeKind.TypeParameter)
{
bool? isNotNullableIfReferenceType = ((TypeParameterSymbol)constraintType.Type).IsNotNullableIfReferenceType;
bool? isNotNullable = ((TypeParameterSymbol)constraintType.Type).IsNotNullable;

if (isNotNullableIfReferenceType == false)
if (isNotNullable == false)
{
return false;
}
else if (isNotNullableIfReferenceType == null)
else if (isNotNullable == null)
{
return null;
}
Expand Down Expand Up @@ -528,7 +528,7 @@ public sealed override bool IsReferenceType
return false;
}

protected bool? CalculateIsNotNullableIfReferenceType()
protected bool? CalculateIsNotNullable()
{
bool? fromNonTypeConstraints = CalculateIsNotNullableFromNonTypeConstraints();

Expand All @@ -544,7 +544,7 @@ public sealed override bool IsReferenceType
return fromNonTypeConstraints;
}

bool? fromTypes = IsNotNullableIfReferenceTypeFromConstraintTypes(constraintTypes);
bool? fromTypes = IsNotNullableFromConstraintTypes(constraintTypes);

if (fromTypes == true || fromNonTypeConstraints == false)
{
Expand All @@ -557,7 +557,7 @@ public sealed override bool IsReferenceType
}

// https://github.com/dotnet/roslyn/issues/26198 Should this API be exposed through ITypeParameterSymbol?
internal abstract bool? IsNotNullableIfReferenceType { get; }
internal abstract bool? IsNotNullable { get; }
Copy link
Member

Choose a reason for hiding this comment

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

IsNotNullable [](start = 32, length = 13)

nit: Consider adding an xml doc while you're here


public sealed override bool IsValueType
{
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static bool IsTypeParameterDisallowingAnnotation(this TypeSymbol type)
var typeParameter = (TypeParameterSymbol)type;
// https://github.com/dotnet/roslyn/issues/30056: Test `where T : unmanaged`. See
// UninitializedNonNullableFieldTests.TypeParameterConstraints for instance.
return !typeParameter.IsValueType && !(typeParameter.IsReferenceType && typeParameter.IsNotNullableIfReferenceType == true);
return !typeParameter.IsValueType && !(typeParameter.IsReferenceType && typeParameter.IsNotNullable == true);
}

/// <summary>
Expand All @@ -73,7 +73,7 @@ public static bool IsTypeParameterDisallowingAnnotation(this TypeSymbol type)
/// </summary>
public static bool IsPossiblyNullableReferenceTypeTypeParameter(this TypeSymbol type)
{
return type is TypeParameterSymbol { IsValueType: false, IsNotNullableIfReferenceType: false };
return type is TypeParameterSymbol { IsValueType: false, IsNotNullable: false };
}

public static bool IsNonNullableValueType(this TypeSymbol typeArgument)
Expand Down
Loading