local.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/generic/local.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/generic/local.carbon
  10. // --- class.carbon
  11. library "[[@TEST_NAME]]";
  12. fn F() {
  13. class C(T:! type) {
  14. var x: T;
  15. }
  16. var v: C(i32) = {.x = 1};
  17. }
  18. // --- fail_param_shadows_class.carbon
  19. library "[[@TEST_NAME]]";
  20. fn F() {
  21. // TODO: Decide on what behavior we want here. We don't reject the corresponding case outside of a function.
  22. // CHECK:STDERR: fail_param_shadows_class.carbon:[[@LINE+7]]:3: error: duplicate name being declared in the same scope [NameDeclDuplicate]
  23. // CHECK:STDERR: class C(C:! type) {
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  25. // CHECK:STDERR: fail_param_shadows_class.carbon:[[@LINE+4]]:11: note: name is previously declared here [NameDeclPrevious]
  26. // CHECK:STDERR: class C(C:! type) {
  27. // CHECK:STDERR: ^
  28. // CHECK:STDERR:
  29. class C(C:! type) {
  30. }
  31. }
  32. // --- nonlocal_param_shadows_class.carbon
  33. library "[[@TEST_NAME]]";
  34. class C(C:! type) {
  35. }
  36. // CHECK:STDOUT: --- class.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  40. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  41. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  42. // CHECK:STDOUT: %T.patt: type = symbolic_binding_pattern T, 0 [symbolic]
  43. // CHECK:STDOUT: %C.type: type = generic_class_type @C [template]
  44. // CHECK:STDOUT: %C.generic: %C.type = struct_value () [template]
  45. // CHECK:STDOUT: %C.f06: type = class_type @C, @C(%T) [symbolic]
  46. // CHECK:STDOUT: %require_complete.4ae: <witness> = require_complete_type %T [symbolic]
  47. // CHECK:STDOUT: %C.elem.cca: type = unbound_element_type %C.f06, %T [symbolic]
  48. // CHECK:STDOUT: %struct_type.x.2ac: type = struct_type {.x: %T} [symbolic]
  49. // CHECK:STDOUT: %complete_type.4339: <witness> = complete_type_witness %struct_type.x.2ac [symbolic]
  50. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  51. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  52. // CHECK:STDOUT: %C.d45: type = class_type @C, @C(%i32) [template]
  53. // CHECK:STDOUT: %i32.builtin: type = int_type signed, %int_32 [template]
  54. // CHECK:STDOUT: %complete_type.f8a: <witness> = complete_type_witness %i32.builtin [template]
  55. // CHECK:STDOUT: %C.elem.f74: type = unbound_element_type %C.d45, %i32 [template]
  56. // CHECK:STDOUT: %struct_type.x.ed6: type = struct_type {.x: %i32} [template]
  57. // CHECK:STDOUT: %complete_type.1ec: <witness> = complete_type_witness %struct_type.x.ed6 [template]
  58. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  59. // CHECK:STDOUT: %struct_type.x.c96: type = struct_type {.x: Core.IntLiteral} [template]
  60. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  61. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  62. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  63. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  64. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  65. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  66. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  67. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  68. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  69. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  70. // CHECK:STDOUT: %C.val: %C.d45 = struct_value (%int_1.5d2) [template]
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: imports {
  74. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  75. // CHECK:STDOUT: .Int = %Core.Int
  76. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  77. // CHECK:STDOUT: import Core//prelude
  78. // CHECK:STDOUT: import Core//prelude/...
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: file {
  83. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  84. // CHECK:STDOUT: .Core = imports.%Core
  85. // CHECK:STDOUT: .F = %F.decl
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: %Core.import = import Core
  88. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: generic class @C(%T.loc5_11.1: type) {
  92. // CHECK:STDOUT: %T.loc5_11.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc5_11.2 (constants.%T)]
  93. // CHECK:STDOUT: %T.patt.loc5_11.2: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc5_11.2 (constants.%T.patt)]
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: !definition:
  96. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type @C.%T.loc5_11.2 (%T) [symbolic = %require_complete (constants.%require_complete.4ae)]
  97. // CHECK:STDOUT: %C: type = class_type @C, @C(%T.loc5_11.2) [symbolic = %C (constants.%C.f06)]
  98. // CHECK:STDOUT: %C.elem: type = unbound_element_type @C.%C (%C.f06), @C.%T.loc5_11.2 (%T) [symbolic = %C.elem (constants.%C.elem.cca)]
  99. // CHECK:STDOUT: %struct_type.x: type = struct_type {.x: @C.%T.loc5_11.2 (%T)} [symbolic = %struct_type.x (constants.%struct_type.x.2ac)]
  100. // CHECK:STDOUT: %complete_type.loc7_3.2: <witness> = complete_type_witness @C.%struct_type.x (%struct_type.x.2ac) [symbolic = %complete_type.loc7_3.2 (constants.%complete_type.4339)]
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: class {
  103. // CHECK:STDOUT: %.loc6_10: @C.%C.elem (%C.elem.cca) = field_decl x, element0 [template]
  104. // CHECK:STDOUT: name_binding_decl {
  105. // CHECK:STDOUT: %.loc6_5: @C.%C.elem (%C.elem.cca) = var_pattern %.loc6_10
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %.var: ref @C.%C.elem (%C.elem.cca) = var <none>
  108. // CHECK:STDOUT: %complete_type.loc7_3.1: <witness> = complete_type_witness %struct_type.x.2ac [symbolic = %complete_type.loc7_3.2 (constants.%complete_type.4339)]
  109. // CHECK:STDOUT: complete_type_witness = %complete_type.loc7_3.1
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: !members:
  112. // CHECK:STDOUT: .Self = constants.%C.f06
  113. // CHECK:STDOUT: .x = %.loc6_10
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @F() {
  118. // CHECK:STDOUT: !entry:
  119. // CHECK:STDOUT: %C.decl: %C.type = class_decl @C [template = constants.%C.generic] {
  120. // CHECK:STDOUT: %T.patt.loc5_11.1: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc5_11.2 (constants.%T.patt)]
  121. // CHECK:STDOUT: %T.param_patt: type = value_param_pattern %T.patt.loc5_11.1, runtime_param<none> [symbolic = %T.patt.loc5_11.2 (constants.%T.patt)]
  122. // CHECK:STDOUT: } {
  123. // CHECK:STDOUT: %T.param: type = value_param runtime_param<none>
  124. // CHECK:STDOUT: %T.loc5_11.1: type = bind_symbolic_name T, 0, %T.param [symbolic = %T.loc5_11.2 (constants.%T)]
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: name_binding_decl {
  127. // CHECK:STDOUT: %v.patt: %C.d45 = binding_pattern v
  128. // CHECK:STDOUT: %.loc8_3.1: %C.d45 = var_pattern %v.patt
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: %v.var: ref %C.d45 = var v
  131. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  132. // CHECK:STDOUT: %.loc8_26.1: %struct_type.x.c96 = struct_literal (%int_1)
  133. // CHECK:STDOUT: %impl.elem0: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  134. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound]
  135. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %bound_method, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  136. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %specific_fn(%int_1) [template = constants.%int_1.5d2]
  137. // CHECK:STDOUT: %.loc8_26.2: init %i32 = converted %int_1, %int.convert_checked [template = constants.%int_1.5d2]
  138. // CHECK:STDOUT: %.loc8_26.3: ref %i32 = class_element_access %v.var, element0
  139. // CHECK:STDOUT: %.loc8_26.4: init %i32 = initialize_from %.loc8_26.2 to %.loc8_26.3 [template = constants.%int_1.5d2]
  140. // CHECK:STDOUT: %.loc8_26.5: init %C.d45 = class_init (%.loc8_26.4), %v.var [template = constants.%C.val]
  141. // CHECK:STDOUT: %.loc8_3.2: init %C.d45 = converted %.loc8_26.1, %.loc8_26.5 [template = constants.%C.val]
  142. // CHECK:STDOUT: assign %v.var, %.loc8_3.2
  143. // CHECK:STDOUT: %.loc8_15: type = splice_block %C [template = constants.%C.d45] {
  144. // CHECK:STDOUT: %C.ref: %C.type = name_ref C, %C.decl [template = constants.%C.generic]
  145. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  146. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  147. // CHECK:STDOUT: %C: type = class_type @C, @C(constants.%i32) [template = constants.%C.d45]
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: %v: ref %C.d45 = bind_name v, %v.var
  150. // CHECK:STDOUT: return
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: specific @C(constants.%T) {
  154. // CHECK:STDOUT: %T.loc5_11.2 => constants.%T
  155. // CHECK:STDOUT: %T.patt.loc5_11.2 => constants.%T
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: specific @C(%T.loc5_11.2) {}
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: specific @C(constants.%i32) {
  161. // CHECK:STDOUT: %T.loc5_11.2 => constants.%i32
  162. // CHECK:STDOUT: %T.patt.loc5_11.2 => constants.%i32
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: !definition:
  165. // CHECK:STDOUT: %require_complete => constants.%complete_type.f8a
  166. // CHECK:STDOUT: %C => constants.%C.d45
  167. // CHECK:STDOUT: %C.elem => constants.%C.elem.f74
  168. // CHECK:STDOUT: %struct_type.x => constants.%struct_type.x.ed6
  169. // CHECK:STDOUT: %complete_type.loc7_3.2 => constants.%complete_type.1ec
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: --- fail_param_shadows_class.carbon
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: constants {
  175. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  176. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  177. // CHECK:STDOUT: %C: type = bind_symbolic_name C, 0 [symbolic]
  178. // CHECK:STDOUT: %C.patt: type = symbolic_binding_pattern C, 0 [symbolic]
  179. // CHECK:STDOUT: %.type: type = generic_class_type @.1 [template]
  180. // CHECK:STDOUT: %.generic: %.type = struct_value () [template]
  181. // CHECK:STDOUT: %.de1: type = class_type @.1, @.1(%C) [symbolic]
  182. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  183. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: imports {
  187. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  188. // CHECK:STDOUT: import Core//prelude
  189. // CHECK:STDOUT: import Core//prelude/...
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: file {
  194. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  195. // CHECK:STDOUT: .Core = imports.%Core
  196. // CHECK:STDOUT: .F = %F.decl
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: %Core.import = import Core
  199. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  200. // CHECK:STDOUT: }
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: generic class @.1(%C.loc13_11.1: type) {
  203. // CHECK:STDOUT: %C.loc13_11.2: type = bind_symbolic_name C, 0 [symbolic = %C.loc13_11.2 (constants.%C)]
  204. // CHECK:STDOUT: %C.patt.loc13_11.2: type = symbolic_binding_pattern C, 0 [symbolic = %C.patt.loc13_11.2 (constants.%C.patt)]
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: !definition:
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: class {
  209. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  210. // CHECK:STDOUT: complete_type_witness = %complete_type
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: !members:
  213. // CHECK:STDOUT: .Self = constants.%.de1
  214. // CHECK:STDOUT: }
  215. // CHECK:STDOUT: }
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: fn @F() {
  218. // CHECK:STDOUT: !entry:
  219. // CHECK:STDOUT: %.decl: %.type = class_decl @.1 [template = constants.%.generic] {
  220. // CHECK:STDOUT: %C.patt.loc13_11.1: type = symbolic_binding_pattern C, 0 [symbolic = %C.patt.loc13_11.2 (constants.%C.patt)]
  221. // CHECK:STDOUT: %C.param_patt: type = value_param_pattern %C.patt.loc13_11.1, runtime_param<none> [symbolic = %C.patt.loc13_11.2 (constants.%C.patt)]
  222. // CHECK:STDOUT: } {
  223. // CHECK:STDOUT: %C.param: type = value_param runtime_param<none>
  224. // CHECK:STDOUT: %C.loc13_11.1: type = bind_symbolic_name C, 0, %C.param [symbolic = %C.loc13_11.2 (constants.%C)]
  225. // CHECK:STDOUT: }
  226. // CHECK:STDOUT: return
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: specific @.1(constants.%C) {
  230. // CHECK:STDOUT: %C.loc13_11.2 => constants.%C
  231. // CHECK:STDOUT: %C.patt.loc13_11.2 => constants.%C
  232. // CHECK:STDOUT: }
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: --- nonlocal_param_shadows_class.carbon
  235. // CHECK:STDOUT:
  236. // CHECK:STDOUT: constants {
  237. // CHECK:STDOUT: %C.8b3: type = bind_symbolic_name C, 0 [symbolic]
  238. // CHECK:STDOUT: %C.patt: type = symbolic_binding_pattern C, 0 [symbolic]
  239. // CHECK:STDOUT: %C.type: type = generic_class_type @C [template]
  240. // CHECK:STDOUT: %C.generic: %C.type = struct_value () [template]
  241. // CHECK:STDOUT: %C.f2e: type = class_type @C, @C(%C.8b3) [symbolic]
  242. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  243. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  244. // CHECK:STDOUT: }
  245. // CHECK:STDOUT:
  246. // CHECK:STDOUT: imports {
  247. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  248. // CHECK:STDOUT: import Core//prelude
  249. // CHECK:STDOUT: import Core//prelude/...
  250. // CHECK:STDOUT: }
  251. // CHECK:STDOUT: }
  252. // CHECK:STDOUT:
  253. // CHECK:STDOUT: file {
  254. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  255. // CHECK:STDOUT: .Core = imports.%Core
  256. // CHECK:STDOUT: .C = %C.decl
  257. // CHECK:STDOUT: }
  258. // CHECK:STDOUT: %Core.import = import Core
  259. // CHECK:STDOUT: %C.decl: %C.type = class_decl @C [template = constants.%C.generic] {
  260. // CHECK:STDOUT: %C.patt.loc4_9.1: type = symbolic_binding_pattern C, 0 [symbolic = %C.patt.loc4_9.2 (constants.%C.patt)]
  261. // CHECK:STDOUT: %C.param_patt: type = value_param_pattern %C.patt.loc4_9.1, runtime_param<none> [symbolic = %C.patt.loc4_9.2 (constants.%C.patt)]
  262. // CHECK:STDOUT: } {
  263. // CHECK:STDOUT: %C.param: type = value_param runtime_param<none>
  264. // CHECK:STDOUT: %C.loc4_9.1: type = bind_symbolic_name C, 0, %C.param [symbolic = %C.loc4_9.2 (constants.%C.8b3)]
  265. // CHECK:STDOUT: }
  266. // CHECK:STDOUT: }
  267. // CHECK:STDOUT:
  268. // CHECK:STDOUT: generic class @C(%C.loc4_9.1: type) {
  269. // CHECK:STDOUT: %C.loc4_9.2: type = bind_symbolic_name C, 0 [symbolic = %C.loc4_9.2 (constants.%C.8b3)]
  270. // CHECK:STDOUT: %C.patt.loc4_9.2: type = symbolic_binding_pattern C, 0 [symbolic = %C.patt.loc4_9.2 (constants.%C.patt)]
  271. // CHECK:STDOUT:
  272. // CHECK:STDOUT: !definition:
  273. // CHECK:STDOUT:
  274. // CHECK:STDOUT: class {
  275. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  276. // CHECK:STDOUT: complete_type_witness = %complete_type
  277. // CHECK:STDOUT:
  278. // CHECK:STDOUT: !members:
  279. // CHECK:STDOUT: .Self = constants.%C.f2e
  280. // CHECK:STDOUT: }
  281. // CHECK:STDOUT: }
  282. // CHECK:STDOUT:
  283. // CHECK:STDOUT: specific @C(constants.%C.8b3) {
  284. // CHECK:STDOUT: %C.loc4_9.2 => constants.%C.8b3
  285. // CHECK:STDOUT: %C.patt.loc4_9.2 => constants.%C.8b3
  286. // CHECK:STDOUT: }
  287. // CHECK:STDOUT: