fail_init.carbon 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 fields from struct with 1 field [StructInitElementCountMismatch]
  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 [StructInitMissingFieldInLiteral]
  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+4]]:3: error: cannot initialize class with 2 fields from struct with 3 fields [StructInitElementCountMismatch]
  26. // CHECK:STDERR: {.a = 1, .b = 2, .c = 3} as Class;
  27. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  28. // CHECK:STDERR:
  29. {.a = 1, .b = 2, .c = 3} as Class;
  30. }
  31. // CHECK:STDOUT: --- fail_init.carbon
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: constants {
  34. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  35. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  36. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  37. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [template]
  38. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template]
  39. // CHECK:STDOUT: %complete_type.705: <witness> = complete_type_witness %struct_type.a.b [template]
  40. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  41. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  42. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  43. // CHECK:STDOUT: %struct_type.a: type = struct_type {.a: Core.IntLiteral} [template]
  44. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template]
  45. // CHECK:STDOUT: %struct_type.a.c: type = struct_type {.a: Core.IntLiteral, .c: Core.IntLiteral} [template]
  46. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  47. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%import_ref.a5b), @impl.1(%int_32) [template]
  48. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  49. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  50. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  51. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  52. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  53. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template]
  54. // CHECK:STDOUT: %struct_type.a.b.c: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral, .c: Core.IntLiteral} [template]
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: imports {
  58. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  59. // CHECK:STDOUT: .Int = %import_ref.485
  60. // CHECK:STDOUT: .ImplicitAs = %import_ref.d44
  61. // CHECK:STDOUT: import Core//prelude
  62. // CHECK:STDOUT: import Core//prelude/...
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: file {
  67. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  68. // CHECK:STDOUT: .Core = imports.%Core
  69. // CHECK:STDOUT: .Class = %Class.decl
  70. // CHECK:STDOUT: .F = %F.decl
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Core.import = import Core
  73. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  74. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: class @Class {
  78. // CHECK:STDOUT: %.loc12_8: %Class.elem = field_decl a, element0 [template]
  79. // CHECK:STDOUT: name_binding_decl {
  80. // CHECK:STDOUT: %.loc12_3: %Class.elem = var_pattern %.loc12_8
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %.var.loc12: ref %Class.elem = var <invalid>
  83. // CHECK:STDOUT: %.loc13_8: %Class.elem = field_decl b, element1 [template]
  84. // CHECK:STDOUT: name_binding_decl {
  85. // CHECK:STDOUT: %.loc13_3: %Class.elem = var_pattern %.loc13_8
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: %.var.loc13: ref %Class.elem = var <invalid>
  88. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.705]
  89. // CHECK:STDOUT: complete_type_witness = %complete_type
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: !members:
  92. // CHECK:STDOUT: .Self = constants.%Class
  93. // CHECK:STDOUT: .a = %.loc12_8
  94. // CHECK:STDOUT: .b = %.loc13_8
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: fn @F() {
  98. // CHECK:STDOUT: !entry:
  99. // CHECK:STDOUT: %int_1.loc21: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  100. // CHECK:STDOUT: %.loc21_10.1: %struct_type.a = struct_literal (%int_1.loc21)
  101. // CHECK:STDOUT: %Class.ref.loc21: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  102. // CHECK:STDOUT: %.loc21_10.2: ref %Class = temporary_storage
  103. // CHECK:STDOUT: %.loc21_10.3: ref %Class = temporary %.loc21_10.2, <error>
  104. // CHECK:STDOUT: %.loc21_12: ref %Class = converted %.loc21_10.1, %.loc21_10.3
  105. // CHECK:STDOUT: %int_1.loc26: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  106. // CHECK:STDOUT: %int_2.loc26: Core.IntLiteral = int_value 2 [template = constants.%int_2]
  107. // CHECK:STDOUT: %.loc26_18.1: %struct_type.a.c = struct_literal (%int_1.loc26, %int_2.loc26)
  108. // CHECK:STDOUT: %Class.ref.loc26: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  109. // CHECK:STDOUT: %impl.elem0: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  110. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.loc26, %impl.elem0 [template = constants.%Convert.bound]
  111. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  112. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1.loc26) [template = constants.%int_1.5d2]
  113. // CHECK:STDOUT: %.loc26_18.2: init %i32 = converted %int_1.loc26, %int.convert_checked [template = constants.%int_1.5d2]
  114. // CHECK:STDOUT: %.loc26_18.3: ref %Class = temporary_storage
  115. // CHECK:STDOUT: %.loc26_18.4: ref %i32 = class_element_access %.loc26_18.3, element0
  116. // CHECK:STDOUT: %.loc26_18.5: init %i32 = initialize_from %.loc26_18.2 to %.loc26_18.4 [template = constants.%int_1.5d2]
  117. // CHECK:STDOUT: %.loc26_18.6: ref %Class = temporary %.loc26_18.3, <error>
  118. // CHECK:STDOUT: %.loc26_20: ref %Class = converted %.loc26_18.1, %.loc26_18.6
  119. // CHECK:STDOUT: %int_1.loc31: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  120. // CHECK:STDOUT: %int_2.loc31: Core.IntLiteral = int_value 2 [template = constants.%int_2]
  121. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3]
  122. // CHECK:STDOUT: %.loc31_26.1: %struct_type.a.b.c = struct_literal (%int_1.loc31, %int_2.loc31, %int_3)
  123. // CHECK:STDOUT: %Class.ref.loc31: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  124. // CHECK:STDOUT: %.loc31_26.2: ref %Class = temporary_storage
  125. // CHECK:STDOUT: %.loc31_26.3: ref %Class = temporary %.loc31_26.2, <error>
  126. // CHECK:STDOUT: %.loc31_28: ref %Class = converted %.loc31_26.1, %.loc31_26.3
  127. // CHECK:STDOUT: return
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: