Export fields of imported Carbon classes to C++ (#8021)

When a Carbon class from another library was used from C++, its fields
were never exported, so C++ member access failed with 'no member named'.
The class scope of an imported class holds import references rather than
`FieldDecl`s, so `LookupClassFieldByStructField` didn't find them.
Resolve scope entries to their constant `FieldDecl`, load the import
references before exporting fields, and consistently key exported fields
by the `FieldDecl`.

Unfortunately `SemIR::LookupClassFieldByStructField` can't call
`LoadImportRef` because `SemIR::LookupClassFieldByStructField` is in
`SemIR` and `LoadImportRef` needs a `Check::Context` (and `check`
depends on `sem_ir`, not the other way around, so `sem_ir` can't use
check). So instead, make a `Check::LookupClassFieldByStructField` that
wraps the `SemIR` version, and does the `LoadImportRef`

Assisted-By: Claude

---------

Co-authored-by: Christopher Di Bella <cjdb.ns@gmail.com>
This commit is contained in:
David BlaikieandChristopher Di Bella authored and GitHub committed 2026-11-02 20:37:01 +00:00
1 parent be0f11be95
commit d99b767dde
7 files changed
+297 -8

No files matched your search

@@ -0,0 +1,53 @@
// 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/field_imported.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/export/field_imported.carbon
// --- other.carbon
package Other;
class Point {
var x: i32;
var y: i32;
}
// --- imported_field.carbon
library "[[@TEST_NAME]]";
import Other;
import Cpp;
inline Cpp '''
void AccessImportedFields() {
Carbon::Other::Point p;
p.x = 12;
p.y = 34;
}
''';
// --- imported_field_same_package.carbon
library "[[@TEST_NAME]]";
import library "point";
import Cpp;
inline Cpp '''
void AccessImportedFields() {
Carbon::Point p;
p.x = 12;
}
''';
// --- point.carbon
library "point";
class Point {
var x: i32;
}