int_float.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/deduce/int_float.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/deduce/int_float.carbon
  10. // --- int.carbon
  11. library "[[@TEST_NAME]]";
  12. fn F[N:! i32](n: Core.Int(N)) -> i32 {
  13. return N;
  14. }
  15. fn G(a: i64) -> i32 {
  16. return F(a);
  17. }
  18. // --- fail_todo_float.carbon
  19. library "[[@TEST_NAME]]";
  20. // TODO: This should pass once we accept symbolic widths for Core.Float.
  21. // CHECK:STDERR: fail_todo_float.carbon:[[@LINE+3]]:18: error: cannot evaluate type expression
  22. // CHECK:STDERR: fn F[N:! i32](n: Core.Float(N)) -> i32 {
  23. // CHECK:STDERR: ^~~~~~~~~~~~~
  24. fn F[N:! i32](n: Core.Float(N)) -> i32 {
  25. return N;
  26. }
  27. fn G(a: f64) -> i32 {
  28. return F(a);
  29. }
  30. // CHECK:STDOUT: --- int.carbon
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: constants {
  33. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  34. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  35. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  36. // CHECK:STDOUT: %N: i32 = bind_symbolic_name N, 0 [symbolic]
  37. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  38. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  39. // CHECK:STDOUT: %.2: type = int_type signed, %N [symbolic]
  40. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  41. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  42. // CHECK:STDOUT: %.3: i32 = int_literal 64 [template]
  43. // CHECK:STDOUT: %.4: type = int_type signed, %.3 [template]
  44. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  45. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: imports {
  49. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  50. // CHECK:STDOUT: .Int32 = %import_ref.1
  51. // CHECK:STDOUT: .Int = %import_ref.2
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/operators
  54. // CHECK:STDOUT: import Core//prelude/types
  55. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  56. // CHECK:STDOUT: import Core//prelude/operators/as
  57. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  58. // CHECK:STDOUT: import Core//prelude/operators/comparison
  59. // CHECK:STDOUT: import Core//prelude/types/bool
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  62. // CHECK:STDOUT: %import_ref.2: %Int.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  67. // CHECK:STDOUT: .Core = imports.%Core
  68. // CHECK:STDOUT: .F = %F.decl
  69. // CHECK:STDOUT: .G = %G.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.import = import Core
  72. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  73. // CHECK:STDOUT: %N.patt: i32 = symbolic_binding_pattern N, 0
  74. // CHECK:STDOUT: %n.patt: @F.%.loc4_26 (%.2) = binding_pattern n
  75. // CHECK:STDOUT: } {
  76. // CHECK:STDOUT: %int.make_type_32.loc4_10: init type = call constants.%Int32() [template = i32]
  77. // CHECK:STDOUT: %.loc4_10.1: type = value_of_initializer %int.make_type_32.loc4_10 [template = i32]
  78. // CHECK:STDOUT: %.loc4_10.2: type = converted %int.make_type_32.loc4_10, %.loc4_10.1 [template = i32]
  79. // CHECK:STDOUT: %N.param: i32 = param N, runtime_param<invalid>
  80. // CHECK:STDOUT: %N.loc4_6.1: i32 = bind_symbolic_name N, 0, %N.param [symbolic = %N.loc4_6.2 (constants.%N)]
  81. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  82. // CHECK:STDOUT: %Int.ref: %Int.type = name_ref Int, imports.%import_ref.2 [template = constants.%Int]
  83. // CHECK:STDOUT: %N.ref.loc4: i32 = name_ref N, %N.loc4_6.1 [symbolic = %N.loc4_6.2 (constants.%N)]
  84. // CHECK:STDOUT: %int.make_type_signed: init type = call %Int.ref(%N.ref.loc4) [symbolic = %.loc4_26 (constants.%.2)]
  85. // CHECK:STDOUT: %.loc4_28.1: type = value_of_initializer %int.make_type_signed [symbolic = %.loc4_26 (constants.%.2)]
  86. // CHECK:STDOUT: %.loc4_28.2: type = converted %int.make_type_signed, %.loc4_28.1 [symbolic = %.loc4_26 (constants.%.2)]
  87. // CHECK:STDOUT: %n.param: @F.%.loc4_26 (%.2) = param n, runtime_param0
  88. // CHECK:STDOUT: %n: @F.%.loc4_26 (%.2) = bind_name n, %n.param
  89. // CHECK:STDOUT: %int.make_type_32.loc4_34: init type = call constants.%Int32() [template = i32]
  90. // CHECK:STDOUT: %.loc4_34.1: type = value_of_initializer %int.make_type_32.loc4_34 [template = i32]
  91. // CHECK:STDOUT: %.loc4_34.2: type = converted %int.make_type_32.loc4_34, %.loc4_34.1 [template = i32]
  92. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  95. // CHECK:STDOUT: %a.patt: %.4 = binding_pattern a
  96. // CHECK:STDOUT: } {
  97. // CHECK:STDOUT: %.loc8_9.1: i32 = int_literal 64 [template = constants.%.3]
  98. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc8_9.1) [template = constants.%.4]
  99. // CHECK:STDOUT: %.loc8_9.2: type = value_of_initializer %int.make_type_signed [template = constants.%.4]
  100. // CHECK:STDOUT: %.loc8_9.3: type = converted %int.make_type_signed, %.loc8_9.2 [template = constants.%.4]
  101. // CHECK:STDOUT: %a.param: %.4 = param a, runtime_param0
  102. // CHECK:STDOUT: %a: %.4 = bind_name a, %a.param
  103. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  104. // CHECK:STDOUT: %.loc8_17.1: type = value_of_initializer %int.make_type_32 [template = i32]
  105. // CHECK:STDOUT: %.loc8_17.2: type = converted %int.make_type_32, %.loc8_17.1 [template = i32]
  106. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: fn @Int(%size: i32) -> type = "int.make_type_signed";
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: generic fn @F(%N.loc4_6.1: i32) {
  115. // CHECK:STDOUT: %N.loc4_6.2: i32 = bind_symbolic_name N, 0 [symbolic = %N.loc4_6.2 (constants.%N)]
  116. // CHECK:STDOUT: %.loc4_26: type = int_type signed, %N.loc4_6.2 [symbolic = %.loc4_26 (constants.%.2)]
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: !definition:
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: fn[%N.loc4_6.1: i32](%n: @F.%.loc4_26 (%.2)) -> i32 {
  121. // CHECK:STDOUT: !entry:
  122. // CHECK:STDOUT: %N.ref.loc5: i32 = name_ref N, %N.loc4_6.1 [symbolic = %N.loc4_6.2 (constants.%N)]
  123. // CHECK:STDOUT: return %N.ref.loc5
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: fn @G(%a: %.4) -> i32 {
  128. // CHECK:STDOUT: !entry:
  129. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  130. // CHECK:STDOUT: %a.ref: %.4 = name_ref a, %a
  131. // CHECK:STDOUT: %F.call: init i32 = call %F.ref(%a.ref)
  132. // CHECK:STDOUT: %.loc9_14.1: i32 = value_of_initializer %F.call
  133. // CHECK:STDOUT: %.loc9_14.2: i32 = converted %F.call, %.loc9_14.1
  134. // CHECK:STDOUT: return %.loc9_14.2
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: specific @F(constants.%N) {
  138. // CHECK:STDOUT: %N.loc4_6.2 => constants.%N
  139. // CHECK:STDOUT: %.loc4_26 => constants.%.2
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: specific @F(constants.%.3) {
  143. // CHECK:STDOUT: %N.loc4_6.2 => constants.%.3
  144. // CHECK:STDOUT: %.loc4_26 => constants.%.4
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: --- fail_todo_float.carbon
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: constants {
  150. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  151. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  152. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  153. // CHECK:STDOUT: %N: i32 = bind_symbolic_name N, 0 [symbolic]
  154. // CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
  155. // CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
  156. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  157. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  158. // CHECK:STDOUT: %.2: i32 = int_literal 64 [template]
  159. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  160. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: imports {
  164. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  165. // CHECK:STDOUT: .Int32 = %import_ref.1
  166. // CHECK:STDOUT: .Float = %import_ref.2
  167. // CHECK:STDOUT: import Core//prelude
  168. // CHECK:STDOUT: import Core//prelude/operators
  169. // CHECK:STDOUT: import Core//prelude/types
  170. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  171. // CHECK:STDOUT: import Core//prelude/operators/as
  172. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  173. // CHECK:STDOUT: import Core//prelude/operators/comparison
  174. // CHECK:STDOUT: import Core//prelude/types/bool
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  177. // CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+35, loaded [template = constants.%Float]
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: file {
  181. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  182. // CHECK:STDOUT: .Core = imports.%Core
  183. // CHECK:STDOUT: .F = %F.decl
  184. // CHECK:STDOUT: .G = %G.decl
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT: %Core.import = import Core
  187. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  188. // CHECK:STDOUT: %N.patt: i32 = symbolic_binding_pattern N, 0
  189. // CHECK:STDOUT: %n.patt: <error> = binding_pattern n
  190. // CHECK:STDOUT: } {
  191. // CHECK:STDOUT: %int.make_type_32.loc8_10: init type = call constants.%Int32() [template = i32]
  192. // CHECK:STDOUT: %.loc8_10.1: type = value_of_initializer %int.make_type_32.loc8_10 [template = i32]
  193. // CHECK:STDOUT: %.loc8_10.2: type = converted %int.make_type_32.loc8_10, %.loc8_10.1 [template = i32]
  194. // CHECK:STDOUT: %N.param: i32 = param N, runtime_param<invalid>
  195. // CHECK:STDOUT: %N.loc8_6.1: i32 = bind_symbolic_name N, 0, %N.param [symbolic = %N.loc8_6.2 (constants.%N)]
  196. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  197. // CHECK:STDOUT: %Float.ref: %Float.type = name_ref Float, imports.%import_ref.2 [template = constants.%Float]
  198. // CHECK:STDOUT: %N.ref.loc8: i32 = name_ref N, %N.loc8_6.1 [symbolic = %N.loc8_6.2 (constants.%N)]
  199. // CHECK:STDOUT: %float.make_type: init type = call %Float.ref(%N.ref.loc8)
  200. // CHECK:STDOUT: %.loc8_30.1: type = value_of_initializer %float.make_type
  201. // CHECK:STDOUT: %.loc8_30.2: type = converted %float.make_type, %.loc8_30.1
  202. // CHECK:STDOUT: %n.param: <error> = param n, runtime_param0
  203. // CHECK:STDOUT: %n: <error> = bind_name n, %n.param
  204. // CHECK:STDOUT: %int.make_type_32.loc8_36: init type = call constants.%Int32() [template = i32]
  205. // CHECK:STDOUT: %.loc8_36.1: type = value_of_initializer %int.make_type_32.loc8_36 [template = i32]
  206. // CHECK:STDOUT: %.loc8_36.2: type = converted %int.make_type_32.loc8_36, %.loc8_36.1 [template = i32]
  207. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  210. // CHECK:STDOUT: %a.patt: f64 = binding_pattern a
  211. // CHECK:STDOUT: } {
  212. // CHECK:STDOUT: %.loc12_9.1: i32 = int_literal 64 [template = constants.%.2]
  213. // CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc12_9.1) [template = f64]
  214. // CHECK:STDOUT: %.loc12_9.2: type = value_of_initializer %float.make_type [template = f64]
  215. // CHECK:STDOUT: %.loc12_9.3: type = converted %float.make_type, %.loc12_9.2 [template = f64]
  216. // CHECK:STDOUT: %a.param: f64 = param a, runtime_param0
  217. // CHECK:STDOUT: %a: f64 = bind_name a, %a.param
  218. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  219. // CHECK:STDOUT: %.loc12_17.1: type = value_of_initializer %int.make_type_32 [template = i32]
  220. // CHECK:STDOUT: %.loc12_17.2: type = converted %int.make_type_32, %.loc12_17.1 [template = i32]
  221. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  226. // CHECK:STDOUT:
  227. // CHECK:STDOUT: fn @Float(%size: i32) -> type = "float.make_type";
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: generic fn @F(%N.loc8_6.1: i32) {
  230. // CHECK:STDOUT: %N.loc8_6.2: i32 = bind_symbolic_name N, 0 [symbolic = %N.loc8_6.2 (constants.%N)]
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: !definition:
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: fn[%N.loc8_6.1: i32](%n: <error>) -> i32 {
  235. // CHECK:STDOUT: !entry:
  236. // CHECK:STDOUT: %N.ref.loc9: i32 = name_ref N, %N.loc8_6.1 [symbolic = %N.loc8_6.2 (constants.%N)]
  237. // CHECK:STDOUT: return %N.ref.loc9
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT: }
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: fn @G(%a: f64) -> i32 {
  242. // CHECK:STDOUT: !entry:
  243. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  244. // CHECK:STDOUT: %a.ref: f64 = name_ref a, %a
  245. // CHECK:STDOUT: return <error>
  246. // CHECK:STDOUT: }
  247. // CHECK:STDOUT:
  248. // CHECK:STDOUT: specific @F(constants.%N) {
  249. // CHECK:STDOUT: %N.loc8_6.2 => constants.%N
  250. // CHECK:STDOUT: }
  251. // CHECK:STDOUT: