Skip to content

Commit be641d7

Browse files
committed
Fix the GUID type not found error on Windows
Predefined declarations (like _GUID) are special forward declarations inserted by Clang and aren't serialized into the pcm and their definition pointers aren't retained across serialization and deserialization, which causes this type not found error. Avoid putting non-defining predefined declarations into the swift lookup table when their definitions exist in the same module so that the definitions will be associated with the base name and avoid this error.
1 parent 73fd67b commit be641d7

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,17 @@ void SwiftLookupTableWriter::populateTableWithDecl(SwiftLookupTable &table,
21802180
if (decl->isFromASTFile())
21812181
return;
21822182

2183+
// Exclude a predefined declaration that's not a definition if its
2184+
// definition exists in the same module so that the definition will
2185+
// be associated with the base name, noting predefined declarations
2186+
// won't be serialized into the pcm and its state including its
2187+
// definition pointer won't be reconstructed after deserialization,
2188+
// which would cause a type not found error.
2189+
if (Writer.isDeclPredefined(decl))
2190+
if (auto tagDecl = dyn_cast<clang::TagDecl>(decl))
2191+
if (!tagDecl->isThisDeclarationADefinition() && tagDecl->getDefinition())
2192+
return;
2193+
21832194
// Iterate into extern "C" {} type declarations.
21842195
if (auto linkageDecl = dyn_cast<clang::LinkageSpecDecl>(decl)) {
21852196
for (auto *decl : linkageDecl->noload_decls()) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// REQUIRES: OS=windows-msvc
2+
3+
// Test that a struct named _GUID is found and usable in C++ when it is
4+
// defined, not just forward declared, within the same Clang module where
5+
// _GUID is specially-handled as a predefined (forward) declaration on
6+
// Windows.
7+
8+
// RUN: %empty-directory(%t)
9+
// RUN: split-file %s %t
10+
// RUN: %target-swift-frontend -c -primary-file %t/Test.swift -cxx-interoperability-mode=default -I %t -Xcc -fmodule-map-file=%t/module.modulemap -Xcc -I -Xcc %t -module-name Test -o %t/Test.swift.o -module-cache-path %t/module-cache
11+
12+
//--- Test.swift
13+
import TestMod
14+
15+
extension TestMod._GUID {
16+
public func m() -> Int { 42 }
17+
}
18+
19+
//--- test.h
20+
#ifndef TEST_H
21+
#define TEST_H
22+
23+
typedef struct _GUID {
24+
int Data;
25+
} GUID;
26+
27+
#endif // TEST_H
28+
29+
//--- module.modulemap
30+
module TestMod {
31+
header "test.h"
32+
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -emit-module %S/Inputs/wrapped-modularization-error-remarks/A/A.swift -cxx-interoperability-mode=default -Xcc -fmodule-map-file=%S/Inputs/wrapped-modularization-error-remarks/CxxLib/include/module.modulemap -Xcc -I -Xcc %S/Inputs/wrapped-modularization-error-remarks/CxxLib/include -module-name A -o %t/A.swiftmodule
3-
// RUN: not %target-swift-frontend -c -primary-file %S/Inputs/wrapped-modularization-error-remarks/B/B.swift -cxx-interoperability-mode=default -I %t -Xcc -fmodule-map-file=%S/Inputs/wrapped-modularization-error-remarks/CxxLib/include/module.modulemap -Xcc -I -Xcc %S/Inputs/wrapped-modularization-error-remarks/CxxLib/include -module-name B -o %t/B.swift.o -Rmodule-recovery 2>&1 | %FileCheck %s
3+
// RUN: %target-swift-frontend -c -primary-file %S/Inputs/wrapped-modularization-error-remarks/B/B.swift -cxx-interoperability-mode=default -I %t -Xcc -fmodule-map-file=%S/Inputs/wrapped-modularization-error-remarks/CxxLib/include/module.modulemap -Xcc -I -Xcc %S/Inputs/wrapped-modularization-error-remarks/CxxLib/include -module-name B -o %t/B.swift.o -Rmodule-recovery
44
// REQUIRES: OS=windows-msvc
5-
6-
// Check that the diagnostics/remark from the wrapped ModularizationError is emitted.
7-
// CHECK: remark: reference to type '_GUID' broken by a context change; '_GUID' is not found, it was expected to be in 'CxxLib'
8-
// CHECK: note: could not deserialize type for 'queryInterface'
9-
// CHECK: error: cannot inherit from class 'Window' because it has overridable members that could not be loaded
10-
// CHECK: note: could not deserialize 'queryInterface'

0 commit comments

Comments
 (0)