Support for passing C++ templates as arguments to template template parameters. (#6475)

Allow passing a C++ template as a template template argument to another
C++ template. Does not allow passing a Carbon generic as an argument.

Depends on #6474.
This commit is contained in:
Richard Smith
2025-12-16 19:00:49 +00:00
committed by GitHub
parent 3ae6f96141
commit 2e65d28a16
2 changed files with 104 additions and 21 deletions
@@ -14,37 +14,92 @@
template<typename> struct A {};
template<typename> struct B {};
template<typename, typename> struct C {};
template<template<typename> typename, template<typename> typename>
struct TwoTemplates {};
// --- fail_todo_valid.carbon
// --- valid.carbon
library "[[@TEST_NAME]]";
import Cpp library "templates.h";
//@dump-sem-ir-begin
// CHECK:STDERR: fail_todo_valid.carbon:[[@LINE+4]]:25: error: semantics TODO: `argument for template template parameter` [SemanticsTodo]
// CHECK:STDERR: var x: Cpp.TwoTemplates(Cpp.A, Cpp.B);
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
var x: Cpp.TwoTemplates(Cpp.A, Cpp.B);
//@dump-sem-ir-end
// --- fail_template_mismatch.carbon
library "[[@TEST_NAME]]";
import Cpp library "templates.h";
//@dump-sem-ir-begin
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE+15]]:35: error: too few template arguments for class template 'C' [CppInteropParseError]
// CHECK:STDERR: 22 | var x: Cpp.TwoTemplates(Cpp.A, Cpp.C);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE+12]]:35: note: template template argument has different template parameters than its corresponding template template parameter [CppInteropParseNote]
// CHECK:STDERR: 22 | var x: Cpp.TwoTemplates(Cpp.A, Cpp.C);
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE-9]]:10: in file included here [InCppInclude]
// CHECK:STDERR: ./templates.h:6:66: note: previous template template parameter is here [CppInteropParseNote]
// CHECK:STDERR: 6 | template<template<typename> typename, template<typename> typename>
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR: fail_template_mismatch.carbon:[[@LINE-13]]:10: in file included here [InCppInclude]
// CHECK:STDERR: ./templates.h:4:37: note: template is declared here [CppInteropParseNote]
// CHECK:STDERR: 4 | template<typename, typename> struct C {};
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
// CHECK:STDERR:
var x: Cpp.TwoTemplates(Cpp.A, Cpp.C);
//@dump-sem-ir-end
// --- fail_type_argument.carbon
library "[[@TEST_NAME]]";
import Cpp library "templates.h";
class X {}
// CHECK:STDERR: fail_type_argument.carbon:[[@LINE+4]]:32: error: template argument for template template parameter must be a class template or type alias template [CppInteropParseError]
// CHECK:STDERR: 12 | var x: Cpp.TwoTemplates(Cpp.A, X);
// CHECK:STDERR: | ^
// CHECK:STDERR:
var x: Cpp.TwoTemplates(Cpp.A, X);
// --- fail_non_type_argument.carbon
library "[[@TEST_NAME]]";
import Cpp library "templates.h";
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+4]]:25: error: semantics TODO: `argument for template template parameter` [SemanticsTodo]
// CHECK:STDERR: var x: Cpp.TwoTemplates(Cpp.A, true);
// CHECK:STDERR: ^~~~~
// CHECK:STDERR: fail_non_type_argument.carbon:[[@LINE+4]]:32: error: template argument for template template parameter must be a class template or type alias template [CppInteropParseError]
// CHECK:STDERR: 10 | var x: Cpp.TwoTemplates(Cpp.A, true);
// CHECK:STDERR: | ^
// CHECK:STDERR:
var x: Cpp.TwoTemplates(Cpp.A, true);
// CHECK:STDOUT: --- fail_todo_valid.carbon
// CHECK:STDOUT: --- valid.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %TwoTemplates: type = class_type @TwoTemplates [concrete]
// CHECK:STDOUT: %pattern_type: type = pattern_type %TwoTemplates [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %x.patt: %pattern_type = ref_binding_pattern x [concrete]
// CHECK:STDOUT: %x.var_patt: %pattern_type = var_pattern %x.patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %x.var: ref %TwoTemplates = var %x.var_patt [concrete]
// CHECK:STDOUT: %x: ref %TwoTemplates = ref_binding x, %x.var [concrete = %x.var]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_template_mismatch.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: }