non_aggregate_init.carbon 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/primitives.carbon
  6. // EXTRA-ARGS: --target=x86_64-linux-gnu --clang-arg=-std=c++20
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/non_aggregate_init.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/non_aggregate_init.carbon
  13. // ============================================================================
  14. // Non-aggregate class should not be initializable from {}
  15. // ============================================================================
  16. // --- non_aggregate.h
  17. // A class with a user-declared constructor is not an aggregate.
  18. struct X {
  19. X();
  20. };
  21. // --- fail_non_aggregate_init.carbon
  22. library "[[@TEST_NAME]]";
  23. import Cpp library "non_aggregate.h";
  24. fn Make() {
  25. // CHECK:STDERR: fail_non_aggregate_init.carbon:[[@LINE+7]]:3: error: cannot implicitly convert expression of type `{}` to `Cpp.X` [ConversionFailure]
  26. // CHECK:STDERR: var _: Cpp.X = {};
  27. // CHECK:STDERR: ^~~~~~~~~~~~
  28. // CHECK:STDERR: fail_non_aggregate_init.carbon:[[@LINE+4]]:3: note: type `{}` does not implement interface `Core.ImplicitAs(Cpp.X)` [MissingImplInMemberAccessInContext]
  29. // CHECK:STDERR: var _: Cpp.X = {};
  30. // CHECK:STDERR: ^~~~~~~~~~~~
  31. // CHECK:STDERR:
  32. var _: Cpp.X = {};
  33. }
  34. // ============================================================================
  35. // Aggregate class should be initializable from {}
  36. // ============================================================================
  37. // --- aggregate.h
  38. // A class with no user-declared constructors is an aggregate.
  39. struct Y {};
  40. // --- aggregate_init.carbon
  41. library "[[@TEST_NAME]]";
  42. import Cpp library "aggregate.h";
  43. fn Make() {
  44. // This should succeed because Y is an aggregate.
  45. var _: Cpp.Y = {};
  46. }