mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:11:12 +01:00
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:
co-authored by
Geoff Romer
parent
355f700b4a
commit
de0ad6730f
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user