prefer_unqualified_lookup.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/call/prefer_unqualified_lookup.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/call/prefer_unqualified_lookup.carbon
  14. // --- prefer_unqualified_lookup.carbon
  15. library "[[@TEST_NAME]]";
  16. // F in the lexical scope.
  17. class Class(F:! type) {
  18. class Inner {
  19. // F in a non-lexical scope.
  20. fn F() -> i32 { return 0; }
  21. fn G() -> i32;
  22. }
  23. }
  24. fn Class(F:! type).Inner.G() -> i32 { return F(); }
  25. // CHECK:STDOUT: --- prefer_unqualified_lookup.carbon
  26. // CHECK:STDOUT:
  27. // CHECK:STDOUT: constants {
  28. // CHECK:STDOUT: %type: type = facet_type <type> [concrete]
  29. // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self]
  30. // CHECK:STDOUT: %F: type = symbolic_binding F, 0 [symbolic]
  31. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  32. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [concrete]
  33. // CHECK:STDOUT: %Class.generic: %Class.type = struct_value () [concrete]
  34. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%F) [symbolic]
  35. // CHECK:STDOUT: %Inner: type = class_type @Inner, @Inner(%F) [symbolic]
  36. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  37. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  38. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  39. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  40. // CHECK:STDOUT: %.941: form = init_form %i32, call_param0 [concrete]
  41. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  42. // CHECK:STDOUT: %Inner.F.type: type = fn_type @Inner.F, @Inner(%F) [symbolic]
  43. // CHECK:STDOUT: %Inner.F: %Inner.F.type = struct_value () [symbolic]
  44. // CHECK:STDOUT: %Inner.G.type: type = fn_type @Inner.G, @Inner(%F) [symbolic]
  45. // CHECK:STDOUT: %Inner.G: %Inner.G.type = struct_value () [symbolic]
  46. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  47. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [concrete]
  48. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  49. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  50. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  51. // CHECK:STDOUT: %ImplicitAs.type.e8c: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  52. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  53. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  54. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  55. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6 = struct_value () [symbolic]
  56. // CHECK:STDOUT: %ImplicitAs.impl_witness.6bc: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.74f, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  57. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  58. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d = struct_value () [concrete]
  59. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.e8c = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.6bc) [concrete]
  60. // CHECK:STDOUT: %.863: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  61. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5 [concrete]
  62. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  63. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  64. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  65. // CHECK:STDOUT: %Inner.F.specific_fn: <specific function> = specific_function %Inner.F, @Inner.F(%F) [symbolic]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: imports {
  69. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  70. // CHECK:STDOUT: .Int = %Core.Int
  71. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  72. // CHECK:STDOUT: import Core//prelude
  73. // CHECK:STDOUT: import Core//prelude/...
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  76. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  77. // CHECK:STDOUT: %Core.import_ref.42d: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2)]
  78. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.74f = impl_witness_table (%Core.import_ref.42d), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: file {
  82. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  83. // CHECK:STDOUT: .Core = imports.%Core
  84. // CHECK:STDOUT: .Class = %Class.decl
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Core.import = import Core
  87. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [concrete = constants.%Class.generic] {
  88. // CHECK:STDOUT: %F.patt: %pattern_type.98f = symbolic_binding_pattern F, 0 [concrete]
  89. // CHECK:STDOUT: } {
  90. // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
  91. // CHECK:STDOUT: %F.loc5_13.2: type = symbolic_binding F, 0 [symbolic = %F.loc5_13.1 (constants.%F)]
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %Inner.G.decl: %Inner.G.type = fn_decl @Inner.G [symbolic = constants.%Inner.G] {
  94. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  95. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  96. // CHECK:STDOUT: } {
  97. // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
  98. // CHECK:STDOUT: %F.loc13_10: type = symbolic_binding F, 0 [symbolic = @Class.%F.loc5_13.1 (constants.%F)]
  99. // CHECK:STDOUT: %int_32.loc13: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  100. // CHECK:STDOUT: %i32.loc13: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  101. // CHECK:STDOUT: %.loc13_33: form = init_form %i32.loc13, call_param0 [concrete = constants.%.941]
  102. // CHECK:STDOUT: %return.param.loc13: ref %i32 = out_param call_param0
  103. // CHECK:STDOUT: %return.loc13: ref %i32 = return_slot %return.param.loc13
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: generic class @Class(%F.loc5_13.2: type) {
  108. // CHECK:STDOUT: %F.loc5_13.1: type = symbolic_binding F, 0 [symbolic = %F.loc5_13.1 (constants.%F)]
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: !definition:
  111. // CHECK:STDOUT: %Inner: type = class_type @Inner, @Inner(%F.loc5_13.1) [symbolic = %Inner (constants.%Inner)]
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: class {
  114. // CHECK:STDOUT: %Inner.decl: type = class_decl @Inner [symbolic = @Class.%Inner (constants.%Inner)] {} {}
  115. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type.357]
  116. // CHECK:STDOUT: complete_type_witness = %complete_type
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: !members:
  119. // CHECK:STDOUT: .Self = constants.%Class
  120. // CHECK:STDOUT: .Inner = %Inner.decl
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: generic class @Inner(@Class.%F.loc5_13.2: type) {
  125. // CHECK:STDOUT: !definition:
  126. // CHECK:STDOUT: %F: type = symbolic_binding F, 0 [symbolic = %F (constants.%F)]
  127. // CHECK:STDOUT: %Inner.F.type: type = fn_type @Inner.F, @Inner(%F) [symbolic = %Inner.F.type (constants.%Inner.F.type)]
  128. // CHECK:STDOUT: %Inner.F: @Inner.%Inner.F.type (%Inner.F.type) = struct_value () [symbolic = %Inner.F (constants.%Inner.F)]
  129. // CHECK:STDOUT: %Inner.G.type: type = fn_type @Inner.G, @Inner(%F) [symbolic = %Inner.G.type (constants.%Inner.G.type)]
  130. // CHECK:STDOUT: %Inner.G: @Inner.%Inner.G.type (%Inner.G.type) = struct_value () [symbolic = %Inner.G (constants.%Inner.G)]
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: class {
  133. // CHECK:STDOUT: %Inner.F.decl: @Inner.%Inner.F.type (%Inner.F.type) = fn_decl @Inner.F [symbolic = @Inner.%Inner.F (constants.%Inner.F)] {
  134. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  135. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  136. // CHECK:STDOUT: } {
  137. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  138. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  139. // CHECK:STDOUT: %.loc8_15: form = init_form %i32, call_param0 [concrete = constants.%.941]
  140. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  141. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: %Inner.G.decl: @Inner.%Inner.G.type (%Inner.G.type) = fn_decl @Inner.G [symbolic = @Inner.%Inner.G (constants.%Inner.G)] {
  144. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  145. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  146. // CHECK:STDOUT: } {
  147. // CHECK:STDOUT: %int_32.loc9: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  148. // CHECK:STDOUT: %i32.loc9: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  149. // CHECK:STDOUT: %.loc9: form = init_form %i32.loc9, call_param0 [concrete = constants.%.941]
  150. // CHECK:STDOUT: %return.param.loc9: ref %i32 = out_param call_param0
  151. // CHECK:STDOUT: %return.loc9: ref %i32 = return_slot %return.param.loc9
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type.357]
  154. // CHECK:STDOUT: complete_type_witness = %complete_type
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: !members:
  157. // CHECK:STDOUT: .Self = constants.%Inner
  158. // CHECK:STDOUT: .F = %Inner.F.decl
  159. // CHECK:STDOUT: .G = %Inner.G.decl
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: generic fn @Inner.F(@Class.%F.loc5_13.2: type) {
  164. // CHECK:STDOUT: !definition:
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn() -> %i32 {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  169. // CHECK:STDOUT: %impl.elem0: %.863 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5]
  170. // CHECK:STDOUT: %bound_method.loc8_29.1: <bound method> = bound_method %int_0, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  171. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  172. // CHECK:STDOUT: %bound_method.loc8_29.2: <bound method> = bound_method %int_0, %specific_fn [concrete = constants.%bound_method]
  173. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call: init %i32 = call %bound_method.loc8_29.2(%int_0) [concrete = constants.%int_0.6a9]
  174. // CHECK:STDOUT: %.loc8_29: init %i32 = converted %int_0, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call [concrete = constants.%int_0.6a9]
  175. // CHECK:STDOUT: return %.loc8_29 to %return.param
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: generic fn @Inner.G(@Class.%F.loc5_13.2: type) {
  180. // CHECK:STDOUT: !definition:
  181. // CHECK:STDOUT: %F.loc13_46: type = symbolic_binding F, 0 [symbolic = %F.loc13_46 (constants.%F)]
  182. // CHECK:STDOUT: %Inner.F.type: type = fn_type @Inner.F, @Inner(%F.loc13_46) [symbolic = %Inner.F.type (constants.%Inner.F.type)]
  183. // CHECK:STDOUT: %Inner.F: @Inner.G.%Inner.F.type (%Inner.F.type) = struct_value () [symbolic = %Inner.F (constants.%Inner.F)]
  184. // CHECK:STDOUT: %Inner.F.specific_fn.loc13_46.2: <specific function> = specific_function %Inner.F, @Inner.F(%F.loc13_46) [symbolic = %Inner.F.specific_fn.loc13_46.2 (constants.%Inner.F.specific_fn)]
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: fn() -> %i32 {
  187. // CHECK:STDOUT: !entry:
  188. // CHECK:STDOUT: %.loc13_46: @Inner.G.%Inner.F.type (%Inner.F.type) = specific_constant @Inner.%Inner.F.decl, @Inner(constants.%F) [symbolic = %Inner.F (constants.%Inner.F)]
  189. // CHECK:STDOUT: %F.ref: @Inner.G.%Inner.F.type (%Inner.F.type) = name_ref F, %.loc13_46 [symbolic = %Inner.F (constants.%Inner.F)]
  190. // CHECK:STDOUT: %Inner.F.specific_fn.loc13_46.1: <specific function> = specific_function %F.ref, @Inner.F(constants.%F) [symbolic = %Inner.F.specific_fn.loc13_46.2 (constants.%Inner.F.specific_fn)]
  191. // CHECK:STDOUT: %Inner.F.call: init %i32 = call %Inner.F.specific_fn.loc13_46.1()
  192. // CHECK:STDOUT: return %Inner.F.call to %return.param.loc13
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: specific @Class(constants.%F) {
  197. // CHECK:STDOUT: %F.loc5_13.1 => constants.%F
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: !definition:
  200. // CHECK:STDOUT: %Inner => constants.%Inner
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: specific @Inner(constants.%F) {
  204. // CHECK:STDOUT: !definition:
  205. // CHECK:STDOUT: %F => constants.%F
  206. // CHECK:STDOUT: %Inner.F.type => constants.%Inner.F.type
  207. // CHECK:STDOUT: %Inner.F => constants.%Inner.F
  208. // CHECK:STDOUT: %Inner.G.type => constants.%Inner.G.type
  209. // CHECK:STDOUT: %Inner.G => constants.%Inner.G
  210. // CHECK:STDOUT: }
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: specific @Inner.F(constants.%F) {
  213. // CHECK:STDOUT: !definition:
  214. // CHECK:STDOUT: }
  215. // CHECK:STDOUT:
  216. // CHECK:STDOUT: specific @Inner.G(constants.%F) {}
  217. // CHECK:STDOUT: