Skip to content

Commit f3ae536

Browse files
authored
Merge pull request #83544 from Xazax-hun/proper-conforms-to-diags
[cxx-interop] Diagnose missing type witness instead of crashing
2 parents c316e93 + 9b0acdb commit f3ae536

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/AST/ConformanceLookup.h"
2828
#include "swift/AST/Decl.h"
2929
#include "swift/AST/DiagnosticsClangImporter.h"
30+
#include "swift/AST/DiagnosticsSema.h"
3031
#include "swift/AST/ExistentialLayout.h"
3132
#include "swift/AST/Expr.h"
3233
#include "swift/AST/GenericEnvironment.h"
@@ -70,9 +71,9 @@
7071
#include "clang/AST/Type.h"
7172
#include "clang/Basic/Specifiers.h"
7273
#include "clang/Basic/TargetInfo.h"
73-
#include "clang/Sema/SemaDiagnostic.h"
7474
#include "clang/Lex/Preprocessor.h"
7575
#include "clang/Sema/Lookup.h"
76+
#include "clang/Sema/SemaDiagnostic.h"
7677

7778
#include "llvm/ADT/STLExtras.h"
7879
#include "llvm/ADT/SmallBitVector.h"
@@ -10113,8 +10114,8 @@ void ClangImporter::Implementation::startedImportingEntity() {
1011310114
}
1011410115

1011510116
/// Look up associated type requirements in the conforming type.
10116-
static void finishTypeWitnesses(
10117-
NormalProtocolConformance *conformance) {
10117+
static void finishTypeWitnesses(NormalProtocolConformance *conformance,
10118+
ASTContext &ctx) {
1011810119
auto *dc = conformance->getDeclContext();
1011910120
auto nominal = dc->getSelfNominalTypeDecl();
1012010121

@@ -10158,11 +10159,11 @@ static void finishTypeWitnesses(
1015810159
}
1015910160

1016010161
if (!satisfied) {
10161-
ABORT([&](auto &out) {
10162-
out << "Cannot look up associated type for imported conformance:\n";
10163-
conformance->getType().dump(out);
10164-
assocType->dump(out);
10165-
});
10162+
// Avoid compiler crash due to missing witness.
10163+
conformance->setTypeWitness(assocType, ErrorType::get(ctx), assocType);
10164+
proto->diagnose(diag::type_does_not_conform, selfType,
10165+
proto->getDeclaredType());
10166+
proto->diagnose(diag::no_witnesses_type, assocType);
1016610167
}
1016710168
}
1016810169
}
@@ -10210,7 +10211,7 @@ void ClangImporter::Implementation::finishNormalConformance(
1021010211
auto *proto = conformance->getProtocol();
1021110212
PrettyStackTraceConformance trace("completing import of", conformance);
1021210213

10213-
finishTypeWitnesses(conformance);
10214+
finishTypeWitnesses(conformance, SwiftContext);
1021410215

1021510216
// Imported conformances to @objc protocols also require additional
1021610217
// initialization to complete the requirement to witness mapping.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 -verify
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+
//--- a.swift
19+
import cxx
20+
// expected-error@+2{{type 'S' does not conform to protocol 'P'}}
21+
// expected-note@+1{{protocol requires nested type 'A'}}
22+
public protocol P {
23+
associatedtype A
24+
func foo(_: A)
25+
}
26+
extension P {
27+
func foo(_: A) {}
28+
}
29+
func test(x : S) {
30+
x.foo(0) // expected-error{{cannot convert value of type 'Int' to expected argument type 'S.A'}}
31+
}

0 commit comments

Comments
 (0)