Skip to content

[Noncopyable] Mangle protocol members without the inverse requirements of the protocol itself #72279

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 1 commit into from
Mar 13, 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
5 changes: 5 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4669,6 +4669,11 @@ ASTMangler::BaseEntitySignature::BaseEntitySignature(const Decl *decl)
case DeclKind::Struct:
case DeclKind::Class:
sig = decl->getInnermostDeclContext()->getGenericSignatureOfContext();

// Protocol members never mangle inverse constraints on `Self`.
if (isa<ProtocolDecl>(decl->getDeclContext()))
setDepth(0);

break;

case DeclKind::TypeAlias: // FIXME: is this right? typealiases have a generic signature!
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ IRGenMangler::appendExtendedExistentialTypeShape(CanGenericSignature genSig,
CanType shapeType) {
// Append the generalization signature.
if (genSig) {
// Generalization signature never reference inverses.
// Generalization signature never mangles inverses.
llvm::SaveAndRestore X(AllowInverses, false);
appendGenericSignature(genSig);
}
Expand Down
33 changes: 0 additions & 33 deletions lib/IRGen/IRGenMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class IRGenMangler : public Mangle::ASTMangler {
IRGenMangler() { }

std::string mangleDispatchThunk(const FuncDecl *func) {
// Dispatch thunks do not mangle inverse conformances.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendEntity(func);
appendOperator("Tj");
Expand All @@ -58,9 +55,6 @@ class IRGenMangler : public Mangle::ASTMangler {
std::string mangleDerivativeDispatchThunk(
const AbstractFunctionDecl *func,
AutoDiffDerivativeFunctionIdentifier *derivativeId) {
// Dispatch thunks do not mangle inverse conformances.
llvm::SaveAndRestore X(AllowInverses, false);

beginManglingWithAutoDiffOriginalFunction(func);
auto kind = Demangle::getAutoDiffFunctionKind(derivativeId->getKind());
auto *resultIndices =
Expand All @@ -77,19 +71,13 @@ class IRGenMangler : public Mangle::ASTMangler {

std::string mangleConstructorDispatchThunk(const ConstructorDecl *ctor,
bool isAllocating) {
// Dispatch thunks do not mangle inverse conformances.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendConstructorEntity(ctor, isAllocating);
appendOperator("Tj");
return finalize();
}

std::string mangleMethodDescriptor(const FuncDecl *func) {
// Method descriptors do not mangle inverse conformances.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendEntity(func);
appendOperator("Tq");
Expand All @@ -99,9 +87,6 @@ class IRGenMangler : public Mangle::ASTMangler {
std::string mangleDerivativeMethodDescriptor(
const AbstractFunctionDecl *func,
AutoDiffDerivativeFunctionIdentifier *derivativeId) {
// Method descriptors do not mangle inverse conformances.
llvm::SaveAndRestore X(AllowInverses, false);

beginManglingWithAutoDiffOriginalFunction(func);
auto kind = Demangle::getAutoDiffFunctionKind(derivativeId->getKind());
auto *resultIndices =
Expand All @@ -118,9 +103,6 @@ class IRGenMangler : public Mangle::ASTMangler {

std::string mangleConstructorMethodDescriptor(const ConstructorDecl *ctor,
bool isAllocating) {
// Method descriptors do not mangle inverse conformances.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendConstructorEntity(ctor, isAllocating);
appendOperator("Tq");
Expand Down Expand Up @@ -346,9 +328,6 @@ class IRGenMangler : public Mangle::ASTMangler {
llvm::SaveAndRestore<bool> optimizeProtocolNames(OptimizeProtocolNames,
false);

// No mangling of inverse conformances inside protocols.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
bool isAssocTypeAtDepth = false;
(void)appendAssocType(
Expand All @@ -363,9 +342,6 @@ class IRGenMangler : public Mangle::ASTMangler {
const ProtocolDecl *proto,
CanType subject,
const ProtocolDecl *requirement) {
// No mangling of inverse conformances inside protocols.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendAnyGenericType(proto);
if (isa<GenericTypeParamType>(subject)) {
Expand All @@ -382,9 +358,6 @@ class IRGenMangler : public Mangle::ASTMangler {
std::string mangleBaseConformanceDescriptor(
const ProtocolDecl *proto,
const ProtocolDecl *requirement) {
// No mangling of inverse conformances inside protocols.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendAnyGenericType(proto);
appendProtocolName(requirement);
Expand All @@ -396,9 +369,6 @@ class IRGenMangler : public Mangle::ASTMangler {
const ProtocolDecl *proto,
CanType subject,
const ProtocolDecl *requirement) {
// No mangling of inverse conformances inside protocols.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendAnyGenericType(proto);
bool isFirstAssociatedTypeIdentifier = true;
Expand All @@ -424,9 +394,6 @@ class IRGenMangler : public Mangle::ASTMangler {
}

std::string mangleFieldOffset(const ValueDecl *Decl) {
// No mangling of inverse conformances.
llvm::SaveAndRestore X(AllowInverses, false);

beginMangling();
appendEntity(Decl);
appendOperator("Wvd");
Expand Down
26 changes: 23 additions & 3 deletions test/IRGen/mangling_inverse_generics_evolution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@

public protocol P: ~Copyable { }

public struct CallMe<U: ~Copyable> {
public enum Maybe<T: ~Copyable>: ~Copyable {
// CHECK-LABEL: @"$s4test6CallMeV5MaybeO4someyAEyx_qd__Gqd__cAGmr__lFWC"
// CHECK: @"$s4test6CallMeV5MaybeO4noneyAEyx_qd__GAGmr__lFWC"
case none
case some(T)
}
}

extension CallMe {
public enum Box<T: ~Copyable>: ~Copyable {
// CHECK-LABEL: @"$s4test6CallMeV3BoxO4someyAEyx_qd__Gqd__cAGmr__lFWC"
// CHECK: @"$s4test6CallMeV3BoxO4noneyAEyx_qd__GAGmr__lFWC"
case none
case some(T)
}
}

public protocol Hello<Person>: ~Copyable {
// CHECK: @"$s4test5HelloP6PersonAC_AA1PTn"
// CHECK: @"$s6Person4test5HelloPTl" =
Expand All @@ -20,8 +38,10 @@ public protocol Hello<Person>: ~Copyable {

// CHECK: @"$s4test5HelloP5greetyy6PersonQzFTq"
func greet(_ person: borrowing Person)
}

public struct Wrapper<T: ~Copyable> {
var wrapped: T { fatalError("boom") }
// CHECK: @"$s4test5HelloP10overloadedyyqd__lFTj"
func overloaded<T>(_: borrowing T)

// CHECK: @"$s4test5HelloP10overloadedyyqd__Ricd__lFTj"
func overloaded<T: ~Copyable>(_: borrowing T)
}