| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 8 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 9 | | |
| | | 10 | | using NexusLabs.Needlr.Generators.Models; |
| | | 11 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 12 | | |
| | | 13 | | namespace NexusLabs.Needlr.Generators; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Shared analyzer logic for constructor guards applied to generated-constructor |
| | | 17 | | /// fields or generated record-overload properties. |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class ConstructorGuardAnalysisHelper |
| | | 20 | | { |
| | | 21 | | private const string ConstructorGuardDefinitionAttributeName = "ConstructorGuardDefinitionAttribute"; |
| | | 22 | | private const string DefaultGuardMethodName = "Validate"; |
| | | 23 | | private const int AttributeTargetsField = 0x0100; |
| | | 24 | | private const int AttributeTargetsProperty = 0x0080; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Builds a normalized occurrence for a direct |
| | | 28 | | /// <c>ConstructorGuardAttribute</c>. |
| | | 29 | | /// </summary> |
| | | 30 | | internal static ConstructorGuardOccurrence BuildDirectGuardOccurrence( |
| | | 31 | | ISymbol member, |
| | | 32 | | ITypeSymbol memberType, |
| | | 33 | | string memberKind, |
| | | 34 | | AttributeData attribute, |
| | | 35 | | string? ineligibilityReason) |
| | | 36 | | { |
| | 102 | 37 | | if (attribute.ConstructorArguments.Length == 0) |
| | | 38 | | { |
| | 0 | 39 | | return CreateOccurrence( |
| | 0 | 40 | | member, |
| | 0 | 41 | | memberType, |
| | 0 | 42 | | memberKind, |
| | 0 | 43 | | attribute, |
| | 0 | 44 | | ConstructorGuardOccurrenceKind.BuiltInNone, |
| | 0 | 45 | | ineligibilityReason, |
| | 0 | 46 | | null, |
| | 0 | 47 | | null, |
| | 0 | 48 | | false, |
| | 0 | 49 | | false); |
| | | 50 | | } |
| | | 51 | | |
| | 102 | 52 | | var first = attribute.ConstructorArguments[0]; |
| | 102 | 53 | | if (first.Kind == TypedConstantKind.Enum && first.Value is int enumValue) |
| | | 54 | | { |
| | 33 | 55 | | var kind = enumValue == 0 |
| | 33 | 56 | | ? ConstructorGuardOccurrenceKind.BuiltInNone |
| | 33 | 57 | | : ConstructorGuardOccurrenceKind.BuiltInPositive; |
| | 33 | 58 | | return CreateOccurrence( |
| | 33 | 59 | | member, |
| | 33 | 60 | | memberType, |
| | 33 | 61 | | memberKind, |
| | 33 | 62 | | attribute, |
| | 33 | 63 | | kind, |
| | 33 | 64 | | ineligibilityReason, |
| | 33 | 65 | | null, |
| | 33 | 66 | | null, |
| | 33 | 67 | | false, |
| | 33 | 68 | | false); |
| | | 69 | | } |
| | | 70 | | |
| | 69 | 71 | | if (first.Value is ITypeSymbol guardType) |
| | | 72 | | { |
| | 69 | 73 | | var methodNameExplicit = attribute.ConstructorArguments.Length > 1 && |
| | 69 | 74 | | attribute.ConstructorArguments[1].Value is string; |
| | 69 | 75 | | var methodName = methodNameExplicit |
| | 69 | 76 | | ? (string)attribute.ConstructorArguments[1].Value! |
| | 69 | 77 | | : DefaultGuardMethodName; |
| | 69 | 78 | | return CreateOccurrence( |
| | 69 | 79 | | member, |
| | 69 | 80 | | memberType, |
| | 69 | 81 | | memberKind, |
| | 69 | 82 | | attribute, |
| | 69 | 83 | | ConstructorGuardOccurrenceKind.CustomType, |
| | 69 | 84 | | ineligibilityReason, |
| | 69 | 85 | | guardType, |
| | 69 | 86 | | methodName, |
| | 69 | 87 | | methodNameExplicit, |
| | 69 | 88 | | false); |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return CreateOccurrence( |
| | 0 | 92 | | member, |
| | 0 | 93 | | memberType, |
| | 0 | 94 | | memberKind, |
| | 0 | 95 | | attribute, |
| | 0 | 96 | | ConstructorGuardOccurrenceKind.BuiltInNone, |
| | 0 | 97 | | ineligibilityReason, |
| | 0 | 98 | | null, |
| | 0 | 99 | | null, |
| | 0 | 100 | | false, |
| | 0 | 101 | | false); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Builds a normalized occurrence for a custom guard alias usage. |
| | | 106 | | /// </summary> |
| | | 107 | | internal static ConstructorGuardOccurrence BuildAliasOccurrence( |
| | | 108 | | ISymbol member, |
| | | 109 | | ITypeSymbol memberType, |
| | | 110 | | string memberKind, |
| | | 111 | | AttributeData attribute, |
| | | 112 | | string? ineligibilityReason, |
| | | 113 | | ITypeSymbol? guardType, |
| | | 114 | | string? methodName, |
| | | 115 | | bool methodNameExplicit, |
| | | 116 | | bool guardTypeUsageIsInSourceAlias) |
| | | 117 | | { |
| | 34 | 118 | | return CreateOccurrence( |
| | 34 | 119 | | member, |
| | 34 | 120 | | memberType, |
| | 34 | 121 | | memberKind, |
| | 34 | 122 | | attribute, |
| | 34 | 123 | | ConstructorGuardOccurrenceKind.Alias, |
| | 34 | 124 | | ineligibilityReason, |
| | 34 | 125 | | guardType, |
| | 34 | 126 | | methodName, |
| | 34 | 127 | | methodNameExplicit, |
| | 34 | 128 | | guardTypeUsageIsInSourceAlias); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Resolves a <c>ConstructorGuardDefinitionAttribute</c> from an application-defined |
| | | 133 | | /// alias attribute type. |
| | | 134 | | /// </summary> |
| | | 135 | | internal static bool TryGetGuardDefinition( |
| | | 136 | | INamedTypeSymbol attributeClass, |
| | | 137 | | out ITypeSymbol? guardType, |
| | | 138 | | out string? methodName, |
| | | 139 | | out bool methodNameExplicit) |
| | | 140 | | { |
| | 506 | 141 | | foreach (var metaAttribute in attributeClass.GetAttributes()) |
| | | 142 | | { |
| | 135 | 143 | | if (metaAttribute.AttributeClass is not { } metaClass || |
| | 135 | 144 | | !GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 135 | 145 | | metaClass, |
| | 135 | 146 | | ConstructorGuardDefinitionAttributeName)) |
| | | 147 | | { |
| | | 148 | | continue; |
| | | 149 | | } |
| | | 150 | | |
| | 34 | 151 | | if (metaAttribute.ConstructorArguments.Length == 0 || |
| | 34 | 152 | | metaAttribute.ConstructorArguments[0].Value is not ITypeSymbol resolvedGuardType) |
| | | 153 | | { |
| | | 154 | | continue; |
| | | 155 | | } |
| | | 156 | | |
| | 34 | 157 | | guardType = resolvedGuardType; |
| | 34 | 158 | | methodNameExplicit = metaAttribute.ConstructorArguments.Length > 1 && |
| | 34 | 159 | | metaAttribute.ConstructorArguments[1].Value is string; |
| | 34 | 160 | | methodName = methodNameExplicit |
| | 34 | 161 | | ? (string)metaAttribute.ConstructorArguments[1].Value! |
| | 34 | 162 | | : DefaultGuardMethodName; |
| | 34 | 163 | | return true; |
| | | 164 | | } |
| | | 165 | | |
| | 101 | 166 | | guardType = null; |
| | 101 | 167 | | methodName = null; |
| | 101 | 168 | | methodNameExplicit = false; |
| | 101 | 169 | | return false; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Reports the built-in or custom guard diagnostics for one positive occurrence. |
| | | 174 | | /// </summary> |
| | | 175 | | internal static void AnalyzePositiveGuardOccurrence( |
| | | 176 | | SyntaxNodeAnalysisContext context, |
| | | 177 | | INamedTypeSymbol containingType, |
| | | 178 | | ConstructorGuardOccurrence occurrence, |
| | | 179 | | Location location) |
| | | 180 | | { |
| | 110 | 181 | | switch (occurrence.Kind) |
| | | 182 | | { |
| | | 183 | | case ConstructorGuardOccurrenceKind.BuiltInPositive: |
| | 14 | 184 | | AnalyzeBuiltInGuard(context, occurrence, location); |
| | 14 | 185 | | break; |
| | | 186 | | case ConstructorGuardOccurrenceKind.CustomType: |
| | 65 | 187 | | AnalyzeCustomGuard( |
| | 65 | 188 | | context, |
| | 65 | 189 | | containingType, |
| | 65 | 190 | | occurrence, |
| | 65 | 191 | | location, |
| | 65 | 192 | | occurrence.GuardType, |
| | 65 | 193 | | occurrence.MethodName, |
| | 65 | 194 | | occurrence.MethodNameExplicit, |
| | 65 | 195 | | ImmutableArray<ITypeSymbol>.Empty); |
| | 65 | 196 | | break; |
| | | 197 | | case ConstructorGuardOccurrenceKind.Alias: |
| | 30 | 198 | | AnalyzeAliasGuardAtUsage( |
| | 30 | 199 | | context, |
| | 30 | 200 | | containingType, |
| | 30 | 201 | | occurrence, |
| | 30 | 202 | | location); |
| | | 203 | | break; |
| | | 204 | | } |
| | 30 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Returns whether a positive guard occurrence can be emitted as a valid direct |
| | | 209 | | /// call. Used by generators to fail closed when analyzer diagnostics identify an |
| | | 210 | | /// invalid guard declaration. |
| | | 211 | | /// </summary> |
| | | 212 | | internal static bool IsPositiveGuardOccurrenceValidForGeneration( |
| | | 213 | | Compilation compilation, |
| | | 214 | | INamedTypeSymbol containingType, |
| | | 215 | | ConstructorGuardOccurrence occurrence) |
| | | 216 | | { |
| | 13 | 217 | | switch (occurrence.Kind) |
| | | 218 | | { |
| | | 219 | | case ConstructorGuardOccurrenceKind.BuiltInPositive: |
| | 5 | 220 | | if (occurrence.Attribute.ConstructorArguments.Length == 0) |
| | 0 | 221 | | return false; |
| | | 222 | | |
| | 5 | 223 | | var constant = occurrence.Attribute.ConstructorArguments[0]; |
| | 5 | 224 | | if (!IsDefinedEnumValue(constant) || |
| | 5 | 225 | | constant.Value is not int rawValue) |
| | | 226 | | { |
| | 0 | 227 | | return false; |
| | | 228 | | } |
| | | 229 | | |
| | 5 | 230 | | var kind = (BuiltInConstructorGuardKindMirror)rawValue; |
| | 5 | 231 | | return kind switch |
| | 5 | 232 | | { |
| | 5 | 233 | | BuiltInConstructorGuardKindMirror.NotNull => |
| | 3 | 234 | | CanBeRuntimeNull(occurrence.MemberType), |
| | 5 | 235 | | BuiltInConstructorGuardKindMirror.NotNullOrEmpty or |
| | 5 | 236 | | BuiltInConstructorGuardKindMirror.NotNullOrWhiteSpace => |
| | 2 | 237 | | occurrence.MemberType.SpecialType == |
| | 2 | 238 | | SpecialType.System_String, |
| | 0 | 239 | | _ => false, |
| | 5 | 240 | | }; |
| | | 241 | | case ConstructorGuardOccurrenceKind.CustomType: |
| | 4 | 242 | | return IsCustomGuardValidForGeneration( |
| | 4 | 243 | | compilation, |
| | 4 | 244 | | containingType, |
| | 4 | 245 | | occurrence, |
| | 4 | 246 | | ImmutableArray<ITypeSymbol>.Empty); |
| | | 247 | | case ConstructorGuardOccurrenceKind.Alias: |
| | 4 | 248 | | if (!TryGetForwardedArgumentTypes( |
| | 4 | 249 | | occurrence.Attribute, |
| | 4 | 250 | | out var forwardedArgumentTypes, |
| | 4 | 251 | | out _)) |
| | | 252 | | { |
| | 0 | 253 | | return false; |
| | | 254 | | } |
| | | 255 | | |
| | 4 | 256 | | return IsCustomGuardValidForGeneration( |
| | 4 | 257 | | compilation, |
| | 4 | 258 | | containingType, |
| | 4 | 259 | | occurrence, |
| | 4 | 260 | | forwardedArgumentTypes); |
| | | 261 | | default: |
| | 0 | 262 | | return true; |
| | | 263 | | } |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Reports NDLRGEN047 when an enum-valued guard argument is undefined. |
| | | 268 | | /// </summary> |
| | | 269 | | internal static bool TryReportUndefinedEnum( |
| | | 270 | | SyntaxNodeAnalysisContext context, |
| | | 271 | | Location location, |
| | | 272 | | TypedConstant constant) |
| | | 273 | | { |
| | 16 | 274 | | if (constant.Kind != TypedConstantKind.Enum || |
| | 16 | 275 | | constant.Value is not int rawValue || |
| | 16 | 276 | | constant.Type is not { } enumType) |
| | | 277 | | { |
| | 0 | 278 | | return false; |
| | | 279 | | } |
| | | 280 | | |
| | 16 | 281 | | if (IsDefinedEnumValue(constant)) |
| | 12 | 282 | | return false; |
| | | 283 | | |
| | 4 | 284 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 285 | | DiagnosticDescriptors.InvalidConstructorGuardEnumValue, |
| | 4 | 286 | | location, |
| | 4 | 287 | | rawValue, |
| | 4 | 288 | | enumType.Name)); |
| | 4 | 289 | | return true; |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Resolves the accessible static guard method compatible with a guarded member and |
| | | 294 | | /// any positional arguments forwarded from an alias usage. |
| | | 295 | | /// </summary> |
| | | 296 | | internal static GuardMethodResolution TryResolveGuardMethod( |
| | | 297 | | Compilation compilation, |
| | | 298 | | INamedTypeSymbol withinType, |
| | | 299 | | ITypeSymbol guardType, |
| | | 300 | | string methodName, |
| | | 301 | | ITypeSymbol? memberType, |
| | | 302 | | string memberKind, |
| | | 303 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 304 | | out IMethodSymbol? method, |
| | | 305 | | out string? reason, |
| | | 306 | | out GuardResolutionFailureKind failureKind) |
| | | 307 | | { |
| | 125 | 308 | | method = null; |
| | 125 | 309 | | reason = null; |
| | 125 | 310 | | failureKind = GuardResolutionFailureKind.None; |
| | | 311 | | |
| | 125 | 312 | | var candidates = guardType.GetMembers(methodName) |
| | 125 | 313 | | .OfType<IMethodSymbol>() |
| | 126 | 314 | | .Where(candidate => candidate.MethodKind == MethodKind.Ordinary) |
| | 125 | 315 | | .ToList(); |
| | 125 | 316 | | if (candidates.Count == 0) |
| | | 317 | | { |
| | 4 | 318 | | reason = $"no method named '{methodName}' was found on '{guardType.ToDisplayString()}'"; |
| | 4 | 319 | | failureKind = GuardResolutionFailureKind.General; |
| | 4 | 320 | | return GuardMethodResolution.NotFound; |
| | | 321 | | } |
| | | 322 | | |
| | 121 | 323 | | var expectedArity = memberType is null |
| | 121 | 324 | | ? (int?)null |
| | 121 | 325 | | : forwardedArgumentTypes.Length + 2; |
| | 121 | 326 | | var matches = new List<IMethodSymbol>(); |
| | | 327 | | |
| | 494 | 328 | | foreach (var candidate in candidates) |
| | | 329 | | { |
| | 126 | 330 | | if (!compilation.IsSymbolAccessibleWithin(candidate, withinType)) |
| | | 331 | | { |
| | 4 | 332 | | reason = "it is not accessible"; |
| | 4 | 333 | | failureKind = GuardResolutionFailureKind.General; |
| | 4 | 334 | | continue; |
| | | 335 | | } |
| | | 336 | | |
| | 122 | 337 | | if (!candidate.IsStatic) |
| | | 338 | | { |
| | 2 | 339 | | reason = "it is not static"; |
| | 2 | 340 | | failureKind = GuardResolutionFailureKind.General; |
| | 2 | 341 | | continue; |
| | | 342 | | } |
| | | 343 | | |
| | 120 | 344 | | if (!candidate.ReturnsVoid) |
| | | 345 | | { |
| | 2 | 346 | | reason = "it does not return void"; |
| | 2 | 347 | | failureKind = GuardResolutionFailureKind.General; |
| | 2 | 348 | | continue; |
| | | 349 | | } |
| | | 350 | | |
| | 118 | 351 | | if (candidate.Parameters.Length < 2) |
| | | 352 | | { |
| | 2 | 353 | | reason = "it does not have at least a value parameter and a trailing string parameter name"; |
| | 2 | 354 | | failureKind = GuardResolutionFailureKind.General; |
| | 2 | 355 | | continue; |
| | | 356 | | } |
| | | 357 | | |
| | 116 | 358 | | if (expectedArity.HasValue && |
| | 116 | 359 | | candidate.Parameters.Length != expectedArity.Value) |
| | | 360 | | { |
| | 2 | 361 | | reason = $"it has {candidate.Parameters.Length - 2} parameter(s) between the value and the parameter nam |
| | 2 | 362 | | failureKind = GuardResolutionFailureKind.ForwardedArgument; |
| | 2 | 363 | | continue; |
| | | 364 | | } |
| | | 365 | | |
| | 114 | 366 | | if (candidate.Parameters[candidate.Parameters.Length - 1].Type.SpecialType != |
| | 114 | 367 | | SpecialType.System_String) |
| | | 368 | | { |
| | 0 | 369 | | reason = "its last parameter is not a string parameter name"; |
| | 0 | 370 | | failureKind = GuardResolutionFailureKind.General; |
| | 0 | 371 | | continue; |
| | | 372 | | } |
| | | 373 | | |
| | 114 | 374 | | var refKindParameter = candidate.Parameters.FirstOrDefault( |
| | 390 | 375 | | parameter => parameter.RefKind != RefKind.None); |
| | 114 | 376 | | if (refKindParameter is not null) |
| | | 377 | | { |
| | 6 | 378 | | reason = $"its '{refKindParameter.Name}' parameter is passed by '{refKindParameter.RefKind.ToString().To |
| | 6 | 379 | | failureKind = GuardResolutionFailureKind.General; |
| | 6 | 380 | | continue; |
| | | 381 | | } |
| | | 382 | | |
| | 108 | 383 | | if (memberType is null) |
| | | 384 | | { |
| | 31 | 385 | | matches.Add(candidate); |
| | 31 | 386 | | continue; |
| | | 387 | | } |
| | | 388 | | |
| | 77 | 389 | | var valueParameterType = candidate.Parameters[0].Type; |
| | 77 | 390 | | var middleParameters = candidate.Parameters |
| | 77 | 391 | | .Skip(1) |
| | 77 | 392 | | .Take(candidate.Parameters.Length - 2) |
| | 77 | 393 | | .ToList(); |
| | | 394 | | |
| | | 395 | | bool isCompatible; |
| | | 396 | | string? candidateReason; |
| | | 397 | | GuardResolutionFailureKind candidateFailureKind; |
| | | 398 | | |
| | 77 | 399 | | if (candidate.IsGenericMethod) |
| | | 400 | | { |
| | 43 | 401 | | isCompatible = TryInferGenericParameterCompatibility( |
| | 43 | 402 | | candidate, |
| | 43 | 403 | | valueParameterType, |
| | 43 | 404 | | memberType, |
| | 43 | 405 | | memberKind, |
| | 43 | 406 | | middleParameters, |
| | 43 | 407 | | forwardedArgumentTypes, |
| | 43 | 408 | | compilation, |
| | 43 | 409 | | out candidateReason, |
| | 43 | 410 | | out candidateFailureKind); |
| | | 411 | | } |
| | | 412 | | else |
| | | 413 | | { |
| | 34 | 414 | | isCompatible = TryCheckNonGenericCompatibility( |
| | 34 | 415 | | memberType, |
| | 34 | 416 | | memberKind, |
| | 34 | 417 | | valueParameterType, |
| | 34 | 418 | | middleParameters, |
| | 34 | 419 | | forwardedArgumentTypes, |
| | 34 | 420 | | compilation, |
| | 34 | 421 | | out candidateReason, |
| | 34 | 422 | | out candidateFailureKind); |
| | | 423 | | } |
| | | 424 | | |
| | 77 | 425 | | if (!isCompatible) |
| | | 426 | | { |
| | 36 | 427 | | reason = candidateReason; |
| | 36 | 428 | | failureKind = candidateFailureKind; |
| | 36 | 429 | | continue; |
| | | 430 | | } |
| | | 431 | | |
| | 41 | 432 | | matches.Add(candidate); |
| | | 433 | | } |
| | | 434 | | |
| | 121 | 435 | | if (matches.Count == 1) |
| | | 436 | | { |
| | 62 | 437 | | method = matches[0]; |
| | 62 | 438 | | failureKind = GuardResolutionFailureKind.None; |
| | 62 | 439 | | return GuardMethodResolution.Found; |
| | | 440 | | } |
| | | 441 | | |
| | 59 | 442 | | if (matches.Count > 1 && memberType is not null) |
| | | 443 | | { |
| | 4 | 444 | | matches = matches |
| | 8 | 445 | | .Where(candidate => !matches.Any(other => |
| | 22 | 446 | | !SymbolEqualityComparer.Default.Equals(candidate, other) && |
| | 22 | 447 | | IsBetterGuardMethod( |
| | 22 | 448 | | other, |
| | 22 | 449 | | candidate, |
| | 22 | 450 | | memberType, |
| | 22 | 451 | | forwardedArgumentTypes, |
| | 22 | 452 | | compilation))) |
| | 4 | 453 | | .ToList(); |
| | | 454 | | } |
| | | 455 | | |
| | 59 | 456 | | if (matches.Count == 1) |
| | | 457 | | { |
| | 2 | 458 | | method = matches[0]; |
| | 2 | 459 | | failureKind = GuardResolutionFailureKind.None; |
| | 2 | 460 | | return GuardMethodResolution.Found; |
| | | 461 | | } |
| | | 462 | | |
| | 57 | 463 | | if (matches.Count > 1) |
| | | 464 | | { |
| | 3 | 465 | | failureKind = GuardResolutionFailureKind.None; |
| | 3 | 466 | | return GuardMethodResolution.Ambiguous; |
| | | 467 | | } |
| | | 468 | | |
| | 54 | 469 | | if (reason is null) |
| | | 470 | | { |
| | 0 | 471 | | reason = "no accessible static method compatible with (value, ...forwarded arguments, string parameterName) |
| | 0 | 472 | | failureKind = GuardResolutionFailureKind.General; |
| | | 473 | | } |
| | | 474 | | |
| | 54 | 475 | | return GuardMethodResolution.NotFound; |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | private static bool IsBetterGuardMethod( |
| | | 479 | | IMethodSymbol candidate, |
| | | 480 | | IMethodSymbol other, |
| | | 481 | | ITypeSymbol memberType, |
| | | 482 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 483 | | Compilation compilation) |
| | | 484 | | { |
| | 8 | 485 | | var argumentTypes = forwardedArgumentTypes.Insert(0, memberType); |
| | 8 | 486 | | var candidateIsBetter = false; |
| | 8 | 487 | | var otherIsBetter = false; |
| | | 488 | | |
| | 36 | 489 | | for (var i = 0; i < argumentTypes.Length; i++) |
| | | 490 | | { |
| | 10 | 491 | | var argumentType = argumentTypes[i]; |
| | 10 | 492 | | var candidateType = candidate.Parameters[i].Type; |
| | 10 | 493 | | var otherType = other.Parameters[i].Type; |
| | 10 | 494 | | if (SymbolEqualityComparer.Default.Equals(candidateType, otherType)) |
| | | 495 | | continue; |
| | | 496 | | |
| | 8 | 497 | | var candidateIdentity = SymbolEqualityComparer.Default.Equals( |
| | 8 | 498 | | argumentType, |
| | 8 | 499 | | candidateType); |
| | 8 | 500 | | var otherIdentity = SymbolEqualityComparer.Default.Equals( |
| | 8 | 501 | | argumentType, |
| | 8 | 502 | | otherType); |
| | 8 | 503 | | if (candidateIdentity != otherIdentity) |
| | | 504 | | { |
| | 4 | 505 | | candidateIsBetter |= candidateIdentity; |
| | 4 | 506 | | otherIsBetter |= otherIdentity; |
| | 4 | 507 | | continue; |
| | | 508 | | } |
| | | 509 | | |
| | 4 | 510 | | var candidateToOther = IsAssignableTo( |
| | 4 | 511 | | candidateType, |
| | 4 | 512 | | otherType, |
| | 4 | 513 | | compilation); |
| | 4 | 514 | | var otherToCandidate = IsAssignableTo( |
| | 4 | 515 | | otherType, |
| | 4 | 516 | | candidateType, |
| | 4 | 517 | | compilation); |
| | 4 | 518 | | candidateIsBetter |= candidateToOther && !otherToCandidate; |
| | 4 | 519 | | otherIsBetter |= otherToCandidate && !candidateToOther; |
| | | 520 | | } |
| | | 521 | | |
| | 8 | 522 | | if (candidateIsBetter != otherIsBetter) |
| | 4 | 523 | | return candidateIsBetter; |
| | | 524 | | |
| | 4 | 525 | | return !candidateIsBetter && |
| | 4 | 526 | | !candidate.IsGenericMethod && |
| | 4 | 527 | | other.IsGenericMethod; |
| | | 528 | | } |
| | | 529 | | |
| | | 530 | | /// <summary> |
| | | 531 | | /// Returns why an alias definition cannot target constructor-guard members. |
| | | 532 | | /// </summary> |
| | | 533 | | internal static string? GetGuardDefinitionTargetInvalidReason( |
| | | 534 | | INamedTypeSymbol attributeClass) |
| | | 535 | | { |
| | 38 | 536 | | if (!InheritsFromSystemAttribute(attributeClass)) |
| | 2 | 537 | | return "not derived from System.Attribute"; |
| | | 538 | | |
| | 36 | 539 | | var usageAttribute = attributeClass.GetAttributes() |
| | 36 | 540 | | .FirstOrDefault(attribute => |
| | 108 | 541 | | attribute.AttributeClass?.ToDisplayString() == |
| | 108 | 542 | | "System.AttributeUsageAttribute"); |
| | 36 | 543 | | if (usageAttribute is not null && |
| | 36 | 544 | | usageAttribute.ConstructorArguments.Length > 0 && |
| | 36 | 545 | | usageAttribute.ConstructorArguments[0].Value is int validOn && |
| | 36 | 546 | | (validOn & (AttributeTargetsField | AttributeTargetsProperty)) == 0) |
| | | 547 | | { |
| | 2 | 548 | | return "not usable on fields or properties ([AttributeUsage] includes neither AttributeTargets.Field nor Att |
| | | 549 | | } |
| | | 550 | | |
| | 34 | 551 | | return null; |
| | | 552 | | } |
| | | 553 | | |
| | | 554 | | /// <summary> |
| | | 555 | | /// Gets the source location of an attribute occurrence. |
| | | 556 | | /// </summary> |
| | | 557 | | internal static Location GetAttributeLocation( |
| | | 558 | | SyntaxNodeAnalysisContext context, |
| | | 559 | | AttributeData attribute) |
| | | 560 | | { |
| | 192 | 561 | | return attribute.ApplicationSyntaxReference? |
| | 192 | 562 | | .GetSyntax(context.CancellationToken) |
| | 192 | 563 | | .GetLocation() ?? Location.None; |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | private static ConstructorGuardOccurrence CreateOccurrence( |
| | | 567 | | ISymbol member, |
| | | 568 | | ITypeSymbol memberType, |
| | | 569 | | string memberKind, |
| | | 570 | | AttributeData attribute, |
| | | 571 | | ConstructorGuardOccurrenceKind kind, |
| | | 572 | | string? ineligibilityReason, |
| | | 573 | | ITypeSymbol? guardType, |
| | | 574 | | string? methodName, |
| | | 575 | | bool methodNameExplicit, |
| | | 576 | | bool guardTypeUsageIsInSourceAlias) |
| | | 577 | | { |
| | 136 | 578 | | return new ConstructorGuardOccurrence( |
| | 136 | 579 | | member, |
| | 136 | 580 | | memberType, |
| | 136 | 581 | | memberKind, |
| | 136 | 582 | | attribute, |
| | 136 | 583 | | kind, |
| | 136 | 584 | | ineligibilityReason, |
| | 136 | 585 | | guardType, |
| | 136 | 586 | | methodName, |
| | 136 | 587 | | methodNameExplicit, |
| | 136 | 588 | | guardTypeUsageIsInSourceAlias); |
| | | 589 | | } |
| | | 590 | | |
| | | 591 | | private static bool IsCustomGuardValidForGeneration( |
| | | 592 | | Compilation compilation, |
| | | 593 | | INamedTypeSymbol containingType, |
| | | 594 | | ConstructorGuardOccurrence occurrence, |
| | | 595 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes) |
| | | 596 | | { |
| | 8 | 597 | | if (occurrence.GuardType is null || |
| | 8 | 598 | | occurrence.GuardType.TypeKind == TypeKind.Error || |
| | 8 | 599 | | !compilation.IsSymbolAccessibleWithin( |
| | 8 | 600 | | occurrence.GuardType, |
| | 8 | 601 | | containingType) || |
| | 8 | 602 | | occurrence.MethodNameExplicit && |
| | 8 | 603 | | string.IsNullOrWhiteSpace(occurrence.MethodName)) |
| | | 604 | | { |
| | 0 | 605 | | return false; |
| | | 606 | | } |
| | | 607 | | |
| | 8 | 608 | | var methodName = string.IsNullOrEmpty(occurrence.MethodName) |
| | 8 | 609 | | ? DefaultGuardMethodName |
| | 8 | 610 | | : occurrence.MethodName!; |
| | 8 | 611 | | return TryResolveGuardMethod( |
| | 8 | 612 | | compilation, |
| | 8 | 613 | | containingType, |
| | 8 | 614 | | occurrence.GuardType, |
| | 8 | 615 | | methodName, |
| | 8 | 616 | | occurrence.MemberType, |
| | 8 | 617 | | occurrence.MemberKind, |
| | 8 | 618 | | forwardedArgumentTypes, |
| | 8 | 619 | | out _, |
| | 8 | 620 | | out _, |
| | 8 | 621 | | out _) == GuardMethodResolution.Found; |
| | | 622 | | } |
| | | 623 | | |
| | | 624 | | private static bool IsDefinedEnumValue(TypedConstant constant) |
| | | 625 | | { |
| | 21 | 626 | | if (constant.Kind != TypedConstantKind.Enum || |
| | 21 | 627 | | constant.Value is not int rawValue || |
| | 21 | 628 | | constant.Type is not { } enumType) |
| | | 629 | | { |
| | 0 | 630 | | return false; |
| | | 631 | | } |
| | | 632 | | |
| | 21 | 633 | | return enumType.GetMembers() |
| | 21 | 634 | | .OfType<IFieldSymbol>() |
| | 21 | 635 | | .Any(field => |
| | 80 | 636 | | field.HasConstantValue && |
| | 80 | 637 | | field.ConstantValue is int memberValue && |
| | 80 | 638 | | memberValue == rawValue); |
| | | 639 | | } |
| | | 640 | | |
| | | 641 | | private static void AnalyzeAliasGuardAtUsage( |
| | | 642 | | SyntaxNodeAnalysisContext context, |
| | | 643 | | INamedTypeSymbol containingType, |
| | | 644 | | ConstructorGuardOccurrence occurrence, |
| | | 645 | | Location location) |
| | | 646 | | { |
| | 30 | 647 | | if (!TryGetForwardedArgumentTypes( |
| | 30 | 648 | | occurrence.Attribute, |
| | 30 | 649 | | out var forwardedArgumentTypes, |
| | 30 | 650 | | out var unsupportedReason)) |
| | | 651 | | { |
| | 6 | 652 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 6 | 653 | | DiagnosticDescriptors.ConstructorGuardAliasUsageArgumentUnsupported, |
| | 6 | 654 | | location, |
| | 6 | 655 | | occurrence.Member.Name, |
| | 6 | 656 | | unsupportedReason)); |
| | 6 | 657 | | return; |
| | | 658 | | } |
| | | 659 | | |
| | 24 | 660 | | if (!occurrence.GuardTypeUsageIsInSourceAlias) |
| | | 661 | | { |
| | 0 | 662 | | AnalyzeCustomGuard( |
| | 0 | 663 | | context, |
| | 0 | 664 | | containingType, |
| | 0 | 665 | | occurrence, |
| | 0 | 666 | | location, |
| | 0 | 667 | | occurrence.GuardType, |
| | 0 | 668 | | occurrence.MethodName, |
| | 0 | 669 | | occurrence.MethodNameExplicit, |
| | 0 | 670 | | forwardedArgumentTypes); |
| | 0 | 671 | | return; |
| | | 672 | | } |
| | | 673 | | |
| | 24 | 674 | | if (occurrence.GuardType is null || |
| | 24 | 675 | | occurrence.GuardType.TypeKind == TypeKind.Error) |
| | | 676 | | { |
| | 0 | 677 | | return; |
| | | 678 | | } |
| | | 679 | | |
| | 24 | 680 | | var methodName = string.IsNullOrEmpty(occurrence.MethodName) |
| | 24 | 681 | | ? DefaultGuardMethodName |
| | 24 | 682 | | : occurrence.MethodName!; |
| | 24 | 683 | | var resolution = TryResolveGuardMethod( |
| | 24 | 684 | | context.Compilation, |
| | 24 | 685 | | containingType, |
| | 24 | 686 | | occurrence.GuardType, |
| | 24 | 687 | | methodName, |
| | 24 | 688 | | occurrence.MemberType, |
| | 24 | 689 | | occurrence.MemberKind, |
| | 24 | 690 | | forwardedArgumentTypes, |
| | 24 | 691 | | out _, |
| | 24 | 692 | | out var reason, |
| | 24 | 693 | | out var failureKind); |
| | | 694 | | |
| | 24 | 695 | | if (resolution == GuardMethodResolution.NotFound && |
| | 24 | 696 | | failureKind == GuardResolutionFailureKind.ForwardedArgument) |
| | | 697 | | { |
| | 10 | 698 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 10 | 699 | | DiagnosticDescriptors.ConstructorGuardForwardedArgumentIncompatible, |
| | 10 | 700 | | location, |
| | 10 | 701 | | methodName, |
| | 10 | 702 | | occurrence.GuardType.ToDisplayString(), |
| | 10 | 703 | | occurrence.Member.Name, |
| | 10 | 704 | | occurrence.MemberType.ToDisplayString(), |
| | 10 | 705 | | reason)); |
| | | 706 | | } |
| | 14 | 707 | | else if (resolution == GuardMethodResolution.NotFound && |
| | 14 | 708 | | reason == GetMemberTypeIncompatibleReason(occurrence.MemberKind)) |
| | | 709 | | { |
| | 2 | 710 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 711 | | DiagnosticDescriptors.ConstructorGuardMethodInvalid, |
| | 2 | 712 | | location, |
| | 2 | 713 | | methodName, |
| | 2 | 714 | | occurrence.GuardType.ToDisplayString(), |
| | 2 | 715 | | occurrence.Member.Name, |
| | 2 | 716 | | occurrence.MemberType.ToDisplayString(), |
| | 2 | 717 | | reason)); |
| | | 718 | | } |
| | 12 | 719 | | else if (resolution == GuardMethodResolution.Ambiguous) |
| | | 720 | | { |
| | 0 | 721 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 722 | | DiagnosticDescriptors.ConstructorGuardMethodAmbiguous, |
| | 0 | 723 | | location, |
| | 0 | 724 | | methodName, |
| | 0 | 725 | | occurrence.GuardType.ToDisplayString(), |
| | 0 | 726 | | occurrence.Member.Name, |
| | 0 | 727 | | occurrence.MemberType.ToDisplayString())); |
| | | 728 | | } |
| | 12 | 729 | | } |
| | | 730 | | |
| | | 731 | | private static void AnalyzeBuiltInGuard( |
| | | 732 | | SyntaxNodeAnalysisContext context, |
| | | 733 | | ConstructorGuardOccurrence occurrence, |
| | | 734 | | Location location) |
| | | 735 | | { |
| | 14 | 736 | | var first = occurrence.Attribute.ConstructorArguments[0]; |
| | 14 | 737 | | if (TryReportUndefinedEnum(context, location, first)) |
| | 2 | 738 | | return; |
| | | 739 | | |
| | 12 | 740 | | if (first.Value is not int rawValue) |
| | 0 | 741 | | return; |
| | | 742 | | |
| | 12 | 743 | | var kind = (BuiltInConstructorGuardKindMirror)rawValue; |
| | | 744 | | switch (kind) |
| | | 745 | | { |
| | | 746 | | case BuiltInConstructorGuardKindMirror.NotNull: |
| | 7 | 747 | | if (!CanBeRuntimeNull(occurrence.MemberType)) |
| | | 748 | | { |
| | 4 | 749 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 750 | | DiagnosticDescriptors.ConstructorGuardIncompatibleWithFieldType, |
| | 4 | 751 | | location, |
| | 4 | 752 | | "NotNull", |
| | 4 | 753 | | occurrence.Member.Name, |
| | 4 | 754 | | occurrence.MemberType.ToDisplayString(), |
| | 4 | 755 | | $"the {occurrence.MemberKind}'s type is a non-nullable value type, so a runtime null value is ne |
| | | 756 | | } |
| | | 757 | | |
| | 4 | 758 | | break; |
| | | 759 | | case BuiltInConstructorGuardKindMirror.NotNullOrEmpty: |
| | | 760 | | case BuiltInConstructorGuardKindMirror.NotNullOrWhiteSpace: |
| | 5 | 761 | | if (occurrence.MemberType.SpecialType != SpecialType.System_String) |
| | | 762 | | { |
| | 4 | 763 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 764 | | DiagnosticDescriptors.ConstructorGuardIncompatibleWithFieldType, |
| | 4 | 765 | | location, |
| | 4 | 766 | | kind.ToString(), |
| | 4 | 767 | | occurrence.Member.Name, |
| | 4 | 768 | | occurrence.MemberType.ToDisplayString(), |
| | 4 | 769 | | $"this guard only applies to string-compatible {GetMemberKindPlural(occurrence.MemberKind)}")); |
| | | 770 | | } |
| | | 771 | | |
| | | 772 | | break; |
| | | 773 | | } |
| | 8 | 774 | | } |
| | | 775 | | |
| | | 776 | | private static void AnalyzeCustomGuard( |
| | | 777 | | SyntaxNodeAnalysisContext context, |
| | | 778 | | INamedTypeSymbol containingType, |
| | | 779 | | ConstructorGuardOccurrence occurrence, |
| | | 780 | | Location location, |
| | | 781 | | ITypeSymbol? guardType, |
| | | 782 | | string? methodName, |
| | | 783 | | bool methodNameExplicit, |
| | | 784 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes) |
| | | 785 | | { |
| | 65 | 786 | | if (guardType is null || guardType.TypeKind == TypeKind.Error) |
| | | 787 | | { |
| | 2 | 788 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 789 | | DiagnosticDescriptors.ConstructorGuardTypeInvalid, |
| | 2 | 790 | | location, |
| | 2 | 791 | | occurrence.Member.Name, |
| | 2 | 792 | | "the guard type could not be resolved")); |
| | 2 | 793 | | return; |
| | | 794 | | } |
| | | 795 | | |
| | 63 | 796 | | if (!context.Compilation.IsSymbolAccessibleWithin(guardType, containingType)) |
| | | 797 | | { |
| | 0 | 798 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 799 | | DiagnosticDescriptors.ConstructorGuardTypeInvalid, |
| | 0 | 800 | | location, |
| | 0 | 801 | | occurrence.Member.Name, |
| | 0 | 802 | | $"'{guardType.ToDisplayString()}' is not accessible from '{containingType.ToDisplayString()}'")); |
| | 0 | 803 | | return; |
| | | 804 | | } |
| | | 805 | | |
| | 63 | 806 | | if (methodNameExplicit && string.IsNullOrWhiteSpace(methodName)) |
| | | 807 | | { |
| | 2 | 808 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 809 | | DiagnosticDescriptors.ConstructorGuardMethodNameInvalid, |
| | 2 | 810 | | location, |
| | 2 | 811 | | occurrence.Member.Name)); |
| | 2 | 812 | | return; |
| | | 813 | | } |
| | | 814 | | |
| | 61 | 815 | | var effectiveMethodName = string.IsNullOrEmpty(methodName) |
| | 61 | 816 | | ? DefaultGuardMethodName |
| | 61 | 817 | | : methodName!; |
| | 61 | 818 | | var resolution = TryResolveGuardMethod( |
| | 61 | 819 | | context.Compilation, |
| | 61 | 820 | | containingType, |
| | 61 | 821 | | guardType, |
| | 61 | 822 | | effectiveMethodName, |
| | 61 | 823 | | occurrence.MemberType, |
| | 61 | 824 | | occurrence.MemberKind, |
| | 61 | 825 | | forwardedArgumentTypes, |
| | 61 | 826 | | out _, |
| | 61 | 827 | | out var reason, |
| | 61 | 828 | | out var failureKind); |
| | | 829 | | |
| | 61 | 830 | | switch (resolution) |
| | | 831 | | { |
| | | 832 | | case GuardMethodResolution.NotFound |
| | 44 | 833 | | when failureKind == GuardResolutionFailureKind.ForwardedArgument: |
| | 0 | 834 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 835 | | DiagnosticDescriptors.ConstructorGuardForwardedArgumentIncompatible, |
| | 0 | 836 | | location, |
| | 0 | 837 | | effectiveMethodName, |
| | 0 | 838 | | guardType.ToDisplayString(), |
| | 0 | 839 | | occurrence.Member.Name, |
| | 0 | 840 | | occurrence.MemberType.ToDisplayString(), |
| | 0 | 841 | | reason)); |
| | 0 | 842 | | break; |
| | | 843 | | case GuardMethodResolution.NotFound: |
| | 44 | 844 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 44 | 845 | | DiagnosticDescriptors.ConstructorGuardMethodInvalid, |
| | 44 | 846 | | location, |
| | 44 | 847 | | effectiveMethodName, |
| | 44 | 848 | | guardType.ToDisplayString(), |
| | 44 | 849 | | occurrence.Member.Name, |
| | 44 | 850 | | occurrence.MemberType.ToDisplayString(), |
| | 44 | 851 | | reason ?? "no compatible method was found")); |
| | 44 | 852 | | break; |
| | | 853 | | case GuardMethodResolution.Ambiguous: |
| | 2 | 854 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 855 | | DiagnosticDescriptors.ConstructorGuardMethodAmbiguous, |
| | 2 | 856 | | location, |
| | 2 | 857 | | effectiveMethodName, |
| | 2 | 858 | | guardType.ToDisplayString(), |
| | 2 | 859 | | occurrence.Member.Name, |
| | 2 | 860 | | occurrence.MemberType.ToDisplayString())); |
| | | 861 | | break; |
| | | 862 | | } |
| | 2 | 863 | | } |
| | | 864 | | |
| | | 865 | | private static bool TryGetForwardedArgumentTypes( |
| | | 866 | | AttributeData attribute, |
| | | 867 | | out ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 868 | | out string? unsupportedReason) |
| | | 869 | | { |
| | 34 | 870 | | if (attribute.NamedArguments.Length > 0) |
| | | 871 | | { |
| | 2 | 872 | | forwardedArgumentTypes = ImmutableArray<ITypeSymbol>.Empty; |
| | 2 | 873 | | unsupportedReason = $"named argument '{attribute.NamedArguments[0].Key}' is not forwarded to the guard metho |
| | 2 | 874 | | return false; |
| | | 875 | | } |
| | | 876 | | |
| | 32 | 877 | | if (attribute.ConstructorArguments.Length == 0) |
| | | 878 | | { |
| | 3 | 879 | | forwardedArgumentTypes = ImmutableArray<ITypeSymbol>.Empty; |
| | 3 | 880 | | unsupportedReason = null; |
| | 3 | 881 | | return true; |
| | | 882 | | } |
| | | 883 | | |
| | 29 | 884 | | var builder = ImmutableArray.CreateBuilder<ITypeSymbol>( |
| | 29 | 885 | | attribute.ConstructorArguments.Length); |
| | 114 | 886 | | for (var i = 0; i < attribute.ConstructorArguments.Length; i++) |
| | | 887 | | { |
| | 32 | 888 | | var constant = attribute.ConstructorArguments[i]; |
| | 32 | 889 | | if (!TypedConstantRenderer.TryRender(constant, out _)) |
| | | 890 | | { |
| | 4 | 891 | | forwardedArgumentTypes = ImmutableArray<ITypeSymbol>.Empty; |
| | 4 | 892 | | unsupportedReason = $"positional argument {i + 1} is {DescribeUnsupportedConstant(constant)}, which is n |
| | 4 | 893 | | return false; |
| | | 894 | | } |
| | | 895 | | |
| | 28 | 896 | | builder.Add(constant.Type!); |
| | | 897 | | } |
| | | 898 | | |
| | 25 | 899 | | forwardedArgumentTypes = builder.MoveToImmutable(); |
| | 25 | 900 | | unsupportedReason = null; |
| | 25 | 901 | | return true; |
| | | 902 | | } |
| | | 903 | | |
| | | 904 | | private static string DescribeUnsupportedConstant(TypedConstant constant) |
| | | 905 | | { |
| | 4 | 906 | | if (constant.Kind == TypedConstantKind.Array) |
| | 2 | 907 | | return "an array"; |
| | | 908 | | |
| | 2 | 909 | | if (constant.Value is float or double) |
| | 2 | 910 | | return "a floating-point value"; |
| | | 911 | | |
| | 0 | 912 | | return "an unsupported value"; |
| | | 913 | | } |
| | | 914 | | |
| | | 915 | | internal static bool CanBeRuntimeNull(ITypeSymbol type) |
| | | 916 | | { |
| | 23 | 917 | | if (type.IsReferenceType) |
| | 16 | 918 | | return true; |
| | | 919 | | |
| | 7 | 920 | | if (type is ITypeParameterSymbol typeParameter) |
| | | 921 | | { |
| | 3 | 922 | | return !typeParameter.HasValueTypeConstraint && |
| | 3 | 923 | | !typeParameter.HasUnmanagedTypeConstraint; |
| | | 924 | | } |
| | | 925 | | |
| | 4 | 926 | | return type is INamedTypeSymbol |
| | 4 | 927 | | { |
| | 4 | 928 | | OriginalDefinition.SpecialType: SpecialType.System_Nullable_T, |
| | 4 | 929 | | }; |
| | | 930 | | } |
| | | 931 | | |
| | | 932 | | private static bool IsAssignableTo( |
| | | 933 | | ITypeSymbol sourceType, |
| | | 934 | | ITypeSymbol parameterType, |
| | | 935 | | Compilation compilation) |
| | | 936 | | { |
| | 65 | 937 | | if (SymbolEqualityComparer.Default.Equals(sourceType, parameterType)) |
| | 39 | 938 | | return true; |
| | | 939 | | |
| | 26 | 940 | | var conversion = compilation.ClassifyConversion(sourceType, parameterType); |
| | 26 | 941 | | return conversion.Exists && (conversion.IsIdentity || conversion.IsImplicit); |
| | | 942 | | } |
| | | 943 | | |
| | | 944 | | private static bool TryCheckNonGenericCompatibility( |
| | | 945 | | ITypeSymbol memberType, |
| | | 946 | | string memberKind, |
| | | 947 | | ITypeSymbol valueParameterType, |
| | | 948 | | List<IParameterSymbol> middleParameters, |
| | | 949 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 950 | | Compilation compilation, |
| | | 951 | | out string? reason, |
| | | 952 | | out GuardResolutionFailureKind failureKind) |
| | | 953 | | { |
| | 34 | 954 | | if (!IsAssignableTo(memberType, valueParameterType, compilation)) |
| | | 955 | | { |
| | 4 | 956 | | reason = GetMemberTypeIncompatibleReason(memberKind); |
| | 4 | 957 | | failureKind = GuardResolutionFailureKind.General; |
| | 4 | 958 | | return false; |
| | | 959 | | } |
| | | 960 | | |
| | 88 | 961 | | for (var i = 0; i < middleParameters.Count; i++) |
| | | 962 | | { |
| | 18 | 963 | | if (IsAssignableTo( |
| | 18 | 964 | | forwardedArgumentTypes[i], |
| | 18 | 965 | | middleParameters[i].Type, |
| | 18 | 966 | | compilation)) |
| | | 967 | | { |
| | | 968 | | continue; |
| | | 969 | | } |
| | | 970 | | |
| | 4 | 971 | | reason = $"its parameter '{middleParameters[i].Name}' of type '{middleParameters[i].Type.ToDisplayString()}' |
| | 4 | 972 | | failureKind = GuardResolutionFailureKind.ForwardedArgument; |
| | 4 | 973 | | return false; |
| | | 974 | | } |
| | | 975 | | |
| | 26 | 976 | | reason = null; |
| | 26 | 977 | | failureKind = GuardResolutionFailureKind.None; |
| | 26 | 978 | | return true; |
| | | 979 | | } |
| | | 980 | | |
| | | 981 | | private static bool TryInferGenericParameterCompatibility( |
| | | 982 | | IMethodSymbol genericMethod, |
| | | 983 | | ITypeSymbol parameterType, |
| | | 984 | | ITypeSymbol memberType, |
| | | 985 | | string memberKind, |
| | | 986 | | List<IParameterSymbol> middleParameters, |
| | | 987 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 988 | | Compilation compilation, |
| | | 989 | | out string? reason, |
| | | 990 | | out GuardResolutionFailureKind failureKind) |
| | | 991 | | { |
| | 43 | 992 | | var methodTypeParameters = new HashSet<ITypeSymbol>( |
| | 43 | 993 | | genericMethod.TypeParameters, |
| | 43 | 994 | | SymbolEqualityComparer.Default); |
| | 43 | 995 | | string? lastReason = null; |
| | 43 | 996 | | var lastFailureKind = GuardResolutionFailureKind.General; |
| | | 997 | | |
| | 43 | 998 | | var candidateTypes = parameterType is ITypeParameterSymbol typeParameter && |
| | 43 | 999 | | methodTypeParameters.Contains(typeParameter) |
| | 43 | 1000 | | ? new[] { memberType } |
| | 43 | 1001 | | : GetTypeAndSupertypes(memberType); |
| | 181 | 1002 | | foreach (var candidateType in candidateTypes) |
| | | 1003 | | { |
| | 55 | 1004 | | var substitution = new Dictionary<ITypeSymbol, ITypeSymbol>( |
| | 55 | 1005 | | SymbolEqualityComparer.Default); |
| | 55 | 1006 | | if (!TryUnify( |
| | 55 | 1007 | | parameterType, |
| | 55 | 1008 | | candidateType, |
| | 55 | 1009 | | methodTypeParameters, |
| | 55 | 1010 | | substitution, |
| | 55 | 1011 | | compilation, |
| | 55 | 1012 | | allowImplicitTypeParameterConversion: |
| | 55 | 1013 | | parameterType is ITypeParameterSymbol)) |
| | | 1014 | | { |
| | | 1015 | | continue; |
| | | 1016 | | } |
| | | 1017 | | |
| | 41 | 1018 | | var forwardedMismatch = false; |
| | 92 | 1019 | | for (var i = 0; i < middleParameters.Count; i++) |
| | | 1020 | | { |
| | 7 | 1021 | | if (TryUnify( |
| | 7 | 1022 | | middleParameters[i].Type, |
| | 7 | 1023 | | forwardedArgumentTypes[i], |
| | 7 | 1024 | | methodTypeParameters, |
| | 7 | 1025 | | substitution, |
| | 7 | 1026 | | compilation, |
| | 7 | 1027 | | allowImplicitTypeParameterConversion: |
| | 7 | 1028 | | middleParameters[i].Type is ITypeParameterSymbol)) |
| | | 1029 | | { |
| | | 1030 | | continue; |
| | | 1031 | | } |
| | | 1032 | | |
| | 2 | 1033 | | lastReason ??= $"its parameter '{middleParameters[i].Name}' cannot accept the forwarded argument of type |
| | 2 | 1034 | | lastFailureKind = GuardResolutionFailureKind.ForwardedArgument; |
| | 2 | 1035 | | forwardedMismatch = true; |
| | 2 | 1036 | | break; |
| | | 1037 | | } |
| | | 1038 | | |
| | 41 | 1039 | | if (forwardedMismatch) |
| | | 1040 | | continue; |
| | | 1041 | | |
| | 39 | 1042 | | var unboundParameter = genericMethod.TypeParameters.FirstOrDefault( |
| | 82 | 1043 | | typeParameter => !substitution.ContainsKey(typeParameter)); |
| | 39 | 1044 | | if (unboundParameter is not null) |
| | | 1045 | | { |
| | 2 | 1046 | | var onlyForwarded = IsReachableOnlyThroughForwardedParameters( |
| | 2 | 1047 | | unboundParameter, |
| | 2 | 1048 | | parameterType, |
| | 2 | 1049 | | middleParameters); |
| | 2 | 1050 | | lastReason ??= onlyForwarded |
| | 2 | 1051 | | ? $"its type parameter '{unboundParameter.Name}' cannot be inferred from the {memberKind}'s type or |
| | 2 | 1052 | | : $"its type parameter '{unboundParameter.Name}' cannot be inferred from the {memberKind}'s type"; |
| | 2 | 1053 | | lastFailureKind = onlyForwarded |
| | 2 | 1054 | | ? GuardResolutionFailureKind.ForwardedArgument |
| | 2 | 1055 | | : GuardResolutionFailureKind.General; |
| | 2 | 1056 | | continue; |
| | | 1057 | | } |
| | | 1058 | | |
| | 37 | 1059 | | var constraintViolation = FindConstraintViolation( |
| | 37 | 1060 | | genericMethod.TypeParameters, |
| | 37 | 1061 | | substitution, |
| | 37 | 1062 | | compilation, |
| | 37 | 1063 | | out var violatingTypeParameter); |
| | 37 | 1064 | | if (constraintViolation is not null) |
| | | 1065 | | { |
| | 22 | 1066 | | var onlyForwarded = violatingTypeParameter is not null && |
| | 22 | 1067 | | IsReachableOnlyThroughForwardedParameters( |
| | 22 | 1068 | | violatingTypeParameter, |
| | 22 | 1069 | | parameterType, |
| | 22 | 1070 | | middleParameters); |
| | 22 | 1071 | | lastReason ??= constraintViolation; |
| | 22 | 1072 | | lastFailureKind = onlyForwarded |
| | 22 | 1073 | | ? GuardResolutionFailureKind.ForwardedArgument |
| | 22 | 1074 | | : GuardResolutionFailureKind.General; |
| | 22 | 1075 | | continue; |
| | | 1076 | | } |
| | | 1077 | | |
| | 15 | 1078 | | reason = null; |
| | 15 | 1079 | | failureKind = GuardResolutionFailureKind.None; |
| | 15 | 1080 | | return true; |
| | | 1081 | | } |
| | | 1082 | | |
| | 28 | 1083 | | reason = lastReason ?? GetMemberTypeIncompatibleReason(memberKind); |
| | 28 | 1084 | | failureKind = lastReason is null |
| | 28 | 1085 | | ? GuardResolutionFailureKind.General |
| | 28 | 1086 | | : lastFailureKind; |
| | 28 | 1087 | | return false; |
| | 15 | 1088 | | } |
| | | 1089 | | |
| | | 1090 | | private static string GetMemberTypeIncompatibleReason(string memberKind) |
| | | 1091 | | { |
| | 8 | 1092 | | return $"its value parameter type is not compatible with the {memberKind}'s type"; |
| | | 1093 | | } |
| | | 1094 | | |
| | | 1095 | | private static string GetMemberKindPlural(string memberKind) |
| | | 1096 | | { |
| | 4 | 1097 | | return memberKind == "property" ? "properties" : memberKind + "s"; |
| | | 1098 | | } |
| | | 1099 | | |
| | | 1100 | | private static bool ContainsTypeParameter( |
| | | 1101 | | ITypeSymbol type, |
| | | 1102 | | ITypeParameterSymbol typeParameter) |
| | | 1103 | | { |
| | 26 | 1104 | | if (SymbolEqualityComparer.Default.Equals(type, typeParameter)) |
| | 22 | 1105 | | return true; |
| | | 1106 | | |
| | 4 | 1107 | | if (type is INamedTypeSymbol namedType) |
| | | 1108 | | { |
| | 0 | 1109 | | return namedType.TypeArguments.Any( |
| | 0 | 1110 | | typeArgument => ContainsTypeParameter(typeArgument, typeParameter)); |
| | | 1111 | | } |
| | | 1112 | | |
| | 4 | 1113 | | if (type is IArrayTypeSymbol arrayType) |
| | 0 | 1114 | | return ContainsTypeParameter(arrayType.ElementType, typeParameter); |
| | | 1115 | | |
| | 4 | 1116 | | return false; |
| | | 1117 | | } |
| | | 1118 | | |
| | | 1119 | | private static bool IsReachableOnlyThroughForwardedParameters( |
| | | 1120 | | ITypeParameterSymbol typeParameter, |
| | | 1121 | | ITypeSymbol valueParameterType, |
| | | 1122 | | List<IParameterSymbol> middleParameters) |
| | | 1123 | | { |
| | 24 | 1124 | | if (ContainsTypeParameter(valueParameterType, typeParameter)) |
| | 20 | 1125 | | return false; |
| | | 1126 | | |
| | 4 | 1127 | | return middleParameters.Any( |
| | 6 | 1128 | | parameter => ContainsTypeParameter(parameter.Type, typeParameter)); |
| | | 1129 | | } |
| | | 1130 | | |
| | | 1131 | | private static string? FindConstraintViolation( |
| | | 1132 | | ImmutableArray<ITypeParameterSymbol> typeParameters, |
| | | 1133 | | Dictionary<ITypeSymbol, ITypeSymbol> substitution, |
| | | 1134 | | Compilation compilation, |
| | | 1135 | | out ITypeParameterSymbol? violatingTypeParameter) |
| | | 1136 | | { |
| | 130 | 1137 | | foreach (var typeParameter in typeParameters) |
| | | 1138 | | { |
| | 39 | 1139 | | if (!substitution.TryGetValue(typeParameter, out var argumentType)) |
| | | 1140 | | continue; |
| | | 1141 | | |
| | 39 | 1142 | | if (typeParameter.HasReferenceTypeConstraint && |
| | 39 | 1143 | | !argumentType.IsReferenceType) |
| | | 1144 | | { |
| | 0 | 1145 | | violatingTypeParameter = typeParameter; |
| | 0 | 1146 | | return $"its type parameter '{typeParameter.Name}' requires a reference type, but '{argumentType.ToDispl |
| | | 1147 | | } |
| | | 1148 | | |
| | 39 | 1149 | | if (typeParameter.HasReferenceTypeConstraint && |
| | 39 | 1150 | | typeParameter.ReferenceTypeConstraintNullableAnnotation != |
| | 39 | 1151 | | NullableAnnotation.Annotated && |
| | 39 | 1152 | | argumentType.NullableAnnotation == NullableAnnotation.Annotated) |
| | | 1153 | | { |
| | 2 | 1154 | | violatingTypeParameter = typeParameter; |
| | 2 | 1155 | | return $"its type parameter '{typeParameter.Name}' requires a non-nullable reference type, but '{argumen |
| | | 1156 | | } |
| | | 1157 | | |
| | 37 | 1158 | | if (typeParameter.HasValueTypeConstraint && |
| | 37 | 1159 | | (!argumentType.IsValueType || |
| | 37 | 1160 | | IsNullableValueType(argumentType))) |
| | | 1161 | | { |
| | 2 | 1162 | | violatingTypeParameter = typeParameter; |
| | 2 | 1163 | | return $"its type parameter '{typeParameter.Name}' requires a non-nullable value type, but '{argumentTyp |
| | | 1164 | | } |
| | | 1165 | | |
| | 35 | 1166 | | if (typeParameter.HasUnmanagedTypeConstraint && |
| | 35 | 1167 | | !argumentType.IsUnmanagedType) |
| | | 1168 | | { |
| | 2 | 1169 | | violatingTypeParameter = typeParameter; |
| | 2 | 1170 | | return $"its type parameter '{typeParameter.Name}' requires an unmanaged type, but '{argumentType.ToDisp |
| | | 1171 | | } |
| | | 1172 | | |
| | 33 | 1173 | | if (typeParameter.HasNotNullConstraint && |
| | 33 | 1174 | | (argumentType.NullableAnnotation == NullableAnnotation.Annotated || |
| | 33 | 1175 | | IsNullableValueType(argumentType))) |
| | | 1176 | | { |
| | 4 | 1177 | | violatingTypeParameter = typeParameter; |
| | 4 | 1178 | | return $"its type parameter '{typeParameter.Name}' requires a non-nullable type, but '{argumentType.ToDi |
| | | 1179 | | } |
| | | 1180 | | |
| | 29 | 1181 | | if (typeParameter.HasConstructorConstraint && |
| | 29 | 1182 | | !SatisfiesConstructorConstraint(argumentType)) |
| | | 1183 | | { |
| | 6 | 1184 | | violatingTypeParameter = typeParameter; |
| | 6 | 1185 | | return $"its type parameter '{typeParameter.Name}' requires a non-abstract type with a public parameterl |
| | | 1186 | | } |
| | | 1187 | | |
| | 56 | 1188 | | foreach (var constraintType in typeParameter.ConstraintTypes) |
| | | 1189 | | { |
| | 8 | 1190 | | if (SymbolEqualityComparer.Default.Equals( |
| | 8 | 1191 | | argumentType, |
| | 8 | 1192 | | constraintType)) |
| | | 1193 | | { |
| | | 1194 | | continue; |
| | | 1195 | | } |
| | | 1196 | | |
| | 8 | 1197 | | var conversion = compilation.ClassifyConversion( |
| | 8 | 1198 | | argumentType, |
| | 8 | 1199 | | constraintType); |
| | 8 | 1200 | | if (conversion.Exists && |
| | 8 | 1201 | | (conversion.IsIdentity || conversion.IsImplicit)) |
| | | 1202 | | { |
| | | 1203 | | continue; |
| | | 1204 | | } |
| | | 1205 | | |
| | 6 | 1206 | | violatingTypeParameter = typeParameter; |
| | 6 | 1207 | | return $"its type parameter '{typeParameter.Name}' requires '{constraintType.ToDisplayString()}', which |
| | | 1208 | | } |
| | | 1209 | | } |
| | | 1210 | | |
| | 15 | 1211 | | violatingTypeParameter = null; |
| | 15 | 1212 | | return null; |
| | | 1213 | | } |
| | | 1214 | | |
| | | 1215 | | private static IEnumerable<ITypeSymbol> GetTypeAndSupertypes(ITypeSymbol type) |
| | | 1216 | | { |
| | 5 | 1217 | | yield return type; |
| | | 1218 | | |
| | 2 | 1219 | | var current = (type as INamedTypeSymbol)?.BaseType; |
| | 2 | 1220 | | while (current is not null) |
| | | 1221 | | { |
| | 0 | 1222 | | yield return current; |
| | 0 | 1223 | | current = current.BaseType; |
| | | 1224 | | } |
| | | 1225 | | |
| | 28 | 1226 | | foreach (var iface in type.AllInterfaces) |
| | | 1227 | | { |
| | 12 | 1228 | | yield return iface; |
| | | 1229 | | } |
| | 2 | 1230 | | } |
| | | 1231 | | |
| | | 1232 | | private static bool TryUnify( |
| | | 1233 | | ITypeSymbol parameterType, |
| | | 1234 | | ITypeSymbol candidateType, |
| | | 1235 | | HashSet<ITypeSymbol> methodTypeParameters, |
| | | 1236 | | Dictionary<ITypeSymbol, ITypeSymbol> substitution, |
| | | 1237 | | Compilation compilation, |
| | | 1238 | | bool allowImplicitTypeParameterConversion) |
| | | 1239 | | { |
| | 65 | 1240 | | if (parameterType is ITypeParameterSymbol typeParameter && |
| | 65 | 1241 | | methodTypeParameters.Contains(typeParameter)) |
| | | 1242 | | { |
| | 47 | 1243 | | if (substitution.TryGetValue(typeParameter, out var bound)) |
| | | 1244 | | { |
| | 4 | 1245 | | if (SymbolEqualityComparer.Default.Equals(bound, candidateType)) |
| | 1 | 1246 | | return true; |
| | | 1247 | | |
| | 3 | 1248 | | if (!allowImplicitTypeParameterConversion) |
| | 0 | 1249 | | return false; |
| | | 1250 | | |
| | 3 | 1251 | | if (IsAssignableTo(candidateType, bound, compilation)) |
| | 1 | 1252 | | return true; |
| | | 1253 | | |
| | 2 | 1254 | | if (!IsAssignableTo(bound, candidateType, compilation)) |
| | 2 | 1255 | | return false; |
| | | 1256 | | |
| | 0 | 1257 | | substitution[typeParameter] = candidateType; |
| | 0 | 1258 | | return true; |
| | | 1259 | | } |
| | | 1260 | | |
| | 43 | 1261 | | substitution[typeParameter] = candidateType; |
| | 43 | 1262 | | return true; |
| | | 1263 | | } |
| | | 1264 | | |
| | 18 | 1265 | | if (parameterType is INamedTypeSymbol namedParameter && |
| | 18 | 1266 | | candidateType is INamedTypeSymbol namedCandidate) |
| | | 1267 | | { |
| | 3 | 1268 | | if (!SymbolEqualityComparer.Default.Equals( |
| | 3 | 1269 | | namedParameter.OriginalDefinition, |
| | 3 | 1270 | | namedCandidate.OriginalDefinition)) |
| | | 1271 | | { |
| | 0 | 1272 | | return false; |
| | | 1273 | | } |
| | | 1274 | | |
| | 3 | 1275 | | if (namedParameter.TypeArguments.Length != |
| | 3 | 1276 | | namedCandidate.TypeArguments.Length) |
| | | 1277 | | { |
| | 0 | 1278 | | return false; |
| | | 1279 | | } |
| | | 1280 | | |
| | 10 | 1281 | | for (var i = 0; i < namedParameter.TypeArguments.Length; i++) |
| | | 1282 | | { |
| | 2 | 1283 | | if (!TryUnify( |
| | 2 | 1284 | | namedParameter.TypeArguments[i], |
| | 2 | 1285 | | namedCandidate.TypeArguments[i], |
| | 2 | 1286 | | methodTypeParameters, |
| | 2 | 1287 | | substitution, |
| | 2 | 1288 | | compilation, |
| | 2 | 1289 | | allowImplicitTypeParameterConversion: false)) |
| | | 1290 | | { |
| | 0 | 1291 | | return false; |
| | | 1292 | | } |
| | | 1293 | | } |
| | | 1294 | | |
| | 3 | 1295 | | return true; |
| | | 1296 | | } |
| | | 1297 | | |
| | 15 | 1298 | | if (parameterType is IArrayTypeSymbol arrayParameter && |
| | 15 | 1299 | | candidateType is IArrayTypeSymbol arrayCandidate) |
| | | 1300 | | { |
| | 3 | 1301 | | if (arrayParameter.Rank != arrayCandidate.Rank) |
| | 2 | 1302 | | return false; |
| | | 1303 | | |
| | 1 | 1304 | | return TryUnify( |
| | 1 | 1305 | | arrayParameter.ElementType, |
| | 1 | 1306 | | arrayCandidate.ElementType, |
| | 1 | 1307 | | methodTypeParameters, |
| | 1 | 1308 | | substitution, |
| | 1 | 1309 | | compilation, |
| | 1 | 1310 | | allowImplicitTypeParameterConversion: false); |
| | | 1311 | | } |
| | | 1312 | | |
| | 12 | 1313 | | return SymbolEqualityComparer.Default.Equals(parameterType, candidateType); |
| | | 1314 | | } |
| | | 1315 | | |
| | | 1316 | | private static bool IsNullableValueType(ITypeSymbol type) |
| | | 1317 | | { |
| | 7 | 1318 | | return type is INamedTypeSymbol |
| | 7 | 1319 | | { |
| | 7 | 1320 | | OriginalDefinition.SpecialType: SpecialType.System_Nullable_T, |
| | 7 | 1321 | | }; |
| | | 1322 | | } |
| | | 1323 | | |
| | | 1324 | | private static bool SatisfiesConstructorConstraint(ITypeSymbol type) |
| | | 1325 | | { |
| | 7 | 1326 | | if (type.IsValueType) |
| | 0 | 1327 | | return true; |
| | | 1328 | | |
| | 7 | 1329 | | if (type is ITypeParameterSymbol typeParameter) |
| | | 1330 | | { |
| | 0 | 1331 | | return typeParameter.HasConstructorConstraint || |
| | 0 | 1332 | | typeParameter.HasValueTypeConstraint || |
| | 0 | 1333 | | typeParameter.HasUnmanagedTypeConstraint; |
| | | 1334 | | } |
| | | 1335 | | |
| | 7 | 1336 | | return type is INamedTypeSymbol namedType && |
| | 7 | 1337 | | !namedType.IsAbstract && |
| | 7 | 1338 | | namedType.InstanceConstructors.Any(constructor => |
| | 10 | 1339 | | constructor.Parameters.Length == 0 && |
| | 10 | 1340 | | constructor.DeclaredAccessibility == Accessibility.Public); |
| | | 1341 | | } |
| | | 1342 | | |
| | | 1343 | | private static bool InheritsFromSystemAttribute(INamedTypeSymbol type) |
| | | 1344 | | { |
| | 38 | 1345 | | var current = type.BaseType; |
| | 40 | 1346 | | while (current is not null) |
| | | 1347 | | { |
| | 38 | 1348 | | if (current.ToDisplayString() == "System.Attribute") |
| | 36 | 1349 | | return true; |
| | | 1350 | | |
| | 2 | 1351 | | current = current.BaseType; |
| | | 1352 | | } |
| | | 1353 | | |
| | 2 | 1354 | | return false; |
| | | 1355 | | } |
| | | 1356 | | } |