Override Clang class layout for Carbon class types. (#7071)

Use the Carbon-determined size and alignment for Carbon-defined classes,
rather than allowing Clang to work one out for itself using the C++
rules.
This commit is contained in:
Richard Smith
2026-04-17 00:03:29 +00:00
committed by GitHub
parent e7626f46cc
commit f91990aa87
5 changed files with 352 additions and 166 deletions
@@ -97,24 +97,3 @@ inline Cpp '''
// CHECK:STDERR:
Carbon::T t;
''';
// --- fail_todo_fields.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {
var a: i32;
var b: i32;
}
inline Cpp '''
// CHECK:STDERR: fail_todo_fields.carbon:[[@LINE+7]]:15: error: static assertion failed due to requirement 'sizeof(Carbon::A) == 2 * sizeof(int)' [CppInteropParseError]
// CHECK:STDERR: 18 | static_assert(sizeof(Carbon::A) == 2 * sizeof(int));
// CHECK:STDERR: | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_fields.carbon:[[@LINE+4]]:33: note: expression evaluates to '1 == 8' [CppInteropParseNote]
// CHECK:STDERR: 18 | static_assert(sizeof(Carbon::A) == 2 * sizeof(int));
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
static_assert(sizeof(Carbon::A) == 2 * sizeof(int));
''';
@@ -0,0 +1,49 @@
// 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/layout.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/export/layout.carbon
// --- simple.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
var a: i32;
var b: i32;
var c: i32;
}
inline Cpp '''c++
static_assert(sizeof(Carbon::C) == 12);
static_assert(alignof(Carbon::C) == 4);
''';
// --- packed.carbon
library "[[@TEST_NAME]]";
import Cpp;
class C {
var a: ()*;
var b: i32;
}
class D {
var c: C;
var d: i32;
}
inline Cpp '''c++
static_assert(sizeof(Carbon::C) == 12);
static_assert(alignof(Carbon::C) == 8);
static_assert(sizeof(Carbon::D) == 16);
static_assert(alignof(Carbon::D) == 8);
''';
@@ -1,123 +0,0 @@
// 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/reverse/class.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/reverse/class.carbon
// --- static_members.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {
fn F() {}
}
inline Cpp '''
void G() {
Carbon::A::F();
}
''';
// --- nested.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {
class B {
class C {
}
}
}
inline Cpp '''
void G() {
Carbon::A::B::C *p = nullptr;
}
''';
// --- fail_incomplete_concrete.carbon
library "[[@TEST_NAME]]";
import Cpp;
// CHECK:STDERR: fail_incomplete_concrete.carbon:[[@LINE+4]]:1: error: class was forward declared here [ClassForwardDeclaredHere]
// CHECK:STDERR: class A;
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR:
class A;
inline Cpp '''
// CHECK:STDERR: fail_incomplete_concrete.carbon:[[@LINE+4]]:11: error: variable has incomplete type 'Carbon::A' [CppInteropParseError]
// CHECK:STDERR: 16 | Carbon::A a;
// CHECK:STDERR: | ^
// CHECK:STDERR:
Carbon::A a;
''';
// --- fail_todo_monomorphization_failure.carbon
library "[[@TEST_NAME]]";
import Cpp;
class B(N:! i32) {
var a: array(i8, N);
}
// TODO: Once we allow this, we should produce an "invalid array bound" error
// during monomorphization triggered by the C++ code.
// CHECK:STDERR: fail_todo_monomorphization_failure.carbon:[[@LINE+4]]:11: error: alias initializer must be a name reference [AliasRequiresNameRef]
// CHECK:STDERR: alias T = B(-1);
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
alias T = B(-1);
inline Cpp '''
// CHECK:STDERR: fail_todo_monomorphization_failure.carbon:[[@LINE+4]]:9: error: no type named 'T' in namespace 'Carbon' [CppInteropParseError]
// CHECK:STDERR: 22 | Carbon::T t;
// CHECK:STDERR: | ~~~~~~~~^
// CHECK:STDERR:
Carbon::T t;
''';
// --- fail_todo_fields.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {
var a: i32;
var b: i32;
}
inline Cpp '''
// CHECK:STDERR: fail_todo_fields.carbon:[[@LINE+7]]:15: error: static assertion failed due to requirement 'sizeof(Carbon::A) == 2 * sizeof(int)' [CppInteropParseError]
// CHECK:STDERR: 18 | static_assert(sizeof(Carbon::A) == 2 * sizeof(int));
// CHECK:STDERR: | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_fields.carbon:[[@LINE+4]]:33: note: expression evaluates to '1 == 8' [CppInteropParseNote]
// CHECK:STDERR: 18 | static_assert(sizeof(Carbon::A) == 2 * sizeof(int));
// CHECK:STDERR: | ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
static_assert(sizeof(Carbon::A) == 2 * sizeof(int));
''';
// --- alias_identity.carbon
library "[[@TEST_NAME]]";
import Cpp;
class A {}
alias B = A;
inline Cpp '''
// OK, Carbon::A and Carbon::B are the same type.
Carbon::A *pa;
Carbon::B *pb = pa;
''';