Skip to content

Commit 6af3d05

Browse files
authored
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.
1 parent fcfb134 commit 6af3d05

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

toolchain/check/import_ref.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,13 +2152,12 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
21522152
SemIR::InstId inst_id) -> ResolveResult {
21532153
// Return the constant for the instruction of the imported constant.
21542154
auto constant_id = resolver.import_constant_values().Get(inst_id);
2155-
if (!constant_id.has_value()) {
2156-
return ResolveResult::Done(SemIR::ErrorInst::SingletonConstantId);
2157-
}
2155+
CARBON_CHECK(constant_id.has_value(),
2156+
"Loaded import ref has no constant value");
21582157
if (!constant_id.is_constant()) {
21592158
resolver.local_context().TODO(
21602159
inst_id, "Non-constant ImportRefLoaded (comes up with var)");
2161-
return ResolveResult::Done(SemIR::ErrorInst::SingletonConstantId);
2160+
return ResolveResult::Done(constant_id);
21622161
}
21632162

21642163
auto new_constant_id = GetLocalConstantId(
@@ -2839,10 +2838,13 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
28392838
auto constant_inst_id =
28402839
resolver.import_constant_values().GetConstantInstId(inst_id);
28412840
if (constant_inst_id == inst_id) {
2841+
// Produce a diagnostic to provide a source location with the CHECK
2842+
// failure.
28422843
resolver.local_context().TODO(
28432844
SemIR::LocId(AddImportIRInst(resolver, inst_id)),
28442845
llvm::formatv("TryResolveInst on {0}", untyped_inst.kind()).str());
2845-
return {.const_id = SemIR::ErrorInst::SingletonConstantId};
2846+
CARBON_FATAL("TryResolveInst on unsupported instruction kind {0}",
2847+
untyped_inst.kind());
28462848
}
28472849
// Try to resolve the constant value instead. Note that this can only
28482850
// retry once.

toolchain/check/testdata/alias/no_prelude/import.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ var c: () = a_alias_alias;
280280
// CHECK:STDOUT: }
281281
// CHECK:STDOUT:
282282
// CHECK:STDOUT: imports {
283-
// CHECK:STDOUT: %Main.a_alias_alias: ref %empty_tuple.type = import_ref Main//var2, a_alias_alias, loaded [template = <error>]
283+
// CHECK:STDOUT: %Main.a_alias_alias: ref %empty_tuple.type = import_ref Main//var2, a_alias_alias, loaded
284284
// CHECK:STDOUT: %Main.b = import_ref Main//var2, b, unloaded
285285
// CHECK:STDOUT: }
286286
// CHECK:STDOUT:
@@ -305,7 +305,7 @@ var c: () = a_alias_alias;
305305
// CHECK:STDOUT:
306306
// CHECK:STDOUT: fn @__global_init() {
307307
// CHECK:STDOUT: !entry:
308-
// CHECK:STDOUT: %a_alias_alias.ref: ref %empty_tuple.type = name_ref a_alias_alias, imports.%Main.a_alias_alias [template = <error>]
308+
// CHECK:STDOUT: %a_alias_alias.ref: ref %empty_tuple.type = name_ref a_alias_alias, imports.%Main.a_alias_alias
309309
// CHECK:STDOUT: %.loc11_13: init %empty_tuple.type = tuple_init () to file.%c.var [template = constants.%empty_tuple]
310310
// CHECK:STDOUT: %.loc11_1: init %empty_tuple.type = converted %a_alias_alias.ref, %.loc11_13 [template = constants.%empty_tuple]
311311
// CHECK:STDOUT: assign file.%c.var, %.loc11_1

toolchain/check/testdata/var/no_prelude/export_name.carbon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ var w: () = v;
9292
// CHECK:STDOUT: }
9393
// CHECK:STDOUT:
9494
// CHECK:STDOUT: imports {
95-
// CHECK:STDOUT: %Main.v: ref %empty_tuple.type = import_ref Main//export, v, loaded [template = <error>]
95+
// CHECK:STDOUT: %Main.v: ref %empty_tuple.type = import_ref Main//export, v, loaded
9696
// CHECK:STDOUT: }
9797
// CHECK:STDOUT:
9898
// CHECK:STDOUT: file {
@@ -115,7 +115,7 @@ var w: () = v;
115115
// CHECK:STDOUT:
116116
// CHECK:STDOUT: fn @__global_init() {
117117
// CHECK:STDOUT: !entry:
118-
// CHECK:STDOUT: %v.ref: ref %empty_tuple.type = name_ref v, imports.%Main.v [template = <error>]
118+
// CHECK:STDOUT: %v.ref: ref %empty_tuple.type = name_ref v, imports.%Main.v
119119
// CHECK:STDOUT: %.loc12_13: init %empty_tuple.type = tuple_init () to file.%w.var [template = constants.%empty_tuple]
120120
// CHECK:STDOUT: %.loc12_1: init %empty_tuple.type = converted %v.ref, %.loc12_13 [template = constants.%empty_tuple]
121121
// CHECK:STDOUT: assign file.%w.var, %.loc12_1

0 commit comments

Comments
 (0)