Files
carbon-lang/toolchain/check/testdata/interop/cpp/class/export/method.carbon
T
Nicholas Bishop 33bd953a2e Deduplicate class export and fix identifier error in ExportNameScopeToCpp (#7550)
Added method_alias.carbon test so that the class export code in
`ExportNameScopeToCpp` is tested. Refactored `ExportClassToCpp` so that
`ExportNameScopeToCpp` can reuse that code.

Moved the `identifier_info` code in `ExportNameScopeToCpp` into the
namespace block, because the name scope's name ID is not valid for
classes.

Added a call to `CompleteType` for classes exported via
`ExportNameScopeToCpp`, otherwise a "queried property of class with no
definition" assert is later reached (when adding methods) in the call
chain `BuildCppToCarbonThunkDecl` -> `DeclContext::addHiddenDecl` ->
`CXXRecordDecl::addedMember` -> `CXXRecordDecl::data`.
2026-07-22 18:02:44 +00:00

105 lines
2.0 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/export/method.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/export/method.carbon
// --- static_method.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M();
}
inline Cpp '''
void F() {
Carbon::C::M();
}
''';
// --- method_lvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M(self);
}
inline Cpp '''
void F() {
Carbon::C c;
c.M();
}
''';
// --- method_rvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M(self);
}
inline Cpp '''
void F() {
Carbon::C().M();
}
''';
// --- ref_method_lvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M(ref self);
}
inline Cpp '''
void F() {
Carbon::C c;
c.M();
}
''';
// --- fail_ref_method_rvalue.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn M(ref self);
}
inline Cpp '''
void F() {
// CHECK:STDERR: fail_ref_method_rvalue.carbon:[[@LINE+7]]:3: error: 'this' argument to member function 'M' is an rvalue, but function has non-const lvalue ref-qualifier [CppInteropParseError]
// CHECK:STDERR: 17 | Carbon::C().M();
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_ref_method_rvalue.carbon:[[@LINE-8]]:17: note: 'M' declared here [CppInteropParseNote]
// CHECK:STDERR: 5 | fn M(ref self);
// CHECK:STDERR: | ^
// CHECK:STDERR:
Carbon::C().M();
}
''';
// --- method_alias.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {
fn F();
}
alias G = A.F;
inline Cpp '''
void call() { Carbon::G(); }
''';