Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
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
34 changes: 20 additions & 14 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ partial class Binder
PropertySymbol? valueProperty = null;
foreach (var m in iUnion.GetMembers(WellKnownMemberNames.ValuePropertyName))
{
if (m is PropertySymbol prop && HasIUnionValueSignature(prop))
if (m is PropertySymbol prop && hasIUnionValueSignature(prop))
{
valueProperty = prop;
break;
Expand All @@ -59,22 +59,28 @@ partial class Binder
}

return valueProperty;

static bool hasIUnionValueSignature(PropertySymbol property)
{
// PROTOTYPE: Cover individual conditions with tests
return property is
{
IsStatic: false,
DeclaredAccessibility: Accessibility.Public,
IsAbstract: true,
GetMethod: not null,
SetMethod: null,
RefKind: RefKind.None,
ParameterCount: 0,
TypeWithAnnotations: { Type.SpecialType: SpecialType.System_Object, CustomModifiers: [] }
};
}
}

public static bool HasIUnionValueSignature(PropertySymbol property)
internal static PropertySymbol? GetUnionTypeValuePropertyNoUseSiteDiagnostics(NamedTypeSymbol inputUnionType)
{
// PROTOTYPE: Cover individual conditions with tests
return property is
{
IsStatic: false,
DeclaredAccessibility: Accessibility.Public,
IsAbstract: true,
GetMethod: not null,
SetMethod: null,
RefKind: RefKind.None,
ParameterCount: 0,
TypeWithAnnotations: { Type.SpecialType: SpecialType.System_Object, CustomModifiers: [] }
};
var useSiteInfo = CompoundUseSiteInfo<AssemblySymbol>.Discarded;
return GetUnionTypeValueProperty(inputUnionType, ref useSiteInfo);
}

private BoundExpression BindIsPatternExpression(IsPatternExpressionSyntax node, BindingDiagnosticBag diagnostics)
Expand Down
5 changes: 3 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/PatternExplainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,9 @@ string tryHandleRecursivePattern(ref bool unnamedEnumValue)
var subInput = new BoundDagTemp(e.Syntax, e.Property.Type, e);
var subPattern = SamplePatternForTemp(subInput, constraintMap, evaluationMap, false, ref unnamedEnumValue);

