Skip to content

[cxx-interop] Do not crash while generating a diagnostic for un-instantiatable template #63063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5657,12 +5657,16 @@ clang::FunctionDecl *ClangImporter::instantiateCXXFunctionTemplate(
std::string failedTypesStr;
llvm::raw_string_ostream failedTypesStrStream(failedTypesStr);
llvm::interleaveComma(error->failedTypes, failedTypesStrStream);

std::string funcName;
llvm::raw_string_ostream funcNameStream(funcName);
func->printQualifiedName(funcNameStream);

// TODO: Use the location of the apply here.
// TODO: This error message should not reference implementation details.
// See: https://github.com/apple/swift/pull/33053#discussion_r477003350
ctx.Diags.diagnose(SourceLoc(),
diag::unable_to_convert_generic_swift_types.ID,
{func->getName(), StringRef(failedTypesStr)});
ctx.Diags.diagnose(SourceLoc(), diag::unable_to_convert_generic_swift_types,
funcName, failedTypesStr);
return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ struct MagicWrapper {
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
};

template<class T>
struct MagicWrapperWithExplicitCtor {
T t;
MagicWrapperWithExplicitCtor(T t) : t(t) {}
};

struct IntWrapper {
int value;
int getValue() const { return value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ func swiftTemplateArgNotSupported() {
var _ = MagicWrapper<Optional>(t: "asdf")
}

// 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.
func swiftTemplateArgNotSupportedExplicitCtor() {
var _ = MagicWrapperWithExplicitCtor<String>("asdf")
}

// CHECK: error: no member named 'doesNotExist' in 'IntWrapper'
// CHECK: note: in instantiation of member function 'CannotBeInstantianted<IntWrapper>::CannotBeInstantianted' requested here

Expand Down