Skip to content

Commit 922c303

Browse files
author
Gabor Horvath
committed
[cxx-interop] Support conforming to protocols with default assoc types
In case the type does not define a type alias with the same name fall back to the default type of the associated type in the protocol. Previously, the compiler crashed. Unfortunately, we are still crashing when we do not find the right conformance. In a follow-up PR I plan to add more graceful handling of the case when the lookup fails. rdar://154098495
1 parent a47d392 commit 922c303

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10148,6 +10148,15 @@ static void finishTypeWitnesses(
1014810148
break;
1014910149
}
1015010150

10151+
if (!satisfied && assocType->hasDefaultDefinitionType()) {
10152+
auto defaultType = assocType->getDefaultDefinitionType();
10153+
auto subMap =
10154+
selfType->getContextSubstitutionMap(assocType->getDeclContext());
10155+
defaultType = defaultType.subst(subMap);
10156+
conformance->setTypeWitness(assocType, defaultType, assocType);
10157+
satisfied = true;
10158+
}
10159+
1015110160
if (!satisfied) {
1015210161
ABORT([&](auto &out) {
1015310162
out << "Cannot look up associated type for imported conformance:\n";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/include)
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %target-swift-frontend -typecheck -module-name a -cxx-interoperability-mode=default -I %t/include %t/a.swift
6+
7+
//--- include/module.modulemap
8+
module cxx {
9+
header "header.h"
10+
export *
11+
}
12+
13+
//--- include/header.h
14+
struct S {
15+
S() {}
16+
} __attribute__((swift_attr("conforms_to:a.P")));
17+
18+
struct S2 {
19+
S2() {}
20+
using A = S2;
21+
} __attribute__((swift_attr("conforms_to:a.P")));
22+
23+
//--- a.swift
24+
import cxx
25+
public protocol P {
26+
associatedtype A = Int
27+
func foo(_: A)
28+
}
29+
extension P {
30+
func foo(_: A) {}
31+
}
32+
func test(s: S) {
33+
let _ = s.foo(0)
34+
}
35+
func test2(s: S2) {
36+
let _ = s.foo(s)
37+
}

0 commit comments

Comments
 (0)