Fix C++ access to Carbon friend members from an unrelated class (#8097)

See access_private_parent_member_in_unrelated_class_with_friend.carbon
for an example of this fix.

The problem with that example, prior to this fix, is that when
`Derived::F` is accessed, clang tries to find `F` via
`CarbonExternalASTSource::FindExternalVisibleDeclsByName`, but
`LookupQualifiedName` doesn't find anything because it (correctly) sees
that `F` is a private member of `Base`, so it's not accessible. What it
doesn't know is that it will be accessed from the context of `A`, which
is a friend of `Base`, and so privacy doesn't matter.

`FindExternalVisibleDeclsByName` is really supposed to report visible
items, regardless of whether they can be accessed. But changing it to
simply ignore access (e.g. by modifying `LookupQualifiedName` to
optionally ignore access) doesn't quite work here. This can make a
private member of a base class appear within the `DeclContext` of a
derived class with access set to private, when it should be set to none.
This causes an assert in clang (this case is tested in
fail_access_private_parent_member_in_unrelated_fn.carbon).

Instead, change `FindExternalVisibleDeclsByName` to call
`LookupNameInExactScope` instead of `LookupQualifiedName`. This prevents
extended scopes from being searched, so members of a base class are not
found when filling in the `DeclContext` of a derived class. Clang will
call `FindExternalVisibleDeclsByName` in the context of the base class,
and the base class members are added at that time.
This commit is contained in:
Nicholas Bishop authored and GitHub committed 2026-11-06 20:09:14 +00:00
1 parent 4e4b35e6e7
commit ca1898506b
3 files changed
+139 -26

No files matched your search

+18 -5
View File
@@ -369,11 +369,24 @@ auto CarbonExternalASTSource::FindExternalVisibleDeclsByName(
auto name_id = AddIdentifierName(*context_, identifier->getName());
// `required=false` so Carbon doesn't diagnose a failure, let Clang diagnose
// it or even SFINAE.
LookupResult result = LookupQualifiedName(*context_, GetCurrentCppLocId(),
name_id, lookup_scopes,
/*required=*/false);
LookupResult result{
.specific_id = SemIR::SpecificId::None,
.scope_result = SemIR::ScopeLookupResult::MakeNotFound(),
};
for (auto lookup_scope : lookup_scopes) {
auto scope_id = lookup_scope.name_scope_id;
result.scope_result =
LookupNameInExactScope(*context_, GetCurrentCppLocId(), name_id,
scope_id, context_->name_scopes().Get(scope_id));
result.specific_id = lookup_scope.specific_id;
// TODO: If the name is found in multiple scopes, it should be treated as
// ambiguous, possibly by adding all the results to the name lookup table
// so Clang reports the ambiguity.
if (result.scope_result.is_found()) {
break;
}
}
if (!result.scope_result.is_found()) {
// Avoid caching a negative lookup, as a declaration with this name may be
// added in Carbon later.