prefer_unqualified_lookup.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/call/prefer_unqualified_lookup.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/call/prefer_unqualified_lookup.carbon
  10. // --- prefer_unqualified_lookup.carbon
  11. library "[[@TEST_NAME]]";
  12. // F in the lexical scope.
  13. class Class(F:! type) {
  14. class Inner {
  15. // F in a non-lexical scope.
  16. fn F() -> i32 { return 0; }
  17. fn G() -> i32;
  18. }
  19. }
  20. fn Class(F:! type).Inner.G() -> i32 { return F(); }
  21. // CHECK:STDOUT: --- prefer_unqualified_lookup.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %F.2ba: type = bind_symbolic_name F, 0 [symbolic]
  25. // CHECK:STDOUT: %F.patt: type = symbolic_binding_pattern F, 0 [symbolic]
  26. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  27. // CHECK:STDOUT: %Class.generic: %Class.type = struct_value () [template]
  28. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%F.2ba) [symbolic]
  29. // CHECK:STDOUT: %Inner.ed2: type = class_type @Inner [template]
  30. // CHECK:STDOUT: %Inner.2fe: type = class_type @Inner, @Inner(%F.2ba) [symbolic]
  31. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  32. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  33. // CHECK:STDOUT: %F.type: type = fn_type @F, @Inner(%F.2ba) [symbolic]
  34. // CHECK:STDOUT: %F.ee3: %F.type = struct_value () [symbolic]
  35. // CHECK:STDOUT: %G.type: type = fn_type @G, @Inner(%F.2ba) [symbolic]
  36. // CHECK:STDOUT: %G: %G.type = struct_value () [symbolic]
  37. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  38. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [template]
  39. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [template]
  40. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  41. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.1(%int_32) [template]
  42. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  43. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  44. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0.5c6, %Convert.925 [template]
  45. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  46. // CHECK:STDOUT: %int_0.f61: %i32 = int_value 0 [template]
  47. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F.ee3, @F(%F.2ba) [symbolic]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: .Int = %import_ref.187
  53. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  54. // CHECK:STDOUT: import Core//prelude
  55. // CHECK:STDOUT: import Core//prelude/...
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  61. // CHECK:STDOUT: .Core = imports.%Core
  62. // CHECK:STDOUT: .Class = %Class.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.import = import Core
  65. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.generic] {
  66. // CHECK:STDOUT: %F.patt.loc5_13.1: type = symbolic_binding_pattern F, 0 [symbolic = %F.patt.loc5_13.2 (constants.%F.patt)]
  67. // CHECK:STDOUT: %F.param_patt: type = value_param_pattern %F.patt.loc5_13.1, runtime_param<invalid> [symbolic = %F.patt.loc5_13.2 (constants.%F.patt)]
  68. // CHECK:STDOUT: } {
  69. // CHECK:STDOUT: %F.param: type = value_param runtime_param<invalid>
  70. // CHECK:STDOUT: %F.loc5_13.1: type = bind_symbolic_name F, 0, %F.param [symbolic = %F.loc5_13.2 (constants.%F.2ba)]
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [symbolic = constants.%G] {
  73. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  74. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  75. // CHECK:STDOUT: } {
  76. // CHECK:STDOUT: %F.param: type = value_param runtime_param<invalid>
  77. // CHECK:STDOUT: %F.loc13_10: type = bind_symbolic_name F, 0, %F.param [symbolic = constants.%F.2ba]
  78. // CHECK:STDOUT: %int_32.loc13: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  79. // CHECK:STDOUT: %i32.loc13: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  80. // CHECK:STDOUT: %return.param.loc13: ref %i32 = out_param runtime_param0
  81. // CHECK:STDOUT: %return.loc13: ref %i32 = return_slot %return.param.loc13
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: generic class @Class(%F.loc5_13.1: type) {
  86. // CHECK:STDOUT: %F.loc5_13.2: type = bind_symbolic_name F, 0 [symbolic = %F.loc5_13.2 (constants.%F.2ba)]
  87. // CHECK:STDOUT: %F.patt.loc5_13.2: type = symbolic_binding_pattern F, 0 [symbolic = %F.patt.loc5_13.2 (constants.%F.patt)]
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: !definition:
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: class {
  92. // CHECK:STDOUT: %Inner.decl: type = class_decl @Inner [template = constants.%Inner.ed2] {} {}
  93. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type.357]
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: !members:
  96. // CHECK:STDOUT: .Self = constants.%Class
  97. // CHECK:STDOUT: .Inner = %Inner.decl
  98. // CHECK:STDOUT: complete_type_witness = %complete_type
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: generic class @Inner(@Class.%F.loc5_13.1: type) {
  103. // CHECK:STDOUT: !definition:
  104. // CHECK:STDOUT: %F.loc8_19.1: type = bind_symbolic_name F, 0 [symbolic = %F.loc8_19.1 (constants.%F.2ba)]
  105. // CHECK:STDOUT: %F.type: type = fn_type @F, @Inner(%F.loc8_19.1) [symbolic = %F.type (constants.%F.type)]
  106. // CHECK:STDOUT: %F.loc8_19.2: @Inner.%F.type (%F.type) = struct_value () [symbolic = %F.loc8_19.2 (constants.%F.ee3)]
  107. // CHECK:STDOUT: %G.type: type = fn_type @G, @Inner(%F.loc8_19.1) [symbolic = %G.type (constants.%G.type)]
  108. // CHECK:STDOUT: %G: @Inner.%G.type (%G.type) = struct_value () [symbolic = %G (constants.%G)]
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: class {
  111. // CHECK:STDOUT: %F.decl: @Inner.%F.type (%F.type) = fn_decl @F [symbolic = @Inner.%F.loc8_19.2 (constants.%F.ee3)] {
  112. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  113. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  114. // CHECK:STDOUT: } {
  115. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  116. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  117. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  118. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: %G.decl: @Inner.%G.type (%G.type) = fn_decl @G [symbolic = @Inner.%G (constants.%G)] {
  121. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  122. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  123. // CHECK:STDOUT: } {
  124. // CHECK:STDOUT: %int_32.loc9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  125. // CHECK:STDOUT: %i32.loc9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  126. // CHECK:STDOUT: %return.param.loc9: ref %i32 = out_param runtime_param0
  127. // CHECK:STDOUT: %return.loc9: ref %i32 = return_slot %return.param.loc9
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type.357]
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: !members:
  132. // CHECK:STDOUT: .Self = constants.%Inner.2fe
  133. // CHECK:STDOUT: .F = %F.decl
  134. // CHECK:STDOUT: .G = %G.decl
  135. // CHECK:STDOUT: complete_type_witness = %complete_type
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: generic fn @F(@Class.%F.loc5_13.1: type) {
  140. // CHECK:STDOUT: !definition:
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: fn() -> %i32 {
  143. // CHECK:STDOUT: !entry:
  144. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  145. // CHECK:STDOUT: %impl.elem0: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  146. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0, %impl.elem0 [template = constants.%Convert.bound]
  147. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  148. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_0) [template = constants.%int_0.f61]
  149. // CHECK:STDOUT: %.loc8_29.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_0.f61]
  150. // CHECK:STDOUT: %.loc8_29.2: %i32 = converted %int_0, %.loc8_29.1 [template = constants.%int_0.f61]
  151. // CHECK:STDOUT: return %.loc8_29.2
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: generic fn @G(@Class.%F.loc5_13.1: type) {
  156. // CHECK:STDOUT: !definition:
  157. // CHECK:STDOUT: %F.loc13_46.1: type = bind_symbolic_name F, 0 [symbolic = %F.loc13_46.1 (constants.%F.2ba)]
  158. // CHECK:STDOUT: %F.type: type = fn_type @F, @Inner(%F.loc13_46.1) [symbolic = %F.type (constants.%F.type)]
  159. // CHECK:STDOUT: %F.loc13_46.2: @G.%F.type (%F.type) = struct_value () [symbolic = %F.loc13_46.2 (constants.%F.ee3)]
  160. // CHECK:STDOUT: %F.specific_fn.loc13_46.2: <specific function> = specific_function %F.loc13_46.2, @F(%F.loc13_46.1) [symbolic = %F.specific_fn.loc13_46.2 (constants.%F.specific_fn)]
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: fn() -> %i32 {
  163. // CHECK:STDOUT: !entry:
  164. // CHECK:STDOUT: %.loc13_46: @G.%F.type (%F.type) = specific_constant @Inner.%F.decl, @Inner(constants.%F.2ba) [symbolic = %F.loc13_46.2 (constants.%F.ee3)]
  165. // CHECK:STDOUT: %F.ref: @G.%F.type (%F.type) = name_ref F, %.loc13_46 [symbolic = %F.loc13_46.2 (constants.%F.ee3)]
  166. // CHECK:STDOUT: %F.specific_fn.loc13_46.1: <specific function> = specific_function %F.ref, @F(constants.%F.2ba) [symbolic = %F.specific_fn.loc13_46.2 (constants.%F.specific_fn)]
  167. // CHECK:STDOUT: %F.call: init %i32 = call %F.specific_fn.loc13_46.1()
  168. // CHECK:STDOUT: %.loc13_49.1: %i32 = value_of_initializer %F.call
  169. // CHECK:STDOUT: %.loc13_49.2: %i32 = converted %F.call, %.loc13_49.1
  170. // CHECK:STDOUT: return %.loc13_49.2
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: specific @Class(constants.%F.2ba) {
  175. // CHECK:STDOUT: %F.loc5_13.2 => constants.%F.2ba
  176. // CHECK:STDOUT: %F.patt.loc5_13.2 => constants.%F.2ba
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: !definition:
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: specific @Inner(constants.%F.2ba) {
  182. // CHECK:STDOUT: !definition:
  183. // CHECK:STDOUT: %F.loc8_19.1 => constants.%F.2ba
  184. // CHECK:STDOUT: %F.type => constants.%F.type
  185. // CHECK:STDOUT: %F.loc8_19.2 => constants.%F.ee3
  186. // CHECK:STDOUT: %G.type => constants.%G.type
  187. // CHECK:STDOUT: %G => constants.%G
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: specific @F(constants.%F.2ba) {
  191. // CHECK:STDOUT: !definition:
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: specific @G(constants.%F.2ba) {}
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: specific @Inner(%F.loc8_19.1) {}
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: specific @Inner(@G.%F.loc13_46.1) {}
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: specific @F(@G.%F.loc13_46.1) {}
  201. // CHECK:STDOUT: