Skip to content

Commit 28f305b

Browse files
committed
MetadataReader: Fix caching of Objective C class metadata
If resolving the type of an instance produces a class metadata for which we cannot build a type (for example, a special class like __NSCFNumber, which the ClangImporter does not produce a ClassDecl for), we try the superclass. The caching logic was broken in this case, so subsequent calls would return an empty type.
1 parent c16fed7 commit 28f305b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

include/swift/Remote/MetadataReader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,14 @@ class MetadataReader {
502502
if (!readObjCClassName(MetadataAddress, className))
503503
return BuiltType();
504504

505-
auto BuiltObjCClass = Builder.createObjCClassType(std::move(className));
505+
BuiltType BuiltObjCClass = Builder.createObjCClassType(std::move(className));
506506
if (!BuiltObjCClass) {
507507
// Try the superclass.
508508
if (!classMeta->Superclass)
509509
return BuiltType();
510510

511-
return readTypeFromMetadata(classMeta->Superclass,
512-
skipArtificialSubclasses);
511+
BuiltObjCClass = readTypeFromMetadata(classMeta->Superclass,
512+
skipArtificialSubclasses);
513513
}
514514

515515
TypeCache[MetadataAddress] = BuiltObjCClass;

test/RemoteAST/existentials_objc.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,22 @@ func printDynamicTypeAndAddressForExistential<T>(_: T)
1212
// CHECK: NSObject
1313
printDynamicTypeAndAddressForExistential(NSObject() as AnyObject)
1414

15+
// Print tagged pointer types three times to ensure the caching works.
16+
17+
// CHECK: NSNumber
18+
printDynamicTypeAndAddressForExistential(NSNumber(123) as AnyObject)
19+
20+
// CHECK: NSNumber
21+
printDynamicTypeAndAddressForExistential(NSNumber(123) as AnyObject)
22+
1523
// CHECK: NSNumber
1624
printDynamicTypeAndAddressForExistential(NSNumber(123) as AnyObject)
25+
26+
// CHECK: NSString
27+
printDynamicTypeAndAddressForExistential(NSString("hello") as AnyObject)
28+
29+
// CHECK: NSString
30+
printDynamicTypeAndAddressForExistential(NSString("hello") as AnyObject)
31+
32+
// CHECK: NSString
33+
printDynamicTypeAndAddressForExistential(NSString("hello") as AnyObject)

0 commit comments

Comments
 (0)