Skip to content

Commit 92d5656

Browse files
authored
Merge pull request #77172 from Azoy/generic-concrete-hassymbol
[IRGen] Support generic but concrete metatypes with hasSymbol
2 parents 6d2d4eb + c58cc44 commit 92d5656

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lib/IRGen/GenHasSymbol.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/AST/GlobalDecl.h"
2727

2828
#include "GenDecl.h"
29+
#include "GenericArguments.h"
2930
#include "IRGenFunction.h"
3031
#include "IRGenModule.h"
3132

@@ -36,9 +37,23 @@ using namespace irgen;
3637
/// additional types of entities that the main utility cannot.
3738
static llvm::Constant *getAddrOfLLVMVariable(IRGenModule &IGM,
3839
LinkEntity entity) {
39-
if (entity.isTypeMetadataAccessFunction())
40-
return IGM.getAddrOfTypeMetadataAccessFunction(entity.getType(),
41-
NotForDefinition);
40+
if (entity.isTypeMetadataAccessFunction()) {
41+
auto type = entity.getType();
42+
auto nominal = type->getAnyNominal();
43+
assert(nominal);
44+
45+
if (nominal->isGenericContext()) {
46+
GenericArguments genericArgs;
47+
genericArgs.collectTypes(IGM, nominal);
48+
49+
return IGM.getAddrOfGenericTypeMetadataAccessFunction(nominal,
50+
genericArgs.Types,
51+
NotForDefinition);
52+
} else {
53+
return IGM.getAddrOfTypeMetadataAccessFunction(type,
54+
NotForDefinition);
55+
}
56+
}
4257
if (entity.isDispatchThunk())
4358
return IGM.getAddrOfDispatchThunk(entity.getSILDeclRef(), NotForDefinition);
4459

test/IRGen/Inputs/has_symbol/has_symbol_helper.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ public actor A {
6464
public func asyncMethod() async {}
6565
}
6666
#endif
67+
68+
public struct Generic<T> {}

test/IRGen/has_symbol.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,15 @@ public func testExistentialParameter(_ p: any P) {
165165
public func testMetatypes() {
166166
// CHECK: %{{[0-9]+}} = call i1 @"$s17has_symbol_helper1SVTwS"()
167167
if #_hasSymbol(S.self) {}
168+
169+
// CHECK: %{{[0-9]+}} = call i1 @"$s17has_symbol_helper7GenericVTwS"()
170+
if #_hasSymbol(Generic<Void>.self) {}
168171
}
169172

170173
// --- S.self ---
171174
// CHECK: define linkonce_odr hidden i1 @"$s17has_symbol_helper1SVTwS"()
172175
// CHECK: ret i1 and (i1 and (i1 icmp ne (ptr @"$s17has_symbol_helper1SVMn", ptr null), i1 icmp ne (ptr @"$s17has_symbol_helper1SVN", ptr null)), i1 icmp ne (ptr @"$s17has_symbol_helper1SVMa", ptr null))
176+
177+
// --- Generic<Void>.self ---
178+
// CHECK: define linkonce_odr hidden i1 @"$s17has_symbol_helper7GenericVTwS"()
179+
// CHECK: ret i1 and (i1 icmp ne (ptr @"$s17has_symbol_helper7GenericVMn", ptr null), i1 icmp ne (ptr @"$s17has_symbol_helper7GenericVMa", ptr null))

0 commit comments

Comments
 (0)