abstract.carbon 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/destroy.carbon
  6. // EXTRA-ARGS: --clang-arg=-Wno-abstract-final-class
  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/abstract.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/abstract.carbon
  13. // --- abstract.h
  14. struct A {
  15. virtual void f() = 0;
  16. };
  17. // --- derive_base_from_abstract.carbon
  18. library "[[@TEST_NAME]]";
  19. import Cpp library "abstract.h";
  20. base class B {
  21. extend base: Cpp.A;
  22. }
  23. // --- todo_fail_derive_final_from_abstract.carbon
  24. library "[[@TEST_NAME]]";
  25. import Cpp library "abstract.h";
  26. class C {
  27. // TODO: We should reject this because `f` is not implemented.
  28. extend base: Cpp.A;
  29. }
  30. // --- fail_todo_impl_abstract_member.carbon
  31. library "[[@TEST_NAME]]";
  32. import Cpp library "abstract.h";
  33. class C {
  34. extend base: Cpp.A;
  35. // CHECK:STDERR: fail_todo_impl_abstract_member.carbon:[[@LINE+4]]:3: error: override without compatible virtual in base class [OverrideWithoutVirtualInBase]
  36. // CHECK:STDERR: override fn f[ref self: Self]() {}
  37. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. // CHECK:STDERR:
  39. override fn f[ref self: Self]() {}
  40. }
  41. // --- abstract_final.h
  42. // C++ allows a class to be both abstract and final.
  43. struct AbstractFinal final {
  44. virtual void f() = 0;
  45. };
  46. // --- fail_derive_from_abstract_final.carbon
  47. library "[[@TEST_NAME]]";
  48. import Cpp library "abstract_final.h";
  49. class C {
  50. // CHECK:STDERR: fail_derive_from_abstract_final.carbon:[[@LINE+4]]:16: error: deriving from final type `AbstractFinal`; base type must be an `abstract` or `base` class [BaseIsFinal]
  51. // CHECK:STDERR: extend base: Cpp.AbstractFinal;
  52. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  53. // CHECK:STDERR:
  54. extend base: Cpp.AbstractFinal;
  55. }
  56. // --- use_abstract_final.carbon
  57. library "[[@TEST_NAME]]";
  58. import Cpp library "abstract_final.h";
  59. // We model an abstract final class as being final, rather than abstract. This
  60. // prevents inheritance from it, and there's no actual way to initialize an
  61. // instance of the type, but that does not prevent defining a function that
  62. // returns the type by value. Make sure that doesn't crash.
  63. //@dump-sem-ir-begin
  64. fn F() -> Cpp.AbstractFinal {
  65. return F();
  66. }
  67. //@dump-sem-ir-end
  68. // CHECK:STDOUT: --- use_abstract_final.carbon
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: constants {
  71. // CHECK:STDOUT: %AbstractFinal: type = class_type @AbstractFinal [concrete]
  72. // CHECK:STDOUT: %pattern_type: type = pattern_type %AbstractFinal [concrete]
  73. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  74. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: imports {
  78. // CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
  79. // CHECK:STDOUT: .AbstractFinal = %AbstractFinal.decl
  80. // CHECK:STDOUT: import Cpp//...
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %AbstractFinal.decl: type = class_decl @AbstractFinal [concrete = constants.%AbstractFinal] {} {}
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: file {
  86. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  87. // CHECK:STDOUT: %return.patt: %pattern_type = return_slot_pattern [concrete]
  88. // CHECK:STDOUT: %return.param_patt: %pattern_type = out_param_pattern %return.patt, call_param0 [concrete]
  89. // CHECK:STDOUT: } {
  90. // CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  91. // CHECK:STDOUT: %AbstractFinal.ref: type = name_ref AbstractFinal, imports.%AbstractFinal.decl [concrete = constants.%AbstractFinal]
  92. // CHECK:STDOUT: %return.param: ref %AbstractFinal = out_param call_param0
  93. // CHECK:STDOUT: %return: ref %AbstractFinal = return_slot %return.param
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: fn @F() -> %return.param: %AbstractFinal {
  98. // CHECK:STDOUT: !entry:
  99. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  100. // CHECK:STDOUT: %.loc11: ref %AbstractFinal = splice_block %return {}
  101. // CHECK:STDOUT: %F.call: init %AbstractFinal = call %F.ref() to %.loc11
  102. // CHECK:STDOUT: return %F.call to %return
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: