shadow.carbon 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 [template]
  29. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  30. // CHECK:STDOUT: %A.1db: %A.type.00d = struct_value () [template]
  31. // CHECK:STDOUT: %A.type.0e8: type = fn_type @A.2 [template]
  32. // CHECK:STDOUT: %A.281: %A.type.0e8 = struct_value () [template]
  33. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  35. // CHECK:STDOUT: %B.type: type = fn_type @B [template]
  36. // CHECK:STDOUT: %B: %B.type = struct_value () [template]
  37. // CHECK:STDOUT: %true: bool = bool_literal true [template]
  38. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [template]
  39. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  40. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  41. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  42. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  43. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  44. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  45. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  46. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0.5c6, %Convert.956 [template]
  47. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  48. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [template]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: imports {
  52. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  53. // CHECK:STDOUT: .Int = %Core.Int
  54. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  55. // CHECK:STDOUT: import Core//prelude
  56. // CHECK:STDOUT: import Core//prelude/...
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: file {
  61. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  62. // CHECK:STDOUT: .Core = imports.%Core
  63. // CHECK:STDOUT: .A = %A.decl.loc11
  64. // CHECK:STDOUT: .N = %N
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT: %Core.import = import Core
  67. // CHECK:STDOUT: %A.decl.loc11: %A.type.00d = fn_decl @A.1 [template = constants.%A.1db] {} {}
  68. // CHECK:STDOUT: %N: <namespace> = namespace [template] {
  69. // CHECK:STDOUT: .A = %A.decl.loc14
  70. // CHECK:STDOUT: .M = %M
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %A.decl.loc14: %A.type.0e8 = fn_decl @A.2 [template = constants.%A.281] {} {}
  73. // CHECK:STDOUT: %M: <namespace> = namespace [template] {
  74. // CHECK:STDOUT: .B = %B.decl
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [template = constants.%B] {
  77. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  78. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param0
  79. // CHECK:STDOUT: } {
  80. // CHECK:STDOUT: %int_32.loc18: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  81. // CHECK:STDOUT: %i32.loc18: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  82. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param0
  83. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: fn @A.1();
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn @A.2();
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: fn @B() -> %i32 {
  92. // CHECK:STDOUT: !entry:
  93. // CHECK:STDOUT: %A.ref.loc20: %A.type.0e8 = name_ref A, file.%A.decl.loc14 [template = constants.%A.281]
  94. // CHECK:STDOUT: %A.call: init %empty_tuple.type = call %A.ref.loc20()
  95. // CHECK:STDOUT: %true: bool = bool_literal true [template = constants.%true]
  96. // CHECK:STDOUT: if %true br !if.then else br !if.else
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: !if.then:
  99. // CHECK:STDOUT: name_binding_decl {
  100. // CHECK:STDOUT: %A.patt: %i32 = binding_pattern A
  101. // CHECK:STDOUT: %.loc22_5.1: %i32 = var_pattern %A.patt
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %A.var: ref %i32 = var A
  104. // CHECK:STDOUT: %int_0.loc22: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  105. // CHECK:STDOUT: %impl.elem0.loc22: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  106. // CHECK:STDOUT: %bound_method.loc22: <bound method> = bound_method %int_0.loc22, %impl.elem0.loc22 [template = constants.%Convert.bound]
  107. // CHECK:STDOUT: %specific_fn.loc22: <specific function> = specific_function %bound_method.loc22, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  108. // CHECK:STDOUT: %int.convert_checked.loc22: init %i32 = call %specific_fn.loc22(%int_0.loc22) [template = constants.%int_0.6a9]
  109. // CHECK:STDOUT: %.loc22_5.2: init %i32 = converted %int_0.loc22, %int.convert_checked.loc22 [template = constants.%int_0.6a9]
  110. // CHECK:STDOUT: assign %A.var, %.loc22_5.2
  111. // CHECK:STDOUT: %.loc22_12: type = splice_block %i32.loc22 [template = constants.%i32] {
  112. // CHECK:STDOUT: %int_32.loc22: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  113. // CHECK:STDOUT: %i32.loc22: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %A: ref %i32 = bind_name A, %A.var
  116. // CHECK:STDOUT: %A.ref.loc25: ref %i32 = name_ref A, %A
  117. // CHECK:STDOUT: %.loc25: %i32 = bind_value %A.ref.loc25
  118. // CHECK:STDOUT: return %.loc25
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: !if.else:
  121. // CHECK:STDOUT: %int_0.loc27: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  122. // CHECK:STDOUT: %impl.elem0.loc27: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  123. // CHECK:STDOUT: %bound_method.loc27: <bound method> = bound_method %int_0.loc27, %impl.elem0.loc27 [template = constants.%Convert.bound]
  124. // CHECK:STDOUT: %specific_fn.loc27: <specific function> = specific_function %bound_method.loc27, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  125. // CHECK:STDOUT: %int.convert_checked.loc27: init %i32 = call %specific_fn.loc27(%int_0.loc27) [template = constants.%int_0.6a9]
  126. // CHECK:STDOUT: %.loc27_11.1: %i32 = value_of_initializer %int.convert_checked.loc27 [template = constants.%int_0.6a9]
  127. // CHECK:STDOUT: %.loc27_11.2: %i32 = converted %int_0.loc27, %.loc27_11.1 [template = constants.%int_0.6a9]
  128. // CHECK:STDOUT: return %.loc27_11.2
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: