Files
carbon-lang/toolchain/check/testdata/interop/cpp/class/export/access.carbon
T
Nicholas BishopandGeoff Romer 54728e47fe Handle ParameterizedClassType in ExportNameScopeToCpp (#8123)
This is a step towards being able to export methods of parameterized
classes. When exporting such a method, the decl context should be a
`ClassTemplateSpecializationDecl`. Thread the scope's specific ID down
from `MapInstIdToClangDeclOrType` and call `ExportClassSpecificToCpp` to
create this context.

Also add a `context.TODO` to prevent too much code from being exercised
with methods of parameterized classes after getting the parent scope, as
that would lead to various test crashes.

The user-visible effect of this change is that the somewhat mysterious
"TODO: non-class non-namespace name scope" error is replaced with "TODO:
support exporting member functions of parameterized classes".

---------

Co-authored-by: Geoff Romer <gromer@google.com>
2026-11-12 23:12:38 +00:00

179 lines
5.6 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/destroy.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/access.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/export/access.carbon
// --- fail_access_exported_private_var.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {}
class B {
fn Make() -> Self { return {}; }
private var a: A = {};
}
inline Cpp '''
void F() {
auto b = Carbon::B::Make();
// CHECK:STDERR: ╭─┤ fail_access_exported_private_var.carbon:[[@LINE+17]]:5
// CHECK:STDERR: │ private var a: A = {};
// CHECK:STDERR: · ┬
// CHECK:STDERR: · ╰──┤ implicitly declared private here [CppInteropParseNote]
// CHECK:STDERR: ┆
// CHECK:STDERR: -> │ b.a;
// CHECK:STDERR: · ┳
// CHECK:STDERR: · ┗━━┫ error: 'a' is a private member of 'Carbon::Main::B'
// CHECK:STDERR: · ┃ [CppInteropParseError]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
// CHECK:STDERR: ╭─┤ fail_access_exported_private_var.carbon:[[@LINE+6]]:5
// CHECK:STDERR: -> │ b.a;
// CHECK:STDERR: · ─ ┳
// CHECK:STDERR: · ┗━━┫ warning: expression result unused [CppInteropParseWarning]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
b.a;
}
''';
// --- fail_access_exported_private_method.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {}
class B {
fn Make() -> Self { return {}; }
private fn F() {}
}
inline Cpp '''
void G() {
auto b = Carbon::B::Make();
// CHECK:STDERR: ╭─┤ fail_access_exported_private_method.carbon:[[@LINE+11]]:5
// CHECK:STDERR: │ private fn F() {}
// CHECK:STDERR: · ┬
// CHECK:STDERR: · ╰──┤ implicitly declared private here [CppInteropParseNote]
// CHECK:STDERR: ┆
// CHECK:STDERR: -> │ b.F();
// CHECK:STDERR: · ┳
// CHECK:STDERR: · ┗━━┫ error: 'F' is a private member of 'Carbon::Main::B'
// CHECK:STDERR: · ┃ [CppInteropParseError]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
b.F();
}
''';
// --- fail_access_exported_private_var_in_generic.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {}
class B(T: type) {
private var a: A = {};
}
fn MakeB() -> B(A) {
return {};
}
inline Cpp '''
void F() {
auto b = Carbon::MakeB();
// CHECK:STDERR: ╭─┤ fail_access_exported_private_var_in_generic.carbon:[[@LINE+17]]:5
// CHECK:STDERR: │ private var a: A = {};
// CHECK:STDERR: · ┬
// CHECK:STDERR: · ╰──┤ implicitly declared private here [CppInteropParseNote]
// CHECK:STDERR: ┆
// CHECK:STDERR: -> │ b.a;
// CHECK:STDERR: · ┳
// CHECK:STDERR: · ┗━━┫ error: 'a' is a private member of 'Carbon::Main::B<Carbon::Main::A>'
// CHECK:STDERR: · ┃ [CppInteropParseError]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
// CHECK:STDERR: ╭─┤ fail_access_exported_private_var_in_generic.carbon:[[@LINE+6]]:5
// CHECK:STDERR: -> │ b.a;
// CHECK:STDERR: · ─ ┳
// CHECK:STDERR: · ┗━━┫ warning: expression result unused [CppInteropParseWarning]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
b.a;
}
''';
// --- fail_todo_access_exported_private_method_in_generic.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {}
class B(T: type) {
// TODO: exporting of methods in generics is not yet supported.
// CHECK:STDERR: ╭─┤ fail_todo_access_exported_private_method_in_generic.carbon:[[@LINE+8]]:3
// CHECK:STDERR: -> │ private fn F(unused self) {}
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: semantics TODO:
// CHECK:STDERR: · ┃ `support exporting member functions of parameterized classes`
// CHECK:STDERR: · ┃ [SemanticsTodo]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
private fn F(unused self) {}
}
fn MakeB() -> B(A) {
return {};
}
inline Cpp '''
void F() {
auto b = Carbon::MakeB();
// TODO: this error should be that `F` is a private member of `B`.
// CHECK:STDERR: ╭─┤ fail_todo_access_exported_private_method_in_generic.carbon:[[@LINE+7]]:5
// CHECK:STDERR: -> │ b.F();
// CHECK:STDERR: · ─ ┳
// CHECK:STDERR: · ┗━━┫ error: no member named 'F' in 'Carbon::Main::B<Carbon::Main::A>'
// CHECK:STDERR: · ┃ [CppInteropParseError]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
b.F();
}
''';
// --- access_exported_private_member_of_friend.carbon
library "[[@TEST_NAME]]";
import Cpp;
inline Cpp '''
class C;
''';
alias C = Cpp.C;
class A {}
class B {
friend C;
fn Make() -> Self { return {}; }
private fn F() {}
}
inline Cpp '''
class C {
void G() {
auto b = Carbon::B::Make();
b.F();
}
};
''';