Skip to content

Commit a6f5143

Browse files
authored
Fix diagnostic for access of protected/private base member. (#5874)
When importing the member, import the access level for the lookup result, not the declared access of the member declaration.
1 parent 105618e commit a6f5143

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

toolchain/check/import_cpp.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,14 +1407,15 @@ static auto MapAccess(clang::AccessSpecifier access_specifier)
14071407
static auto ImportNameDeclIntoScope(Context& context, SemIR::LocId loc_id,
14081408
SemIR::NameScopeId scope_id,
14091409
SemIR::NameId name_id,
1410-
clang::NamedDecl* clang_decl)
1410+
clang::NamedDecl* clang_decl,
1411+
clang::AccessSpecifier access)
14111412
-> SemIR::ScopeLookupResult {
14121413
SemIR::InstId inst_id =
14131414
ImportDeclAndDependencies(context, loc_id, clang_decl);
14141415
if (!inst_id.has_value()) {
14151416
return SemIR::ScopeLookupResult::MakeNotFound();
14161417
}
1417-
SemIR::AccessKind access_kind = MapAccess(clang_decl->getAccess());
1418+
SemIR::AccessKind access_kind = MapAccess(access);
14181419
AddNameToScope(context, scope_id, name_id, access_kind, inst_id);
14191420
return SemIR::ScopeLookupResult::MakeWrappedLookupResult(inst_id,
14201421
access_kind);
@@ -1447,7 +1448,8 @@ auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
14471448
}
14481449

14491450
return ImportNameDeclIntoScope(context, loc_id, scope_id, name_id,
1450-
lookup->getFoundDecl());
1451+
lookup->getFoundDecl(),
1452+
lookup->begin().getAccess());
14511453
}
14521454

14531455
} // namespace Carbon::Check

toolchain/check/testdata/interop/cpp/class/access.carbon

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,48 @@ fn F() {
253253
//@dump-sem-ir-end
254254
}
255255

256-
// --- todo_fail_import_base_class_protected.carbon
256+
// --- fail_import_base_class_protected.carbon
257257

258258
library "[[@TEST_NAME]]";
259259

260260
import Cpp library "base_class.h";
261261

262262
fn F() {
263+
// CHECK:STDERR: fail_import_base_class_protected.carbon:[[@LINE+5]]:3: error: cannot access protected member `foo` of type `Cpp.DerivedProtected` [ClassInvalidMemberAccess]
264+
// CHECK:STDERR: Cpp.DerivedProtected.foo();
265+
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
266+
// CHECK:STDERR: fail_import_base_class_protected.carbon: note: declared here [ClassMemberDeclaration]
267+
// CHECK:STDERR:
263268
Cpp.DerivedProtected.foo();
264269
}
265270

266-
// --- todo_fail_import_base_class_private.carbon
271+
// --- use_base_class_protected_from_derived.carbon
272+
273+
library "[[@TEST_NAME]]";
274+
275+
import Cpp library "base_class.h";
276+
277+
class Derived {
278+
extend base: Cpp.DerivedProtected;
279+
280+
fn F() {
281+
// OK, we can access a protected member of our base class.
282+
Cpp.DerivedProtected.foo();
283+
}
284+
}
285+
286+
// --- fail_import_base_class_private.carbon
267287

268288
library "[[@TEST_NAME]]";
269289

270290
import Cpp library "base_class.h";
271291

272292
fn F() {
293+
// CHECK:STDERR: fail_import_base_class_private.carbon:[[@LINE+5]]:3: error: cannot access private member `foo` of type `Cpp.DerivedPrivate` [ClassInvalidMemberAccess]
294+
// CHECK:STDERR: Cpp.DerivedPrivate.foo();
295+
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
296+
// CHECK:STDERR: fail_import_base_class_private.carbon: note: declared here [ClassMemberDeclaration]
297+
// CHECK:STDERR:
273298
Cpp.DerivedPrivate.foo();
274299
}
275300

0 commit comments

Comments
 (0)