|
| 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 | + |
| 14 | +// --- abstract.h |
| 15 | + |
| 16 | +struct A { |
| 17 | + virtual void f() = 0; |
| 18 | +}; |
| 19 | + |
| 20 | +// --- derive_base_from_abstract.carbon |
| 21 | + |
| 22 | +library "[[@TEST_NAME]]"; |
| 23 | + |
| 24 | +import Cpp library "abstract.h"; |
| 25 | + |
| 26 | +base class B { |
| 27 | + extend base: Cpp.A; |
| 28 | +} |
| 29 | + |
| 30 | +// --- todo_fail_derive_final_from_abstract.carbon |
| 31 | + |
| 32 | +library "[[@TEST_NAME]]"; |
| 33 | + |
| 34 | +import Cpp library "abstract.h"; |
| 35 | + |
| 36 | +class C { |
| 37 | + // TODO: We should reject this because `f` is not implemented. |
| 38 | + extend base: Cpp.A; |
| 39 | +} |
| 40 | + |
| 41 | +// --- fail_todo_impl_abstract_member.carbon |
| 42 | + |
| 43 | +library "[[@TEST_NAME]]"; |
| 44 | + |
| 45 | +import Cpp library "abstract.h"; |
| 46 | + |
| 47 | +class C { |
| 48 | + extend base: Cpp.A; |
| 49 | + // CHECK:STDERR: fail_todo_impl_abstract_member.carbon:[[@LINE+4]]:3: error: impl without compatible virtual in base class [ImplWithoutVirtualInBase] |
| 50 | + // CHECK:STDERR: impl fn f[addr self: Self*]() {} |
| 51 | + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 52 | + // CHECK:STDERR: |
| 53 | + impl fn f[addr self: Self*]() {} |
| 54 | +} |
| 55 | + |
| 56 | +// --- abstract_final.h |
| 57 | + |
| 58 | +// C++ allows a class to be both abstract and final. |
| 59 | +struct AbstractFinal final { |
| 60 | + virtual void f() = 0; |
| 61 | +}; |
| 62 | + |
| 63 | +// --- fail_derive_from_abstract_final.carbon |
| 64 | + |
| 65 | +library "[[@TEST_NAME]]"; |
| 66 | + |
| 67 | +import Cpp library "abstract_final.h"; |
| 68 | + |
| 69 | +class C { |
| 70 | + // 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] |
| 71 | + // CHECK:STDERR: extend base: Cpp.AbstractFinal; |
| 72 | + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~ |
| 73 | + // CHECK:STDERR: |
| 74 | + extend base: Cpp.AbstractFinal; |
| 75 | +} |
| 76 | + |
| 77 | +// --- use_abstract_final.carbon |
| 78 | + |
| 79 | +library "[[@TEST_NAME]]"; |
| 80 | + |
| 81 | +import Cpp library "abstract_final.h"; |
| 82 | + |
| 83 | +// We model an abstract final class as being final, rather than abstract. This |
| 84 | +// prevents inheritance from it, and there's no actual way to initialize an |
| 85 | +// instance of the type, but that does not prevent defining a function that |
| 86 | +// returns the type by value. Make sure that doesn't crash. |
| 87 | +//@dump-sem-ir-begin |
| 88 | +fn F() -> Cpp.AbstractFinal { |
| 89 | + return F(); |
| 90 | +} |
| 91 | +//@dump-sem-ir-end |
| 92 | + |
| 93 | +// CHECK:STDOUT: --- use_abstract_final.carbon |
| 94 | +// CHECK:STDOUT: |
| 95 | +// CHECK:STDOUT: constants { |
| 96 | +// CHECK:STDOUT: %AbstractFinal: type = class_type @AbstractFinal [concrete] |
| 97 | +// CHECK:STDOUT: %pattern_type.2e0: type = pattern_type %AbstractFinal [concrete] |
| 98 | +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] |
| 99 | +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] |
| 100 | +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] |
| 101 | +// CHECK:STDOUT: %T.as.Destroy.impl.Op.type.4da: type = fn_type @T.as.Destroy.impl.Op, @T.as.Destroy.impl(%AbstractFinal) [concrete] |
| 102 | +// CHECK:STDOUT: %T.as.Destroy.impl.Op.0fa: %T.as.Destroy.impl.Op.type.4da = struct_value () [concrete] |
| 103 | +// CHECK:STDOUT: %ptr.d4c: type = ptr_type %AbstractFinal [concrete] |
| 104 | +// CHECK:STDOUT: } |
| 105 | +// CHECK:STDOUT: |
| 106 | +// CHECK:STDOUT: imports { |
| 107 | +// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] { |
| 108 | +// CHECK:STDOUT: .AbstractFinal = %AbstractFinal.decl |
| 109 | +// CHECK:STDOUT: import Cpp//... |
| 110 | +// CHECK:STDOUT: } |
| 111 | +// CHECK:STDOUT: %AbstractFinal.decl: type = class_decl @AbstractFinal [concrete = constants.%AbstractFinal] {} {} |
| 112 | +// CHECK:STDOUT: } |
| 113 | +// CHECK:STDOUT: |
| 114 | +// CHECK:STDOUT: file { |
| 115 | +// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] { |
| 116 | +// CHECK:STDOUT: %return.patt: %pattern_type.2e0 = return_slot_pattern [concrete] |
| 117 | +// CHECK:STDOUT: %return.param_patt: %pattern_type.2e0 = out_param_pattern %return.patt, call_param0 [concrete] |
| 118 | +// CHECK:STDOUT: } { |
| 119 | +// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp] |
| 120 | +// CHECK:STDOUT: %AbstractFinal.ref: type = name_ref AbstractFinal, imports.%AbstractFinal.decl [concrete = constants.%AbstractFinal] |
| 121 | +// CHECK:STDOUT: %return.param: ref %AbstractFinal = out_param call_param0 |
| 122 | +// CHECK:STDOUT: %return: ref %AbstractFinal = return_slot %return.param |
| 123 | +// CHECK:STDOUT: } |
| 124 | +// CHECK:STDOUT: } |
| 125 | +// CHECK:STDOUT: |
| 126 | +// CHECK:STDOUT: fn @F() -> %return.param: %AbstractFinal { |
| 127 | +// CHECK:STDOUT: !entry: |
| 128 | +// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F] |
| 129 | +// CHECK:STDOUT: %.loc11: ref %AbstractFinal = splice_block %return {} |
| 130 | +// CHECK:STDOUT: %F.call: init %AbstractFinal = call %F.ref() to %.loc11 |
| 131 | +// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc11, constants.%T.as.Destroy.impl.Op.0fa |
| 132 | +// CHECK:STDOUT: <elided> |
| 133 | +// CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc11, %T.as.Destroy.impl.Op.specific_fn |
| 134 | +// CHECK:STDOUT: %addr: %ptr.d4c = addr_of %.loc11 |
| 135 | +// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method(%addr) |
| 136 | +// CHECK:STDOUT: return %F.call to %return |
| 137 | +// CHECK:STDOUT: } |
| 138 | +// CHECK:STDOUT: |
0 commit comments