tuple.carbon 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/no_prelude/tuple.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/no_prelude/tuple.carbon
  10. class X {
  11. // ...
  12. }
  13. fn Make() -> X;
  14. fn Let() {
  15. // This should create value bindings for both tuple elements.
  16. let a: (X, X) = (Make(), Make()) as (X, X);
  17. }
  18. fn Var() {
  19. // This should initialize both tuple elements in place.
  20. var b: (X, X) = (Make(), Make()) as (X, X);
  21. }
  22. // CHECK:STDOUT: --- tuple.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %X: type = class_type @X [template]
  26. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  27. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  28. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  29. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  30. // CHECK:STDOUT: %Let.type: type = fn_type @Let [template]
  31. // CHECK:STDOUT: %Let: %Let.type = struct_value () [template]
  32. // CHECK:STDOUT: %.3: type = tuple_type (type, type) [template]
  33. // CHECK:STDOUT: %.4: type = tuple_type (%X, %X) [template]
  34. // CHECK:STDOUT: %.5: type = ptr_type %.1 [template]
  35. // CHECK:STDOUT: %.6: type = tuple_type (%.5, %.5) [template]
  36. // CHECK:STDOUT: %.7: type = ptr_type %.6 [template]
  37. // CHECK:STDOUT: %Var.type: type = fn_type @Var [template]
  38. // CHECK:STDOUT: %Var: %Var.type = struct_value () [template]
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT:
  41. // CHECK:STDOUT: file {
  42. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  43. // CHECK:STDOUT: .X = %X.decl
  44. // CHECK:STDOUT: .Make = %Make.decl
  45. // CHECK:STDOUT: .Let = %Let.decl
  46. // CHECK:STDOUT: .Var = %Var.decl
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT: %X.decl: type = class_decl @X [template = constants.%X] {}
  49. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  50. // CHECK:STDOUT: %X.ref: type = name_ref X, %X.decl [template = constants.%X]
  51. // CHECK:STDOUT: @Make.%return: ref %X = var <return slot>
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Let.decl: %Let.type = fn_decl @Let [template = constants.%Let] {}
  54. // CHECK:STDOUT: %Var.decl: %Var.type = fn_decl @Var [template = constants.%Var] {}
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: class @X {
  58. // CHECK:STDOUT: !members:
  59. // CHECK:STDOUT: .Self = constants.%X
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: fn @Make() -> %X;
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: fn @Let() {
  65. // CHECK:STDOUT: !entry:
  66. // CHECK:STDOUT: %X.ref.loc19_11: type = name_ref X, file.%X.decl [template = constants.%X]
  67. // CHECK:STDOUT: %X.ref.loc19_14: type = name_ref X, file.%X.decl [template = constants.%X]
  68. // CHECK:STDOUT: %.loc19_15.1: %.3 = tuple_literal (%X.ref.loc19_11, %X.ref.loc19_14)
  69. // CHECK:STDOUT: %.loc19_15.2: type = converted %.loc19_15.1, constants.%.4 [template = constants.%.4]
  70. // CHECK:STDOUT: %Make.ref.loc19_20: %Make.type = name_ref Make, file.%Make.decl [template = constants.%Make]
  71. // CHECK:STDOUT: %.loc19_24.1: ref %X = temporary_storage
  72. // CHECK:STDOUT: %Make.call.loc19_24: init %X = call %Make.ref.loc19_20() to %.loc19_24.1
  73. // CHECK:STDOUT: %Make.ref.loc19_28: %Make.type = name_ref Make, file.%Make.decl [template = constants.%Make]
  74. // CHECK:STDOUT: %.loc19_32.1: ref %X = temporary_storage
  75. // CHECK:STDOUT: %Make.call.loc19_32: init %X = call %Make.ref.loc19_28() to %.loc19_32.1
  76. // CHECK:STDOUT: %.loc19_34: %.4 = tuple_literal (%Make.call.loc19_24, %Make.call.loc19_32)
  77. // CHECK:STDOUT: %X.ref.loc19_40: type = name_ref X, file.%X.decl [template = constants.%X]
  78. // CHECK:STDOUT: %X.ref.loc19_43: type = name_ref X, file.%X.decl [template = constants.%X]
  79. // CHECK:STDOUT: %.loc19_44.1: %.3 = tuple_literal (%X.ref.loc19_40, %X.ref.loc19_43)
  80. // CHECK:STDOUT: %.loc19_44.2: type = converted %.loc19_44.1, constants.%.4 [template = constants.%.4]
  81. // CHECK:STDOUT: %.loc19_24.2: ref %X = temporary %.loc19_24.1, %Make.call.loc19_24
  82. // CHECK:STDOUT: %.loc19_24.3: %X = bind_value %.loc19_24.2
  83. // CHECK:STDOUT: %.loc19_32.2: ref %X = temporary %.loc19_32.1, %Make.call.loc19_32
  84. // CHECK:STDOUT: %.loc19_32.3: %X = bind_value %.loc19_32.2
  85. // CHECK:STDOUT: %tuple: %.4 = tuple_value (%.loc19_24.3, %.loc19_32.3)
  86. // CHECK:STDOUT: %.loc19_45: %.4 = converted %.loc19_34, %tuple
  87. // CHECK:STDOUT: %a: %.4 = bind_name a, %.loc19_45
  88. // CHECK:STDOUT: return
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: fn @Var() {
  92. // CHECK:STDOUT: !entry:
  93. // CHECK:STDOUT: %X.ref.loc24_11: type = name_ref X, file.%X.decl [template = constants.%X]
  94. // CHECK:STDOUT: %X.ref.loc24_14: type = name_ref X, file.%X.decl [template = constants.%X]
  95. // CHECK:STDOUT: %.loc24_15.1: %.3 = tuple_literal (%X.ref.loc24_11, %X.ref.loc24_14)
  96. // CHECK:STDOUT: %.loc24_15.2: type = converted %.loc24_15.1, constants.%.4 [template = constants.%.4]
  97. // CHECK:STDOUT: %b.var: ref %.4 = var b
  98. // CHECK:STDOUT: %b: ref %.4 = bind_name b, %b.var
  99. // CHECK:STDOUT: %Make.ref.loc24_20: %Make.type = name_ref Make, file.%Make.decl [template = constants.%Make]
  100. // CHECK:STDOUT: %.loc24_34.1: ref %X = tuple_access %b.var, element0
  101. // CHECK:STDOUT: %Make.call.loc24_24: init %X = call %Make.ref.loc24_20() to %.loc24_34.1
  102. // CHECK:STDOUT: %Make.ref.loc24_28: %Make.type = name_ref Make, file.%Make.decl [template = constants.%Make]
  103. // CHECK:STDOUT: %.loc24_34.2: ref %X = tuple_access %b.var, element1
  104. // CHECK:STDOUT: %Make.call.loc24_32: init %X = call %Make.ref.loc24_28() to %.loc24_34.2
  105. // CHECK:STDOUT: %.loc24_34.3: %.4 = tuple_literal (%Make.call.loc24_24, %Make.call.loc24_32)
  106. // CHECK:STDOUT: %X.ref.loc24_40: type = name_ref X, file.%X.decl [template = constants.%X]
  107. // CHECK:STDOUT: %X.ref.loc24_43: type = name_ref X, file.%X.decl [template = constants.%X]
  108. // CHECK:STDOUT: %.loc24_44.1: %.3 = tuple_literal (%X.ref.loc24_40, %X.ref.loc24_43)
  109. // CHECK:STDOUT: %.loc24_44.2: type = converted %.loc24_44.1, constants.%.4 [template = constants.%.4]
  110. // CHECK:STDOUT: %.loc24_34.4: init %.4 = tuple_init (%Make.call.loc24_24, %Make.call.loc24_32) to %b.var
  111. // CHECK:STDOUT: %.loc24_45: init %.4 = converted %.loc24_34.3, %.loc24_34.4
  112. // CHECK:STDOUT: assign %b.var, %.loc24_45
  113. // CHECK:STDOUT: return
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: