fail_init.carbon 7.4 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 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+3]]: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. {.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: %int_32: Core.IntLiteral = int_value 32 [template]
  35. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  36. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [template]
  37. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [template]
  38. // CHECK:STDOUT: %complete_type.e46: <witness> = complete_type_witness %struct_type.a.b [template]
  39. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  40. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  41. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  42. // CHECK:STDOUT: %struct_type.a: type = struct_type {.a: Core.IntLiteral} [template]
  43. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template]
  44. // CHECK:STDOUT: %struct_type.a.c: type = struct_type {.a: Core.IntLiteral, .c: Core.IntLiteral} [template]
  45. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  46. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.1(%int_32) [template]
  47. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  48. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  49. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.5b8, %Convert.925 [template]
  50. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  51. // CHECK:STDOUT: %int_1.c60: %i32 = int_value 1 [template]
  52. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template]
  53. // CHECK:STDOUT: %struct_type.a.b.c: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral, .c: Core.IntLiteral} [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  58. // CHECK:STDOUT: .Int = %import_ref.187
  59. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  60. // CHECK:STDOUT: import Core//prelude
  61. // CHECK:STDOUT: import Core//prelude/...
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  67. // CHECK:STDOUT: .Core = imports.%Core
  68. // CHECK:STDOUT: .Class = %Class.decl
  69. // CHECK:STDOUT: .F = %F.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.import = import Core
  72. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  73. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: class @Class {
  77. // CHECK:STDOUT: %.loc12: %Class.elem = field_decl a, element0 [template]
  78. // CHECK:STDOUT: %.loc13: %Class.elem = field_decl b, element1 [template]
  79. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b [template = constants.%complete_type.e46]
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: !members:
  82. // CHECK:STDOUT: .Self = constants.%Class
  83. // CHECK:STDOUT: .a = %.loc12
  84. // CHECK:STDOUT: .b = %.loc13
  85. // CHECK:STDOUT: complete_type_witness = %complete_type
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: fn @F() {
  89. // CHECK:STDOUT: !entry:
  90. // CHECK:STDOUT: %int_1.loc21: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  91. // CHECK:STDOUT: %.loc21_10.1: %struct_type.a = struct_literal (%int_1.loc21)
  92. // CHECK:STDOUT: %Class.ref.loc21: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  93. // CHECK:STDOUT: %.loc21_10.2: ref %Class = temporary_storage
  94. // CHECK:STDOUT: %.loc21_10.3: ref %Class = temporary %.loc21_10.2, <error>
  95. // CHECK:STDOUT: %.loc21_12: ref %Class = converted %.loc21_10.1, %.loc21_10.3
  96. // CHECK:STDOUT: %int_1.loc26: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  97. // CHECK:STDOUT: %int_2.loc26: Core.IntLiteral = int_value 2 [template = constants.%int_2]
  98. // CHECK:STDOUT: %.loc26_18.1: %struct_type.a.c = struct_literal (%int_1.loc26, %int_2.loc26)
  99. // CHECK:STDOUT: %Class.ref.loc26: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  100. // CHECK:STDOUT: %impl.elem0: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  101. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.loc26, %impl.elem0 [template = constants.%Convert.bound]
  102. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  103. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1.loc26) [template = constants.%int_1.c60]
  104. // CHECK:STDOUT: %.loc26_18.2: init %i32 = converted %int_1.loc26, %int.convert_checked [template = constants.%int_1.c60]
  105. // CHECK:STDOUT: %.loc26_18.3: ref %Class = temporary_storage
  106. // CHECK:STDOUT: %.loc26_18.4: ref %i32 = class_element_access %.loc26_18.3, element0
  107. // CHECK:STDOUT: %.loc26_18.5: init %i32 = initialize_from %.loc26_18.2 to %.loc26_18.4 [template = constants.%int_1.c60]
  108. // CHECK:STDOUT: %.loc26_18.6: ref %Class = temporary %.loc26_18.3, <error>
  109. // CHECK:STDOUT: %.loc26_20: ref %Class = converted %.loc26_18.1, %.loc26_18.6
  110. // CHECK:STDOUT: %int_1.loc30: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  111. // CHECK:STDOUT: %int_2.loc30: Core.IntLiteral = int_value 2 [template = constants.%int_2]
  112. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3]
  113. // CHECK:STDOUT: %.loc30_26.1: %struct_type.a.b.c = struct_literal (%int_1.loc30, %int_2.loc30, %int_3)
  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: