resolve_used.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/function/generic/resolve_used.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/generic/resolve_used.carbon
  10. // --- fail_todo_call_monomorphization_error.carbon
  11. library "[[@TEST_NAME]]";
  12. fn ErrorIfNIsZero(N:! Core.IntLiteral()) {
  13. // Check that we resolve the definition of a used specific function by
  14. // ensuring we produce an error when doing so. Notionally this error is
  15. // produced as a result of instantiating the `Core.Int` template, although
  16. // that's not how we currently model `Core.Int`.
  17. var v: Core.Int(N);
  18. }
  19. fn CallNegative() {
  20. // TODO: This should fail during monomorphization once integer literals have
  21. // the type `Core.IntLiteral`.
  22. // CHECK:STDERR: fail_todo_call_monomorphization_error.carbon:[[@LINE+9]]:3: error: cannot implicitly convert from `i32` to `Core.IntLiteral` [ImplicitAsConversionFailure]
  23. // CHECK:STDERR: ErrorIfNIsZero(0);
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  25. // CHECK:STDERR: fail_todo_call_monomorphization_error.carbon:[[@LINE+6]]:3: note: type `i32` does not implement interface `ImplicitAs` [MissingImplInMemberAccessNote]
  26. // CHECK:STDERR: ErrorIfNIsZero(0);
  27. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  28. // CHECK:STDERR: fail_todo_call_monomorphization_error.carbon:[[@LINE-17]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  29. // CHECK:STDERR: fn ErrorIfNIsZero(N:! Core.IntLiteral()) {
  30. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. ErrorIfNIsZero(0);
  32. }
  33. // CHECK:STDOUT: --- fail_todo_call_monomorphization_error.carbon
  34. // CHECK:STDOUT:
  35. // CHECK:STDOUT: constants {
  36. // CHECK:STDOUT: %IntLiteral.type: type = fn_type @IntLiteral [template]
  37. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  38. // CHECK:STDOUT: %IntLiteral: %IntLiteral.type = struct_value () [template]
  39. // CHECK:STDOUT: %N: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic]
  40. // CHECK:STDOUT: %N.patt: Core.IntLiteral = symbolic_binding_pattern N, 0 [symbolic]
  41. // CHECK:STDOUT: %ErrorIfNIsZero.type: type = fn_type @ErrorIfNIsZero [template]
  42. // CHECK:STDOUT: %ErrorIfNIsZero: %ErrorIfNIsZero.type = struct_value () [template]
  43. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  44. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  45. // CHECK:STDOUT: %.2: type = int_type signed, %N [symbolic]
  46. // CHECK:STDOUT: %CallNegative.type: type = fn_type @CallNegative [template]
  47. // CHECK:STDOUT: %CallNegative: %CallNegative.type = struct_value () [template]
  48. // CHECK:STDOUT: %.3: i32 = int_value 0 [template]
  49. // CHECK:STDOUT: %ImplicitAs.type.1: type = generic_interface_type @ImplicitAs [template]
  50. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type.1 = struct_value () [template]
  51. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic]
  52. // CHECK:STDOUT: %ImplicitAs.type.2: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  53. // CHECK:STDOUT: %Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2) = bind_symbolic_name Self, 1 [symbolic]
  54. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic]
  55. // CHECK:STDOUT: %Self.2: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic]
  56. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
  57. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  58. // CHECK:STDOUT: %.4: type = assoc_entity_type %ImplicitAs.type.2, %Convert.type.1 [symbolic]
  59. // CHECK:STDOUT: %.5: %.4 = assoc_entity element0, imports.%import_ref.7 [symbolic]
  60. // CHECK:STDOUT: %ImplicitAs.type.3: type = interface_type @ImplicitAs, @ImplicitAs(Core.IntLiteral) [template]
  61. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(Core.IntLiteral) [template]
  62. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  63. // CHECK:STDOUT: %.6: type = assoc_entity_type %ImplicitAs.type.3, %Convert.type.2 [template]
  64. // CHECK:STDOUT: %.7: %.6 = assoc_entity element0, imports.%import_ref.7 [template]
  65. // CHECK:STDOUT: %.8: %.4 = assoc_entity element0, imports.%import_ref.8 [symbolic]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: imports {
  69. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  70. // CHECK:STDOUT: .IntLiteral = %import_ref.1
  71. // CHECK:STDOUT: .Int = %import_ref.2
  72. // CHECK:STDOUT: .ImplicitAs = %import_ref.3
  73. // CHECK:STDOUT: import Core//prelude
  74. // CHECK:STDOUT: import Core//prelude/...
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %import_ref.1: %IntLiteral.type = import_ref Core//prelude/types, inst+7, loaded [template = constants.%IntLiteral]
  77. // CHECK:STDOUT: %import_ref.2: %Int.type = import_ref Core//prelude/types, inst+30, loaded [template = constants.%Int]
  78. // CHECK:STDOUT: %import_ref.3: %ImplicitAs.type.1 = import_ref Core//prelude/operators/as, inst+49, loaded [template = constants.%ImplicitAs]
  79. // CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/as, inst+55, unloaded
  80. // CHECK:STDOUT: %import_ref.5: @ImplicitAs.%.1 (%.4) = import_ref Core//prelude/operators/as, inst+77, loaded [symbolic = @ImplicitAs.%.2 (constants.%.8)]
  81. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+70, unloaded
  82. // CHECK:STDOUT: %import_ref.7 = import_ref Core//prelude/operators/as, inst+70, unloaded
  83. // CHECK:STDOUT: %import_ref.8 = import_ref Core//prelude/operators/as, inst+70, unloaded
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: file {
  87. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  88. // CHECK:STDOUT: .Core = imports.%Core
  89. // CHECK:STDOUT: .ErrorIfNIsZero = %ErrorIfNIsZero.decl
  90. // CHECK:STDOUT: .CallNegative = %CallNegative.decl
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %Core.import = import Core
  93. // CHECK:STDOUT: %ErrorIfNIsZero.decl: %ErrorIfNIsZero.type = fn_decl @ErrorIfNIsZero [template = constants.%ErrorIfNIsZero] {
  94. // CHECK:STDOUT: %N.patt.loc4_19.1: Core.IntLiteral = symbolic_binding_pattern N, 0 [symbolic = %N.patt.loc4_19.2 (constants.%N.patt)]
  95. // CHECK:STDOUT: %N.param_patt: Core.IntLiteral = value_param_pattern %N.patt.loc4_19.1, runtime_param<invalid> [symbolic = %N.patt.loc4_19.2 (constants.%N.patt)]
  96. // CHECK:STDOUT: } {
  97. // CHECK:STDOUT: %Core.ref.loc4: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  98. // CHECK:STDOUT: %IntLiteral.ref: %IntLiteral.type = name_ref IntLiteral, imports.%import_ref.1 [template = constants.%IntLiteral]
  99. // CHECK:STDOUT: %int_literal.make_type: init type = call %IntLiteral.ref() [template = Core.IntLiteral]
  100. // CHECK:STDOUT: %.loc4_39.1: type = value_of_initializer %int_literal.make_type [template = Core.IntLiteral]
  101. // CHECK:STDOUT: %.loc4_39.2: type = converted %int_literal.make_type, %.loc4_39.1 [template = Core.IntLiteral]
  102. // CHECK:STDOUT: %N.param: Core.IntLiteral = value_param runtime_param<invalid>
  103. // CHECK:STDOUT: %N.loc4_19.1: Core.IntLiteral = bind_symbolic_name N, 0, %N.param [symbolic = %N.loc4_19.2 (constants.%N)]
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %CallNegative.decl: %CallNegative.type = fn_decl @CallNegative [template = constants.%CallNegative] {} {}
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  109. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  110. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic = %Dest.patt (constants.%Dest.patt)]
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: !definition:
  113. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  114. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  115. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  116. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  117. // CHECK:STDOUT: %.1: type = assoc_entity_type @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.1 (constants.%.4)]
  118. // CHECK:STDOUT: %.2: @ImplicitAs.%.1 (%.4) = assoc_entity element0, imports.%import_ref.7 [symbolic = %.2 (constants.%.5)]
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: interface {
  121. // CHECK:STDOUT: !members:
  122. // CHECK:STDOUT: .Self = imports.%import_ref.4
  123. // CHECK:STDOUT: .Convert = imports.%import_ref.5
  124. // CHECK:STDOUT: witness = (imports.%import_ref.6)
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: fn @IntLiteral() -> type = "int_literal.make_type";
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: generic fn @ErrorIfNIsZero(%N.loc4_19.1: Core.IntLiteral) {
  131. // CHECK:STDOUT: %N.loc4_19.2: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic = %N.loc4_19.2 (constants.%N)]
  132. // CHECK:STDOUT: %N.patt.loc4_19.2: Core.IntLiteral = symbolic_binding_pattern N, 0 [symbolic = %N.patt.loc4_19.2 (constants.%N.patt)]
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: !definition:
  135. // CHECK:STDOUT: %.loc9_18: type = int_type signed, %N.loc4_19.2 [symbolic = %.loc9_18 (constants.%.2)]
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: fn(%N.param_patt: Core.IntLiteral) {
  138. // CHECK:STDOUT: !entry:
  139. // CHECK:STDOUT: %Core.ref.loc9: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  140. // CHECK:STDOUT: %Int.ref: %Int.type = name_ref Int, imports.%import_ref.2 [template = constants.%Int]
  141. // CHECK:STDOUT: %N.ref: Core.IntLiteral = name_ref N, %N.loc4_19.1 [symbolic = %N.loc4_19.2 (constants.%N)]
  142. // CHECK:STDOUT: %int.make_type_signed: init type = call %Int.ref(%N.ref) [symbolic = %.loc9_18 (constants.%.2)]
  143. // CHECK:STDOUT: %.loc9_20.1: type = value_of_initializer %int.make_type_signed [symbolic = %.loc9_18 (constants.%.2)]
  144. // CHECK:STDOUT: %.loc9_20.2: type = converted %int.make_type_signed, %.loc9_20.1 [symbolic = %.loc9_18 (constants.%.2)]
  145. // CHECK:STDOUT: %v.var: ref @ErrorIfNIsZero.%.loc9_18 (%.2) = var v
  146. // CHECK:STDOUT: %v: ref @ErrorIfNIsZero.%.loc9_18 (%.2) = bind_name v, %v.var
  147. // CHECK:STDOUT: return
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: fn @Int(%size.param_patt: Core.IntLiteral) -> type = "int.make_type_signed";
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn @CallNegative() {
  154. // CHECK:STDOUT: !entry:
  155. // CHECK:STDOUT: %ErrorIfNIsZero.ref: %ErrorIfNIsZero.type = name_ref ErrorIfNIsZero, file.%ErrorIfNIsZero.decl [template = constants.%ErrorIfNIsZero]
  156. // CHECK:STDOUT: %.loc24_18: i32 = int_value 0 [template = constants.%.3]
  157. // CHECK:STDOUT: %.loc24_17: Core.IntLiteral = converted %.loc24_18, <error> [template = <error>]
  158. // CHECK:STDOUT: return
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2)) {
  162. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  163. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  164. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn[%self.param_patt: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: specific @ErrorIfNIsZero(constants.%N) {
  170. // CHECK:STDOUT: %N.loc4_19.2 => constants.%N
  171. // CHECK:STDOUT: %N.patt.loc4_19.2 => constants.%N
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  175. // CHECK:STDOUT: %Dest => constants.%Dest
  176. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  180. // CHECK:STDOUT: %Dest => constants.%Dest
  181. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  185. // CHECK:STDOUT: %Dest => constants.%Dest
  186. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  190. // CHECK:STDOUT: %Dest => constants.%Dest
  191. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.2
  192. // CHECK:STDOUT: %Self => constants.%Self.1
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: specific @ImplicitAs(Core.IntLiteral) {
  196. // CHECK:STDOUT: %Dest => Core.IntLiteral
  197. // CHECK:STDOUT: %Dest.patt => Core.IntLiteral
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: !definition:
  200. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.3
  201. // CHECK:STDOUT: %Self => constants.%Self.2
  202. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  203. // CHECK:STDOUT: %Convert => constants.%Convert.2
  204. // CHECK:STDOUT: %.1 => constants.%.6
  205. // CHECK:STDOUT: %.2 => constants.%.7
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT: