Skip to content

Commit d73c812

Browse files
committed
Simplify _gatherWrittenGenericParameters
1 parent 3503ab3 commit d73c812

File tree

2 files changed

+17
-71
lines changed

2 files changed

+17
-71
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ _gatherGenericParameters(const ContextDescriptor *context,
12761276
auto generics = context->getGenericContext();
12771277
assert(generics);
12781278

1279-
// If we have a parent, gather it's generic arguments "as written". If our
1279+
// If we have a parent, gather its generic arguments "as written". If our
12801280
// parent is not generic, there are no generic arguments to add.
12811281
if (parent && parent->getTypeContextDescriptor() &&
12821282
parent->getTypeContextDescriptor()->getGenericContext()) {
@@ -3297,19 +3297,9 @@ bool swift::_gatherWrittenGenericParameters(
32973297
missingWrittenArguments = true;
32983298
}
32993299

3300-
switch (param.getKind()) {
3301-
case GenericParamKind::Type:
3302-
// Already handled by the above.
3303-
break;
3304-
3305-
case GenericParamKind::TypePack:
3306-
// Already handled by the above.
3307-
break;
3308-
3309-
default:
3310-
// We don't know about this kind of parameter.
3311-
return false;
3312-
}
3300+
assert((param.getKind() == GenericParamKind::Type ||
3301+
param.getKind() == GenericParamKind::TypePack) &&
3302+
"Unknown generic parameter kind");
33133303
}
33143304

33153305
// If there is no follow-up work to do, we're done.
@@ -3326,8 +3316,6 @@ bool swift::_gatherWrittenGenericParameters(
33263316
SubstGenericParametersFromWrittenArgs substitutions(genericArgs,
33273317
genericParamCounts);
33283318

3329-
bool needToCheckAssociatedSameTypes = false;
3330-
33313319
// Walk through the generic requirements to evaluate same-type
33323320
// constraints that are needed to fill in missing generic arguments.
33333321
for (const auto &req : genericContext->getGenericRequirements()) {
@@ -3375,10 +3363,10 @@ bool swift::_gatherWrittenGenericParameters(
33753363
auto rhsParam = demangleToGenericParamRef(req.getMangledTypeName());
33763364

33773365
// If the rhs parameter is not a generic parameter itself with
3378-
// (depth, index), it could potentially be some associated type. Check it
3379-
// again later once we've found all of the other same types.
3366+
// (depth, index), it could potentially be some associated type. If that's
3367+
// the case, then we don't need to do anything else for this rhs because it
3368+
// won't appear in the key arguments list.
33803369
if (!rhsParam) {
3381-
needToCheckAssociatedSameTypes = true;
33823370
continue;
33833371
}
33843372

@@ -3394,58 +3382,6 @@ bool swift::_gatherWrittenGenericParameters(
33943382
genericArgs[*rhsFlatIndex] = genericArgs[*lhsFlatIndex];
33953383
}
33963384

3397-
if (!needToCheckAssociatedSameTypes) {
3398-
return true;
3399-
}
3400-
3401-
// Once again, loop through our list and look for same type constraints where
3402-
// the rhs is an associated type of sorts.
3403-
for (const auto &req : genericContext->getGenericRequirements()) {
3404-
// We only care about same-type constraints.
3405-
if (req.Flags.getKind() != GenericRequirementKind::SameType) {
3406-
continue;
3407-
}
3408-
3409-
auto lhsParam = demangleToGenericParamRef(req.getParam());
3410-
3411-
if (!lhsParam) {
3412-
continue;
3413-
}
3414-
3415-
auto lhsFlatIndex =
3416-
_depthIndexToFlatIndex(lhsParam->first, lhsParam->second,
3417-
genericParamCounts);
3418-
if (!lhsFlatIndex || *lhsFlatIndex >= genericArgs.size())
3419-
return false;
3420-
3421-
auto rhsParam =
3422-
swift_getTypeByMangledName(MetadataState::Abstract,
3423-
req.getMangledTypeName(),
3424-
keyArgs.data(),
3425-
[&substitutions](unsigned depth, unsigned index) {
3426-
return substitutions.getMetadata(depth, index).Ptr;
3427-
},
3428-
[&substitutions](const Metadata *type, unsigned index) {
3429-
return substitutions.getWitnessTable(type, index);
3430-
}).getType().getMetadata();
3431-
3432-
if (!rhsParam) {
3433-
return false;
3434-
}
3435-
3436-
// If we already have an argument for the lhs, then just check that it is
3437-
// indeed == to the rhs type.
3438-
if (auto genericArg = genericArgs[*lhsFlatIndex]) {
3439-
if (genericArg.getMetadata() != rhsParam) {
3440-
return false;
3441-
}
3442-
} else {
3443-
// If we don't have a lhs yet, then it's just the rhs.
3444-
genericArgs[*lhsFlatIndex] = MetadataOrPack(rhsParam);
3445-
}
3446-
3447-
}
3448-
34493385
return true;
34503386
}
34513387

test/Runtime/demangleToMetadata.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ class CG2<T, U> {
242242
}
243243
}
244244

245+
struct ReallyLongGeneric<T, U, V, W> {}
246+
247+
extension ReallyLongGeneric where T == U, U == V.Element, V == W, W: Collection {
248+
struct Nested {}
249+
}
250+
245251
DemangleToMetadataTests.test("nested generic specializations") {
246252
expectEqual(EG<Int, String>.NestedSG<Double>.self,
247253
_typeByName("4main2EGO8NestedSGVySiSS_SdG")!)
@@ -252,6 +258,10 @@ DemangleToMetadataTests.test("nested generic specializations") {
252258
expectEqual(
253259
CG2<Int, String>.Inner<Double>.Innermost<Int8, Int16, Int32, Int64>.self,
254260
_typeByName("4main3CG2C5InnerC9InnermostVySiSS_Sd_s4Int8Vs5Int16Vs5Int32Vs5Int64VG")!)
261+
expectEqual(
262+
ReallyLongGeneric<Int, Int, [Int], [Int]>.Nested.self,
263+
_typeByName("4main17ReallyLongGenericVAAE6NestedVyS2iSaySiGAF_G")!
264+
)
255265
}
256266

257267
DemangleToMetadataTests.test("demangle built-in types") {

0 commit comments

Comments
 (0)