fail_init.carbon 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.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.carbon
  10. class Class {
  11. var a: i32;
  12. var b: i32;
  13. }
  14. fn F() {
  15. // CHECK:STDERR: fail_init.carbon:[[@LINE+4]]:3: ERROR: Cannot initialize class with 2 field(s) from struct with 1 field(s).
  16. // CHECK:STDERR: {.a = 1} as Class;
  17. // CHECK:STDERR: ^~~~~~~~
  18. // CHECK:STDERR:
  19. {.a = 1} as Class;
  20. // CHECK:STDERR: fail_init.carbon:[[@LINE+4]]:3: ERROR: Missing value for field `b` in struct initialization.
  21. // CHECK:STDERR: {.a = 1, .c = 2} as Class;
  22. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  23. // CHECK:STDERR:
  24. {.a = 1, .c = 2} as Class;
  25. // CHECK:STDERR: fail_init.carbon:[[@LINE+3]]:3: ERROR: Cannot initialize class with 2 field(s) from struct with 3 field(s).
  26. // CHECK:STDERR: {.a = 1, .b = 2, .c = 3} as Class;
  27. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  28. {.a = 1, .b = 2, .c = 3} as Class;
  29. }
  30. // CHECK:STDOUT: --- fail_init.carbon
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: constants {
  33. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  34. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  35. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  36. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  37. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, i32 [template]
  38. // CHECK:STDOUT: %.3: type = struct_type {.a: i32, .b: i32} [template]
  39. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  40. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  41. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  42. // CHECK:STDOUT: %.5: type = struct_type {.a: i32} [template]
  43. // CHECK:STDOUT: %.6: type = ptr_type %.3 [template]
  44. // CHECK:STDOUT: %.7: i32 = int_literal 2 [template]
  45. // CHECK:STDOUT: %.8: type = struct_type {.a: i32, .c: i32} [template]
  46. // CHECK:STDOUT: %.9: i32 = int_literal 3 [template]
  47. // CHECK:STDOUT: %.10: type = struct_type {.a: i32, .b: i32, .c: i32} [template]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: .Int32 = %import_ref
  53. // CHECK:STDOUT: import Core//prelude
  54. // CHECK:STDOUT: import Core//prelude/operators
  55. // CHECK:STDOUT: import Core//prelude/types
  56. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  57. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  58. // CHECK:STDOUT: import Core//prelude/operators/comparison
  59. // CHECK:STDOUT: import Core//prelude/types/bool
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: file {
  65. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  66. // CHECK:STDOUT: .Core = imports.%Core
  67. // CHECK:STDOUT: .Class = %Class.decl
  68. // CHECK:STDOUT: .F = %F.decl
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %Core.import = import Core
  71. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  72. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {}
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: class @Class {
  76. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  77. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  78. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  79. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  80. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  82. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  83. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl b, element1 [template]
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: !members:
  86. // CHECK:STDOUT: .Self = constants.%Class
  87. // CHECK:STDOUT: .a = %.loc12_8
  88. // CHECK:STDOUT: .b = %.loc13_8
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: fn @F() {
  94. // CHECK:STDOUT: !entry:
  95. // CHECK:STDOUT: %.loc21_9: i32 = int_literal 1 [template = constants.%.4]
  96. // CHECK:STDOUT: %.loc21_10.1: %.5 = struct_literal (%.loc21_9)
  97. // CHECK:STDOUT: %Class.ref.loc21: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  98. // CHECK:STDOUT: %.loc21_10.2: ref %Class = temporary_storage
  99. // CHECK:STDOUT: %.loc21_10.3: ref %Class = temporary %.loc21_10.2, <error>
  100. // CHECK:STDOUT: %.loc21_12: ref %Class = converted %.loc21_10.1, %.loc21_10.3
  101. // CHECK:STDOUT: %.loc26_9: i32 = int_literal 1 [template = constants.%.4]
  102. // CHECK:STDOUT: %.loc26_17: i32 = int_literal 2 [template = constants.%.7]
  103. // CHECK:STDOUT: %.loc26_18.1: %.8 = struct_literal (%.loc26_9, %.loc26_17)
  104. // CHECK:STDOUT: %Class.ref.loc26: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  105. // CHECK:STDOUT: %.loc26_18.2: ref %Class = temporary_storage
  106. // CHECK:STDOUT: %.loc26_18.3: ref i32 = class_element_access %.loc26_18.2, element0
  107. // CHECK:STDOUT: %.loc26_18.4: init i32 = initialize_from %.loc26_9 to %.loc26_18.3 [template = constants.%.4]
  108. // CHECK:STDOUT: %.loc26_18.5: ref %Class = temporary %.loc26_18.2, <error>
  109. // CHECK:STDOUT: %.loc26_20: ref %Class = converted %.loc26_18.1, %.loc26_18.5
  110. // CHECK:STDOUT: %.loc30_9: i32 = int_literal 1 [template = constants.%.4]
  111. // CHECK:STDOUT: %.loc30_17: i32 = int_literal 2 [template = constants.%.7]
  112. // CHECK:STDOUT: %.loc30_25: i32 = int_literal 3 [template = constants.%.9]
  113. // CHECK:STDOUT: %.loc30_26.1: %.10 = struct_literal (%.loc30_9, %.loc30_17, %.loc30_25)
  114. // CHECK:STDOUT: %Class.ref.loc30: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  115. // CHECK:STDOUT: %.loc30_26.2: ref %Class = temporary_storage
  116. // CHECK:STDOUT: %.loc30_26.3: ref %Class = temporary %.loc30_26.2, <error>
  117. // CHECK:STDOUT: %.loc30_28: ref %Class = converted %.loc30_26.1, %.loc30_26.3
  118. // CHECK:STDOUT: return
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: