Fix lowering of imported global variables. (#6567)

*   When a C++ static data member is imported, evaluate its address to a
    constant like we would for a namespace-scope variable.
*   When an imported variable is used in a way that doesn't require its
    type to be complete, emit the variable with an opaque type instead
    of skipping it (and potentially crashing later).
This commit is contained in:
Richard Smith
2026-01-11 19:22:43 +00:00
committed by GitHub
parent 64fa9cc6ae
commit 935ccce2a6
9 changed files with 247 additions and 26 deletions
+10 -4
View File
@@ -742,10 +742,16 @@ auto EvalConstantInst(Context& context, SemIR::InstId inst_id,
}
auto scope_id = context.entity_names().Get(entity_name_id).parent_scope_id;
if (!scope_id.has_value() ||
!context.insts().Is<SemIR::Namespace>(
context.name_scopes().Get(scope_id).inst_id())) {
// Only namespace-scope variables are reference constants.
if (!scope_id.has_value()) {
return ConstantEvalResult::NotConstant;
}
auto scope_inst =
context.insts().Get(context.name_scopes().Get(scope_id).inst_id());
if (!scope_inst.Is<SemIR::Namespace>() &&
!scope_inst.Is<SemIR::ClassDecl>()) {
// Only namespace-scope and class-scope variables are reference constants.
// Class-scope variables cannot currently be declared directly, but can
// occur when static data members are imported from C++.
return ConstantEvalResult::NotConstant;
}