Skip to content

Commit e0df8ff

Browse files
committed
Runtime: Fix runtime type resolution when mangled names refer to protocol extensions with Self same type constraints.
If a type or opaque type descriptor appears inside of a protocol extension of the form: ``` extension P where Self == Nominal { ... } ``` then the runtime representation of the extension context was interpreted by the runtime demangler as a nominal type extension, even though the parameterization is on the `<Self>` generic signature of the protocol extension and not the generic signature of the nominal type. This would cause spurious rejection of mangled names referencing the extension context because we mistakenly thought that the type arguments mismatched with the nominal type signature rather than matching them to the extension context. Add some code to `_findExtendedTypeContextDescriptor` to detect when an extension is a protocol extension with a `Self == SameType` constraint, and pass the extension along rather than treating it as a nominal type extension. Fixes rdar://130168101.
1 parent dd8a21e commit e0df8ff

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,38 @@ _findExtendedTypeContextDescriptor(const ContextDescriptor *maybeExtension,
402402
Demangle::NodePointer &node = demangledNode ? *demangledNode : localNode;
403403

404404
auto mangledName = extension->getMangledExtendedContext();
405+
406+
// A extension of the form `extension Protocol where Self == ConcreteType`
407+
// is formally a protocol extension, so the formal generic parameter list
408+
// is `<Self>`, but because of the same type constraint, the extended context
409+
// looks like a reference to that nominal type. We want to match the
410+
// extension's formal generic environment rather than the nominal type's
411+
// in this case, so we should skip out on this case.
412+
//
413+
// We can detect this by looking at whether the generic context of the
414+
// extension has a first generic parameter, which would be the Self parameter,
415+
// with a same type constraint matching the extended type.
416+
for (auto &reqt : extension->getGenericRequirements()) {
417+
if (reqt.getKind() != GenericRequirementKind::SameType) {
418+
continue;
419+
}
420+
// 'x' is the mangling of the first generic parameter
421+
if (!reqt.getParam().equals("x")) {
422+
continue;
423+
}
424+
// Is the generic parameter same-type-constrained to the same type
425+
// we're extending? Then this is a `Self == ExtendedType` constraint.
426+
// This is impossible for normal generic nominal type extensions because
427+
// that would mean that you had:
428+
// struct Foo<T> {...}
429+
// extension Foo where T == Foo<T> {...}
430+
// which would mean that the extended type is the infinite expansion
431+
// Foo<Foo<Foo<Foo<...>>>>, which we don't allow.
432+
if (reqt.getMangledTypeName().data() == mangledName.data()) {
433+
return nullptr;
434+
}
435+
}
436+
405437
node = demangler.demangleType(mangledName,
406438
ResolveAsSymbolicReference(demangler));
407439
if (!node)
@@ -1254,7 +1286,8 @@ _gatherGenericParameters(const ContextDescriptor *context,
12541286
(void)_gatherGenericParameterCounts(context,
12551287
genericParamCounts, demangler);
12561288
unsigned numTotalGenericParams =
1257-
genericParamCounts.empty() ? 0 : genericParamCounts.back();
1289+
genericParamCounts.empty() ? context->getNumGenericParams()
1290+
: genericParamCounts.back();
12581291

12591292
// Check whether we have the right number of generic arguments.
12601293
if (genericArgs.size() == getLocalGenericParams(context).size()) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-run-simple-swift | %FileCheck %s
2+
// REQUIRES: executable_test
3+
// UNSUPPORTED: use_os_stdlib
4+
// UNSUPPORTED: back_deployment_runtime
5+
6+
// rdar://130168101: Make sure that we correctly resolve types in
7+
// the generic context of a protocol extension with a `Self` same
8+
// type constraint.
9+
10+
11+
protocol P { }
12+
13+
struct P2: P { }
14+
15+
extension P where Self == P2 {
16+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
17+
dynamic func p2() -> some P {
18+
return self
19+
}
20+
}
21+
22+
// CHECK: P2()
23+
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
24+
print(P2().p2())
25+
} else {
26+
print(P2())
27+
}

0 commit comments

Comments
 (0)