resolve_used.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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:11: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]]:17: note: in `i0` used here [ResolvingSpecificHere]
  25. // CHECK:STDERR: var unused v: Core.Int(N);
  26. // CHECK:STDERR: ^~~~~~~~~~~
  27. var unused 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 = symbolic_binding .Self [symbolic_self]
  41. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  42. // CHECK:STDOUT: %IntLiteral.type: type = fn_type @IntLiteral [concrete]
  43. // CHECK:STDOUT: %IntLiteral: %IntLiteral.type = struct_value () [concrete]
  44. // CHECK:STDOUT: %N: Core.IntLiteral = symbolic_binding N, 0 [symbolic]
  45. // CHECK:STDOUT: %pattern_type.dc0: type = pattern_type Core.IntLiteral [concrete]
  46. // CHECK:STDOUT: %ErrorIfNIsZero.type: type = fn_type @ErrorIfNIsZero [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.901: <witness> = require_complete_type %Int [symbolic]
  52. // CHECK:STDOUT: %pattern_type.764: type = pattern_type %Int [symbolic]
  53. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  54. // CHECK:STDOUT: %Destroy.lookup_impl_witness: <witness> = lookup_impl_witness %Int, @Destroy [symbolic]
  55. // CHECK:STDOUT: %Destroy.facet.1c0: %Destroy.type = facet_value %Int, (%Destroy.lookup_impl_witness) [symbolic]
  56. // CHECK:STDOUT: %Destroy.WithSelf.Op.type.297: type = fn_type @Destroy.WithSelf.Op, @Destroy(%Destroy.facet.1c0) [symbolic]
  57. // CHECK:STDOUT: %.e63: type = fn_type_with_self_type %Destroy.WithSelf.Op.type.297, %Destroy.facet.1c0 [symbolic]
  58. // CHECK:STDOUT: %impl.elem0: %.e63 = impl_witness_access %Destroy.lookup_impl_witness, element0 [symbolic]
  59. // CHECK:STDOUT: %specific_impl_fn: <specific function> = specific_impl_function %impl.elem0, @Destroy.WithSelf.Op(%Destroy.facet.1c0) [symbolic]
  60. // CHECK:STDOUT: %CallNegative.type: type = fn_type @CallNegative [concrete]
  61. // CHECK:STDOUT: %CallNegative: %CallNegative.type = struct_value () [concrete]
  62. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  63. // CHECK:STDOUT: %ErrorIfNIsZero.specific_fn: <specific function> = specific_function %ErrorIfNIsZero, @ErrorIfNIsZero(%int_0) [concrete]
  64. // CHECK:STDOUT: %i0: type = class_type @Int, @Int(%int_0) [concrete]
  65. // CHECK:STDOUT: %complete_type.d94: <witness> = complete_type_witness <error> [concrete]
  66. // CHECK:STDOUT: %pattern_type.47b: type = pattern_type %i0 [concrete]
  67. // CHECK:STDOUT: %DestroyOp.type: type = fn_type @DestroyOp [concrete]
  68. // CHECK:STDOUT: %DestroyOp: %DestroyOp.type = struct_value () [concrete]
  69. // CHECK:STDOUT: %custom_witness.809: <witness> = custom_witness (%DestroyOp), @Destroy [concrete]
  70. // CHECK:STDOUT: %Destroy.facet.7e1: %Destroy.type = facet_value %i0, (%custom_witness.809) [concrete]
  71. // CHECK:STDOUT: %Destroy.WithSelf.Op.type.e5a: type = fn_type @Destroy.WithSelf.Op, @Destroy(%Destroy.facet.7e1) [concrete]
  72. // CHECK:STDOUT: %.011: type = fn_type_with_self_type %Destroy.WithSelf.Op.type.e5a, %Destroy.facet.7e1 [concrete]
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: imports {
  76. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  77. // CHECK:STDOUT: .IntLiteral = %Core.IntLiteral
  78. // CHECK:STDOUT: .Int = %Core.Int
  79. // CHECK:STDOUT: .Destroy = %Core.Destroy
  80. // CHECK:STDOUT: import Core//prelude
  81. // CHECK:STDOUT: import Core//prelude/...
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %Core.IntLiteral: %IntLiteral.type = import_ref Core//prelude/parts/int_literal, IntLiteral, loaded [concrete = constants.%IntLiteral]
  84. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  85. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: file {
  89. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  90. // CHECK:STDOUT: .Core = imports.%Core
  91. // CHECK:STDOUT: .ErrorIfNIsZero = %ErrorIfNIsZero.decl
  92. // CHECK:STDOUT: .CallNegative = %CallNegative.decl
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %Core.import = import Core
  95. // CHECK:STDOUT: %ErrorIfNIsZero.decl: %ErrorIfNIsZero.type = fn_decl @ErrorIfNIsZero [concrete = constants.%ErrorIfNIsZero] {
  96. // CHECK:STDOUT: %N.patt: %pattern_type.dc0 = symbolic_binding_pattern N, 0 [concrete]
  97. // CHECK:STDOUT: } {
  98. // CHECK:STDOUT: %.loc4_39.1: type = splice_block %.loc4_39.3 [concrete = Core.IntLiteral] {
  99. // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
  100. // CHECK:STDOUT: %Core.ref.loc4: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  101. // CHECK:STDOUT: %IntLiteral.ref: %IntLiteral.type = name_ref IntLiteral, imports.%Core.IntLiteral [concrete = constants.%IntLiteral]
  102. // CHECK:STDOUT: %IntLiteral.call: init type = call %IntLiteral.ref() [concrete = Core.IntLiteral]
  103. // CHECK:STDOUT: %.loc4_39.2: type = value_of_initializer %IntLiteral.call [concrete = Core.IntLiteral]
  104. // CHECK:STDOUT: %.loc4_39.3: type = converted %IntLiteral.call, %.loc4_39.2 [concrete = Core.IntLiteral]
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %N.loc4_19.2: Core.IntLiteral = symbolic_binding N, 0 [symbolic = %N.loc4_19.1 (constants.%N)]
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %CallNegative.decl: %CallNegative.type = fn_decl @CallNegative [concrete = constants.%CallNegative] {} {}
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: generic fn @ErrorIfNIsZero(%N.loc4_19.2: Core.IntLiteral) {
  112. // CHECK:STDOUT: %N.loc4_19.1: Core.IntLiteral = symbolic_binding N, 0 [symbolic = %N.loc4_19.1 (constants.%N)]
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: !definition:
  115. // CHECK:STDOUT: %Int.loc15_27.2: type = class_type @Int, @Int(%N.loc4_19.1) [symbolic = %Int.loc15_27.2 (constants.%Int)]
  116. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type %Int.loc15_27.2 [symbolic = %require_complete (constants.%require_complete.901)]
  117. // CHECK:STDOUT: %pattern_type: type = pattern_type %Int.loc15_27.2 [symbolic = %pattern_type (constants.%pattern_type.764)]
  118. // CHECK:STDOUT: %Destroy.lookup_impl_witness: <witness> = lookup_impl_witness %Int.loc15_27.2, @Destroy [symbolic = %Destroy.lookup_impl_witness (constants.%Destroy.lookup_impl_witness)]
  119. // CHECK:STDOUT: %Destroy.facet: %Destroy.type = facet_value %Int.loc15_27.2, (%Destroy.lookup_impl_witness) [symbolic = %Destroy.facet (constants.%Destroy.facet.1c0)]
  120. // CHECK:STDOUT: %Destroy.WithSelf.Op.type: type = fn_type @Destroy.WithSelf.Op, @Destroy(%Destroy.facet) [symbolic = %Destroy.WithSelf.Op.type (constants.%Destroy.WithSelf.Op.type.297)]
  121. // CHECK:STDOUT: %.loc15_3: type = fn_type_with_self_type %Destroy.WithSelf.Op.type, %Destroy.facet [symbolic = %.loc15_3 (constants.%.e63)]
  122. // CHECK:STDOUT: %impl.elem0.loc15_3.2: @ErrorIfNIsZero.%.loc15_3 (%.e63) = impl_witness_access %Destroy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc15_3.2 (constants.%impl.elem0)]
  123. // CHECK:STDOUT: %specific_impl_fn.loc15_3.2: <specific function> = specific_impl_function %impl.elem0.loc15_3.2, @Destroy.WithSelf.Op(%Destroy.facet) [symbolic = %specific_impl_fn.loc15_3.2 (constants.%specific_impl_fn)]
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: fn() {
  126. // CHECK:STDOUT: !entry:
  127. // CHECK:STDOUT: name_binding_decl {
  128. // CHECK:STDOUT: %v.patt: @ErrorIfNIsZero.%pattern_type (%pattern_type.764) = ref_binding_pattern v [concrete]
  129. // CHECK:STDOUT: %v.var_patt: @ErrorIfNIsZero.%pattern_type (%pattern_type.764) = var_pattern %v.patt [concrete]
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: %v.var: ref @ErrorIfNIsZero.%Int.loc15_27.2 (%Int) = var %v.var_patt
  132. // CHECK:STDOUT: %.loc15_27: type = splice_block %Int.loc15_27.1 [symbolic = %Int.loc15_27.2 (constants.%Int)] {
  133. // CHECK:STDOUT: %Core.ref.loc15: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  134. // CHECK:STDOUT: %Int.ref: %Int.type = name_ref Int, imports.%Core.Int [concrete = constants.%Int.generic]
  135. // CHECK:STDOUT: %N.ref: Core.IntLiteral = name_ref N, %N.loc4_19.2 [symbolic = %N.loc4_19.1 (constants.%N)]
  136. // CHECK:STDOUT: %Int.loc15_27.1: type = class_type @Int, @Int(constants.%N) [symbolic = %Int.loc15_27.2 (constants.%Int)]
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %v: ref @ErrorIfNIsZero.%Int.loc15_27.2 (%Int) = ref_binding v, %v.var
  139. // CHECK:STDOUT: %impl.elem0.loc15_3.1: @ErrorIfNIsZero.%.loc15_3 (%.e63) = impl_witness_access constants.%Destroy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc15_3.2 (constants.%impl.elem0)]
  140. // CHECK:STDOUT: %bound_method.loc15_3.1: <bound method> = bound_method %v.var, %impl.elem0.loc15_3.1
  141. // CHECK:STDOUT: %specific_impl_fn.loc15_3.1: <specific function> = specific_impl_function %impl.elem0.loc15_3.1, @Destroy.WithSelf.Op(constants.%Destroy.facet.1c0) [symbolic = %specific_impl_fn.loc15_3.2 (constants.%specific_impl_fn)]
  142. // CHECK:STDOUT: %bound_method.loc15_3.2: <bound method> = bound_method %v.var, %specific_impl_fn.loc15_3.1
  143. // CHECK:STDOUT: %Destroy.WithSelf.Op.call: init %empty_tuple.type = call %bound_method.loc15_3.2(%v.var)
  144. // CHECK:STDOUT: return
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: fn @CallNegative() {
  149. // CHECK:STDOUT: !entry:
  150. // CHECK:STDOUT: %ErrorIfNIsZero.ref: %ErrorIfNIsZero.type = name_ref ErrorIfNIsZero, file.%ErrorIfNIsZero.decl [concrete = constants.%ErrorIfNIsZero]
  151. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  152. // CHECK:STDOUT: %ErrorIfNIsZero.specific_fn: <specific function> = specific_function %ErrorIfNIsZero.ref, @ErrorIfNIsZero(constants.%int_0) [concrete = constants.%ErrorIfNIsZero.specific_fn]
  153. // CHECK:STDOUT: %ErrorIfNIsZero.call: init %empty_tuple.type = call %ErrorIfNIsZero.specific_fn()
  154. // CHECK:STDOUT: return
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: fn @DestroyOp(%self.param: ref %i0) = "no_op";
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: specific @ErrorIfNIsZero(constants.%N) {
  160. // CHECK:STDOUT: %N.loc4_19.1 => constants.%N
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: specific @ErrorIfNIsZero(constants.%int_0) {
  164. // CHECK:STDOUT: %N.loc4_19.1 => constants.%int_0
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: !definition:
  167. // CHECK:STDOUT: %Int.loc15_27.2 => constants.%i0
  168. // CHECK:STDOUT: %require_complete => constants.%complete_type.d94
  169. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.47b
  170. // CHECK:STDOUT: %Destroy.lookup_impl_witness => constants.%custom_witness.809
  171. // CHECK:STDOUT: %Destroy.facet => constants.%Destroy.facet.7e1
  172. // CHECK:STDOUT: %Destroy.WithSelf.Op.type => constants.%Destroy.WithSelf.Op.type.e5a
  173. // CHECK:STDOUT: %.loc15_3 => constants.%.011
  174. // CHECK:STDOUT: %impl.elem0.loc15_3.2 => constants.%DestroyOp
  175. // CHECK:STDOUT: %specific_impl_fn.loc15_3.2 => constants.%DestroyOp
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT: