Fix crash if a member of std::string_view is found in a derived class. (#6604)

Members of `std::string_view` can't be accessed directly, because that
type maps into Carbon's `str` type (`Core.String`), so member access
doesn't find the C++ members. But they can be named via qualified name
lookup into a derived type. That crashed because we didn't expect the
non-Cpp type `Core.String` to be the parent of a Cpp-imported member.

Plus add some more test coverage for related cases (not involving `str`)
that already worked.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Richard Smith
2026-01-15 22:36:18 +00:00
committed by GitHub
co-authored by Geoff Romer
parent 355f700b4a
commit de0ad6730f
4 changed files with 264 additions and 5 deletions
+14 -3
View File
@@ -254,9 +254,20 @@ static auto GetParentNameScopeId(Context& context, clang::Decl* clang_decl)
auto class_inst_id =
LookupClangDeclInstId(context, SemIR::ClangDeclKey(tag_decl));
CARBON_CHECK(class_inst_id.has_value());
return context.classes()
.Get(context.insts().GetAs<SemIR::ClassDecl>(class_inst_id).class_id)
.scope_id;
auto class_inst = context.insts().Get(class_inst_id);
auto class_id = SemIR::ClassId::None;
if (auto class_decl = class_inst.TryAs<SemIR::ClassDecl>()) {
// Common case: the tag was imported as a new Carbon class.
class_id = class_decl->class_id;
} else {
// Rare case: the tag was imported as an existing Carbon class. This
// happens for C++ classes that get mapped to Carbon prelude types, such
// as `std::string_view`.
// TODO: In this case, should we import the C++ class declaration and use
// it as the parent, rather than using the existing Carbon class?
class_id = class_inst.As<SemIR::ClassType>().class_id;
}
return context.classes().Get(class_id).scope_id;
}
if (isa<clang::NamespaceDecl, clang::TranslationUnitDecl>(parent_decl)) {