shadowing.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/destroy.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/var/shadowing.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/var/shadowing.carbon
  14. // --- basics.carbon
  15. namespace NS;
  16. fn Main() {
  17. var NS: () = ();
  18. NS = ();
  19. var x: () = ();
  20. if (true) {
  21. var x: () = ();
  22. // TODO: We should reject this use of the shadowed variable `x`.
  23. x = ();
  24. }
  25. }
  26. // CHECK:STDOUT: --- basics.carbon
  27. // CHECK:STDOUT:
  28. // CHECK:STDOUT: constants {
  29. // CHECK:STDOUT: %Main.type: type = fn_type @Main [concrete]
  30. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  31. // CHECK:STDOUT: %Main: %Main.type = struct_value () [concrete]
  32. // CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
  33. // CHECK:STDOUT: %pattern_type.cb1: type = pattern_type %empty_tuple.type [concrete]
  34. // CHECK:STDOUT: %true: bool = bool_literal true [concrete]
  35. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  36. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls <CanDestroy>> [concrete]
  37. // CHECK:STDOUT: %facet_value: %type_where = facet_value %empty_tuple.type, () [concrete]
  38. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.bb8: type = fn_type @DestroyT.binding.as_type.as.Destroy.impl.Op, @DestroyT.binding.as_type.as.Destroy.impl(%facet_value) [concrete]
  39. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.72f: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.bb8 = struct_value () [concrete]
  40. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn: <specific function> = specific_function %DestroyT.binding.as_type.as.Destroy.impl.Op.72f, @DestroyT.binding.as_type.as.Destroy.impl.Op(%facet_value) [concrete]
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: imports {
  44. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  45. // CHECK:STDOUT: .Destroy = %Core.Destroy
  46. // CHECK:STDOUT: import Core//prelude
  47. // CHECK:STDOUT: import Core//prelude/...
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: file {
  53. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  54. // CHECK:STDOUT: .Core = imports.%Core
  55. // CHECK:STDOUT: .NS = %NS
  56. // CHECK:STDOUT: .Main = %Main.decl
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %Core.import = import Core
  59. // CHECK:STDOUT: %NS: <namespace> = namespace [concrete] {}
  60. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [concrete = constants.%Main] {} {}
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: fn @Main() {
  64. // CHECK:STDOUT: !entry:
  65. // CHECK:STDOUT: name_binding_decl {
  66. // CHECK:STDOUT: %NS.patt: %pattern_type.cb1 = ref_binding_pattern NS [concrete]
  67. // CHECK:STDOUT: %NS.var_patt: %pattern_type.cb1 = var_pattern %NS.patt [concrete]
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %NS.var: ref %empty_tuple.type = var %NS.var_patt
  70. // CHECK:STDOUT: %.loc5_17.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  71. // CHECK:STDOUT: %.loc5_17.2: init %empty_tuple.type = tuple_init () to %NS.var [concrete = constants.%empty_tuple]
  72. // CHECK:STDOUT: %.loc5_3: init %empty_tuple.type = converted %.loc5_17.1, %.loc5_17.2 [concrete = constants.%empty_tuple]
  73. // CHECK:STDOUT: assign %NS.var, %.loc5_3
  74. // CHECK:STDOUT: %.loc5_12.1: type = splice_block %.loc5_12.3 [concrete = constants.%empty_tuple.type] {
  75. // CHECK:STDOUT: %.loc5_12.2: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  76. // CHECK:STDOUT: %.loc5_12.3: type = converted %.loc5_12.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %NS: ref %empty_tuple.type = ref_binding NS, %NS.var
  79. // CHECK:STDOUT: %NS.ref: ref %empty_tuple.type = name_ref NS, %NS
  80. // CHECK:STDOUT: %.loc6_9.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  81. // CHECK:STDOUT: %.loc6_9.2: init %empty_tuple.type = tuple_init () to %NS.ref [concrete = constants.%empty_tuple]
  82. // CHECK:STDOUT: %.loc6_6: init %empty_tuple.type = converted %.loc6_9.1, %.loc6_9.2 [concrete = constants.%empty_tuple]
  83. // CHECK:STDOUT: assign %NS.ref, %.loc6_6
  84. // CHECK:STDOUT: name_binding_decl {
  85. // CHECK:STDOUT: %x.patt.loc8: %pattern_type.cb1 = ref_binding_pattern x [concrete]
  86. // CHECK:STDOUT: %x.var_patt.loc8: %pattern_type.cb1 = var_pattern %x.patt.loc8 [concrete]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %x.var.loc8: ref %empty_tuple.type = var %x.var_patt.loc8
  89. // CHECK:STDOUT: %.loc8_16.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  90. // CHECK:STDOUT: %.loc8_16.2: init %empty_tuple.type = tuple_init () to %x.var.loc8 [concrete = constants.%empty_tuple]
  91. // CHECK:STDOUT: %.loc8_3: init %empty_tuple.type = converted %.loc8_16.1, %.loc8_16.2 [concrete = constants.%empty_tuple]
  92. // CHECK:STDOUT: assign %x.var.loc8, %.loc8_3
  93. // CHECK:STDOUT: %.loc8_11.1: type = splice_block %.loc8_11.3 [concrete = constants.%empty_tuple.type] {
  94. // CHECK:STDOUT: %.loc8_11.2: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  95. // CHECK:STDOUT: %.loc8_11.3: type = converted %.loc8_11.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %x.loc8: ref %empty_tuple.type = ref_binding x, %x.var.loc8
  98. // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
  99. // CHECK:STDOUT: if %true br !if.then else br !if.else
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: !if.then:
  102. // CHECK:STDOUT: name_binding_decl {
  103. // CHECK:STDOUT: %x.patt.loc10: %pattern_type.cb1 = ref_binding_pattern x [concrete]
  104. // CHECK:STDOUT: %x.var_patt.loc10: %pattern_type.cb1 = var_pattern %x.patt.loc10 [concrete]
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %x.var.loc10: ref %empty_tuple.type = var %x.var_patt.loc10
  107. // CHECK:STDOUT: %.loc10_18.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  108. // CHECK:STDOUT: %.loc10_18.2: init %empty_tuple.type = tuple_init () to %x.var.loc10 [concrete = constants.%empty_tuple]
  109. // CHECK:STDOUT: %.loc10_5: init %empty_tuple.type = converted %.loc10_18.1, %.loc10_18.2 [concrete = constants.%empty_tuple]
  110. // CHECK:STDOUT: assign %x.var.loc10, %.loc10_5
  111. // CHECK:STDOUT: %.loc10_13.1: type = splice_block %.loc10_13.3 [concrete = constants.%empty_tuple.type] {
  112. // CHECK:STDOUT: %.loc10_13.2: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  113. // CHECK:STDOUT: %.loc10_13.3: type = converted %.loc10_13.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %x.loc10: ref %empty_tuple.type = ref_binding x, %x.var.loc10
  116. // CHECK:STDOUT: %x.ref: ref %empty_tuple.type = name_ref x, %x.loc10
  117. // CHECK:STDOUT: %.loc13_10.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  118. // CHECK:STDOUT: %.loc13_10.2: init %empty_tuple.type = tuple_init () to %x.ref [concrete = constants.%empty_tuple]
  119. // CHECK:STDOUT: %.loc13_7: init %empty_tuple.type = converted %.loc13_10.1, %.loc13_10.2 [concrete = constants.%empty_tuple]
  120. // CHECK:STDOUT: assign %x.ref, %.loc13_7
  121. // CHECK:STDOUT: br !if.else
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: !if.else:
  124. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound.loc10: <bound method> = bound_method %x.var.loc10, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.72f
  125. // 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.72f, @DestroyT.binding.as_type.as.Destroy.impl.Op(constants.%facet_value) [concrete = constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn]
  126. // CHECK:STDOUT: %bound_method.loc10: <bound method> = bound_method %x.var.loc10, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.1
  127. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call.loc10: init %empty_tuple.type = call %bound_method.loc10(%x.var.loc10)
  128. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound.loc8: <bound method> = bound_method %x.var.loc8, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.72f
  129. // 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.72f, @DestroyT.binding.as_type.as.Destroy.impl.Op(constants.%facet_value) [concrete = constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn]
  130. // CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %x.var.loc8, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.2
  131. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call.loc8: init %empty_tuple.type = call %bound_method.loc8(%x.var.loc8)
  132. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound.loc5: <bound method> = bound_method %NS.var, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.72f
  133. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.3: <specific function> = specific_function constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.72f, @DestroyT.binding.as_type.as.Destroy.impl.Op(constants.%facet_value) [concrete = constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn]
  134. // CHECK:STDOUT: %bound_method.loc5: <bound method> = bound_method %NS.var, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn.3
  135. // CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call.loc5: init %empty_tuple.type = call %bound_method.loc5(%NS.var)
  136. // CHECK:STDOUT: return
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: