Reflect modern generics, opaque types & generalized existentials#77
Open
NSExceptional wants to merge 4 commits into
Open
Reflect modern generics, opaque types & generalized existentials#77NSExceptional wants to merge 4 commits into
NSExceptional wants to merge 4 commits into
Conversation
GenericParameterKind only had .type, and GenericRequirementKind lacked .sameShape/.invertedProtocols, so the force-unwrapped rawValue decode crashed on any variadic-generic (each T), integer-generic (let N: Int), or noncopyable-generic type. Add: - GenericParameterKind: .typePack, .value - GenericRequirementKind: .sameShape, .invertedProtocols Test reflects a variadic-generic type and confirms its pack parameter decodes as .typePack without trapping.
OpaqueDescriptor.underlyingType(at:genericArguments:) resolves a 'some P' opaque type's mangled underlying-type name into a live metatype via the in-context resolver, threading the opaque descriptor's generic context. Out-of-range indices return nil. Test enumerates the image's opaque descriptors and confirms two opaque-returning functions' underlying types realize to Int and String.
…load) Adds GenericContext.descriptorFlags (hasTypePacks / hasConditionalInverted Protocols / hasValues), packShapeHeader, and packShapeDescriptors, decoding the GenericPackShapeHeader + GenericPackShapeDescriptor records that trail a variadic-generic context's parameters and requirements. Each descriptor reports its pack kind (metadata / witness-table), generic-argument index, and same-shape equivalence class. Also relaxes GenericRequirementDescriptor. mangledTypeName to accept .sameShape requirements. Tests confirm PackGeneric<each Element> exposes a metadata pack shape and a non-pack generic exposes none.
Reflecting a parameterized-protocol existential like 'any Collection<Int>' crashed Echo with 'unknown kind 775' (ExtendedExistential = 0x307). Adds the .extendedExistential MetadataKind, an ExtendedExistentialMetadata type exposing the existential shape, and ExtendedExistentialTypeShape with flags (specialKind: opaque/class/metatype/explicit-layout; hasGeneralization Signature / hasTypeExpression / hasSuggestedValueWitnesses), wired into getMetadata. Test reflects 'any Collection<Int>': kind .extendedExistential, the shape reports a generalization signature and opaque special kind, and the metadata round-trips; a plain 'any Equatable' still uses the classic layout.
1ad1609 to
c13ef33
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Completes modern-Swift type reflection: variadic/value generic parameters, parameter-pack shapes, opaque-type realization, and generalized (parameterized-protocol) existentials — the last of which was an outright crash.
New Features
Generic parameter & requirement kinds —
GenericParameterKindonly had.type, andGenericRequirementKindlacked.sameShape/.invertedProtocols, so the force-unwrapped decode crashed when reflecting any variadic-generic (each T), integer-generic (let N: Int), or noncopyable-constrained type. Adds.typePack/.valueand.sameShape/.invertedProtocols.Variadic-generic pack shapes (
GenericContext.swift) —descriptorFlags(hasTypePacks/hasConditionalInvertedProtocols/hasValues),packShapeHeader, andpackShapeDescriptors(pack kind, generic-argument index, same-shape equivalence class).Opaque type realization —
OpaqueDescriptor.underlyingType(at:genericArguments:)resolves asome Popaque type's mangled underlying type into a live metatype.Generalized existential metadata — reflecting
any Collection<Int>(a parameterized-protocol existential) crashed Echo with "unknown kind 775". Adds the.extendedExistentialMetadataKind, anExtendedExistentialMetadatatype, andExtendedExistentialTypeShape(special kind + generalization/type-expression/value-witness flags), wired intogetMetadata.Tests
Tests reflect a variadic-generic type (pack parameter decodes as
.typePack, pack shape exposes a metadata pack), realize two opaque-returning functions' underlying types toInt/Stringvia image enumeration, and reflectany Collection<Int>(kind.extendedExistential, generalization signature present, round-trips) while confirming a plainany Equatablekeeps the classic layout.swift testgreen (32 tests).