identity.carbon 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/as/identity.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/identity.carbon
  10. // `expr as T` should behave the same as `expr` if `T` is the type of `expr`.
  11. class X {
  12. // ...
  13. }
  14. fn Value(n: X) {
  15. let m: X = n as X;
  16. }
  17. fn Reference(p: X*) {
  18. let q: X* = &(*p as X);
  19. }
  20. fn Make() -> X;
  21. fn Initializing() {
  22. var x: X = (Make() as X);
  23. }
  24. // CHECK:STDOUT: --- identity.carbon
  25. // CHECK:STDOUT:
  26. // CHECK:STDOUT: constants {
  27. // CHECK:STDOUT: %X: type = class_type @X [template]
  28. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  29. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  30. // CHECK:STDOUT: %Value.type: type = fn_type @Value [template]
  31. // CHECK:STDOUT: %Value: %Value.type = struct_value () [template]
  32. // CHECK:STDOUT: %ptr.d17: type = ptr_type %X [template]
  33. // CHECK:STDOUT: %Reference.type: type = fn_type @Reference [template]
  34. // CHECK:STDOUT: %Reference: %Reference.type = struct_value () [template]
  35. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  36. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  37. // CHECK:STDOUT: %Initializing.type: type = fn_type @Initializing [template]
  38. // CHECK:STDOUT: %Initializing: %Initializing.type = struct_value () [template]
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT:
  41. // CHECK:STDOUT: imports {
  42. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  43. // CHECK:STDOUT: import Core//prelude
  44. // CHECK:STDOUT: import Core//prelude/...
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: file {
  49. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  50. // CHECK:STDOUT: .Core = imports.%Core
  51. // CHECK:STDOUT: .X = %X.decl
  52. // CHECK:STDOUT: .Value = %Value.decl
  53. // CHECK:STDOUT: .Reference = %Reference.decl
  54. // CHECK:STDOUT: .Make = %Make.decl
  55. // CHECK:STDOUT: .Initializing = %Initializing.decl
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: %Core.import = import Core
  58. // CHECK:STDOUT: %X.decl: type = class_decl @X [template = constants.%X] {} {}
  59. // CHECK:STDOUT: %Value.decl: %Value.type = fn_decl @Value [template = constants.%Value] {
  60. // CHECK:STDOUT: %n.patt: %X = binding_pattern n
  61. // CHECK:STDOUT: %n.param_patt: %X = value_param_pattern %n.patt, runtime_param0
  62. // CHECK:STDOUT: } {
  63. // CHECK:STDOUT: %n.param: %X = value_param runtime_param0
  64. // CHECK:STDOUT: %X.ref.loc17: type = name_ref X, file.%X.decl [template = constants.%X]
  65. // CHECK:STDOUT: %n: %X = bind_name n, %n.param
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Reference.decl: %Reference.type = fn_decl @Reference [template = constants.%Reference] {
  68. // CHECK:STDOUT: %p.patt: %ptr.d17 = binding_pattern p
  69. // CHECK:STDOUT: %p.param_patt: %ptr.d17 = value_param_pattern %p.patt, runtime_param0
  70. // CHECK:STDOUT: } {
  71. // CHECK:STDOUT: %p.param: %ptr.d17 = value_param runtime_param0
  72. // CHECK:STDOUT: %.loc21: type = splice_block %ptr.loc21 [template = constants.%ptr.d17] {
  73. // CHECK:STDOUT: %X.ref.loc21: type = name_ref X, file.%X.decl [template = constants.%X]
  74. // CHECK:STDOUT: %ptr.loc21: type = ptr_type %X [template = constants.%ptr.d17]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %p: %ptr.d17 = bind_name p, %p.param
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  79. // CHECK:STDOUT: %return.patt: %X = return_slot_pattern
  80. // CHECK:STDOUT: %return.param_patt: %X = out_param_pattern %return.patt, runtime_param0
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  83. // CHECK:STDOUT: %return.param: ref %X = out_param runtime_param0
  84. // CHECK:STDOUT: %return: ref %X = return_slot %return.param
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Initializing.decl: %Initializing.type = fn_decl @Initializing [template = constants.%Initializing] {} {}
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: class @X {
  90. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  91. // CHECK:STDOUT: complete_type_witness = %complete_type
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: !members:
  94. // CHECK:STDOUT: .Self = constants.%X
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: fn @Value(%n.param_patt: %X) {
  98. // CHECK:STDOUT: !entry:
  99. // CHECK:STDOUT: name_binding_decl {
  100. // CHECK:STDOUT: %m.patt: %X = binding_pattern m
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %n.ref: %X = name_ref n, %n
  103. // CHECK:STDOUT: %X.ref.loc18_19: type = name_ref X, file.%X.decl [template = constants.%X]
  104. // CHECK:STDOUT: %X.ref.loc18_10: type = name_ref X, file.%X.decl [template = constants.%X]
  105. // CHECK:STDOUT: %m: %X = bind_name m, %n.ref
  106. // CHECK:STDOUT: return
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: fn @Reference(%p.param_patt: %ptr.d17) {
  110. // CHECK:STDOUT: !entry:
  111. // CHECK:STDOUT: name_binding_decl {
  112. // CHECK:STDOUT: %q.patt: %ptr.d17 = binding_pattern q
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %p.ref: %ptr.d17 = name_ref p, %p
  115. // CHECK:STDOUT: %.loc22_17: ref %X = deref %p.ref
  116. // CHECK:STDOUT: %X.ref.loc22_23: type = name_ref X, file.%X.decl [template = constants.%X]
  117. // CHECK:STDOUT: %addr: %ptr.d17 = addr_of %.loc22_17
  118. // CHECK:STDOUT: %.loc22_11: type = splice_block %ptr.loc22 [template = constants.%ptr.d17] {
  119. // CHECK:STDOUT: %X.ref.loc22_10: type = name_ref X, file.%X.decl [template = constants.%X]
  120. // CHECK:STDOUT: %ptr.loc22: type = ptr_type %X [template = constants.%ptr.d17]
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: %q: %ptr.d17 = bind_name q, %addr
  123. // CHECK:STDOUT: return
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @Make() -> %X;
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: fn @Initializing() {
  129. // CHECK:STDOUT: !entry:
  130. // CHECK:STDOUT: name_binding_decl {
  131. // CHECK:STDOUT: %x.patt: %X = binding_pattern x
  132. // CHECK:STDOUT: %.loc28_3.1: %X = var_pattern %x.patt
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: %x.var: ref %X = var x
  135. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, file.%Make.decl [template = constants.%Make]
  136. // CHECK:STDOUT: %.loc28_3.2: ref %X = splice_block %x.var {}
  137. // CHECK:STDOUT: %Make.call: init %X = call %Make.ref() to %.loc28_3.2
  138. // CHECK:STDOUT: %X.ref.loc28_25: type = name_ref X, file.%X.decl [template = constants.%X]
  139. // CHECK:STDOUT: assign %x.var, %Make.call
  140. // CHECK:STDOUT: %X.ref.loc28_10: type = name_ref X, file.%X.decl [template = constants.%X]
  141. // CHECK:STDOUT: %x: ref %X = bind_name x, %x.var
  142. // CHECK:STDOUT: return
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: