-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix nullability checks in type parameter constraints
- Loading branch information
1 parent
f7f3e86
commit fd9faea
Showing
3 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...DependencyPropertyGenerator.SourceGenerators/Extensions/ITypeParameterSymbolExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
|
||
namespace CommunityToolkit.GeneratedDependencyProperty.Extensions; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="ITypeParameterSymbol"/> types. | ||
/// </summary> | ||
internal static class ITypeParameterSymbolExtensions | ||
{ | ||
/// <summary> | ||
/// Checks whether a given type parameter is a reference type. | ||
/// </summary> | ||
/// <param name="symbol">The input <see cref="ITypeParameterSymbol"/> instance to check.</param> | ||
/// <returns>Whether the input type parameter is a reference type.</returns> | ||
public static bool IsReferenceTypeOrIndirectlyConstrainedToReferenceType(this ITypeParameterSymbol symbol) | ||
{ | ||
// The type is definitely a reference type (e.g. it has the 'class' constraint) | ||
if (symbol.IsReferenceType) | ||
{ | ||
return true; | ||
} | ||
|
||
// The type is definitely a value type (e.g. it has the 'struct' constraint) | ||
if (symbol.IsValueType) | ||
{ | ||
return false; | ||
} | ||
|
||
foreach (ITypeSymbol constraintType in symbol.ConstraintTypes) | ||
{ | ||
// Recurse on the type parameter first (e. g. we might indirectly be getting a 'class' constraint) | ||
if (constraintType is ITypeParameterSymbol typeParameter && | ||
typeParameter.IsReferenceTypeOrIndirectlyConstrainedToReferenceType()) | ||
{ | ||
return true; | ||
} | ||
|
||
// Special constraint type that type parameters can derive from. Note that for concrete enum | ||
// types, the 'Enum' constraint isn't sufficient, they'd also have e.g. 'struct', which is | ||
// already checked before. If a type parameter only has 'Enum', then it should be considered | ||
// a reference type. | ||
if (constraintType.SpecialType is SpecialType.System_Delegate or SpecialType.System_Enum) | ||
{ | ||
return true; | ||
} | ||
|
||
// Only check for classes (an interface doesn't guarantee the type argument will be a reference type) | ||
if (constraintType.TypeKind is TypeKind.Class) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters