Skip to content

Commit ca6fd61

Browse files
authored
Merge pull request #63063 from apple/egorzhdan/cxx-diagnostic-crash
[cxx-interop] Do not crash while generating a diagnostic for un-instantiatable template
2 parents f441e27 + 65f319a commit ca6fd61

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5657,12 +5657,16 @@ clang::FunctionDecl *ClangImporter::instantiateCXXFunctionTemplate(
56575657
std::string failedTypesStr;
56585658
llvm::raw_string_ostream failedTypesStrStream(failedTypesStr);
56595659
llvm::interleaveComma(error->failedTypes, failedTypesStrStream);
5660+
5661+
std::string funcName;
5662+
llvm::raw_string_ostream funcNameStream(funcName);
5663+
func->printQualifiedName(funcNameStream);
5664+
56605665
// TODO: Use the location of the apply here.
56615666
// TODO: This error message should not reference implementation details.
56625667
// See: https://github.com/apple/swift/pull/33053#discussion_r477003350
5663-
ctx.Diags.diagnose(SourceLoc(),
5664-
diag::unable_to_convert_generic_swift_types.ID,
5665-
{func->getName(), StringRef(failedTypesStr)});
5668+
ctx.Diags.diagnose(SourceLoc(), diag::unable_to_convert_generic_swift_types,
5669+
funcName, failedTypesStr);
56665670
return nullptr;
56675671
}
56685672

test/Interop/Cxx/templates/Inputs/class-template-instantiation-errors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ struct MagicWrapper {
77
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
88
};
99

10+
template<class T>
11+
struct MagicWrapperWithExplicitCtor {
12+
T t;
13+
MagicWrapperWithExplicitCtor(T t) : t(t) {}
14+
};
15+
1016
struct IntWrapper {
1117
int value;
1218
int getValue() const { return value; }

test/Interop/Cxx/templates/class-template-instantiation-typechecker.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ func swiftTemplateArgNotSupported() {
77
var _ = MagicWrapper<Optional>(t: "asdf")
88
}
99

10+
// CHECK: class-template-instantiation-typechecker.swift:12:11: error: could not generate C++ types from the generic Swift types provided. The following Swift type(s) provided to 'MagicWrapperWithExplicitCtor' could not be converted: String.
11+
func swiftTemplateArgNotSupportedExplicitCtor() {
12+
var _ = MagicWrapperWithExplicitCtor<String>("asdf")
13+
}
14+
1015
// CHECK: error: no member named 'doesNotExist' in 'IntWrapper'
1116
// CHECK: note: in instantiation of member function 'CannotBeInstantianted<IntWrapper>::CannotBeInstantianted' requested here
1217

0 commit comments

Comments
 (0)