shadow.carbon 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/namespace/shadow.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/namespace/shadow.carbon
  10. fn A();
  11. namespace N;
  12. fn N.A();
  13. namespace N.M;
  14. fn N.M.B() -> i32 {
  15. // This is N.A, not package.A.
  16. A();
  17. if (true) {
  18. var A: i32 = 0;
  19. // This is the local A.
  20. // TODO: Decide if we should warn or error on the shadowing of N.A here.
  21. return A;
  22. }
  23. return 0;
  24. }
  25. // CHECK:STDOUT: --- shadow.carbon
  26. // CHECK:STDOUT:
  27. // CHECK:STDOUT: constants {
  28. // CHECK:STDOUT: %A.type.00d: type = fn_type @A.1 [concrete]
  29. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  30. // CHECK:STDOUT: %A.1db: %A.type.00d = struct_value () [concrete]
  31. // CHECK:STDOUT: %A.type.0e8: type = fn_type @A.2 [concrete]
  32. // CHECK:STDOUT: %A.281: %A.type.0e8 = struct_value () [concrete]
  33. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  35. // CHECK:STDOUT: %B.type: type = fn_type @B [concrete]
  36. // CHECK:STDOUT: %B: %B.type = struct_value () [concrete]
  37. // CHECK:STDOUT: %true: bool = bool_literal true [concrete]
  38. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  39. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  40. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  41. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.4f9(%int_32) [concrete]
  42. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  43. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  44. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%impl_witness.d39) [concrete]
  45. // CHECK:STDOUT: %.be7: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  46. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0.5c6, %Convert.956 [concrete]
  47. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  48. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Convert.specific_fn [concrete]
  49. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: imports {
  53. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  54. // CHECK:STDOUT: .Int = %Core.Int
  55. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  56. // CHECK:STDOUT: import Core//prelude
  57. // CHECK:STDOUT: import Core//prelude/...
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: file {
  62. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  63. // CHECK:STDOUT: .Core = imports.%Core
  64. // CHECK:STDOUT: .A = %A.decl.loc11
  65. // CHECK:STDOUT: .N = %N
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Core.import = import Core
  68. // CHECK:STDOUT: %A.decl.loc11: %A.type.00d = fn_decl @A.1 [concrete = constants.%A.1db] {} {}
  69. // CHECK:STDOUT: %N: <namespace> = namespace [concrete] {
  70. // CHECK:STDOUT: .A = %A.decl.loc14
  71. // CHECK:STDOUT: .M = %M
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %A.decl.loc14: %A.type.0e8 = fn_decl @A.2 [concrete = constants.%A.281] {} {}
  74. // CHECK:STDOUT: %M: <namespace> = namespace [concrete] {
  75. // CHECK:STDOUT: .B = %B.decl
  76. // CHECK:STDOUT: .A = <poisoned>
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [concrete = constants.%B] {
  79. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  80. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, call_param0
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %int_32.loc18: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  83. // CHECK:STDOUT: %i32.loc18: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  84. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  85. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn @A.1();
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: fn @A.2();
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: fn @B() -> %i32 {
  94. // CHECK:STDOUT: !entry:
  95. // CHECK:STDOUT: %A.ref.loc20: %A.type.0e8 = name_ref A, file.%A.decl.loc14 [concrete = constants.%A.281]
  96. // CHECK:STDOUT: %A.call: init %empty_tuple.type = call %A.ref.loc20()
  97. // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
  98. // CHECK:STDOUT: if %true br !if.then else br !if.else
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: !if.then:
  101. // CHECK:STDOUT: name_binding_decl {
  102. // CHECK:STDOUT: %A.patt: %i32 = binding_pattern A
  103. // CHECK:STDOUT: %.loc22_5.1: %i32 = var_pattern %A.patt
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %A.var: ref %i32 = var A
  106. // CHECK:STDOUT: %int_0.loc22: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  107. // CHECK:STDOUT: %impl.elem0.loc22: %.be7 = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  108. // CHECK:STDOUT: %bound_method.loc22_5.1: <bound method> = bound_method %int_0.loc22, %impl.elem0.loc22 [concrete = constants.%Convert.bound]
  109. // CHECK:STDOUT: %specific_fn.loc22: <specific function> = specific_function %impl.elem0.loc22, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  110. // CHECK:STDOUT: %bound_method.loc22_5.2: <bound method> = bound_method %int_0.loc22, %specific_fn.loc22 [concrete = constants.%bound_method]
  111. // CHECK:STDOUT: %int.convert_checked.loc22: init %i32 = call %bound_method.loc22_5.2(%int_0.loc22) [concrete = constants.%int_0.6a9]
  112. // CHECK:STDOUT: %.loc22_5.2: init %i32 = converted %int_0.loc22, %int.convert_checked.loc22 [concrete = constants.%int_0.6a9]
  113. // CHECK:STDOUT: assign %A.var, %.loc22_5.2
  114. // CHECK:STDOUT: %.loc22_12: type = splice_block %i32.loc22 [concrete = constants.%i32] {
  115. // CHECK:STDOUT: %int_32.loc22: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  116. // CHECK:STDOUT: %i32.loc22: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %A: ref %i32 = bind_name A, %A.var
  119. // CHECK:STDOUT: %A.ref.loc25: ref %i32 = name_ref A, %A
  120. // CHECK:STDOUT: %.loc25: %i32 = bind_value %A.ref.loc25
  121. // CHECK:STDOUT: return %.loc25
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: !if.else:
  124. // CHECK:STDOUT: %int_0.loc27: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  125. // CHECK:STDOUT: %impl.elem0.loc27: %.be7 = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  126. // CHECK:STDOUT: %bound_method.loc27_11.1: <bound method> = bound_method %int_0.loc27, %impl.elem0.loc27 [concrete = constants.%Convert.bound]
  127. // CHECK:STDOUT: %specific_fn.loc27: <specific function> = specific_function %impl.elem0.loc27, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  128. // CHECK:STDOUT: %bound_method.loc27_11.2: <bound method> = bound_method %int_0.loc27, %specific_fn.loc27 [concrete = constants.%bound_method]
  129. // CHECK:STDOUT: %int.convert_checked.loc27: init %i32 = call %bound_method.loc27_11.2(%int_0.loc27) [concrete = constants.%int_0.6a9]
  130. // CHECK:STDOUT: %.loc27_11.1: %i32 = value_of_initializer %int.convert_checked.loc27 [concrete = constants.%int_0.6a9]
  131. // CHECK:STDOUT: %.loc27_11.2: %i32 = converted %int_0.loc27, %.loc27_11.1 [concrete = constants.%int_0.6a9]
  132. // CHECK:STDOUT: return %.loc27_11.2
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: