fail_init.carbon 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_init.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_init.carbon
  12. class Class {
  13. var a: i32;
  14. var b: i32;
  15. }
  16. fn F() {
  17. // CHECK:STDERR: fail_init.carbon:[[@LINE+4]]:3: error: cannot initialize class with 2 fields from struct with 1 field [StructInitElementCountMismatch]
  18. // CHECK:STDERR: {.a = 1} as Class;
  19. // CHECK:STDERR: ^~~~~~~~
  20. // CHECK:STDERR:
  21. {.a = 1} as Class;
  22. // CHECK:STDERR: fail_init.carbon:[[@LINE+4]]:3: error: missing value for field `b` in struct initialization [StructInitMissingFieldInLiteral]
  23. // CHECK:STDERR: {.a = 1, .c = 2} as Class;
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. {.a = 1, .c = 2} as Class;
  27. // CHECK:STDERR: fail_init.carbon:[[@LINE+4]]:3: error: cannot initialize class with 2 fields from struct with 3 fields [StructInitElementCountMismatch]
  28. // CHECK:STDERR: {.a = 1, .b = 2, .c = 3} as Class;
  29. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  30. // CHECK:STDERR:
  31. {.a = 1, .b = 2, .c = 3} as Class;
  32. }