Fix crash when initializing class with omitted base class (#7740)

### Description
When looking up default initializers for class elements in
`ConvertStructToClass`, the compiler previously assumed that every
member looked up from the class scope was a `FieldDecl` and called
`GetAs<SemIR::FieldDecl>` directly.

For a derived class with a base class, looking up `base` returns a
`BaseDecl`, which caused a `CHECK` assertion failure when casting to
`FieldDecl`. Use `TryGetAs<SemIR::FieldDecl>` instead so non-`FieldDecl`
entries like `BaseDecl` are recognized as having no default initializer,
cleanly diagnosing that the `base` field is missing.

Fixes #7722

Assisted-by: Google Deepmind Antigravity
This commit is contained in:
ATHARVA
2026-09-10 19:29:24 +00:00
committed by GitHub
parent 6f1ae86ce4
commit d98784b972
2 changed files with 30 additions and 2 deletions
+5 -2
View File
@@ -920,8 +920,11 @@ static auto ConvertStructToClass(Context& context, SemIR::StructType src_type,
dest_class_scope.GetEntry(*entry_id).result.target_inst_id();
LoadImportRef(context, field_inst_id);
field_inst_id = context.constant_values().GetConstantInstId(field_inst_id);
auto field_decl = context.insts().GetAs<SemIR::FieldDecl>(field_inst_id);
auto field = context.fields().Get(field_decl.field_id);
auto field_decl = context.insts().TryGetAs<SemIR::FieldDecl>(field_inst_id);
if (!field_decl) {
return SemIR::InstId::None;
}
auto field = context.fields().Get(field_decl->field_id);
if (!field.initializer_id.has_value()) {
return SemIR::InstId::None;
}