if (evaluations.Length == 1 && e.Property is { Name: WellKnownMemberNames.ValuePropertyName } &&
Binder.HasIUnionValueSignature(e.Property) && e.Input.Type is NamedTypeSymbol { IsUnionTypeNoUseSiteDiagnostics: true })
if (evaluations.Length == 1 && e.Property is { Name: WellKnownMemberNames.ValuePropertyName } property &&
e.Input.Type is NamedTypeSymbol { IsUnionTypeNoUseSiteDiagnostics: true } unionType &&
Binder.GetUnionTypeValuePropertyNoUseSiteDiagnostics(unionType) == (object)property)
{
return subPattern;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1908,8 +1908,9 @@ public bool HasAnyNullabilityImplicitConversion(TypeWithAnnotations source, Type
{
Debug.Assert(IncludeNullability);
var discardedUseSiteInfo = CompoundUseSiteInfo<AssemblySymbol>.Discarded;
return HasTopLevelNullabilityImplicitConversion(source, destination) &&
ClassifyImplicitConversionFromType(source.Type, destination.Type, ref discardedUseSiteInfo).Kind != ConversionKind.NoConversion;
Conversion conversion = ClassifyImplicitConversionFromType(source.Type, destination.Type, ref discardedUseSiteInfo);
return conversion.Kind != ConversionKind.NoConversion &&
(conversion.IsUnion || conversion.IsUserDefined || HasTopLevelNullabilityImplicitConversion(source, destination));
}

private static bool HasIdentityConversionToAny(NamedTypeSymbol type, ArrayBuilder<(NamedTypeSymbol ParticipatingType, TypeParameterSymbol ConstrainedToTypeOpt)> targetTypes)
Expand Down Expand Up @@ -2382,11 +2383,14 @@ private Conversion ClassifyImplicitTupleConversion(TypeSymbol source, TypeSymbol
ConversionKind.ImplicitTuple,
(ConversionsBase conversions, TypeWithAnnotations s, TypeWithAnnotations d, bool _, ref CompoundUseSiteInfo<AssemblySymbol> u, bool _) =>
{
if (!conversions.HasTopLevelNullabilityImplicitConversion(s, d))
Conversion conversion = conversions.ClassifyImplicitConversionFromType(s.Type, d.Type, ref u);

if (!conversion.IsUserDefined && !conversion.IsUnion && !conversions.HasTopLevelNullabilityImplicitConversion(s, d))
{
return Conversion.NoConversion;
}
return conversions.ClassifyImplicitConversionFromType(s.Type, d.Type, ref u);

return conversion;
},
isChecked: false,
forCast: false);
Expand All @@ -2401,11 +2405,14 @@ private Conversion ClassifyExplicitTupleConversion(TypeSymbol source, TypeSymbol
ConversionKind.ExplicitTuple,
(ConversionsBase conversions, TypeWithAnnotations s, TypeWithAnnotations d, bool isChecked, ref CompoundUseSiteInfo<AssemblySymbol> u, bool forCast) =>
{
if (!conversions.HasTopLevelNullabilityImplicitConversion(s, d))
Conversion conversion = conversions.ClassifyConversionFromType(s.Type, d.Type, isChecked: isChecked, ref u, forCast);

if (!conversion.IsUserDefined && !conversion.IsUnion && !conversions.HasTopLevelNullabilityImplicitConversion(s, d))
{
return Conversion.NoConversion;
}
return conversions.ClassifyConversionFromType(s.Type, d.Type, isChecked: isChecked, ref u, forCast);

return conversion;
},
isChecked: isChecked,
forCast);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3048,12 +3048,8 @@ private static bool ImplicitConversionExists(TypeWithAnnotations sourceWithAnnot
return false;
}

if (!conversions.HasTopLevelNullabilityImplicitConversion(sourceWithAnnotations, destinationWithAnnotations))
{
return false;
}

return conversions.ClassifyImplicitConversionFromTypeWhenNeitherOrBothFunctionTypes(source, destination, ref useSiteInfo).Exists;
Conversion conversion = conversions.ClassifyImplicitConversionFromTypeWhenNeitherOrBothFunctionTypes(source, destination, ref useSiteInfo);
return conversion.Exists && (conversion.IsUnion || conversion.IsUserDefined || conversions.HasTopLevelNullabilityImplicitConversion(sourceWithAnnotations, destinationWithAnnotations));
}
#nullable disable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ private static BoundPatternWithUnionMatching CreatePatternWithUnionMatching(Name
Debug.Assert(inputUnionType.IsUnionTypeNoUseSiteDiagnostics);
Debug.Assert(innerPattern.InputType.IsObjectType());

var discardedUseSiteInfo = CompoundUseSiteInfo<AssemblySymbol>.Discarded;
PropertySymbol? valueProperty = Binder.GetUnionTypeValueProperty(inputUnionType, ref discardedUseSiteInfo);
PropertySymbol? valueProperty = Binder.GetUnionTypeValuePropertyNoUseSiteDiagnostics(inputUnionType);

var member = new BoundPropertySubpatternMember(innerPattern.Syntax, receiver: null, valueProperty, type: innerPattern.InputType, hasErrors: valueProperty is null).MakeCompilerGenerated();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,8 @@ BoundDeclarationPattern or BoundConstantPattern or BoundNegatedPattern or BoundB
VariableAccess: null,
IsUnionMatching: false,
} rewritten &&
(possibleUnionValueSymbol is null || (Binder.HasIUnionValueSignature((PropertySymbol)possibleUnionValueSymbol) && possibleUnionValueSymbol.ContainingType.IsWellKnownTypeIUnion())))
(possibleUnionValueSymbol is null ||
(possibleUnionValueSymbol.ContainingType.IsWellKnownTypeIUnion() && Binder.GetUnionTypeValuePropertyNoUseSiteDiagnostics(inputType) == (object)possibleUnionValueSymbol)))
{
Debug.Assert(!inputType.IsNullableType());
Debug.Assert(!negated);
Expand Down
Loading