fail_init_as_inplace.carbon 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/class/fail_init_as_inplace.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_init_as_inplace.carbon
  10. class Class {
  11. var a: i32;
  12. var b: i32;
  13. }
  14. fn G(p: Class*);
  15. fn F() {
  16. // TODO: This case should presumably work: `{...} as Class` should be an
  17. // initializing expression, not a value expression.
  18. //
  19. // CHECK:STDERR: fail_init_as_inplace.carbon:[[@LINE+3]]:18: error: cannot copy value of type `Class`
  20. // CHECK:STDERR: var c: Class = {.a = 1, .b = 2} as Class;
  21. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  22. var c: Class = {.a = 1, .b = 2} as Class;
  23. G(&c);
  24. }
  25. // CHECK:STDOUT: --- fail_init_as_inplace.carbon
  26. // CHECK:STDOUT:
  27. // CHECK:STDOUT: constants {
  28. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  29. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  30. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  31. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  32. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, i32 [template]
  33. // CHECK:STDOUT: %.3: type = struct_type {.a: i32, .b: i32} [template]
  34. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  35. // CHECK:STDOUT: %.5: type = ptr_type %Class [template]
  36. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  37. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  38. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  39. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  40. // CHECK:STDOUT: %.6: type = ptr_type %.3 [template]
  41. // CHECK:STDOUT: %.7: i32 = int_literal 1 [template]
  42. // CHECK:STDOUT: %.8: i32 = int_literal 2 [template]
  43. // CHECK:STDOUT: %struct: %Class = struct_value (%.7, %.8) [template]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: imports {
  47. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  48. // CHECK:STDOUT: .Int32 = %import_ref
  49. // CHECK:STDOUT: import Core//prelude
  50. // CHECK:STDOUT: import Core//prelude/operators
  51. // CHECK:STDOUT: import Core//prelude/types
  52. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  53. // CHECK:STDOUT: import Core//prelude/operators/as
  54. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  55. // CHECK:STDOUT: import Core//prelude/operators/comparison
  56. // CHECK:STDOUT: import Core//prelude/types/bool
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: file {
  62. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  63. // CHECK:STDOUT: .Core = imports.%Core
  64. // CHECK:STDOUT: .Class = %Class.decl
  65. // CHECK:STDOUT: .G = %G.decl
  66. // CHECK:STDOUT: .F = %F.decl
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %Core.import = import Core
  69. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  70. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  71. // CHECK:STDOUT: %p.patt: %.5 = binding_pattern p
  72. // CHECK:STDOUT: } {
  73. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  74. // CHECK:STDOUT: %.loc16: type = ptr_type %Class [template = constants.%.5]
  75. // CHECK:STDOUT: %p.param: %.5 = param p, runtime_param0
  76. // CHECK:STDOUT: %p: %.5 = bind_name p, %p.param
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: class @Class {
  82. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  83. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  84. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  85. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  86. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  87. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  88. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  89. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl b, element1 [template]
  90. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.3 [template = constants.%.4]
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: !members:
  93. // CHECK:STDOUT: .Self = constants.%Class
  94. // CHECK:STDOUT: .a = %.loc12_8
  95. // CHECK:STDOUT: .b = %.loc13_8
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: fn @G(%p: %.5);
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: fn @F() {
  103. // CHECK:STDOUT: !entry:
  104. // CHECK:STDOUT: %Class.ref.loc25_10: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  105. // CHECK:STDOUT: %c.var: ref %Class = var c
  106. // CHECK:STDOUT: %c: ref %Class = bind_name c, %c.var
  107. // CHECK:STDOUT: %.loc25_24: i32 = int_literal 1 [template = constants.%.7]
  108. // CHECK:STDOUT: %.loc25_32: i32 = int_literal 2 [template = constants.%.8]
  109. // CHECK:STDOUT: %.loc25_33.1: %.3 = struct_literal (%.loc25_24, %.loc25_32)
  110. // CHECK:STDOUT: %Class.ref.loc25_38: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  111. // CHECK:STDOUT: %.loc25_33.2: ref %Class = temporary_storage
  112. // CHECK:STDOUT: %.loc25_33.3: ref i32 = class_element_access %.loc25_33.2, element0
  113. // CHECK:STDOUT: %.loc25_33.4: init i32 = initialize_from %.loc25_24 to %.loc25_33.3 [template = constants.%.7]
  114. // CHECK:STDOUT: %.loc25_33.5: ref i32 = class_element_access %.loc25_33.2, element1
  115. // CHECK:STDOUT: %.loc25_33.6: init i32 = initialize_from %.loc25_32 to %.loc25_33.5 [template = constants.%.8]
  116. // CHECK:STDOUT: %.loc25_33.7: init %Class = class_init (%.loc25_33.4, %.loc25_33.6), %.loc25_33.2 [template = constants.%struct]
  117. // CHECK:STDOUT: %.loc25_33.8: ref %Class = temporary %.loc25_33.2, %.loc25_33.7
  118. // CHECK:STDOUT: %.loc25_35.1: ref %Class = converted %.loc25_33.1, %.loc25_33.8
  119. // CHECK:STDOUT: %.loc25_35.2: %Class = bind_value %.loc25_35.1
  120. // CHECK:STDOUT: assign %c.var, <error>
  121. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  122. // CHECK:STDOUT: %c.ref: ref %Class = name_ref c, %c
  123. // CHECK:STDOUT: %.loc26: %.5 = addr_of %c.ref
  124. // CHECK:STDOUT: %G.call: init %.1 = call %G.ref(%.loc26)
  125. // CHECK:STDOUT: return
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: