identity.carbon 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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: %.1: type = struct_type {} [template]
  29. // CHECK:STDOUT: %.2: <witness> = complete_type_witness %.1 [template]
  30. // CHECK:STDOUT: %Value.type: type = fn_type @Value [template]
  31. // CHECK:STDOUT: %.3: type = tuple_type () [template]
  32. // CHECK:STDOUT: %Value: %Value.type = struct_value () [template]
  33. // CHECK:STDOUT: %.4: type = ptr_type %.1 [template]
  34. // CHECK:STDOUT: %.5: type = ptr_type %X [template]
  35. // CHECK:STDOUT: %Reference.type: type = fn_type @Reference [template]
  36. // CHECK:STDOUT: %Reference: %Reference.type = struct_value () [template]
  37. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  38. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  39. // CHECK:STDOUT: %Initializing.type: type = fn_type @Initializing [template]
  40. // CHECK:STDOUT: %Initializing: %Initializing.type = struct_value () [template]
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: imports {
  44. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  45. // CHECK:STDOUT: import Core//prelude
  46. // CHECK:STDOUT: import Core//prelude/operators
  47. // CHECK:STDOUT: import Core//prelude/types
  48. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  49. // CHECK:STDOUT: import Core//prelude/operators/as
  50. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  51. // CHECK:STDOUT: import Core//prelude/operators/comparison
  52. // CHECK:STDOUT: import Core//prelude/types/bool
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: file {
  57. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  58. // CHECK:STDOUT: .Core = imports.%Core
  59. // CHECK:STDOUT: .X = %X.decl
  60. // CHECK:STDOUT: .Value = %Value.decl
  61. // CHECK:STDOUT: .Reference = %Reference.decl
  62. // CHECK:STDOUT: .Make = %Make.decl
  63. // CHECK:STDOUT: .Initializing = %Initializing.decl
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.import = import Core
  66. // CHECK:STDOUT: %X.decl: type = class_decl @X [template = constants.%X] {} {}
  67. // CHECK:STDOUT: %Value.decl: %Value.type = fn_decl @Value [template = constants.%Value] {
  68. // CHECK:STDOUT: %n.patt: %X = binding_pattern n
  69. // CHECK:STDOUT: } {
  70. // CHECK:STDOUT: %X.ref.loc17: type = name_ref X, file.%X.decl [template = constants.%X]
  71. // CHECK:STDOUT: %n.param: %X = param n, runtime_param0
  72. // CHECK:STDOUT: %n: %X = bind_name n, %n.param
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %Reference.decl: %Reference.type = fn_decl @Reference [template = constants.%Reference] {
  75. // CHECK:STDOUT: %p.patt: %.5 = binding_pattern p
  76. // CHECK:STDOUT: } {
  77. // CHECK:STDOUT: %X.ref.loc21: type = name_ref X, file.%X.decl [template = constants.%X]
  78. // CHECK:STDOUT: %.loc21: type = ptr_type %X [template = constants.%.5]
  79. // CHECK:STDOUT: %p.param: %.5 = param p, runtime_param0
  80. // CHECK:STDOUT: %p: %.5 = bind_name p, %p.param
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {} {
  83. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  84. // CHECK:STDOUT: %return: ref %X = var <return slot>
  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: %.loc15: <witness> = complete_type_witness %.1 [template = constants.%.2]
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: !members:
  93. // CHECK:STDOUT: .Self = constants.%X
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: fn @Value(%n: %X) {
  97. // CHECK:STDOUT: !entry:
  98. // CHECK:STDOUT: %X.ref.loc18_10: type = name_ref X, file.%X.decl [template = constants.%X]
  99. // CHECK:STDOUT: %n.ref: %X = name_ref n, %n
  100. // CHECK:STDOUT: %X.ref.loc18_19: type = name_ref X, file.%X.decl [template = constants.%X]
  101. // CHECK:STDOUT: %m: %X = bind_name m, %n.ref
  102. // CHECK:STDOUT: return
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn @Reference(%p: %.5) {
  106. // CHECK:STDOUT: !entry:
  107. // CHECK:STDOUT: %X.ref.loc22_10: type = name_ref X, file.%X.decl [template = constants.%X]
  108. // CHECK:STDOUT: %.loc22_11: type = ptr_type %X [template = constants.%.5]
  109. // CHECK:STDOUT: %p.ref: %.5 = name_ref p, %p
  110. // CHECK:STDOUT: %.loc22_17: ref %X = deref %p.ref
  111. // CHECK:STDOUT: %X.ref.loc22_23: type = name_ref X, file.%X.decl [template = constants.%X]
  112. // CHECK:STDOUT: %.loc22_15: %.5 = addr_of %.loc22_17
  113. // CHECK:STDOUT: %q: %.5 = bind_name q, %.loc22_15
  114. // CHECK:STDOUT: return
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @Make() -> %X;
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @Initializing() {
  120. // CHECK:STDOUT: !entry:
  121. // CHECK:STDOUT: %X.ref.loc28_10: type = name_ref X, file.%X.decl [template = constants.%X]
  122. // CHECK:STDOUT: %x.var: ref %X = var x
  123. // CHECK:STDOUT: %x: ref %X = bind_name x, %x.var
  124. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, file.%Make.decl [template = constants.%Make]
  125. // CHECK:STDOUT: %.loc28: ref %X = splice_block %x.var {}
  126. // CHECK:STDOUT: %Make.call: init %X = call %Make.ref() to %.loc28
  127. // CHECK:STDOUT: %X.ref.loc28_25: type = name_ref X, file.%X.decl [template = constants.%X]
  128. // CHECK:STDOUT: assign %x.var, %Make.call
  129. // CHECK:STDOUT: return
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: