Set correct C++ access type for fields and static vars (#7312)

This commit is contained in:
Nicholas Bishop
2026-06-05 20:07:27 +00:00
committed by GitHub
parent bf2ed6174d
commit 44b17ff436
3 changed files with 69 additions and 8 deletions
@@ -138,3 +138,29 @@ class A {
inline Cpp '''
Carbon::A x;
''';
// --- fail_private_field.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
fn Make() -> C {
return {.x = 123};
}
private var x: i32;
}
inline Cpp '''
int F() {
auto c = Carbon::C::Make();
// CHECK:STDERR: fail_private_field.carbon:[[@LINE+7]]:12: error: 'x' is a private member of 'Carbon::C' [CppInteropParseError]
// CHECK:STDERR: 22 | return c.x;
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_private_field.carbon:[[@LINE-9]]:16: note: implicitly declared private here [CppInteropParseNote]
// CHECK:STDERR: 9 | private var x: i32;
// CHECK:STDERR: | ^
// CHECK:STDERR:
return c.x;
}
''';
@@ -23,3 +23,24 @@ int F() {
return Carbon::C::x;
}
''';
// --- fail_static_private.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
private static var x: i32 = 123;
}
inline Cpp '''
int F() {
// CHECK:STDERR: fail_static_private.carbon:[[@LINE+7]]:21: error: 'x' is a private member of 'Carbon::C' [CppInteropParseError]
// CHECK:STDERR: 17 | return Carbon::C::x;
// CHECK:STDERR: | ^
// CHECK:STDERR: fail_static_private.carbon:[[@LINE-8]]:23: note: implicitly declared private here [CppInteropParseNote]
// CHECK:STDERR: 5 | private static var x: i32 = 123;
// CHECK:STDERR: | ^
// CHECK:STDERR:
return Carbon::C::x;
}
''';