| | | 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 | | { |
| | 60 | 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 | | |
| | 60 | 52 | | var first = attribute.ConstructorArguments[0]; |
| | 60 | 53 | | if (first.Kind == TypedConstantKind.Enum && first.Value is int enumValue) |
| | | 54 | | { |
| | 31 | 55 | | var kind = enumValue == 0 |
| | 31 | 56 | | ? ConstructorGuardOccurrenceKind.BuiltInNone |
| | 31 | 57 | | : ConstructorGuardOccurrenceKind.BuiltInPositive; |
| | 31 | 58 | | return CreateOccurrence( |
| | 31 | 59 | | member, |
| | 31 | 60 | | memberType, |
| | 31 | 61 | | memberKind, |
| | 31 | 62 | | attribute, |
| | 31 | 63 | | kind, |
| | 31 | 64 | | ineligibilityReason, |
| | 31 | 65 | | null, |
| | 31 | 66 | | null, |
| | 31 | 67 | | false, |
| | 31 | 68 | | false); |
| | | 69 | | } |
| | | 70 | | |
| | 29 | 71 | | if (first.Value is ITypeSymbol guardType) |
| | | 72 | | { |
| | 29 | 73 | | var methodNameExplicit = attribute.ConstructorArguments.Length > 1 && |
| | 29 | 74 | | attribute.ConstructorArguments[1].Value is string; |
| | 29 | 75 | | var methodName = methodNameExplicit |
| | 29 | 76 | | ? (string)attribute.ConstructorArguments[1].Value! |
| | 29 | 77 | | : DefaultGuardMethodName; |
| | 29 | 78 | | return CreateOccurrence( |
| | 29 | 79 | | member, |
| | 29 | 80 | | memberType, |
| | 29 | 81 | | memberKind, |
| | 29 | 82 | | attribute, |
| | 29 | 83 | | ConstructorGuardOccurrenceKind.CustomType, |
| | 29 | 84 | | ineligibilityReason, |
| | 29 | 85 | | guardType, |
| | 29 | 86 | | methodName, |
| | 29 | 87 | | methodNameExplicit, |
| | 29 | 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 | | { |
| | 32 | 118 | | return CreateOccurrence( |
| | 32 | 119 | | member, |
| | 32 | 120 | | memberType, |
| | 32 | 121 | | memberKind, |
| | 32 | 122 | | attribute, |
| | 32 | 123 | | ConstructorGuardOccurrenceKind.Alias, |
| | 32 | 124 | | ineligibilityReason, |
| | 32 | 125 | | guardType, |
| | 32 | 126 | | methodName, |
| | 32 | 127 | | methodNameExplicit, |
| | 32 | 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 | | { |
| | 236 | 141 | | foreach (var metaAttribute in attributeClass.GetAttributes()) |
| | | 142 | | { |
| | 67 | 143 | | if (metaAttribute.AttributeClass is not { } metaClass || |
| | 67 | 144 | | !GeneratedConstructorEligibility.IsNeedlrGeneratorsAttribute( |
| | 67 | 145 | | metaClass, |
| | 67 | 146 | | ConstructorGuardDefinitionAttributeName)) |
| | | 147 | | { |
| | | 148 | | continue; |
| | | 149 | | } |
| | | 150 | | |
| | 32 | 151 | | if (metaAttribute.ConstructorArguments.Length == 0 || |
| | 32 | 152 | | metaAttribute.ConstructorArguments[0].Value is not ITypeSymbol resolvedGuardType) |
| | | 153 | | { |
| | | 154 | | continue; |
| | | 155 | | } |
| | | 156 | | |
| | 32 | 157 | | guardType = resolvedGuardType; |
| | 32 | 158 | | methodNameExplicit = metaAttribute.ConstructorArguments.Length > 1 && |
| | 32 | 159 | | metaAttribute.ConstructorArguments[1].Value is string; |
| | 32 | 160 | | methodName = methodNameExplicit |
| | 32 | 161 | | ? (string)metaAttribute.ConstructorArguments[1].Value! |
| | 32 | 162 | | : DefaultGuardMethodName; |
| | 32 | 163 | | return true; |
| | | 164 | | } |
| | | 165 | | |
| | 35 | 166 | | guardType = null; |
| | 35 | 167 | | methodName = null; |
| | 35 | 168 | | methodNameExplicit = false; |
| | 35 | 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 | | { |
| | 72 | 181 | | switch (occurrence.Kind) |
| | | 182 | | { |
| | | 183 | | case ConstructorGuardOccurrenceKind.BuiltInPositive: |
| | 14 | 184 | | AnalyzeBuiltInGuard(context, occurrence, location); |
| | 14 | 185 | | break; |
| | | 186 | | case ConstructorGuardOccurrenceKind.CustomType: |
| | 27 | 187 | | AnalyzeCustomGuard( |
| | 27 | 188 | | context, |
| | 27 | 189 | | containingType, |
| | 27 | 190 | | occurrence, |
| | 27 | 191 | | location, |
| | 27 | 192 | | occurrence.GuardType, |
| | 27 | 193 | | occurrence.MethodName, |
| | 27 | 194 | | occurrence.MethodNameExplicit, |
| | 27 | 195 | | ImmutableArray<ITypeSymbol>.Empty); |
| | 27 | 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 | | { |
| | 7 | 217 | | switch (occurrence.Kind) |
| | | 218 | | { |
| | | 219 | | case ConstructorGuardOccurrenceKind.BuiltInPositive: |
| | 3 | 220 | | if (occurrence.Attribute.ConstructorArguments.Length == 0) |
| | 0 | 221 | | return false; |
| | | 222 | | |
| | 3 | 223 | | var constant = occurrence.Attribute.ConstructorArguments[0]; |
| | 3 | 224 | | if (!IsDefinedEnumValue(constant) || |
| | 3 | 225 | | constant.Value is not int rawValue) |
| | | 226 | | { |
| | 0 | 227 | | return false; |
| | | 228 | | } |
| | | 229 | | |
| | 3 | 230 | | var kind = (BuiltInConstructorGuardKindMirror)rawValue; |
| | 3 | 231 | | return kind switch |
| | 3 | 232 | | { |
| | 3 | 233 | | BuiltInConstructorGuardKindMirror.NotNull => |
| | 2 | 234 | | CanBeRuntimeNull(occurrence.MemberType), |
| | 3 | 235 | | BuiltInConstructorGuardKindMirror.NotNullOrEmpty or |
| | 3 | 236 | | BuiltInConstructorGuardKindMirror.NotNullOrWhiteSpace => |
| | 1 | 237 | | occurrence.MemberType.SpecialType == |
| | 1 | 238 | | SpecialType.System_String, |
| | 0 | 239 | | _ => false, |
| | 3 | 240 | | }; |
| | | 241 | | case ConstructorGuardOccurrenceKind.CustomType: |
| | 2 | 242 | | return IsCustomGuardValidForGeneration( |
| | 2 | 243 | | compilation, |
| | 2 | 244 | | containingType, |
| | 2 | 245 | | occurrence, |
| | 2 | 246 | | ImmutableArray<ITypeSymbol>.Empty); |
| | | 247 | | case ConstructorGuardOccurrenceKind.Alias: |
| | 2 | 248 | | if (!TryGetForwardedArgumentTypes( |
| | 2 | 249 | | occurrence.Attribute, |
| | 2 | 250 | | out var forwardedArgumentTypes, |
| | 2 | 251 | | out _)) |
| | | 252 | | { |
| | 0 | 253 | | return false; |
| | | 254 | | } |
| | | 255 | | |
| | 2 | 256 | | return IsCustomGuardValidForGeneration( |
| | 2 | 257 | | compilation, |
| | 2 | 258 | | containingType, |
| | 2 | 259 | | occurrence, |
| | 2 | 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 | | { |
| | 83 | 308 | | method = null; |
| | 83 | 309 | | reason = null; |
| | 83 | 310 | | failureKind = GuardResolutionFailureKind.None; |
| | | 311 | | |
| | 83 | 312 | | var candidates = guardType.GetMembers(methodName) |
| | 83 | 313 | | .OfType<IMethodSymbol>() |
| | 85 | 314 | | .Where(candidate => candidate.MethodKind == MethodKind.Ordinary) |
| | 83 | 315 | | .ToList(); |
| | 83 | 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 | | |
| | 79 | 323 | | var expectedArity = memberType is null |
| | 79 | 324 | | ? (int?)null |
| | 79 | 325 | | : forwardedArgumentTypes.Length + 2; |
| | 79 | 326 | | var matches = new List<IMethodSymbol>(); |
| | | 327 | | |
| | 328 | 328 | | foreach (var candidate in candidates) |
| | | 329 | | { |
| | 85 | 330 | | if (!compilation.IsSymbolAccessibleWithin(candidate, withinType)) |
| | | 331 | | { |
| | 0 | 332 | | reason = "it is not accessible"; |
| | 0 | 333 | | failureKind = GuardResolutionFailureKind.General; |
| | 0 | 334 | | continue; |
| | | 335 | | } |
| | | 336 | | |
| | 85 | 337 | | if (!candidate.IsStatic) |
| | | 338 | | { |
| | 2 | 339 | | reason = "it is not static"; |
| | 2 | 340 | | failureKind = GuardResolutionFailureKind.General; |
| | 2 | 341 | | continue; |
| | | 342 | | } |
| | | 343 | | |
| | 83 | 344 | | if (!candidate.ReturnsVoid) |
| | | 345 | | { |
| | 2 | 346 | | reason = "it does not return void"; |
| | 2 | 347 | | failureKind = GuardResolutionFailureKind.General; |
| | 2 | 348 | | continue; |
| | | 349 | | } |
| | | 350 | | |
| | 81 | 351 | | if (candidate.Parameters.Length < 2) |
| | | 352 | | { |
| | 0 | 353 | | reason = "it does not have at least a value parameter and a trailing string parameter name"; |
| | 0 | 354 | | failureKind = GuardResolutionFailureKind.General; |
| | 0 | 355 | | continue; |
| | | 356 | | } |
| | | 357 | | |
| | 81 | 358 | | if (expectedArity.HasValue && |
| | 81 | 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 | | |
| | 79 | 366 | | if (candidate.Parameters[candidate.Parameters.Length - 1].Type.SpecialType != |
| | 79 | 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 | | |
| | 79 | 374 | | var refKindParameter = candidate.Parameters.FirstOrDefault( |
| | 285 | 375 | | parameter => parameter.RefKind != RefKind.None); |
| | 79 | 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 | | |
| | 73 | 383 | | if (memberType is null) |
| | | 384 | | { |
| | 32 | 385 | | matches.Add(candidate); |
| | 32 | 386 | | continue; |
| | | 387 | | } |
| | | 388 | | |
| | 41 | 389 | | var valueParameterType = candidate.Parameters[0].Type; |
| | 41 | 390 | | var middleParameters = candidate.Parameters |
| | 41 | 391 | | .Skip(1) |
| | 41 | 392 | | .Take(candidate.Parameters.Length - 2) |
| | 41 | 393 | | .ToList(); |
| | | 394 | | |
| | | 395 | | bool isCompatible; |
| | | 396 | | string? candidateReason; |
| | | 397 | | GuardResolutionFailureKind candidateFailureKind; |
| | | 398 | | |
| | 41 | 399 | | if (candidate.IsGenericMethod) |
| | | 400 | | { |
| | 15 | 401 | | isCompatible = TryInferGenericParameterCompatibility( |
| | 15 | 402 | | candidate, |
| | 15 | 403 | | valueParameterType, |
| | 15 | 404 | | memberType, |
| | 15 | 405 | | memberKind, |
| | 15 | 406 | | middleParameters, |
| | 15 | 407 | | forwardedArgumentTypes, |
| | 15 | 408 | | compilation, |
| | 15 | 409 | | out candidateReason, |
| | 15 | 410 | | out candidateFailureKind); |
| | | 411 | | } |
| | | 412 | | else |
| | | 413 | | { |
| | 26 | 414 | | isCompatible = TryCheckNonGenericCompatibility( |
| | 26 | 415 | | memberType, |
| | 26 | 416 | | memberKind, |
| | 26 | 417 | | valueParameterType, |
| | 26 | 418 | | middleParameters, |
| | 26 | 419 | | forwardedArgumentTypes, |
| | 26 | 420 | | compilation, |
| | 26 | 421 | | out candidateReason, |
| | 26 | 422 | | out candidateFailureKind); |
| | | 423 | | } |
| | | 424 | | |
| | 41 | 425 | | if (!isCompatible) |
| | | 426 | | { |
| | 16 | 427 | | reason = candidateReason; |
| | 16 | 428 | | failureKind = candidateFailureKind; |
| | 16 | 429 | | continue; |
| | | 430 | | } |
| | | 431 | | |
| | 25 | 432 | | matches.Add(candidate); |
| | | 433 | | } |
| | | 434 | | |
| | 79 | 435 | | if (matches.Count == 1) |
| | | 436 | | { |
| | 45 | 437 | | method = matches[0]; |
| | 45 | 438 | | failureKind = GuardResolutionFailureKind.None; |
| | 45 | 439 | | return GuardMethodResolution.Found; |
| | | 440 | | } |
| | | 441 | | |
| | 34 | 442 | | if (matches.Count > 1) |
| | | 443 | | { |
| | 6 | 444 | | failureKind = GuardResolutionFailureKind.None; |
| | 6 | 445 | | return GuardMethodResolution.Ambiguous; |
| | | 446 | | } |
| | | 447 | | |
| | 28 | 448 | | if (reason is null) |
| | | 449 | | { |
| | 0 | 450 | | reason = "no accessible static method compatible with (value, ...forwarded arguments, string parameterName) |
| | 0 | 451 | | failureKind = GuardResolutionFailureKind.General; |
| | | 452 | | } |
| | | 453 | | |
| | 28 | 454 | | return GuardMethodResolution.NotFound; |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <summary> |
| | | 458 | | /// Returns why an alias definition cannot target constructor-guard members. |
| | | 459 | | /// </summary> |
| | | 460 | | internal static string? GetGuardDefinitionTargetInvalidReason( |
| | | 461 | | INamedTypeSymbol attributeClass) |
| | | 462 | | { |
| | 38 | 463 | | if (!InheritsFromSystemAttribute(attributeClass)) |
| | 2 | 464 | | return "not derived from System.Attribute"; |
| | | 465 | | |
| | 36 | 466 | | var usageAttribute = attributeClass.GetAttributes() |
| | 36 | 467 | | .FirstOrDefault(attribute => |
| | 108 | 468 | | attribute.AttributeClass?.ToDisplayString() == |
| | 108 | 469 | | "System.AttributeUsageAttribute"); |
| | 36 | 470 | | if (usageAttribute is not null && |
| | 36 | 471 | | usageAttribute.ConstructorArguments.Length > 0 && |
| | 36 | 472 | | usageAttribute.ConstructorArguments[0].Value is int validOn && |
| | 36 | 473 | | (validOn & (AttributeTargetsField | AttributeTargetsProperty)) == 0) |
| | | 474 | | { |
| | 2 | 475 | | return "not usable on fields or properties ([AttributeUsage] includes neither AttributeTargets.Field nor Att |
| | | 476 | | } |
| | | 477 | | |
| | 34 | 478 | | return null; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | /// <summary> |
| | | 482 | | /// Gets the source location of an attribute occurrence. |
| | | 483 | | /// </summary> |
| | | 484 | | internal static Location GetAttributeLocation( |
| | | 485 | | SyntaxNodeAnalysisContext context, |
| | | 486 | | AttributeData attribute) |
| | | 487 | | { |
| | 142 | 488 | | return attribute.ApplicationSyntaxReference? |
| | 142 | 489 | | .GetSyntax(context.CancellationToken) |
| | 142 | 490 | | .GetLocation() ?? Location.None; |
| | | 491 | | } |
| | | 492 | | |
| | | 493 | | private static ConstructorGuardOccurrence CreateOccurrence( |
| | | 494 | | ISymbol member, |
| | | 495 | | ITypeSymbol memberType, |
| | | 496 | | string memberKind, |
| | | 497 | | AttributeData attribute, |
| | | 498 | | ConstructorGuardOccurrenceKind kind, |
| | | 499 | | string? ineligibilityReason, |
| | | 500 | | ITypeSymbol? guardType, |
| | | 501 | | string? methodName, |
| | | 502 | | bool methodNameExplicit, |
| | | 503 | | bool guardTypeUsageIsInSourceAlias) |
| | | 504 | | { |
| | 92 | 505 | | return new ConstructorGuardOccurrence( |
| | 92 | 506 | | member, |
| | 92 | 507 | | memberType, |
| | 92 | 508 | | memberKind, |
| | 92 | 509 | | attribute, |
| | 92 | 510 | | kind, |
| | 92 | 511 | | ineligibilityReason, |
| | 92 | 512 | | guardType, |
| | 92 | 513 | | methodName, |
| | 92 | 514 | | methodNameExplicit, |
| | 92 | 515 | | guardTypeUsageIsInSourceAlias); |
| | | 516 | | } |
| | | 517 | | |
| | | 518 | | private static bool IsCustomGuardValidForGeneration( |
| | | 519 | | Compilation compilation, |
| | | 520 | | INamedTypeSymbol containingType, |
| | | 521 | | ConstructorGuardOccurrence occurrence, |
| | | 522 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes) |
| | | 523 | | { |
| | 4 | 524 | | if (occurrence.GuardType is null || |
| | 4 | 525 | | occurrence.GuardType.TypeKind == TypeKind.Error || |
| | 4 | 526 | | !compilation.IsSymbolAccessibleWithin( |
| | 4 | 527 | | occurrence.GuardType, |
| | 4 | 528 | | containingType) || |
| | 4 | 529 | | occurrence.MethodNameExplicit && |
| | 4 | 530 | | string.IsNullOrWhiteSpace(occurrence.MethodName)) |
| | | 531 | | { |
| | 0 | 532 | | return false; |
| | | 533 | | } |
| | | 534 | | |
| | 4 | 535 | | var methodName = string.IsNullOrEmpty(occurrence.MethodName) |
| | 4 | 536 | | ? DefaultGuardMethodName |
| | 4 | 537 | | : occurrence.MethodName!; |
| | 4 | 538 | | return TryResolveGuardMethod( |
| | 4 | 539 | | compilation, |
| | 4 | 540 | | containingType, |
| | 4 | 541 | | occurrence.GuardType, |
| | 4 | 542 | | methodName, |
| | 4 | 543 | | occurrence.MemberType, |
| | 4 | 544 | | occurrence.MemberKind, |
| | 4 | 545 | | forwardedArgumentTypes, |
| | 4 | 546 | | out _, |
| | 4 | 547 | | out _, |
| | 4 | 548 | | out _) == GuardMethodResolution.Found; |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | private static bool IsDefinedEnumValue(TypedConstant constant) |
| | | 552 | | { |
| | 19 | 553 | | if (constant.Kind != TypedConstantKind.Enum || |
| | 19 | 554 | | constant.Value is not int rawValue || |
| | 19 | 555 | | constant.Type is not { } enumType) |
| | | 556 | | { |
| | 0 | 557 | | return false; |
| | | 558 | | } |
| | | 559 | | |
| | 19 | 560 | | return enumType.GetMembers() |
| | 19 | 561 | | .OfType<IFieldSymbol>() |
| | 19 | 562 | | .Any(field => |
| | 73 | 563 | | field.HasConstantValue && |
| | 73 | 564 | | field.ConstantValue is int memberValue && |
| | 73 | 565 | | memberValue == rawValue); |
| | | 566 | | } |
| | | 567 | | |
| | | 568 | | private static void AnalyzeAliasGuardAtUsage( |
| | | 569 | | SyntaxNodeAnalysisContext context, |
| | | 570 | | INamedTypeSymbol containingType, |
| | | 571 | | ConstructorGuardOccurrence occurrence, |
| | | 572 | | Location location) |
| | | 573 | | { |
| | 30 | 574 | | if (!TryGetForwardedArgumentTypes( |
| | 30 | 575 | | occurrence.Attribute, |
| | 30 | 576 | | out var forwardedArgumentTypes, |
| | 30 | 577 | | out var unsupportedReason)) |
| | | 578 | | { |
| | 6 | 579 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 6 | 580 | | DiagnosticDescriptors.ConstructorGuardAliasUsageArgumentUnsupported, |
| | 6 | 581 | | location, |
| | 6 | 582 | | occurrence.Member.Name, |
| | 6 | 583 | | unsupportedReason)); |
| | 6 | 584 | | return; |
| | | 585 | | } |
| | | 586 | | |
| | 24 | 587 | | if (!occurrence.GuardTypeUsageIsInSourceAlias) |
| | | 588 | | { |
| | 0 | 589 | | AnalyzeCustomGuard( |
| | 0 | 590 | | context, |
| | 0 | 591 | | containingType, |
| | 0 | 592 | | occurrence, |
| | 0 | 593 | | location, |
| | 0 | 594 | | occurrence.GuardType, |
| | 0 | 595 | | occurrence.MethodName, |
| | 0 | 596 | | occurrence.MethodNameExplicit, |
| | 0 | 597 | | forwardedArgumentTypes); |
| | 0 | 598 | | return; |
| | | 599 | | } |
| | | 600 | | |
| | 24 | 601 | | if (occurrence.GuardType is null || |
| | 24 | 602 | | occurrence.GuardType.TypeKind == TypeKind.Error) |
| | | 603 | | { |
| | 0 | 604 | | return; |
| | | 605 | | } |
| | | 606 | | |
| | 24 | 607 | | var methodName = string.IsNullOrEmpty(occurrence.MethodName) |
| | 24 | 608 | | ? DefaultGuardMethodName |
| | 24 | 609 | | : occurrence.MethodName!; |
| | 24 | 610 | | var resolution = TryResolveGuardMethod( |
| | 24 | 611 | | context.Compilation, |
| | 24 | 612 | | containingType, |
| | 24 | 613 | | occurrence.GuardType, |
| | 24 | 614 | | methodName, |
| | 24 | 615 | | occurrence.MemberType, |
| | 24 | 616 | | occurrence.MemberKind, |
| | 24 | 617 | | forwardedArgumentTypes, |
| | 24 | 618 | | out _, |
| | 24 | 619 | | out var reason, |
| | 24 | 620 | | out var failureKind); |
| | | 621 | | |
| | 24 | 622 | | if (resolution == GuardMethodResolution.NotFound && |
| | 24 | 623 | | failureKind == GuardResolutionFailureKind.ForwardedArgument) |
| | | 624 | | { |
| | 10 | 625 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 10 | 626 | | DiagnosticDescriptors.ConstructorGuardForwardedArgumentIncompatible, |
| | 10 | 627 | | location, |
| | 10 | 628 | | methodName, |
| | 10 | 629 | | occurrence.GuardType.ToDisplayString(), |
| | 10 | 630 | | occurrence.Member.Name, |
| | 10 | 631 | | occurrence.MemberType.ToDisplayString(), |
| | 10 | 632 | | reason)); |
| | | 633 | | } |
| | 14 | 634 | | else if (resolution == GuardMethodResolution.NotFound && |
| | 14 | 635 | | reason == GetMemberTypeIncompatibleReason(occurrence.MemberKind)) |
| | | 636 | | { |
| | 2 | 637 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 638 | | DiagnosticDescriptors.ConstructorGuardMethodInvalid, |
| | 2 | 639 | | location, |
| | 2 | 640 | | methodName, |
| | 2 | 641 | | occurrence.GuardType.ToDisplayString(), |
| | 2 | 642 | | occurrence.Member.Name, |
| | 2 | 643 | | occurrence.MemberType.ToDisplayString(), |
| | 2 | 644 | | reason)); |
| | | 645 | | } |
| | 12 | 646 | | else if (resolution == GuardMethodResolution.Ambiguous) |
| | | 647 | | { |
| | 2 | 648 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 649 | | DiagnosticDescriptors.ConstructorGuardMethodAmbiguous, |
| | 2 | 650 | | location, |
| | 2 | 651 | | methodName, |
| | 2 | 652 | | occurrence.GuardType.ToDisplayString(), |
| | 2 | 653 | | occurrence.Member.Name, |
| | 2 | 654 | | occurrence.MemberType.ToDisplayString())); |
| | | 655 | | } |
| | 12 | 656 | | } |
| | | 657 | | |
| | | 658 | | private static void AnalyzeBuiltInGuard( |
| | | 659 | | SyntaxNodeAnalysisContext context, |
| | | 660 | | ConstructorGuardOccurrence occurrence, |
| | | 661 | | Location location) |
| | | 662 | | { |
| | 14 | 663 | | var first = occurrence.Attribute.ConstructorArguments[0]; |
| | 14 | 664 | | if (TryReportUndefinedEnum(context, location, first)) |
| | 2 | 665 | | return; |
| | | 666 | | |
| | 12 | 667 | | if (first.Value is not int rawValue) |
| | 0 | 668 | | return; |
| | | 669 | | |
| | 12 | 670 | | var kind = (BuiltInConstructorGuardKindMirror)rawValue; |
| | | 671 | | switch (kind) |
| | | 672 | | { |
| | | 673 | | case BuiltInConstructorGuardKindMirror.NotNull: |
| | 7 | 674 | | if (!CanBeRuntimeNull(occurrence.MemberType)) |
| | | 675 | | { |
| | 4 | 676 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 677 | | DiagnosticDescriptors.ConstructorGuardIncompatibleWithFieldType, |
| | 4 | 678 | | location, |
| | 4 | 679 | | "NotNull", |
| | 4 | 680 | | occurrence.Member.Name, |
| | 4 | 681 | | occurrence.MemberType.ToDisplayString(), |
| | 4 | 682 | | $"the {occurrence.MemberKind}'s type is a non-nullable value type, so a runtime null value is ne |
| | | 683 | | } |
| | | 684 | | |
| | 4 | 685 | | break; |
| | | 686 | | case BuiltInConstructorGuardKindMirror.NotNullOrEmpty: |
| | | 687 | | case BuiltInConstructorGuardKindMirror.NotNullOrWhiteSpace: |
| | 5 | 688 | | if (occurrence.MemberType.SpecialType != SpecialType.System_String) |
| | | 689 | | { |
| | 4 | 690 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 691 | | DiagnosticDescriptors.ConstructorGuardIncompatibleWithFieldType, |
| | 4 | 692 | | location, |
| | 4 | 693 | | kind.ToString(), |
| | 4 | 694 | | occurrence.Member.Name, |
| | 4 | 695 | | occurrence.MemberType.ToDisplayString(), |
| | 4 | 696 | | $"this guard only applies to string-compatible {GetMemberKindPlural(occurrence.MemberKind)}")); |
| | | 697 | | } |
| | | 698 | | |
| | | 699 | | break; |
| | | 700 | | } |
| | 8 | 701 | | } |
| | | 702 | | |
| | | 703 | | private static void AnalyzeCustomGuard( |
| | | 704 | | SyntaxNodeAnalysisContext context, |
| | | 705 | | INamedTypeSymbol containingType, |
| | | 706 | | ConstructorGuardOccurrence occurrence, |
| | | 707 | | Location location, |
| | | 708 | | ITypeSymbol? guardType, |
| | | 709 | | string? methodName, |
| | | 710 | | bool methodNameExplicit, |
| | | 711 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes) |
| | | 712 | | { |
| | 27 | 713 | | if (guardType is null || guardType.TypeKind == TypeKind.Error) |
| | | 714 | | { |
| | 2 | 715 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 716 | | DiagnosticDescriptors.ConstructorGuardTypeInvalid, |
| | 2 | 717 | | location, |
| | 2 | 718 | | occurrence.Member.Name, |
| | 2 | 719 | | "the guard type could not be resolved")); |
| | 2 | 720 | | return; |
| | | 721 | | } |
| | | 722 | | |
| | 25 | 723 | | if (!context.Compilation.IsSymbolAccessibleWithin(guardType, containingType)) |
| | | 724 | | { |
| | 0 | 725 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 726 | | DiagnosticDescriptors.ConstructorGuardTypeInvalid, |
| | 0 | 727 | | location, |
| | 0 | 728 | | occurrence.Member.Name, |
| | 0 | 729 | | $"'{guardType.ToDisplayString()}' is not accessible from '{containingType.ToDisplayString()}'")); |
| | 0 | 730 | | return; |
| | | 731 | | } |
| | | 732 | | |
| | 25 | 733 | | if (methodNameExplicit && string.IsNullOrWhiteSpace(methodName)) |
| | | 734 | | { |
| | 2 | 735 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 736 | | DiagnosticDescriptors.ConstructorGuardMethodNameInvalid, |
| | 2 | 737 | | location, |
| | 2 | 738 | | occurrence.Member.Name)); |
| | 2 | 739 | | return; |
| | | 740 | | } |
| | | 741 | | |
| | 23 | 742 | | var effectiveMethodName = string.IsNullOrEmpty(methodName) |
| | 23 | 743 | | ? DefaultGuardMethodName |
| | 23 | 744 | | : methodName!; |
| | 23 | 745 | | var resolution = TryResolveGuardMethod( |
| | 23 | 746 | | context.Compilation, |
| | 23 | 747 | | containingType, |
| | 23 | 748 | | guardType, |
| | 23 | 749 | | effectiveMethodName, |
| | 23 | 750 | | occurrence.MemberType, |
| | 23 | 751 | | occurrence.MemberKind, |
| | 23 | 752 | | forwardedArgumentTypes, |
| | 23 | 753 | | out _, |
| | 23 | 754 | | out var reason, |
| | 23 | 755 | | out var failureKind); |
| | | 756 | | |
| | 23 | 757 | | switch (resolution) |
| | | 758 | | { |
| | | 759 | | case GuardMethodResolution.NotFound |
| | 18 | 760 | | when failureKind == GuardResolutionFailureKind.ForwardedArgument: |
| | 0 | 761 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 762 | | DiagnosticDescriptors.ConstructorGuardForwardedArgumentIncompatible, |
| | 0 | 763 | | location, |
| | 0 | 764 | | effectiveMethodName, |
| | 0 | 765 | | guardType.ToDisplayString(), |
| | 0 | 766 | | occurrence.Member.Name, |
| | 0 | 767 | | occurrence.MemberType.ToDisplayString(), |
| | 0 | 768 | | reason)); |
| | 0 | 769 | | break; |
| | | 770 | | case GuardMethodResolution.NotFound: |
| | 18 | 771 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 18 | 772 | | DiagnosticDescriptors.ConstructorGuardMethodInvalid, |
| | 18 | 773 | | location, |
| | 18 | 774 | | effectiveMethodName, |
| | 18 | 775 | | guardType.ToDisplayString(), |
| | 18 | 776 | | occurrence.Member.Name, |
| | 18 | 777 | | occurrence.MemberType.ToDisplayString(), |
| | 18 | 778 | | reason ?? "no compatible method was found")); |
| | 18 | 779 | | break; |
| | | 780 | | case GuardMethodResolution.Ambiguous: |
| | 2 | 781 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 782 | | DiagnosticDescriptors.ConstructorGuardMethodAmbiguous, |
| | 2 | 783 | | location, |
| | 2 | 784 | | effectiveMethodName, |
| | 2 | 785 | | guardType.ToDisplayString(), |
| | 2 | 786 | | occurrence.Member.Name, |
| | 2 | 787 | | occurrence.MemberType.ToDisplayString())); |
| | | 788 | | break; |
| | | 789 | | } |
| | 2 | 790 | | } |
| | | 791 | | |
| | | 792 | | private static bool TryGetForwardedArgumentTypes( |
| | | 793 | | AttributeData attribute, |
| | | 794 | | out ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 795 | | out string? unsupportedReason) |
| | | 796 | | { |
| | 32 | 797 | | if (attribute.NamedArguments.Length > 0) |
| | | 798 | | { |
| | 2 | 799 | | forwardedArgumentTypes = ImmutableArray<ITypeSymbol>.Empty; |
| | 2 | 800 | | unsupportedReason = $"named argument '{attribute.NamedArguments[0].Key}' is not forwarded to the guard metho |
| | 2 | 801 | | return false; |
| | | 802 | | } |
| | | 803 | | |
| | 30 | 804 | | if (attribute.ConstructorArguments.Length == 0) |
| | | 805 | | { |
| | 3 | 806 | | forwardedArgumentTypes = ImmutableArray<ITypeSymbol>.Empty; |
| | 3 | 807 | | unsupportedReason = null; |
| | 3 | 808 | | return true; |
| | | 809 | | } |
| | | 810 | | |
| | 27 | 811 | | var builder = ImmutableArray.CreateBuilder<ITypeSymbol>( |
| | 27 | 812 | | attribute.ConstructorArguments.Length); |
| | 106 | 813 | | for (var i = 0; i < attribute.ConstructorArguments.Length; i++) |
| | | 814 | | { |
| | 30 | 815 | | var constant = attribute.ConstructorArguments[i]; |
| | 30 | 816 | | if (!TypedConstantRenderer.TryRender(constant, out _)) |
| | | 817 | | { |
| | 4 | 818 | | forwardedArgumentTypes = ImmutableArray<ITypeSymbol>.Empty; |
| | 4 | 819 | | unsupportedReason = $"positional argument {i + 1} is {DescribeUnsupportedConstant(constant)}, which is n |
| | 4 | 820 | | return false; |
| | | 821 | | } |
| | | 822 | | |
| | 26 | 823 | | builder.Add(constant.Type!); |
| | | 824 | | } |
| | | 825 | | |
| | 23 | 826 | | forwardedArgumentTypes = builder.MoveToImmutable(); |
| | 23 | 827 | | unsupportedReason = null; |
| | 23 | 828 | | return true; |
| | | 829 | | } |
| | | 830 | | |
| | | 831 | | private static string DescribeUnsupportedConstant(TypedConstant constant) |
| | | 832 | | { |
| | 4 | 833 | | if (constant.Kind == TypedConstantKind.Array) |
| | 2 | 834 | | return "an array"; |
| | | 835 | | |
| | 2 | 836 | | if (constant.Value is float or double) |
| | 2 | 837 | | return "a floating-point value"; |
| | | 838 | | |
| | 0 | 839 | | return "an unsupported value"; |
| | | 840 | | } |
| | | 841 | | |
| | | 842 | | private static bool CanBeRuntimeNull(ITypeSymbol type) |
| | | 843 | | { |
| | 9 | 844 | | if (type.IsReferenceType) |
| | 3 | 845 | | return true; |
| | | 846 | | |
| | 6 | 847 | | if (type is ITypeParameterSymbol typeParameter) |
| | | 848 | | { |
| | 3 | 849 | | return !typeParameter.HasValueTypeConstraint && |
| | 3 | 850 | | !typeParameter.HasUnmanagedTypeConstraint; |
| | | 851 | | } |
| | | 852 | | |
| | 3 | 853 | | return type is INamedTypeSymbol |
| | 3 | 854 | | { |
| | 3 | 855 | | OriginalDefinition.SpecialType: SpecialType.System_Nullable_T, |
| | 3 | 856 | | }; |
| | | 857 | | } |
| | | 858 | | |
| | | 859 | | private static bool IsAssignableTo( |
| | | 860 | | ITypeSymbol sourceType, |
| | | 861 | | ITypeSymbol parameterType, |
| | | 862 | | Compilation compilation) |
| | | 863 | | { |
| | 43 | 864 | | if (SymbolEqualityComparer.Default.Equals(sourceType, parameterType)) |
| | 35 | 865 | | return true; |
| | | 866 | | |
| | 8 | 867 | | var conversion = compilation.ClassifyConversion(sourceType, parameterType); |
| | 8 | 868 | | return conversion.Exists && (conversion.IsIdentity || conversion.IsImplicit); |
| | | 869 | | } |
| | | 870 | | |
| | | 871 | | private static bool TryCheckNonGenericCompatibility( |
| | | 872 | | ITypeSymbol memberType, |
| | | 873 | | string memberKind, |
| | | 874 | | ITypeSymbol valueParameterType, |
| | | 875 | | List<IParameterSymbol> middleParameters, |
| | | 876 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 877 | | Compilation compilation, |
| | | 878 | | out string? reason, |
| | | 879 | | out GuardResolutionFailureKind failureKind) |
| | | 880 | | { |
| | 26 | 881 | | if (!IsAssignableTo(memberType, valueParameterType, compilation)) |
| | | 882 | | { |
| | 4 | 883 | | reason = GetMemberTypeIncompatibleReason(memberKind); |
| | 4 | 884 | | failureKind = GuardResolutionFailureKind.General; |
| | 4 | 885 | | return false; |
| | | 886 | | } |
| | | 887 | | |
| | 70 | 888 | | for (var i = 0; i < middleParameters.Count; i++) |
| | | 889 | | { |
| | 17 | 890 | | if (IsAssignableTo( |
| | 17 | 891 | | forwardedArgumentTypes[i], |
| | 17 | 892 | | middleParameters[i].Type, |
| | 17 | 893 | | compilation)) |
| | | 894 | | { |
| | | 895 | | continue; |
| | | 896 | | } |
| | | 897 | | |
| | 4 | 898 | | reason = $"its parameter '{middleParameters[i].Name}' of type '{middleParameters[i].Type.ToDisplayString()}' |
| | 4 | 899 | | failureKind = GuardResolutionFailureKind.ForwardedArgument; |
| | 4 | 900 | | return false; |
| | | 901 | | } |
| | | 902 | | |
| | 18 | 903 | | reason = null; |
| | 18 | 904 | | failureKind = GuardResolutionFailureKind.None; |
| | 18 | 905 | | return true; |
| | | 906 | | } |
| | | 907 | | |
| | | 908 | | private static bool TryInferGenericParameterCompatibility( |
| | | 909 | | IMethodSymbol genericMethod, |
| | | 910 | | ITypeSymbol parameterType, |
| | | 911 | | ITypeSymbol memberType, |
| | | 912 | | string memberKind, |
| | | 913 | | List<IParameterSymbol> middleParameters, |
| | | 914 | | ImmutableArray<ITypeSymbol> forwardedArgumentTypes, |
| | | 915 | | Compilation compilation, |
| | | 916 | | out string? reason, |
| | | 917 | | out GuardResolutionFailureKind failureKind) |
| | | 918 | | { |
| | 15 | 919 | | var methodTypeParameters = new HashSet<ITypeSymbol>( |
| | 15 | 920 | | genericMethod.TypeParameters, |
| | 15 | 921 | | SymbolEqualityComparer.Default); |
| | 15 | 922 | | string? lastReason = null; |
| | 15 | 923 | | var lastFailureKind = GuardResolutionFailureKind.General; |
| | | 924 | | |
| | 173 | 925 | | foreach (var candidateType in GetTypeAndSupertypes(memberType)) |
| | | 926 | | { |
| | 75 | 927 | | var substitution = new Dictionary<ITypeSymbol, ITypeSymbol>( |
| | 75 | 928 | | SymbolEqualityComparer.Default); |
| | 75 | 929 | | if (!TryUnify( |
| | 75 | 930 | | parameterType, |
| | 75 | 931 | | candidateType, |
| | 75 | 932 | | methodTypeParameters, |
| | 75 | 933 | | substitution)) |
| | | 934 | | { |
| | | 935 | | continue; |
| | | 936 | | } |
| | | 937 | | |
| | 75 | 938 | | var forwardedMismatch = false; |
| | 188 | 939 | | for (var i = 0; i < middleParameters.Count; i++) |
| | | 940 | | { |
| | 35 | 941 | | if (TryUnify( |
| | 35 | 942 | | middleParameters[i].Type, |
| | 35 | 943 | | forwardedArgumentTypes[i], |
| | 35 | 944 | | methodTypeParameters, |
| | 35 | 945 | | substitution)) |
| | | 946 | | { |
| | | 947 | | continue; |
| | | 948 | | } |
| | | 949 | | |
| | 16 | 950 | | lastReason ??= $"its parameter '{middleParameters[i].Name}' cannot accept the forwarded argument of type |
| | 16 | 951 | | lastFailureKind = GuardResolutionFailureKind.ForwardedArgument; |
| | 16 | 952 | | forwardedMismatch = true; |
| | 16 | 953 | | break; |
| | | 954 | | } |
| | | 955 | | |
| | 75 | 956 | | if (forwardedMismatch) |
| | | 957 | | continue; |
| | | 958 | | |
| | 59 | 959 | | var unboundParameter = genericMethod.TypeParameters.FirstOrDefault( |
| | 152 | 960 | | typeParameter => !substitution.ContainsKey(typeParameter)); |
| | 59 | 961 | | if (unboundParameter is not null) |
| | | 962 | | { |
| | 18 | 963 | | var onlyForwarded = IsReachableOnlyThroughForwardedParameters( |
| | 18 | 964 | | unboundParameter, |
| | 18 | 965 | | parameterType, |
| | 18 | 966 | | middleParameters); |
| | 18 | 967 | | lastReason ??= onlyForwarded |
| | 18 | 968 | | ? $"its type parameter '{unboundParameter.Name}' cannot be inferred from the {memberKind}'s type or |
| | 18 | 969 | | : $"its type parameter '{unboundParameter.Name}' cannot be inferred from the {memberKind}'s type"; |
| | 18 | 970 | | lastFailureKind = onlyForwarded |
| | 18 | 971 | | ? GuardResolutionFailureKind.ForwardedArgument |
| | 18 | 972 | | : GuardResolutionFailureKind.General; |
| | 18 | 973 | | continue; |
| | | 974 | | } |
| | | 975 | | |
| | 41 | 976 | | var constraintViolation = FindConstraintViolation( |
| | 41 | 977 | | genericMethod.TypeParameters, |
| | 41 | 978 | | substitution, |
| | 41 | 979 | | compilation, |
| | 41 | 980 | | out var violatingTypeParameter); |
| | 41 | 981 | | if (constraintViolation is not null) |
| | | 982 | | { |
| | 34 | 983 | | var onlyForwarded = violatingTypeParameter is not null && |
| | 34 | 984 | | IsReachableOnlyThroughForwardedParameters( |
| | 34 | 985 | | violatingTypeParameter, |
| | 34 | 986 | | parameterType, |
| | 34 | 987 | | middleParameters); |
| | 34 | 988 | | lastReason ??= constraintViolation; |
| | 34 | 989 | | lastFailureKind = onlyForwarded |
| | 34 | 990 | | ? GuardResolutionFailureKind.ForwardedArgument |
| | 34 | 991 | | : GuardResolutionFailureKind.General; |
| | 34 | 992 | | continue; |
| | | 993 | | } |
| | | 994 | | |
| | 7 | 995 | | reason = null; |
| | 7 | 996 | | failureKind = GuardResolutionFailureKind.None; |
| | 7 | 997 | | return true; |
| | | 998 | | } |
| | | 999 | | |
| | 8 | 1000 | | reason = lastReason ?? GetMemberTypeIncompatibleReason(memberKind); |
| | 8 | 1001 | | failureKind = lastReason is null |
| | 8 | 1002 | | ? GuardResolutionFailureKind.General |
| | 8 | 1003 | | : lastFailureKind; |
| | 8 | 1004 | | return false; |
| | 7 | 1005 | | } |
| | | 1006 | | |
| | | 1007 | | private static string GetMemberTypeIncompatibleReason(string memberKind) |
| | | 1008 | | { |
| | 6 | 1009 | | return $"its value parameter type is not compatible with the {memberKind}'s type"; |
| | | 1010 | | } |
| | | 1011 | | |
| | | 1012 | | private static string GetMemberKindPlural(string memberKind) |
| | | 1013 | | { |
| | 4 | 1014 | | return memberKind == "property" ? "properties" : memberKind + "s"; |
| | | 1015 | | } |
| | | 1016 | | |
| | | 1017 | | private static bool ContainsTypeParameter( |
| | | 1018 | | ITypeSymbol type, |
| | | 1019 | | ITypeParameterSymbol typeParameter) |
| | | 1020 | | { |
| | 68 | 1021 | | if (SymbolEqualityComparer.Default.Equals(type, typeParameter)) |
| | 34 | 1022 | | return true; |
| | | 1023 | | |
| | 34 | 1024 | | if (type is INamedTypeSymbol namedType) |
| | | 1025 | | { |
| | 0 | 1026 | | return namedType.TypeArguments.Any( |
| | 0 | 1027 | | typeArgument => ContainsTypeParameter(typeArgument, typeParameter)); |
| | | 1028 | | } |
| | | 1029 | | |
| | 34 | 1030 | | if (type is IArrayTypeSymbol arrayType) |
| | 0 | 1031 | | return ContainsTypeParameter(arrayType.ElementType, typeParameter); |
| | | 1032 | | |
| | 34 | 1033 | | return false; |
| | | 1034 | | } |
| | | 1035 | | |
| | | 1036 | | private static bool IsReachableOnlyThroughForwardedParameters( |
| | | 1037 | | ITypeParameterSymbol typeParameter, |
| | | 1038 | | ITypeSymbol valueParameterType, |
| | | 1039 | | List<IParameterSymbol> middleParameters) |
| | | 1040 | | { |
| | 52 | 1041 | | if (ContainsTypeParameter(valueParameterType, typeParameter)) |
| | 18 | 1042 | | return false; |
| | | 1043 | | |
| | 34 | 1044 | | return middleParameters.Any( |
| | 50 | 1045 | | parameter => ContainsTypeParameter(parameter.Type, typeParameter)); |
| | | 1046 | | } |
| | | 1047 | | |
| | | 1048 | | private static string? FindConstraintViolation( |
| | | 1049 | | ImmutableArray<ITypeParameterSymbol> typeParameters, |
| | | 1050 | | Dictionary<ITypeSymbol, ITypeSymbol> substitution, |
| | | 1051 | | Compilation compilation, |
| | | 1052 | | out ITypeParameterSymbol? violatingTypeParameter) |
| | | 1053 | | { |
| | 162 | 1054 | | foreach (var typeParameter in typeParameters) |
| | | 1055 | | { |
| | 57 | 1056 | | if (!substitution.TryGetValue(typeParameter, out var argumentType)) |
| | | 1057 | | continue; |
| | | 1058 | | |
| | 57 | 1059 | | if (typeParameter.HasReferenceTypeConstraint && |
| | 57 | 1060 | | !argumentType.IsReferenceType) |
| | | 1061 | | { |
| | 0 | 1062 | | violatingTypeParameter = typeParameter; |
| | 0 | 1063 | | return $"its type parameter '{typeParameter.Name}' requires a reference type, but '{argumentType.ToDispl |
| | | 1064 | | } |
| | | 1065 | | |
| | 57 | 1066 | | if (typeParameter.HasValueTypeConstraint && |
| | 57 | 1067 | | !argumentType.IsValueType) |
| | | 1068 | | { |
| | 0 | 1069 | | violatingTypeParameter = typeParameter; |
| | 0 | 1070 | | return $"its type parameter '{typeParameter.Name}' requires a non-nullable value type, but '{argumentTyp |
| | | 1071 | | } |
| | | 1072 | | |
| | 57 | 1073 | | if (typeParameter.HasUnmanagedTypeConstraint && |
| | 57 | 1074 | | !argumentType.IsUnmanagedType) |
| | | 1075 | | { |
| | 0 | 1076 | | violatingTypeParameter = typeParameter; |
| | 0 | 1077 | | return $"its type parameter '{typeParameter.Name}' requires an unmanaged type, but '{argumentType.ToDisp |
| | | 1078 | | } |
| | | 1079 | | |
| | 57 | 1080 | | if (typeParameter.HasConstructorConstraint && |
| | 57 | 1081 | | argumentType is INamedTypeSymbol namedArgumentType && |
| | 57 | 1082 | | namedArgumentType.TypeKind != TypeKind.Struct && |
| | 57 | 1083 | | !GeneratedConstructorEligibility.HasAccessibleParameterlessConstructor( |
| | 57 | 1084 | | namedArgumentType)) |
| | | 1085 | | { |
| | 0 | 1086 | | violatingTypeParameter = typeParameter; |
| | 0 | 1087 | | return $"its type parameter '{typeParameter.Name}' requires a public parameterless constructor, which '{ |
| | | 1088 | | } |
| | | 1089 | | |
| | 148 | 1090 | | foreach (var constraintType in typeParameter.ConstraintTypes) |
| | | 1091 | | { |
| | 34 | 1092 | | if (SymbolEqualityComparer.Default.Equals( |
| | 34 | 1093 | | argumentType, |
| | 34 | 1094 | | constraintType)) |
| | | 1095 | | { |
| | | 1096 | | continue; |
| | | 1097 | | } |
| | | 1098 | | |
| | 34 | 1099 | | var conversion = compilation.ClassifyConversion( |
| | 34 | 1100 | | argumentType, |
| | 34 | 1101 | | constraintType); |
| | 34 | 1102 | | if (conversion.Exists && |
| | 34 | 1103 | | (conversion.IsIdentity || conversion.IsImplicit)) |
| | | 1104 | | { |
| | | 1105 | | continue; |
| | | 1106 | | } |
| | | 1107 | | |
| | 34 | 1108 | | violatingTypeParameter = typeParameter; |
| | 34 | 1109 | | return $"its type parameter '{typeParameter.Name}' requires '{constraintType.ToDisplayString()}', which |
| | | 1110 | | } |
| | | 1111 | | } |
| | | 1112 | | |
| | 7 | 1113 | | violatingTypeParameter = null; |
| | 7 | 1114 | | return null; |
| | | 1115 | | } |
| | | 1116 | | |
| | | 1117 | | private static IEnumerable<ITypeSymbol> GetTypeAndSupertypes(ITypeSymbol type) |
| | | 1118 | | { |
| | 15 | 1119 | | yield return type; |
| | | 1120 | | |
| | 8 | 1121 | | var current = (type as INamedTypeSymbol)?.BaseType; |
| | 20 | 1122 | | while (current is not null) |
| | | 1123 | | { |
| | 12 | 1124 | | yield return current; |
| | 12 | 1125 | | current = current.BaseType; |
| | | 1126 | | } |
| | | 1127 | | |
| | 112 | 1128 | | foreach (var iface in type.AllInterfaces) |
| | | 1129 | | { |
| | 48 | 1130 | | yield return iface; |
| | | 1131 | | } |
| | 8 | 1132 | | } |
| | | 1133 | | |
| | | 1134 | | private static bool TryUnify( |
| | | 1135 | | ITypeSymbol parameterType, |
| | | 1136 | | ITypeSymbol candidateType, |
| | | 1137 | | HashSet<ITypeSymbol> methodTypeParameters, |
| | | 1138 | | Dictionary<ITypeSymbol, ITypeSymbol> substitution) |
| | | 1139 | | { |
| | 112 | 1140 | | if (parameterType is ITypeParameterSymbol typeParameter && |
| | 112 | 1141 | | methodTypeParameters.Contains(typeParameter)) |
| | | 1142 | | { |
| | 108 | 1143 | | if (substitution.TryGetValue(typeParameter, out var bound)) |
| | 17 | 1144 | | return SymbolEqualityComparer.Default.Equals(bound, candidateType); |
| | | 1145 | | |
| | 91 | 1146 | | substitution[typeParameter] = candidateType; |
| | 91 | 1147 | | return true; |
| | | 1148 | | } |
| | | 1149 | | |
| | 4 | 1150 | | if (parameterType is INamedTypeSymbol namedParameter && |
| | 4 | 1151 | | candidateType is INamedTypeSymbol namedCandidate) |
| | | 1152 | | { |
| | 4 | 1153 | | if (!SymbolEqualityComparer.Default.Equals( |
| | 4 | 1154 | | namedParameter.OriginalDefinition, |
| | 4 | 1155 | | namedCandidate.OriginalDefinition)) |
| | | 1156 | | { |
| | 0 | 1157 | | return false; |
| | | 1158 | | } |
| | | 1159 | | |
| | 4 | 1160 | | if (namedParameter.TypeArguments.Length != |
| | 4 | 1161 | | namedCandidate.TypeArguments.Length) |
| | | 1162 | | { |
| | 0 | 1163 | | return false; |
| | | 1164 | | } |
| | | 1165 | | |
| | 12 | 1166 | | for (var i = 0; i < namedParameter.TypeArguments.Length; i++) |
| | | 1167 | | { |
| | 2 | 1168 | | if (!TryUnify( |
| | 2 | 1169 | | namedParameter.TypeArguments[i], |
| | 2 | 1170 | | namedCandidate.TypeArguments[i], |
| | 2 | 1171 | | methodTypeParameters, |
| | 2 | 1172 | | substitution)) |
| | | 1173 | | { |
| | 0 | 1174 | | return false; |
| | | 1175 | | } |
| | | 1176 | | } |
| | | 1177 | | |
| | 4 | 1178 | | return true; |
| | | 1179 | | } |
| | | 1180 | | |
| | 0 | 1181 | | if (parameterType is IArrayTypeSymbol arrayParameter && |
| | 0 | 1182 | | candidateType is IArrayTypeSymbol arrayCandidate) |
| | | 1183 | | { |
| | 0 | 1184 | | return TryUnify( |
| | 0 | 1185 | | arrayParameter.ElementType, |
| | 0 | 1186 | | arrayCandidate.ElementType, |
| | 0 | 1187 | | methodTypeParameters, |
| | 0 | 1188 | | substitution); |
| | | 1189 | | } |
| | | 1190 | | |
| | 0 | 1191 | | return SymbolEqualityComparer.Default.Equals(parameterType, candidateType); |
| | | 1192 | | } |
| | | 1193 | | |
| | | 1194 | | private static bool InheritsFromSystemAttribute(INamedTypeSymbol type) |
| | | 1195 | | { |
| | 38 | 1196 | | var current = type.BaseType; |
| | 40 | 1197 | | while (current is not null) |
| | | 1198 | | { |
| | 38 | 1199 | | if (current.ToDisplayString() == "System.Attribute") |
| | 36 | 1200 | | return true; |
| | | 1201 | | |
| | 2 | 1202 | | current = current.BaseType; |
| | | 1203 | | } |
| | | 1204 | | |
| | 2 | 1205 | | return false; |
| | | 1206 | | } |
| | | 1207 | | } |