template.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/template.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/template.carbon
  12. // --- class_template.h
  13. struct Base {};
  14. template<typename T> struct A : Base {
  15. void f() const;
  16. T n;
  17. };
  18. using Aint = A<int>;
  19. using Along = A<long>;
  20. // --- use_class_template.carbon
  21. library "[[@TEST_NAME]]";
  22. import Cpp library "class_template.h";
  23. fn F(x: Cpp.Aint) -> i32 {
  24. // Trigger instantiation through name lookup.
  25. //@dump-sem-ir-begin
  26. x.f();
  27. return x.n;
  28. //@dump-sem-ir-end
  29. }
  30. fn G(p: Cpp.Along*) -> Cpp.Base* {
  31. // Trigger instantiation through type completion.
  32. //@dump-sem-ir-begin
  33. return p;
  34. //@dump-sem-ir-end
  35. }
  36. // --- instantiation_error.h
  37. template<typename T> struct X {
  38. using type = typename T::member;
  39. };
  40. struct Y {
  41. using member = int;
  42. };
  43. using XY = X<Y>;
  44. using Xint = X<int>;
  45. // --- fail_use_instantiation_error.carbon
  46. library "[[@TEST_NAME]]";
  47. // CHECK:STDERR: fail_use_instantiation_error.carbon:[[@LINE+11]]:1: in import [InImport]
  48. // CHECK:STDERR: ./instantiation_error.h:3: error: In file included from fail_use_instantiation_error.carbon:[[@LINE+10]]:
  49. // CHECK:STDERR: ./instantiation_error.h:3:25: error: type 'int' cannot be used prior to '::' because it has no members
  50. // CHECK:STDERR: 3 | using type = typename T::member;
  51. // CHECK:STDERR: | ^
  52. // CHECK:STDERR: [CppInteropParseError]
  53. // CHECK:STDERR: fail_use_instantiation_error.carbon:[[@LINE+5]]:1: in import [InImport]
  54. // CHECK:STDERR: ./instantiation_error.h:2: note: ./instantiation_error.h:2:29: note: in instantiation of template class 'X<int>' requested here
  55. // CHECK:STDERR: 2 | template<typename T> struct X {
  56. // CHECK:STDERR: | ^
  57. // CHECK:STDERR: [CppInteropParseNote]
  58. import Cpp library "instantiation_error.h";
  59. //@dump-sem-ir-begin
  60. var x: Cpp.XY.r#type = 0;
  61. // CHECK:STDERR: fail_use_instantiation_error.carbon:[[@LINE+4]]:8: note: while completing C++ class type `Cpp.X` [InCppTypeCompletion]
  62. // CHECK:STDERR: var y: Cpp.Xint.r#type = 0;
  63. // CHECK:STDERR: ^~~~~~~~~~~~~
  64. // CHECK:STDERR:
  65. var y: Cpp.Xint.r#type = 0;
  66. //@dump-sem-ir-end
  67. // CHECK:STDOUT: --- use_class_template.carbon
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: constants {
  70. // CHECK:STDOUT: %A.0bedf0.1: type = class_type @A.1 [concrete]
  71. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  72. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  73. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  74. // CHECK:STDOUT: %Base: type = class_type @Base [concrete]
  75. // CHECK:STDOUT: %A.elem.c3f: type = unbound_element_type %A.0bedf0.1, %i32 [concrete]
  76. // CHECK:STDOUT: %A.f.type: type = fn_type @A.f [concrete]
  77. // CHECK:STDOUT: %A.f: %A.f.type = struct_value () [concrete]
  78. // CHECK:STDOUT: %A.0bedf0.2: type = class_type @A.2 [concrete]
  79. // CHECK:STDOUT: %ptr.270: type = ptr_type %A.0bedf0.2 [concrete]
  80. // CHECK:STDOUT: %ptr.fb2: type = ptr_type %Base [concrete]
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: imports {
  84. // CHECK:STDOUT: %A.f.decl: %A.f.type = fn_decl @A.f [concrete = constants.%A.f] {
  85. // CHECK:STDOUT: <elided>
  86. // CHECK:STDOUT: } {
  87. // CHECK:STDOUT: <elided>
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: fn @F(%x.param: %A.0bedf0.1) -> %i32 {
  92. // CHECK:STDOUT: !entry:
  93. // CHECK:STDOUT: %x.ref.loc9: %A.0bedf0.1 = name_ref x, %x
  94. // CHECK:STDOUT: %f.ref: %A.f.type = name_ref f, imports.%A.f.decl [concrete = constants.%A.f]
  95. // CHECK:STDOUT: %A.f.bound: <bound method> = bound_method %x.ref.loc9, %f.ref
  96. // CHECK:STDOUT: %A.f.call: init %empty_tuple.type = call %A.f.bound(%x.ref.loc9)
  97. // CHECK:STDOUT: %x.ref.loc10: %A.0bedf0.1 = name_ref x, %x
  98. // CHECK:STDOUT: %n.ref: %A.elem.c3f = name_ref n, @A.1.%.2 [concrete = @A.1.%.2]
  99. // CHECK:STDOUT: %.loc10_11.1: ref %i32 = class_element_access %x.ref.loc10, element1
  100. // CHECK:STDOUT: %.loc10_11.2: %i32 = bind_value %.loc10_11.1
  101. // CHECK:STDOUT: return %.loc10_11.2
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: fn @G(%p.param: %ptr.270) -> %ptr.fb2 {
  105. // CHECK:STDOUT: !entry:
  106. // CHECK:STDOUT: %p.ref: %ptr.270 = name_ref p, %p
  107. // CHECK:STDOUT: %.loc17_11.1: ref %A.0bedf0.2 = deref %p.ref
  108. // CHECK:STDOUT: %.loc17_11.2: ref %Base = class_element_access %.loc17_11.1, element0
  109. // CHECK:STDOUT: %addr: %ptr.fb2 = addr_of %.loc17_11.2
  110. // CHECK:STDOUT: %.loc17_11.3: %ptr.fb2 = converted %p.ref, %addr
  111. // CHECK:STDOUT: return %.loc17_11.3
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: --- fail_use_instantiation_error.carbon
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: constants {
  117. // CHECK:STDOUT: %X.32d59e.1: type = class_type @X.1 [concrete]
  118. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  119. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  120. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  121. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  122. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  123. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  124. // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  125. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.0f9: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  126. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.f06: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.0f9 = struct_value () [symbolic]
  127. // CHECK:STDOUT: %ImplicitAs.impl_witness.c75: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.a2f, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  128. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.035: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  129. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.956: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.035 = struct_value () [concrete]
  130. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.c75) [concrete]
  131. // CHECK:STDOUT: %.9c3: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  132. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.956 [concrete]
  133. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.956, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  134. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  135. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  136. // CHECK:STDOUT: %X.32d59e.2: type = class_type @X.2 [concrete]
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: imports {
  140. // CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
  141. // CHECK:STDOUT: .XY = %X.decl.dec4cb.1
  142. // CHECK:STDOUT: .Xint = %X.decl.dec4cb.2
  143. // CHECK:STDOUT: import Cpp//...
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: %X.decl.dec4cb.1: type = class_decl @X.1 [concrete = constants.%X.32d59e.1] {} {}
  146. // CHECK:STDOUT: %Core.import_ref.a5b: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.0f9) = import_ref Core//prelude/parts/int, loc16_39, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.f06)]
  147. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (%Core.import_ref.a5b), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  148. // CHECK:STDOUT: %X.decl.dec4cb.2: type = class_decl @X.2 [concrete = constants.%X.32d59e.2] {} {}
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: file {
  152. // CHECK:STDOUT: name_binding_decl {
  153. // CHECK:STDOUT: %x.patt: %pattern_type.7ce = binding_pattern x [concrete]
  154. // CHECK:STDOUT: %x.var_patt: %pattern_type.7ce = var_pattern %x.patt [concrete]
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: %x.var: ref %i32 = var %x.var_patt [concrete]
  157. // CHECK:STDOUT: %.loc18: type = splice_block %type.ref.loc18 [concrete = constants.%i32] {
  158. // CHECK:STDOUT: %Cpp.ref.loc18: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  159. // CHECK:STDOUT: %XY.ref: type = name_ref XY, imports.%X.decl.dec4cb.1 [concrete = constants.%X.32d59e.1]
  160. // CHECK:STDOUT: <elided>
  161. // CHECK:STDOUT: %type.ref.loc18: type = name_ref r#type, %i32.2 [concrete = constants.%i32]
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT: %x: ref %i32 = bind_name x, %x.var [concrete = %x.var]
  164. // CHECK:STDOUT: name_binding_decl {
  165. // CHECK:STDOUT: %y.patt: %pattern_type.7ce = binding_pattern y [concrete]
  166. // CHECK:STDOUT: %y.var_patt: %pattern_type.7ce = var_pattern %y.patt [concrete]
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: %y.var: ref %i32 = var %y.var_patt [concrete]
  169. // CHECK:STDOUT: %.loc24: type = splice_block %type.ref.loc24 [concrete = constants.%i32] {
  170. // CHECK:STDOUT: %Cpp.ref.loc24: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  171. // CHECK:STDOUT: %Xint.ref: type = name_ref Xint, imports.%X.decl.dec4cb.2 [concrete = constants.%X.32d59e.2]
  172. // CHECK:STDOUT: <elided>
  173. // CHECK:STDOUT: %type.ref.loc24: type = name_ref r#type, %i32.1 [concrete = constants.%i32]
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: %y: ref %i32 = bind_name y, %y.var [concrete = %y.var]
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: fn @__global_init() {
  179. // CHECK:STDOUT: !entry:
  180. // CHECK:STDOUT: %int_0.loc18: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  181. // CHECK:STDOUT: %impl.elem0.loc18: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956]
  182. // CHECK:STDOUT: %bound_method.loc18_1.1: <bound method> = bound_method %int_0.loc18, %impl.elem0.loc18 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  183. // CHECK:STDOUT: %specific_fn.loc18: <specific function> = specific_function %impl.elem0.loc18, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  184. // CHECK:STDOUT: %bound_method.loc18_1.2: <bound method> = bound_method %int_0.loc18, %specific_fn.loc18 [concrete = constants.%bound_method]
  185. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc18: init %i32 = call %bound_method.loc18_1.2(%int_0.loc18) [concrete = constants.%int_0.6a9]
  186. // CHECK:STDOUT: %.loc18: init %i32 = converted %int_0.loc18, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc18 [concrete = constants.%int_0.6a9]
  187. // CHECK:STDOUT: assign file.%x.var, %.loc18
  188. // CHECK:STDOUT: %int_0.loc24: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  189. // CHECK:STDOUT: %impl.elem0.loc24: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.956]
  190. // CHECK:STDOUT: %bound_method.loc24_1.1: <bound method> = bound_method %int_0.loc24, %impl.elem0.loc24 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  191. // CHECK:STDOUT: %specific_fn.loc24: <specific function> = specific_function %impl.elem0.loc24, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  192. // CHECK:STDOUT: %bound_method.loc24_1.2: <bound method> = bound_method %int_0.loc24, %specific_fn.loc24 [concrete = constants.%bound_method]
  193. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc24: init %i32 = call %bound_method.loc24_1.2(%int_0.loc24) [concrete = constants.%int_0.6a9]
  194. // CHECK:STDOUT: %.loc24: init %i32 = converted %int_0.loc24, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc24 [concrete = constants.%int_0.6a9]
  195. // CHECK:STDOUT: assign file.%y.var, %.loc24
  196. // CHECK:STDOUT: <elided>
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: