mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 14:22:44 +00:00
Fix export of generic functions with generic class params (#8168)
For example, when exporting `fn F[T: type](c: C(T))`, where `C` is a generic class, the exported type of the `c` param should be a `clang::TemplateSpecializationType`. Prior to this commit it was instead exported as the tag type for a `clang::ClassTemplateSpecializationDecl`. This wasn't rejected by clang, but it prevented type substitution from working, causing a failure to infer the template argument.
This commit is contained in:
1 parent
0072b28129
commit
30516f07b5
2 files changed
+40
No files matched your search
@@ -266,6 +266,29 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type)
|
||||
if (!tag_decl) {
|
||||
return clang::QualType();
|
||||
}
|
||||
|
||||
// If the decl is a `ClassTemplateSpecializationDecl` where some of the
|
||||
// template args are dependent, it must be represented as a
|
||||
// `TemplateSpecializationType` instead of a `TagType`.
|
||||
if (auto* specialization_decl =
|
||||
llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(tag_decl)) {
|
||||
auto args = specialization_decl->getTemplateArgs().asArray();
|
||||
auto is_dependent =
|
||||
llvm::any_of(args, [](const auto& arg) { return arg.isDependent(); });
|
||||
|
||||
if (is_dependent) {
|
||||
llvm::SmallVector<clang::TemplateArgument> canonical_args(
|
||||
llvm::map_range(args, [&ast_context](auto& arg) {
|
||||
return ast_context.getCanonicalTemplateArgument(arg);
|
||||
}));
|
||||
|
||||
return ast_context.getTemplateSpecializationType(
|
||||
clang::ElaboratedTypeKeyword::None,
|
||||
clang::TemplateName(specialization_decl->getSpecializedTemplate()),
|
||||
args, canonical_args);
|
||||
}
|
||||
}
|
||||
|
||||
return ast_context.getCanonicalTagType(tag_decl);
|
||||
}
|
||||
|
||||
|
||||
@@ -185,6 +185,23 @@ void CallUnsupportedF() {
|
||||
}
|
||||
''';
|
||||
|
||||
// --- generic_class_param.carbon
|
||||
library "[[@TEST_NAME]]";
|
||||
import Cpp;
|
||||
|
||||
class A {}
|
||||
|
||||
class C(T: type) {}
|
||||
|
||||
fn F[T: type](unused c: C(T)) {}
|
||||
|
||||
inline Cpp '''
|
||||
void G() {
|
||||
Carbon::C<Carbon::A> c;
|
||||
Carbon::F(c);
|
||||
}
|
||||
''';
|
||||
|
||||
// CHECK:STDOUT: --- generic_facet.carbon = $generic_facet
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: constants {
|
||||
|
||||
Reference in new issue
Block a user