shadow.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.816: type = fn_type @A.loc15 [concrete]
  33. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  34. // CHECK:STDOUT: %A.8ae: %A.type.816 = struct_value () [concrete]
  35. // CHECK:STDOUT: %A.type.b86: type = fn_type @A.loc18 [concrete]
  36. // CHECK:STDOUT: %A.b64: %A.type.b86 = 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 = symbolic_binding N, 0 [symbolic]
  41. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  42. // CHECK:STDOUT: %.ff5: Core.Form = init_form %i32 [concrete]
  43. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  44. // CHECK:STDOUT: %B.type: type = fn_type @B [concrete]
  45. // CHECK:STDOUT: %B: %B.type = struct_value () [concrete]
  46. // CHECK:STDOUT: %true: bool = bool_literal true [concrete]
  47. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  48. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  49. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  50. // CHECK:STDOUT: %ImplicitAs.type.e8c: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  51. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  52. // 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]
  53. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6 = struct_value () [symbolic]
  54. // CHECK:STDOUT: %ImplicitAs.impl_witness.6bc: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.74f, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  55. // 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]
  56. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d = struct_value () [concrete]
  57. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.e8c = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.6bc) [concrete]
  58. // CHECK:STDOUT: %ImplicitAs.WithSelf.Convert.type.b37: type = fn_type @ImplicitAs.WithSelf.Convert, @ImplicitAs.WithSelf(%i32, %ImplicitAs.facet) [concrete]
  59. // CHECK:STDOUT: %.545: type = fn_type_with_self_type %ImplicitAs.WithSelf.Convert.type.b37, %ImplicitAs.facet [concrete]
  60. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5 [concrete]
  61. // 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]
  62. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  63. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  64. // CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
  65. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.824: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%N) [symbolic]
  66. // CHECK:STDOUT: %Int.as.Copy.impl.Op.9b9: %Int.as.Copy.impl.Op.type.824 = struct_value () [symbolic]
  67. // CHECK:STDOUT: %Copy.impl_witness.f17: <witness> = impl_witness imports.%Copy.impl_witness_table.e76, @Int.as.Copy.impl(%int_32) [concrete]
  68. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.546: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%int_32) [concrete]
  69. // CHECK:STDOUT: %Int.as.Copy.impl.Op.664: %Int.as.Copy.impl.Op.type.546 = struct_value () [concrete]
  70. // CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value %i32, (%Copy.impl_witness.f17) [concrete]
  71. // CHECK:STDOUT: %Copy.WithSelf.Op.type.081: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet) [concrete]
  72. // CHECK:STDOUT: %.8e2: type = fn_type_with_self_type %Copy.WithSelf.Op.type.081, %Copy.facet [concrete]
  73. // CHECK:STDOUT: %Int.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %Int.as.Copy.impl.Op.664, @Int.as.Copy.impl.Op(%int_32) [concrete]
  74. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  75. // CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete]
  76. // CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: imports {
  80. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  81. // CHECK:STDOUT: .Int = %Core.Int
  82. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  83. // CHECK:STDOUT: .Copy = %Core.Copy
  84. // CHECK:STDOUT: .Destroy = %Core.Destroy
  85. // CHECK:STDOUT: import Core//prelude
  86. // CHECK:STDOUT: import Core//prelude/...
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  89. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  90. // 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)]
  91. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.74f = impl_witness_table (%Core.import_ref.42d), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  92. // CHECK:STDOUT: %Core.Copy: type = import_ref Core//prelude/parts/copy, Copy, loaded [concrete = constants.%Copy.type]
  93. // CHECK:STDOUT: %Core.import_ref.18d: @Int.as.Copy.impl.%Int.as.Copy.impl.Op.type (%Int.as.Copy.impl.Op.type.824) = 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.9b9)]
  94. // CHECK:STDOUT: %Copy.impl_witness_table.e76 = impl_witness_table (%Core.import_ref.18d), @Int.as.Copy.impl [concrete]
  95. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: file {
  99. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  100. // CHECK:STDOUT: .Core = imports.%Core
  101. // CHECK:STDOUT: .A = %A.decl.loc15
  102. // CHECK:STDOUT: .N = %N
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %Core.import = import Core
  105. // CHECK:STDOUT: %A.decl.loc15: %A.type.816 = fn_decl @A.loc15 [concrete = constants.%A.8ae] {} {}
  106. // CHECK:STDOUT: %N: <namespace> = namespace [concrete] {
  107. // CHECK:STDOUT: .A = %A.decl.loc18
  108. // CHECK:STDOUT: .M = %M
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %A.decl.loc18: %A.type.b86 = fn_decl @A.loc18 [concrete = constants.%A.b64] {} {}
  111. // CHECK:STDOUT: %M: <namespace> = namespace [concrete] {
  112. // CHECK:STDOUT: .B = %B.decl
  113. // CHECK:STDOUT: .A = <poisoned>
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [concrete = constants.%B] {
  116. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern [concrete]
  117. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern %return.param_patt, %i32.loc22 [concrete]
  118. // CHECK:STDOUT: } {
  119. // CHECK:STDOUT: %i32.loc22: type = type_literal constants.%i32 [concrete = constants.%i32]
  120. // CHECK:STDOUT: %.loc22: Core.Form = init_form %i32.loc22 [concrete = constants.%.ff5]
  121. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  122. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @A.loc15();
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: fn @A.loc18();
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @B() -> out %return.param: %i32 {
  131. // CHECK:STDOUT: !entry:
  132. // CHECK:STDOUT: %A.ref.loc24: %A.type.b86 = name_ref A, file.%A.decl.loc18 [concrete = constants.%A.b64]
  133. // CHECK:STDOUT: %A.call: init %empty_tuple.type = call %A.ref.loc24()
  134. // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
  135. // CHECK:STDOUT: if %true br !if.then else br !if.else
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !if.then:
  138. // CHECK:STDOUT: name_binding_decl {
  139. // CHECK:STDOUT: %A.patt: %pattern_type.7ce = ref_binding_pattern A [concrete]
  140. // CHECK:STDOUT: %A.var_patt: %pattern_type.7ce = var_pattern %A.patt [concrete]
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: %A.var: ref %i32 = var %A.var_patt
  143. // CHECK:STDOUT: %int_0.loc26: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  144. // CHECK:STDOUT: %impl.elem0.loc26: %.545 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5]
  145. // 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]
  146. // 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]
  147. // CHECK:STDOUT: %bound_method.loc26_5.2: <bound method> = bound_method %int_0.loc26, %specific_fn.loc26 [concrete = constants.%bound_method]
  148. // 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]
  149. // CHECK:STDOUT: %.loc26: init %i32 = converted %int_0.loc26, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc26 [concrete = constants.%int_0.6a9]
  150. // CHECK:STDOUT: assign %A.var, %.loc26
  151. // CHECK:STDOUT: %i32.loc26: type = type_literal constants.%i32 [concrete = constants.%i32]
  152. // CHECK:STDOUT: %A: ref %i32 = ref_binding A, %A.var
  153. // CHECK:STDOUT: %A.ref.loc29: ref %i32 = name_ref A, %A
  154. // CHECK:STDOUT: %.loc29: %i32 = acquire_value %A.ref.loc29
  155. // CHECK:STDOUT: %impl.elem0.loc29: %.8e2 = impl_witness_access constants.%Copy.impl_witness.f17, element0 [concrete = constants.%Int.as.Copy.impl.Op.664]
  156. // CHECK:STDOUT: %bound_method.loc29_12.1: <bound method> = bound_method %.loc29, %impl.elem0.loc29
  157. // 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]
  158. // CHECK:STDOUT: %bound_method.loc29_12.2: <bound method> = bound_method %.loc29, %specific_fn.loc29
  159. // CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc29_12.2(%.loc29)
  160. // CHECK:STDOUT: %Destroy.Op.bound.loc26_5.1: <bound method> = bound_method %A.var, constants.%Destroy.Op
  161. // CHECK:STDOUT: %Destroy.Op.call.loc26_5.1: init %empty_tuple.type = call %Destroy.Op.bound.loc26_5.1(%A.var)
  162. // CHECK:STDOUT: return %Int.as.Copy.impl.Op.call
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: !if.else:
  165. // CHECK:STDOUT: %int_0.loc31: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  166. // CHECK:STDOUT: %impl.elem0.loc31: %.545 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5]
  167. // 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]
  168. // 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]
  169. // CHECK:STDOUT: %bound_method.loc31_11.2: <bound method> = bound_method %int_0.loc31, %specific_fn.loc31 [concrete = constants.%bound_method]
  170. // 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]
  171. // CHECK:STDOUT: %.loc31: init %i32 = converted %int_0.loc31, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc31 [concrete = constants.%int_0.6a9]
  172. // CHECK:STDOUT: %Destroy.Op.bound.loc26_5.2: <bound method> = bound_method %A.var, constants.%Destroy.Op
  173. // CHECK:STDOUT: %Destroy.Op.call.loc26_5.2: init %empty_tuple.type = call %Destroy.Op.bound.loc26_5.2(%A.var)
  174. // CHECK:STDOUT: return %.loc31
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %i32) = "no_op";
  178. // CHECK:STDOUT: