Skip to content

Demangler: edge case in existential demangling #74436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,36 +755,53 @@ Type ASTBuilder::createExistentialMetatypeType(
Type ASTBuilder::createConstrainedExistentialType(
Type base, ArrayRef<BuiltRequirement> constraints,
ArrayRef<BuiltInverseRequirement> inverseRequirements) {
// FIXME: Generalize to other kinds of bases.
if (!base->getAs<ProtocolType>())
return Type();
auto baseTy = base->castTo<ProtocolType>();
auto baseDecl = baseTy->getDecl();
llvm::SmallDenseMap<Identifier, Type> cmap;
for (const auto &req : constraints) {
switch (req.getKind()) {
case RequirementKind::SameShape:
llvm_unreachable("Same-shape requirement not supported here");
case RequirementKind::Conformance:
case RequirementKind::Superclass:
case RequirementKind::Layout:
continue;
Type constrainedBase;

if (auto baseTy = base->getAs<ProtocolType>()) {
auto baseDecl = baseTy->getDecl();
llvm::SmallDenseMap<Identifier, Type> cmap;
for (const auto &req : constraints) {
switch (req.getKind()) {
case RequirementKind::SameShape:
llvm_unreachable("Same-shape requirement not supported here");
case RequirementKind::Conformance:
case RequirementKind::Superclass:
case RequirementKind::Layout:
continue;

case RequirementKind::SameType:
if (auto *DMT = req.getFirstType()->getAs<DependentMemberType>())
cmap[DMT->getName()] = req.getSecondType();
case RequirementKind::SameType:
if (auto *DMT = req.getFirstType()->getAs<DependentMemberType>())
cmap[DMT->getName()] = req.getSecondType();
}
}
}
llvm::SmallVector<Type, 4> args;
for (auto *assocTy : baseDecl->getPrimaryAssociatedTypes()) {
auto argTy = cmap.find(assocTy->getName());
if (argTy == cmap.end()) {
return Type();
llvm::SmallVector<Type, 4> args;
for (auto *assocTy : baseDecl->getPrimaryAssociatedTypes()) {
auto argTy = cmap.find(assocTy->getName());
if (argTy == cmap.end()) {
return Type();
}
args.push_back(argTy->getSecond());
}

// We may not have any arguments because the constrained existential is a
// plain protocol with an inverse requirement.
if (args.empty()) {
constrainedBase =
ProtocolType::get(baseDecl, baseTy, base->getASTContext());
} else {
constrainedBase =
ParameterizedProtocolType::get(base->getASTContext(), baseTy, args);
}
args.push_back(argTy->getSecond());
} else if (base->isAny()) {
// The only other case should be that we got an empty PCT, which is equal to
// the Any type. The other constraints should have been encoded in the
// existential's generic signature (and arrive as BuiltInverseRequirement).
constrainedBase = base;
} else {
return Type();
}
Type constrainedBase =
ParameterizedProtocolType::get(base->getASTContext(), baseTy, args);

assert(constrainedBase);

// Handle inverse requirements.
if (!inverseRequirements.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion test/Interpreter/moveonly_existentials.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/bin
// RUN: %target-build-swift -g %s -o %t/bin
// RUN: %target-codesign %t/bin
// RUN: %target-run %t/bin | %FileCheck %s

Expand Down
14 changes: 14 additions & 0 deletions test/TypeDecoder/constrained_existentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// RUN: %lldb-moduleimport-test %t/constrained_existentials -type-from-mangled=%t/input | %FileCheck %s --match-full-lines

func blackHole(_: Any...) {}
func blackHole_noncopyable(_: consuming any ~Copyable) {}

protocol BaseProto<A, B> {
associatedtype A
Expand Down Expand Up @@ -43,3 +44,16 @@ do {

blackHole(e0, e1, e2)
}

protocol NCProto: ~Copyable {}
struct NC: ~Copyable {}
struct GenNC<T: ~Copyable>: ~Copyable, NCProto {}

do {
let e0: any NCProto & ~Copyable = GenNC<NC>()
let e1: any NCProto & ~Copyable = GenNC<String>()

// FIXME: breaks the MoveChecker (rdar://129885532)
// blackHole_noncopyable(consume e0)
// blackHole_noncopyable(consume e1)
}