Don't recover from import errors by producing an error constant. (#4892)

Instead, produce a CARBON_FATAL error. Returning an Error constant from
the importer seems reasonable but turns out to not work well in
practice, because it violates the invariant that a constant value should
not have an error as an operand.

Also, don't produce an error constant for a valid ImportRefLoaded that
whose value is not constant; preserve the non-constant value instead.
This commit is contained in:
Richard Smith
2025-02-04 23:36:55 +00:00
committed by GitHub
parent fcfb1345d5
commit 6af3d050ea
3 changed files with 11 additions and 9 deletions
+7 -5
View File
@@ -2152,13 +2152,12 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
SemIR::InstId inst_id) -> ResolveResult {
// Return the constant for the instruction of the imported constant.
auto constant_id = resolver.import_constant_values().Get(inst_id);
if (!constant_id.has_value()) {
return ResolveResult::Done(SemIR::ErrorInst::SingletonConstantId);
}
CARBON_CHECK(constant_id.has_value(),
"Loaded import ref has no constant value");
if (!constant_id.is_constant()) {
resolver.local_context().TODO(
inst_id, "Non-constant ImportRefLoaded (comes up with var)");
return ResolveResult::Done(SemIR::ErrorInst::SingletonConstantId);
return ResolveResult::Done(constant_id);
}
auto new_constant_id = GetLocalConstantId(
@@ -2839,10 +2838,13 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
auto constant_inst_id =
resolver.import_constant_values().GetConstantInstId(inst_id);
if (constant_inst_id == inst_id) {
// Produce a diagnostic to provide a source location with the CHECK
// failure.
resolver.local_context().TODO(
SemIR::LocId(AddImportIRInst(resolver, inst_id)),
llvm::formatv("TryResolveInst on {0}", untyped_inst.kind()).str());
return {.const_id = SemIR::ErrorInst::SingletonConstantId};
CARBON_FATAL("TryResolveInst on unsupported instruction kind {0}",
untyped_inst.kind());
}
// Try to resolve the constant value instead. Note that this can only
// retry once.