shadow.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/namespace/shadow.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/namespace/shadow.carbon
  14. fn A();
  15. namespace N;
  16. fn N.A();
  17. namespace N.M;
  18. fn N.M.B() -> i32 {
  19. // This is N.A, not package.A.
  20. A();
  21. if (true) {
  22. var A: i32 = 0;
  23. // This is the local A.
  24. // TODO: Decide if we should warn or error on the shadowing of N.A here.
  25. return A;
  26. }
  27. return 0;
  28. }
  29. // CHECK:STDOUT: --- shadow.carbon
  30. // CHECK:STDOUT:
  31. // CHECK:STDOUT: constants {
  32. // CHECK:STDOUT: %A.type.00d: type = fn_type @A.loc15 [concrete]
  33. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  34. // CHECK:STDOUT: %A.1db: %A.type.00d = struct_value () [concrete]
  35. // CHECK:STDOUT: %A.type.0e8: type = fn_type @A.loc18 [concrete]
  36. // CHECK:STDOUT: %A.281: %A.type.0e8 = struct_value () [concrete]
  37. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  38. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  39. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  40. // CHECK:STDOUT: %N: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic]
  41. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  42. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  43. // CHECK:STDOUT: %B.type: type = fn_type @B [concrete]
  44. // CHECK:STDOUT: %B: %B.type = struct_value () [concrete]
  45. // CHECK:STDOUT: %true: bool = bool_literal true [concrete]
  46. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  47. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  48. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  49. // CHECK:STDOUT: %ImplicitAs.type.d14: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  50. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  51. // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  52. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  53. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.1c0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340 = struct_value () [symbolic]
  54. // CHECK:STDOUT: %ImplicitAs.impl_witness.204: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.9e9, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  55. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  56. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584 = struct_value () [concrete]
  57. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.d14 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.204) [concrete]
  58. // CHECK:STDOUT: %.1df: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  59. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0 [concrete]
  60. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  61. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  62. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  63. // CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
  64. // CHECK:STDOUT: %Copy.Op.type: type = fn_type @Copy.Op [concrete]
  65. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.afd: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%N) [symbolic]
  66. // CHECK:STDOUT: %Int.as.Copy.impl.Op.6cd: %Int.as.Copy.impl.Op.type.afd = struct_value () [symbolic]
  67. // CHECK:STDOUT: %Copy.impl_witness.a32: <witness> = impl_witness imports.%Copy.impl_witness_table.1ed, @Int.as.Copy.impl(%int_32) [concrete]
  68. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.276: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%int_32) [concrete]
  69. // CHECK:STDOUT: %Int.as.Copy.impl.Op.f59: %Int.as.Copy.impl.Op.type.276 = struct_value () [concrete]
  70. // CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value %i32, (%Copy.impl_witness.a32) [concrete]
  71. // CHECK:STDOUT: %.7fa: type = fn_type_with_self_type %Copy.Op.type, %Copy.facet [concrete]
  72. // CHECK:STDOUT: %Int.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %Int.as.Copy.impl.Op.f59, @Int.as.Copy.impl.Op(%int_32) [concrete]
  73. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  74. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls <CanDestroy>> [concrete]
  75. // CHECK:STDOUT: %facet_value: %type_where = facet_value %i32, () [concrete]
  76. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.a12: type = fn_type @DestroyT.binding.as_type.as.Destroy.impl.Op, @DestroyT.binding.as_type.as.Destroy.impl(%facet_value) [concrete]
  77. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.a57: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.a12 = struct_value () [concrete]
  78. // CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [concrete]
  79. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn: <specific function> = specific_function %DestroyT.binding.as_type.as.Destroy.impl.Op.a57, @DestroyT.binding.as_type.as.Destroy.impl.Op(%facet_value) [concrete]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: imports {
  83. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  84. // CHECK:STDOUT: .Int = %Core.Int
  85. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  86. // CHECK:STDOUT: .Copy = %Core.Copy
  87. // CHECK:STDOUT: .Destroy = %Core.Destroy
  88. // CHECK:STDOUT: import Core//prelude
  89. // CHECK:STDOUT: import Core//prelude/...
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  92. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  93. // CHECK:STDOUT: %Core.import_ref.ee7: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340) = 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.1c0)]
  94. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.9e9 = impl_witness_table (%Core.import_ref.ee7), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  95. // CHECK:STDOUT: %Core.Copy: type = import_ref Core//prelude/parts/copy, Copy, loaded [concrete = constants.%Copy.type]
  96. // CHECK:STDOUT: %Core.import_ref.d0f6: @Int.as.Copy.impl.%Int.as.Copy.impl.Op.type (%Int.as.Copy.impl.Op.type.afd) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Int.as.Copy.impl.%Int.as.Copy.impl.Op (constants.%Int.as.Copy.impl.Op.6cd)]
  97. // CHECK:STDOUT: %Copy.impl_witness_table.1ed = impl_witness_table (%Core.import_ref.d0f6), @Int.as.Copy.impl [concrete]
  98. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: file {
  102. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  103. // CHECK:STDOUT: .Core = imports.%Core
  104. // CHECK:STDOUT: .A = %A.decl.loc15
  105. // CHECK:STDOUT: .N = %N
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %Core.import = import Core
  108. // CHECK:STDOUT: %A.decl.loc15: %A.type.00d = fn_decl @A.loc15 [concrete = constants.%A.1db] {} {}
  109. // CHECK:STDOUT: %N: <namespace> = namespace [concrete] {
  110. // CHECK:STDOUT: .A = %A.decl.loc18
  111. // CHECK:STDOUT: .M = %M
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: %A.decl.loc18: %A.type.0e8 = fn_decl @A.loc18 [concrete = constants.%A.281] {} {}
  114. // CHECK:STDOUT: %M: <namespace> = namespace [concrete] {
  115. // CHECK:STDOUT: .B = %B.decl
  116. // CHECK:STDOUT: .A = <poisoned>
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [concrete = constants.%B] {
  119. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  120. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  121. // CHECK:STDOUT: } {
  122. // CHECK:STDOUT: %int_32.loc22: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  123. // CHECK:STDOUT: %i32.loc22: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  124. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  125. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: fn @A.loc15();
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: fn @A.loc18();
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: fn @B() -> %i32 {
  134. // CHECK:STDOUT: !entry:
  135. // CHECK:STDOUT: %A.ref.loc24: %A.type.0e8 = name_ref A, file.%A.decl.loc18 [concrete = constants.%A.281]
  136. // CHECK:STDOUT: %A.call: init %empty_tuple.type = call %A.ref.loc24()
  137. // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
  138. // CHECK:STDOUT: if %true br !if.then else br !if.else
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: !if.then:
  141. // CHECK:STDOUT: name_binding_decl {
  142. // CHECK:STDOUT: %A.patt: %pattern_type.7ce = binding_pattern A [concrete]
  143. // CHECK:STDOUT: %A.var_patt: %pattern_type.7ce = var_pattern %A.patt [concrete]
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: %A.var: ref %i32 = var %A.var_patt
  146. // CHECK:STDOUT: %int_0.loc26: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  147. // CHECK:STDOUT: %impl.elem0.loc26: %.1df = impl_witness_access constants.%ImplicitAs.impl_witness.204, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0]
  148. // CHECK:STDOUT: %bound_method.loc26_5.1: <bound method> = bound_method %int_0.loc26, %impl.elem0.loc26 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  149. // CHECK:STDOUT: %specific_fn.loc26: <specific function> = specific_function %impl.elem0.loc26, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  150. // CHECK:STDOUT: %bound_method.loc26_5.2: <bound method> = bound_method %int_0.loc26, %specific_fn.loc26 [concrete = constants.%bound_method]
  151. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc26: init %i32 = call %bound_method.loc26_5.2(%int_0.loc26) [concrete = constants.%int_0.6a9]
  152. // CHECK:STDOUT: %.loc26_5: init %i32 = converted %int_0.loc26, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc26 [concrete = constants.%int_0.6a9]
  153. // CHECK:STDOUT: assign %A.var, %.loc26_5
  154. // CHECK:STDOUT: %.loc26_12: type = splice_block %i32.loc26 [concrete = constants.%i32] {
  155. // CHECK:STDOUT: %int_32.loc26: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  156. // CHECK:STDOUT: %i32.loc26: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: %A: ref %i32 = bind_name A, %A.var
  159. // CHECK:STDOUT: %A.ref.loc29: ref %i32 = name_ref A, %A
  160. // CHECK:STDOUT: %.loc29: %i32 = bind_value %A.ref.loc29
  161. // CHECK:STDOUT: %impl.elem0.loc29: %.7fa = impl_witness_access constants.%Copy.impl_witness.a32, element0 [concrete = constants.%Int.as.Copy.impl.Op.f59]
  162. // CHECK:STDOUT: %bound_method.loc29_12.1: <bound method> = bound_method %.loc29, %impl.elem0.loc29
  163. // CHECK:STDOUT: %specific_fn.loc29: <specific function> = specific_function %impl.elem0.loc29, @Int.as.Copy.impl.Op(constants.%int_32) [concrete = constants.%Int.as.Copy.impl.Op.specific_fn]
  164. // CHECK:STDOUT: %bound_method.loc29_12.2: <bound method> = bound_method %.loc29, %specific_fn.loc29
  165. // CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc29_12.2(%.loc29)
  166. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound.loc26_5.1: <bound method> = bound_method %A.var, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.a57
  167. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.1: <specific function> = specific_function constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.a57, @DestroyT.binding.as_type.as.Destroy.impl.Op(constants.%facet_value) [concrete = constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn]
  168. // CHECK:STDOUT: %bound_method.loc26_5.3: <bound method> = bound_method %A.var, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.1
  169. // CHECK:STDOUT: %addr.loc26_5.1: %ptr.235 = addr_of %A.var
  170. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call.loc26_5.1: init %empty_tuple.type = call %bound_method.loc26_5.3(%addr.loc26_5.1)
  171. // CHECK:STDOUT: return %Int.as.Copy.impl.Op.call to %return
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !if.else:
  174. // CHECK:STDOUT: %int_0.loc31: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  175. // CHECK:STDOUT: %impl.elem0.loc31: %.1df = impl_witness_access constants.%ImplicitAs.impl_witness.204, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0]
  176. // CHECK:STDOUT: %bound_method.loc31_11.1: <bound method> = bound_method %int_0.loc31, %impl.elem0.loc31 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  177. // CHECK:STDOUT: %specific_fn.loc31: <specific function> = specific_function %impl.elem0.loc31, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  178. // CHECK:STDOUT: %bound_method.loc31_11.2: <bound method> = bound_method %int_0.loc31, %specific_fn.loc31 [concrete = constants.%bound_method]
  179. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc31: init %i32 = call %bound_method.loc31_11.2(%int_0.loc31) [concrete = constants.%int_0.6a9]
  180. // CHECK:STDOUT: %.loc31: init %i32 = converted %int_0.loc31, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc31 [concrete = constants.%int_0.6a9]
  181. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound.loc26_5.2: <bound method> = bound_method %A.var, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.a57
  182. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.2: <specific function> = specific_function constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.a57, @DestroyT.binding.as_type.as.Destroy.impl.Op(constants.%facet_value) [concrete = constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn]
  183. // CHECK:STDOUT: %bound_method.loc26_5.4: <bound method> = bound_method %A.var, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.2
  184. // CHECK:STDOUT: %addr.loc26_5.2: %ptr.235 = addr_of %A.var
  185. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call.loc26_5.2: init %empty_tuple.type = call %bound_method.loc26_5.4(%addr.loc26_5.2)
  186. // CHECK:STDOUT: return %.loc31 to %return
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT: