fail_init.carbon 8.2 KB

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