Skip to content

Commit 09018a8

Browse files
committed
[NFC] Store interface shape types in opened element environments
1 parent 32fa74d commit 09018a8

File tree

11 files changed

+56
-29
lines changed

11 files changed

+56
-29
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ class ASTContext final {
13851385
/// This drops the parameter pack bit from each generic parameter,
13861386
/// and converts same-element requirements to same-type requirements.
13871387
CanGenericSignature getOpenedElementSignature(CanGenericSignature baseGenericSig,
1388-
CanType shapeClass);
1388+
CanGenericTypeParamType shapeClass);
13891389

13901390
GenericSignature getOverrideGenericSignature(const ValueDecl *base,
13911391
const ValueDecl *derived);

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,8 @@ ERROR(pack_element_attribute_expected_rparen,none,
16761676
"expected ')' after id value for 'pack_element' attribute", ())
16771677
ERROR(multiple_open_pack_element,none,
16781678
"multiple 'open_pack_element' instructions with same UUID", ())
1679+
ERROR(opened_shape_class_not_pack_param,none,
1680+
"shape class type must be a pack type parameter", ())
16791681

16801682
// unowned
16811683
ERROR(attr_unowned_invalid_specifier,none,

include/swift/AST/GenericEnvironment.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct OpenedExistentialEnvironmentData {
7171
/// Extra data in a generic environment for an opened pack element.
7272
struct OpenedElementEnvironmentData {
7373
UUID uuid;
74-
CanType shapeClass;
74+
CanGenericTypeParamType shapeClass;
7575
SubstitutionMap outerSubstitutions;
7676
};
7777

@@ -146,7 +146,8 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
146146

147147
/// Private constructor for opened element environments.
148148
explicit GenericEnvironment(GenericSignature signature,
149-
UUID uuid, CanType shapeClass,
149+
UUID uuid,
150+
CanGenericTypeParamType shapeClass,
150151
SubstitutionMap outerSubs);
151152

152153
friend ArchetypeType;
@@ -194,7 +195,8 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
194195
SubstitutionMap getPackElementContextSubstitutions() const;
195196

196197
/// Retrieve the shape equivalence class for an opened element environment.
197-
CanType getOpenedElementShapeClass() const;
198+
/// This is always a pack parameter.
199+
CanGenericTypeParamType getOpenedElementShapeClass() const;
198200

199201
/// Retrieve the UUID for an opened element environment.
200202
UUID getOpenedElementUUID() const;
@@ -238,12 +240,12 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
238240
/// signature of the context whose element type is being opened, but with
239241
/// the pack parameter bit erased from one or more generic parameters
240242
/// \param uuid The unique identifier for this opened element
241-
/// \param shapeClass The shape equivalence class for the originating packs.
243+
/// \param shapeClass The shape equivalence class for the originating packs
242244
/// \param outerSubs The substitution map containing archetypes from the
243245
/// outer generic context.
244246
static GenericEnvironment *
245247
forOpenedElement(GenericSignature signature,
246-
UUID uuid, CanType shapeClass,
248+
UUID uuid, CanGenericTypeParamType shapeClass,
247249
SubstitutionMap outerSubs);
248250

249251
/// Make vanilla new/delete illegal.

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5144,7 +5144,8 @@ GenericEnvironment::forOpenedExistential(
51445144
/// Create a new generic environment for an element archetype.
51455145
GenericEnvironment *
51465146
GenericEnvironment::forOpenedElement(GenericSignature signature,
5147-
UUID uuid, CanType shapeClass,
5147+
UUID uuid,
5148+
CanGenericTypeParamType shapeClass,
51485149
SubstitutionMap outerSubs) {
51495150
auto &ctx = signature->getASTContext();
51505151

@@ -5735,7 +5736,7 @@ ASTContext::getOpenedExistentialSignature(Type type, GenericSignature parentSig)
57355736

57365737
CanGenericSignature
57375738
ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig,
5738-
CanType shapeClass) {
5739+
CanGenericTypeParamType shapeClass) {
57395740
auto &sigs = getImpl().ElementSignatures;
57405741
auto key = std::make_pair(shapeClass, baseGenericSig.getPointer());
57415742
auto found = sigs.find(key);
@@ -5774,7 +5775,7 @@ ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig,
57745775

57755776
// Only include opened element parameters for packs in the given
57765777
// shape equivalence class.
5777-
if (!baseGenericSig->haveSameShape(paramType, shapeClass->mapTypeOutOfContext()))
5778+
if (!baseGenericSig->haveSameShape(paramType, shapeClass))
57785779
continue;
57795780

57805781
auto *elementParam = GenericTypeParamType::get(/*isParameterPack*/false,

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6765,8 +6765,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
67656765
auto sig = subs.getGenericSignature();
67666766
auto params = sig.getGenericParams();
67676767

6768-
auto elementShapeClass =
6769-
env->getOpenedElementShapeClass()->mapTypeOutOfContext();
6768+
auto elementShapeClass = env->getOpenedElementShapeClass();
67706769

67716770
// The element archetypes are at a depth one past the max depth
67726771
// of the base signature.

lib/AST/GenericEnvironment.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ GenericEnvironment::getPackElementContextSubstitutions() const {
104104
return environmentData->outerSubstitutions;
105105
}
106106

107-
CanType GenericEnvironment::getOpenedElementShapeClass() const {
107+
CanGenericTypeParamType
108+
GenericEnvironment::getOpenedElementShapeClass() const {
108109
assert(getKind() == Kind::OpenedElement);
109110
auto environmentData = getTrailingObjects<OpenedElementEnvironmentData>();
110111
return environmentData->shapeClass;
@@ -160,7 +161,7 @@ void GenericEnvironment::forEachPackElementGenericTypeParam(
160161

161162
// Only include opened element parameters for packs in the given
162163
// shape equivalence class.
163-
if (!sig->haveSameShape(genericParam, shapeClass->mapTypeOutOfContext()))
164+
if (!sig->haveSameShape(genericParam, shapeClass))
164165
continue;
165166

166167
function(genericParam);
@@ -218,7 +219,8 @@ GenericEnvironment::GenericEnvironment(
218219
}
219220

220221
GenericEnvironment::GenericEnvironment(GenericSignature signature,
221-
UUID uuid, CanType shapeClass,
222+
UUID uuid,
223+
CanGenericTypeParamType shapeClass,
222224
SubstitutionMap outerSubs)
223225
: SignatureAndKind(signature, Kind::OpenedElement)
224226
{
@@ -627,7 +629,7 @@ GenericEnvironment::mapPackTypeIntoElementContext(Type type) const {
627629
if (!genericParam->isParameterPack())
628630
continue;
629631

630-
if (!sig->haveSameShape(genericParam, shapeClass->mapTypeOutOfContext()))
632+
if (!sig->haveSameShape(genericParam, shapeClass))
631633
continue;
632634

633635
auto elementIndex = elementParamForPack.size();
@@ -680,7 +682,7 @@ GenericEnvironment::mapElementTypeIntoPackContext(Type type) const {
680682
if (!genericParam->isParameterPack())
681683
continue;
682684

683-
if (!sig->haveSameShape(genericParam, shapeClass->mapTypeOutOfContext()))
685+
if (!sig->haveSameShape(genericParam, shapeClass))
684686
continue;
685687

686688
GenericParamKey elementKey(/*isParameterPack*/false,
@@ -767,16 +769,19 @@ OpenedElementContext::createForContextualExpansion(ASTContext &ctx,
767769
"must be given a contextual type");
768770

769771
// Get the outer generic signature and environment.
770-
auto *genericEnv = cast<ArchetypeType>(expansionType.getCountType())
771-
->getGenericEnvironment();
772+
auto countArchetype = cast<ArchetypeType>(expansionType.getCountType());
773+
auto *genericEnv = countArchetype->getGenericEnvironment();
772774
auto subMap = genericEnv->getForwardingSubstitutionMap();
773775

776+
auto countType = cast<GenericTypeParamType>(
777+
countArchetype->getInterfaceType()->getCanonicalType());
778+
774779
auto genericSig = genericEnv->getGenericSignature().getCanonicalSignature();
775780
// Create an opened element signature and environment.
776781
auto elementSig = ctx.getOpenedElementSignature(
777-
genericSig, expansionType.getCountType());
782+
genericSig, countType);
778783
auto *elementEnv = GenericEnvironment::forOpenedElement(
779-
elementSig, UUID::fromTime(), expansionType.getCountType(), subMap);
784+
elementSig, UUID::fromTime(), countType, subMap);
780785

781786
return {elementEnv, elementSig};
782787
}

lib/SIL/IR/SILInstructions.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,6 @@ OpenPackElementInst *OpenPackElementInst::create(
23792379
PackType *packSubstitution) {
23802380
collector.collect(packSubstitution->getCanonicalType());
23812381
});
2382-
collector.collect(env->getOpenedElementShapeClass());
23832382
collector.addTo(typeDependentOperands, F);
23842383

23852384
SILType type = SILType::getSILTokenType(F.getASTContext());

lib/SIL/IR/SILPrinter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2312,7 +2312,13 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
23122312
<< " of " << subs.getGenericSignature()
23132313
<< " at ";
23142314
printSubstitutions(subs);
2315-
*this << ", shape $" << env->getOpenedElementShapeClass()
2315+
// The shape class in the opened environment is a canonical interface
2316+
// type, which won't resolve in the generic signature we just printed.
2317+
// Map it back to the sugared generic parameter.
2318+
auto sugaredShapeClass =
2319+
subs.getGenericSignature()->getSugaredType(
2320+
env->getOpenedElementShapeClass());
2321+
*this << ", shape $" << sugaredShapeClass
23162322
<< ", uuid \"" << env->getOpenedElementUUID() << "\"";
23172323
}
23182324
void visitPackElementGetInst(PackElementGetInst *I) {

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,14 +3387,23 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
33873387
// Parse the shape class that should be opened. This is a contextual
33883388
// type within the signature we just parsed.
33893389
CanType shapeClass;
3390+
SourceLoc shapeClassLoc;
33903391
if (!P.consumeIf(tok::comma) ||
33913392
parseVerbatim("shape") ||
33923393
P.parseToken(tok::sil_dollar,
33933394
diag::expected_tok_in_sil_instr, "$") ||
3394-
parseASTType(shapeClass, openedGenericsSig, openedGenerics,
3395-
/*wantContextualType*/ true))
3395+
parseASTType(shapeClass, shapeClassLoc, openedGenericsSig,
3396+
openedGenerics, /*wantContextualType*/ true))
33963397
return true;
33973398

3399+
// Map it out of context. It should be a type pack parameter.
3400+
shapeClass = shapeClass->mapTypeOutOfContext()->getCanonicalType();
3401+
auto shapeParam = dyn_cast<GenericTypeParamType>(shapeClass);
3402+
if (!shapeParam || !shapeParam->isParameterPack()) {
3403+
P.diagnose(shapeClassLoc, diag::opened_shape_class_not_pack_param);
3404+
return true;
3405+
}
3406+
33983407
// Parse the UUID for the opening.
33993408
UUID uuid;
34003409
if (!P.consumeIf(tok::comma) ||
@@ -3406,10 +3415,10 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
34063415
// the opened elements to the signature we parsed above.
34073416
auto openedElementSig =
34083417
P.Context.getOpenedElementSignature(
3409-
openedGenericsSig.getCanonicalSignature(), shapeClass);
3418+
openedGenericsSig.getCanonicalSignature(), shapeParam);
34103419

34113420
auto openedEnv = GenericEnvironment::forOpenedElement(openedElementSig,
3412-
uuid, shapeClass, openedSubMap);
3421+
uuid, shapeParam, openedSubMap);
34133422

34143423
auto openInst = B.createOpenPackElement(InstLoc, Val, openedEnv);
34153424
ResultVal = openInst;

lib/Sema/ConstraintSystem.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,13 +667,16 @@ ConstraintSystem::getPackElementEnvironment(ConstraintLocator *locator,
667667
!shapeClass->isEqual(uuidAndShape.second))
668668
return nullptr;
669669

670+
auto shapeParam = cast<GenericTypeParamType>(
671+
shapeClass->mapTypeOutOfContext()->getCanonicalType());
672+
670673
auto &ctx = getASTContext();
671674
auto elementSig = ctx.getOpenedElementSignature(
672-
DC->getGenericSignatureOfContext().getCanonicalSignature(), shapeClass);
675+
DC->getGenericSignatureOfContext().getCanonicalSignature(), shapeParam);
673676
auto *contextEnv = DC->getGenericEnvironmentOfContext();
674677
auto contextSubs = contextEnv->getForwardingSubstitutionMap();
675678
return GenericEnvironment::forOpenedElement(elementSig, uuidAndShape.first,
676-
shapeClass, contextSubs);
679+
shapeParam, contextSubs);
677680
}
678681

679682
/// Extend the given depth map by adding depths for all of the subexpressions

0 commit comments

Comments
 (0)