resolve_used.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/function/generic/resolve_used.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/generic/resolve_used.carbon
  14. // --- fail_todo_call_monomorphization_error.carbon
  15. library "[[@TEST_NAME]]";
  16. fn ErrorIfNIsZero(N:! Core.IntLiteral()) {
  17. // Check that we resolve the definition of a used specific function by
  18. // ensuring we produce an error when doing so. Notionally this error is
  19. // produced as a result of instantiating the `Core.Int` template, although
  20. // that's not how we currently model `Core.Int`.
  21. // CHECK:STDERR: min_prelude/parts/int.carbon:10:9: error: integer type width of 0 is not positive [IntWidthNotPositive]
  22. // CHECK:STDERR: adapt MakeInt(N);
  23. // CHECK:STDERR: ^~~~~~~~~~
  24. // CHECK:STDERR: fail_todo_call_monomorphization_error.carbon:[[@LINE+3]]:10: note: in `i0` used here [ResolvingSpecificHere]
  25. // CHECK:STDERR: var v: Core.Int(N);
  26. // CHECK:STDERR: ^~~~~~~~~~~
  27. var v: Core.Int(N);
  28. }
  29. fn CallNegative() {
  30. // CHECK:STDERR: fail_todo_call_monomorphization_error.carbon:[[@LINE+4]]:3: note: in `ErrorIfNIsZero(0)` used here [ResolvingSpecificHere]
  31. // CHECK:STDERR: ErrorIfNIsZero(0);
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~
  33. // CHECK:STDERR:
  34. ErrorIfNIsZero(0);
  35. }
  36. // CHECK:STDOUT: --- fail_todo_call_monomorphization_error.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %type: type = facet_type <type> [concrete]
  40. // CHECK:STDOUT: %.Self: %type = bind_symbolic_name .Self [symbolic_self]
  41. // CHECK:STDOUT: %IntLiteral.type: type = fn_type @IntLiteral [concrete]
  42. // CHECK:STDOUT: %IntLiteral: %IntLiteral.type = struct_value () [concrete]
  43. // CHECK:STDOUT: %N: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic]
  44. // CHECK:STDOUT: %pattern_type.dc0: type = pattern_type Core.IntLiteral [concrete]
  45. // CHECK:STDOUT: %ErrorIfNIsZero.type: type = fn_type @ErrorIfNIsZero [concrete]
  46. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  47. // CHECK:STDOUT: %ErrorIfNIsZero: %ErrorIfNIsZero.type = struct_value () [concrete]
  48. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  49. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  50. // CHECK:STDOUT: %Int: type = class_type @Int, @Int(%N) [symbolic]
  51. // CHECK:STDOUT: %require_complete.b4f: <witness> = require_complete_type %Int [symbolic]
  52. // CHECK:STDOUT: %pattern_type.896: type = pattern_type %Int [symbolic]
  53. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  54. // CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete]
  55. // CHECK:STDOUT: %Destroy.impl_witness.b15: <witness> = impl_witness imports.%Destroy.impl_witness_table.fea, @Int.as.Destroy.impl(%N) [symbolic]
  56. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.type.a60: type = fn_type @Int.as.Destroy.impl.Op, @Int.as.Destroy.impl(%N) [symbolic]
  57. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.340: %Int.as.Destroy.impl.Op.type.a60 = struct_value () [symbolic]
  58. // CHECK:STDOUT: %ptr.784: type = ptr_type %Int [symbolic]
  59. // CHECK:STDOUT: %Destroy.facet.80c: %Destroy.type = facet_value %Int, (%Destroy.impl_witness.b15) [symbolic]
  60. // CHECK:STDOUT: %.11a: type = fn_type_with_self_type %Destroy.Op.type, %Destroy.facet.80c [symbolic]
  61. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.specific_fn.f79: <specific function> = specific_function %Int.as.Destroy.impl.Op.340, @Int.as.Destroy.impl.Op(%N) [symbolic]
  62. // CHECK:STDOUT: %require_complete.0f5: <witness> = require_complete_type %ptr.784 [symbolic]
  63. // CHECK:STDOUT: %CallNegative.type: type = fn_type @CallNegative [concrete]
  64. // CHECK:STDOUT: %CallNegative: %CallNegative.type = struct_value () [concrete]
  65. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  66. // CHECK:STDOUT: %ErrorIfNIsZero.specific_fn: <specific function> = specific_function %ErrorIfNIsZero, @ErrorIfNIsZero(%int_0) [concrete]
  67. // CHECK:STDOUT: %i0: type = class_type @Int, @Int(%int_0) [concrete]
  68. // CHECK:STDOUT: %complete_type.d94: <witness> = complete_type_witness <error> [concrete]
  69. // CHECK:STDOUT: %pattern_type.47b: type = pattern_type %i0 [concrete]
  70. // CHECK:STDOUT: %Destroy.impl_witness.e65: <witness> = impl_witness imports.%Destroy.impl_witness_table.fea, @Int.as.Destroy.impl(%int_0) [concrete]
  71. // CHECK:STDOUT: %Destroy.facet.0e5: %Destroy.type = facet_value %i0, (%Destroy.impl_witness.e65) [concrete]
  72. // CHECK:STDOUT: %.8fe: type = fn_type_with_self_type %Destroy.Op.type, %Destroy.facet.0e5 [concrete]
  73. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.type.742: type = fn_type @Int.as.Destroy.impl.Op, @Int.as.Destroy.impl(%int_0) [concrete]
  74. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.2da: %Int.as.Destroy.impl.Op.type.742 = struct_value () [concrete]
  75. // CHECK:STDOUT: %ptr.3f1: type = ptr_type %i0 [concrete]
  76. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.specific_fn.8a7: <specific function> = specific_function %Int.as.Destroy.impl.Op.2da, @Int.as.Destroy.impl.Op(%int_0) [concrete]
  77. // CHECK:STDOUT: %complete_type.588: <witness> = complete_type_witness %ptr.3f1 [concrete]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: imports {
  81. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  82. // CHECK:STDOUT: .IntLiteral = %Core.IntLiteral
  83. // CHECK:STDOUT: .Int = %Core.Int
  84. // CHECK:STDOUT: .Destroy = %Core.Destroy
  85. // CHECK:STDOUT: import Core//prelude
  86. // CHECK:STDOUT: import Core//prelude/...
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %Core.IntLiteral: %IntLiteral.type = import_ref Core//prelude/parts/int_literal, IntLiteral, loaded [concrete = constants.%IntLiteral]
  89. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  90. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  91. // CHECK:STDOUT: %Core.import_ref.16a: @Int.as.Destroy.impl.%Int.as.Destroy.impl.Op.type (%Int.as.Destroy.impl.Op.type.a60) = import_ref Core//prelude/parts/int, loc9_29, loaded [symbolic = @Int.as.Destroy.impl.%Int.as.Destroy.impl.Op (constants.%Int.as.Destroy.impl.Op.340)]
  92. // CHECK:STDOUT: %Destroy.impl_witness_table.fea = impl_witness_table (%Core.import_ref.16a), @Int.as.Destroy.impl [concrete]
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: file {
  96. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  97. // CHECK:STDOUT: .Core = imports.%Core
  98. // CHECK:STDOUT: .ErrorIfNIsZero = %ErrorIfNIsZero.decl
  99. // CHECK:STDOUT: .CallNegative = %CallNegative.decl
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %Core.import = import Core
  102. // CHECK:STDOUT: %ErrorIfNIsZero.decl: %ErrorIfNIsZero.type = fn_decl @ErrorIfNIsZero [concrete = constants.%ErrorIfNIsZero] {
  103. // CHECK:STDOUT: %N.patt: %pattern_type.dc0 = symbolic_binding_pattern N, 0 [concrete]
  104. // CHECK:STDOUT: } {
  105. // CHECK:STDOUT: %.loc4_39.1: type = splice_block %.loc4_39.3 [concrete = Core.IntLiteral] {
  106. // CHECK:STDOUT: %.Self: %type = bind_symbolic_name .Self [symbolic_self = constants.%.Self]
  107. // CHECK:STDOUT: %Core.ref.loc4: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  108. // CHECK:STDOUT: %IntLiteral.ref: %IntLiteral.type = name_ref IntLiteral, imports.%Core.IntLiteral [concrete = constants.%IntLiteral]
  109. // CHECK:STDOUT: %IntLiteral.call: init type = call %IntLiteral.ref() [concrete = Core.IntLiteral]
  110. // CHECK:STDOUT: %.loc4_39.2: type = value_of_initializer %IntLiteral.call [concrete = Core.IntLiteral]
  111. // CHECK:STDOUT: %.loc4_39.3: type = converted %IntLiteral.call, %.loc4_39.2 [concrete = Core.IntLiteral]
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: %N.loc4_19.2: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic = %N.loc4_19.1 (constants.%N)]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %CallNegative.decl: %CallNegative.type = fn_decl @CallNegative [concrete = constants.%CallNegative] {} {}
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: generic fn @ErrorIfNIsZero(%N.loc4_19.2: Core.IntLiteral) {
  119. // CHECK:STDOUT: %N.loc4_19.1: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic = %N.loc4_19.1 (constants.%N)]
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: !definition:
  122. // CHECK:STDOUT: %Int.loc15_20.2: type = class_type @Int, @Int(%N.loc4_19.1) [symbolic = %Int.loc15_20.2 (constants.%Int)]
  123. // CHECK:STDOUT: %require_complete.loc15_20: <witness> = require_complete_type %Int.loc15_20.2 [symbolic = %require_complete.loc15_20 (constants.%require_complete.b4f)]
  124. // CHECK:STDOUT: %pattern_type: type = pattern_type %Int.loc15_20.2 [symbolic = %pattern_type (constants.%pattern_type.896)]
  125. // CHECK:STDOUT: %Destroy.impl_witness: <witness> = impl_witness imports.%Destroy.impl_witness_table.fea, @Int.as.Destroy.impl(%N.loc4_19.1) [symbolic = %Destroy.impl_witness (constants.%Destroy.impl_witness.b15)]
  126. // CHECK:STDOUT: %Destroy.facet: %Destroy.type = facet_value %Int.loc15_20.2, (%Destroy.impl_witness) [symbolic = %Destroy.facet (constants.%Destroy.facet.80c)]
  127. // CHECK:STDOUT: %.loc15_3: type = fn_type_with_self_type constants.%Destroy.Op.type, %Destroy.facet [symbolic = %.loc15_3 (constants.%.11a)]
  128. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.type: type = fn_type @Int.as.Destroy.impl.Op, @Int.as.Destroy.impl(%N.loc4_19.1) [symbolic = %Int.as.Destroy.impl.Op.type (constants.%Int.as.Destroy.impl.Op.type.a60)]
  129. // CHECK:STDOUT: %Int.as.Destroy.impl.Op: @ErrorIfNIsZero.%Int.as.Destroy.impl.Op.type (%Int.as.Destroy.impl.Op.type.a60) = struct_value () [symbolic = %Int.as.Destroy.impl.Op (constants.%Int.as.Destroy.impl.Op.340)]
  130. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.specific_fn: <specific function> = specific_function %Int.as.Destroy.impl.Op, @Int.as.Destroy.impl.Op(%N.loc4_19.1) [symbolic = %Int.as.Destroy.impl.Op.specific_fn (constants.%Int.as.Destroy.impl.Op.specific_fn.f79)]
  131. // CHECK:STDOUT: %ptr: type = ptr_type %Int.loc15_20.2 [symbolic = %ptr (constants.%ptr.784)]
  132. // CHECK:STDOUT: %require_complete.loc15_3: <witness> = require_complete_type %ptr [symbolic = %require_complete.loc15_3 (constants.%require_complete.0f5)]
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: fn() {
  135. // CHECK:STDOUT: !entry:
  136. // CHECK:STDOUT: name_binding_decl {
  137. // CHECK:STDOUT: %v.patt: @ErrorIfNIsZero.%pattern_type (%pattern_type.896) = binding_pattern v [concrete]
  138. // CHECK:STDOUT: %v.var_patt: @ErrorIfNIsZero.%pattern_type (%pattern_type.896) = var_pattern %v.patt [concrete]
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: %v.var: ref @ErrorIfNIsZero.%Int.loc15_20.2 (%Int) = var %v.var_patt
  141. // CHECK:STDOUT: %.loc15_20: type = splice_block %Int.loc15_20.1 [symbolic = %Int.loc15_20.2 (constants.%Int)] {
  142. // CHECK:STDOUT: %Core.ref.loc15: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  143. // CHECK:STDOUT: %Int.ref: %Int.type = name_ref Int, imports.%Core.Int [concrete = constants.%Int.generic]
  144. // CHECK:STDOUT: %N.ref: Core.IntLiteral = name_ref N, %N.loc4_19.2 [symbolic = %N.loc4_19.1 (constants.%N)]
  145. // CHECK:STDOUT: %Int.loc15_20.1: type = class_type @Int, @Int(constants.%N) [symbolic = %Int.loc15_20.2 (constants.%Int)]
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: %v: ref @ErrorIfNIsZero.%Int.loc15_20.2 (%Int) = bind_name v, %v.var
  148. // CHECK:STDOUT: %impl.elem0: @ErrorIfNIsZero.%.loc15_3 (%.11a) = impl_witness_access constants.%Destroy.impl_witness.b15, element0 [symbolic = %Int.as.Destroy.impl.Op (constants.%Int.as.Destroy.impl.Op.340)]
  149. // CHECK:STDOUT: %bound_method.loc15_3.1: <bound method> = bound_method %v.var, %impl.elem0
  150. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Int.as.Destroy.impl.Op(constants.%N) [symbolic = %Int.as.Destroy.impl.Op.specific_fn (constants.%Int.as.Destroy.impl.Op.specific_fn.f79)]
  151. // CHECK:STDOUT: %bound_method.loc15_3.2: <bound method> = bound_method %v.var, %specific_fn
  152. // CHECK:STDOUT: %addr: @ErrorIfNIsZero.%ptr (%ptr.784) = addr_of %v.var
  153. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc15_3.2(%addr)
  154. // CHECK:STDOUT: return
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: fn @CallNegative() {
  159. // CHECK:STDOUT: !entry:
  160. // CHECK:STDOUT: %ErrorIfNIsZero.ref: %ErrorIfNIsZero.type = name_ref ErrorIfNIsZero, file.%ErrorIfNIsZero.decl [concrete = constants.%ErrorIfNIsZero]
  161. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  162. // CHECK:STDOUT: %ErrorIfNIsZero.specific_fn: <specific function> = specific_function %ErrorIfNIsZero.ref, @ErrorIfNIsZero(constants.%int_0) [concrete = constants.%ErrorIfNIsZero.specific_fn]
  163. // CHECK:STDOUT: %ErrorIfNIsZero.call: init %empty_tuple.type = call %ErrorIfNIsZero.specific_fn()
  164. // CHECK:STDOUT: return
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: specific @ErrorIfNIsZero(constants.%N) {
  168. // CHECK:STDOUT: %N.loc4_19.1 => constants.%N
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: specific @ErrorIfNIsZero(constants.%int_0) {
  172. // CHECK:STDOUT: %N.loc4_19.1 => constants.%int_0
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: !definition:
  175. // CHECK:STDOUT: %Int.loc15_20.2 => constants.%i0
  176. // CHECK:STDOUT: %require_complete.loc15_20 => constants.%complete_type.d94
  177. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.47b
  178. // CHECK:STDOUT: %Destroy.impl_witness => constants.%Destroy.impl_witness.e65
  179. // CHECK:STDOUT: %Destroy.facet => constants.%Destroy.facet.0e5
  180. // CHECK:STDOUT: %.loc15_3 => constants.%.8fe
  181. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.type => constants.%Int.as.Destroy.impl.Op.type.742
  182. // CHECK:STDOUT: %Int.as.Destroy.impl.Op => constants.%Int.as.Destroy.impl.Op.2da
  183. // CHECK:STDOUT: %Int.as.Destroy.impl.Op.specific_fn => constants.%Int.as.Destroy.impl.Op.specific_fn.8a7
  184. // CHECK:STDOUT: %ptr => constants.%ptr.3f1
  185. // CHECK:STDOUT: %require_complete.loc15_3 => constants.%complete_type.588
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT: