eval.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/eval.h"
  5. #include "toolchain/sem_ir/ids.h"
  6. #include "toolchain/sem_ir/typed_insts.h"
  7. namespace Carbon::Check {
  8. namespace {
  9. // The evaluation phase for an expression, computed by evaluation. These are
  10. // ordered so that the phase of an expression is the numerically highest phase
  11. // of its constituent evaluations. Note that an expression with any runtime
  12. // component is known to have Runtime phase even if it involves an evaluation
  13. // with UnknownDueToError phase.
  14. enum class Phase : uint8_t {
  15. // Value could be entirely and concretely computed.
  16. Template,
  17. // Evaluation phase is symbolic because the expression involves a reference to
  18. // a symbolic binding.
  19. Symbolic,
  20. // The evaluation phase is unknown because evaluation encountered an
  21. // already-diagnosed semantic or syntax error. This is treated as being
  22. // potentially constant, but with an unknown phase.
  23. UnknownDueToError,
  24. // The expression has runtime phase because of a non-constant subexpression.
  25. Runtime,
  26. };
  27. } // namespace
  28. // Gets the phase in which the value of a constant will become available.
  29. static auto GetPhase(SemIR::ConstantId constant_id) -> Phase {
  30. if (!constant_id.is_constant()) {
  31. return Phase::Runtime;
  32. } else if (constant_id == SemIR::ConstantId::Error) {
  33. return Phase::UnknownDueToError;
  34. } else if (constant_id.is_template()) {
  35. return Phase::Template;
  36. } else {
  37. CARBON_CHECK(constant_id.is_symbolic());
  38. return Phase::Symbolic;
  39. }
  40. }
  41. // Returns the later of two phases.
  42. static auto LatestPhase(Phase a, Phase b) -> Phase {
  43. return static_cast<Phase>(
  44. std::max(static_cast<uint8_t>(a), static_cast<uint8_t>(b)));
  45. }
  46. // Forms a `constant_id` describing a given evaluation result.
  47. static auto MakeConstantResult(Context& context, SemIR::Inst inst, Phase phase)
  48. -> SemIR::ConstantId {
  49. switch (phase) {
  50. case Phase::Template:
  51. return context.AddConstant(inst, /*is_symbolic=*/false);
  52. case Phase::Symbolic:
  53. return context.AddConstant(inst, /*is_symbolic=*/true);
  54. case Phase::UnknownDueToError:
  55. return SemIR::ConstantId::Error;
  56. case Phase::Runtime:
  57. return SemIR::ConstantId::NotConstant;
  58. }
  59. }
  60. // Forms a `constant_id` describing why an evaluation was not constant.
  61. static auto MakeNonConstantResult(Phase phase) -> SemIR::ConstantId {
  62. return phase == Phase::UnknownDueToError ? SemIR::ConstantId::Error
  63. : SemIR::ConstantId::NotConstant;
  64. }
  65. // `GetConstantValue` checks to see whether the provided ID describes a value
  66. // with constant phase, and if so, returns the corresponding constant value.
  67. // Overloads are provided for different kinds of ID.
  68. // If the given instruction is constant, returns its constant value.
  69. static auto GetConstantValue(Context& context, SemIR::InstId inst_id,
  70. Phase* phase) -> SemIR::InstId {
  71. auto const_id = context.constant_values().Get(inst_id);
  72. *phase = LatestPhase(*phase, GetPhase(const_id));
  73. return const_id.inst_id();
  74. }
  75. // A type is always constant, but we still need to extract its phase.
  76. static auto GetConstantValue(Context& context, SemIR::TypeId type_id,
  77. Phase* phase) -> SemIR::TypeId {
  78. auto const_id = context.types().GetConstantId(type_id);
  79. *phase = LatestPhase(*phase, GetPhase(const_id));
  80. return type_id;
  81. }
  82. // If the given instruction block contains only constants, returns a
  83. // corresponding block of those values.
  84. static auto GetConstantValue(Context& context, SemIR::InstBlockId inst_block_id,
  85. Phase* phase) -> SemIR::InstBlockId {
  86. auto insts = context.inst_blocks().Get(inst_block_id);
  87. llvm::SmallVector<SemIR::InstId> const_insts;
  88. for (auto inst_id : insts) {
  89. auto const_inst_id = GetConstantValue(context, inst_id, phase);
  90. if (!const_inst_id.is_valid()) {
  91. return SemIR::InstBlockId::Invalid;
  92. }
  93. // Once we leave the small buffer, we know the first few elements are all
  94. // constant, so it's likely that the entire block is constant. Resize to the
  95. // target size given that we're going to allocate memory now anyway.
  96. if (const_insts.size() == const_insts.capacity()) {
  97. const_insts.reserve(insts.size());
  98. }
  99. const_insts.push_back(const_inst_id);
  100. }
  101. // TODO: If the new block is identical to the original block, return the
  102. // original ID.
  103. return context.inst_blocks().Add(const_insts);
  104. }
  105. // The constant value of a type block is that type block, but we still need to
  106. // extract its phase.
  107. static auto GetConstantValue(Context& context, SemIR::TypeBlockId type_block_id,
  108. Phase* phase) -> SemIR::TypeBlockId {
  109. auto types = context.type_blocks().Get(type_block_id);
  110. for (auto type_id : types) {
  111. GetConstantValue(context, type_id, phase);
  112. }
  113. return type_block_id;
  114. }
  115. // Replaces the specified field of the given typed instruction with its constant
  116. // value, if it has constant phase. Returns true on success, false if the value
  117. // has runtime phase.
  118. template <typename InstT, typename FieldIdT>
  119. static auto ReplaceFieldWithConstantValue(Context& context, InstT* inst,
  120. FieldIdT InstT::*field, Phase* phase)
  121. -> bool {
  122. auto unwrapped = GetConstantValue(context, inst->*field, phase);
  123. if (!unwrapped.is_valid()) {
  124. return false;
  125. }
  126. inst->*field = unwrapped;
  127. return true;
  128. }
  129. // If the specified fields of the given typed instruction have constant values,
  130. // replaces the fields with their constant values and builds a corresponding
  131. // constant value. Otherwise returns `ConstantId::NotConstant`. Returns
  132. // `ConstantId::Error` if any subexpression is an error.
  133. //
  134. // The constant value is then checked by calling `validate_fn(typed_inst)`,
  135. // which should return a `bool` indicating whether the new constant is valid. If
  136. // validation passes, a corresponding ConstantId for the new constant is
  137. // returned. If validation fails, it should produce a suitable error message.
  138. // `ConstantId::Error` is returned.
  139. template <typename InstT, typename ValidateFn, typename... EachFieldIdT>
  140. static auto RebuildAndValidateIfFieldsAreConstant(
  141. Context& context, SemIR::Inst inst, ValidateFn validate_fn,
  142. EachFieldIdT InstT::*... each_field_id) -> SemIR::ConstantId {
  143. // Build a constant instruction by replacing each non-constant operand with
  144. // its constant value.
  145. auto typed_inst = inst.As<InstT>();
  146. Phase phase = Phase::Template;
  147. if ((ReplaceFieldWithConstantValue(context, &typed_inst, each_field_id,
  148. &phase) &&
  149. ...)) {
  150. if (!validate_fn(typed_inst)) {
  151. return SemIR::ConstantId::Error;
  152. }
  153. return MakeConstantResult(context, typed_inst, phase);
  154. }
  155. return MakeNonConstantResult(phase);
  156. }
  157. // Same as above but with no validation step.
  158. template <typename InstT, typename... EachFieldIdT>
  159. static auto RebuildIfFieldsAreConstant(Context& context, SemIR::Inst inst,
  160. EachFieldIdT InstT::*... each_field_id)
  161. -> SemIR::ConstantId {
  162. return RebuildAndValidateIfFieldsAreConstant(
  163. context, inst, [](...) { return true; }, each_field_id...);
  164. }
  165. // Rebuilds the given aggregate initialization instruction as a corresponding
  166. // constant aggregate value, if its elements are all constants.
  167. static auto RebuildInitAsValue(Context& context, SemIR::Inst inst,
  168. SemIR::InstKind value_kind)
  169. -> SemIR::ConstantId {
  170. auto init_inst = inst.As<SemIR::AnyAggregateInit>();
  171. Phase phase = Phase::Template;
  172. auto elements_id = GetConstantValue(context, init_inst.elements_id, &phase);
  173. return MakeConstantResult(
  174. context,
  175. SemIR::AnyAggregateValue{.kind = value_kind,
  176. .type_id = init_inst.type_id,
  177. .elements_id = elements_id},
  178. phase);
  179. }
  180. // Performs an access into an aggregate, retrieving the specified element.
  181. static auto PerformAggregateAccess(Context& context, SemIR::Inst inst)
  182. -> SemIR::ConstantId {
  183. auto access_inst = inst.As<SemIR::AnyAggregateAccess>();
  184. Phase phase = Phase::Template;
  185. if (auto aggregate_id =
  186. GetConstantValue(context, access_inst.aggregate_id, &phase);
  187. aggregate_id.is_valid()) {
  188. if (auto aggregate =
  189. context.insts().TryGetAs<SemIR::AnyAggregateValue>(aggregate_id)) {
  190. auto elements = context.inst_blocks().Get(aggregate->elements_id);
  191. auto index = static_cast<size_t>(access_inst.index.index);
  192. CARBON_CHECK(index < elements.size()) << "Access out of bounds.";
  193. // `Phase` is not used here. If this element is a template constant, then
  194. // so is the result of indexing, even if the aggregate also contains a
  195. // symbolic context.
  196. return context.constant_values().Get(elements[index]);
  197. } else {
  198. CARBON_CHECK(phase != Phase::Template)
  199. << "Failed to evaluate template constant " << inst;
  200. }
  201. }
  202. return MakeNonConstantResult(phase);
  203. }
  204. // Performs an index into a homogeneous aggregate, retrieving the specified
  205. // element.
  206. static auto PerformAggregateIndex(Context& context, SemIR::Inst inst)
  207. -> SemIR::ConstantId {
  208. auto index_inst = inst.As<SemIR::AnyAggregateIndex>();
  209. Phase phase = Phase::Template;
  210. auto aggregate_id =
  211. GetConstantValue(context, index_inst.aggregate_id, &phase);
  212. auto index_id = GetConstantValue(context, index_inst.index_id, &phase);
  213. if (!index_id.is_valid()) {
  214. return MakeNonConstantResult(phase);
  215. }
  216. auto index = context.insts().TryGetAs<SemIR::IntLiteral>(index_id);
  217. if (!index) {
  218. CARBON_CHECK(phase != Phase::Template)
  219. << "Template constant integer should be a literal";
  220. return MakeNonConstantResult(phase);
  221. }
  222. // Array indexing is invalid if the index is constant and out of range.
  223. auto aggregate_type_id =
  224. context.insts().Get(index_inst.aggregate_id).type_id();
  225. const auto& index_val = context.ints().Get(index->int_id);
  226. if (auto array_type =
  227. context.types().TryGetAs<SemIR::ArrayType>(aggregate_type_id)) {
  228. if (auto bound =
  229. context.insts().TryGetAs<SemIR::IntLiteral>(array_type->bound_id)) {
  230. // This awkward call to `getZExtValue` is a workaround for APInt not
  231. // supporting comparisons between integers of different bit widths.
  232. if (index_val.getActiveBits() > 64 ||
  233. context.ints().Get(bound->int_id).ule(index_val.getZExtValue())) {
  234. CARBON_DIAGNOSTIC(ArrayIndexOutOfBounds, Error,
  235. "Array index `{0}` is past the end of type `{1}`.",
  236. llvm::APSInt, SemIR::TypeId);
  237. context.emitter().Emit(index_inst.index_id, ArrayIndexOutOfBounds,
  238. llvm::APSInt(index_val, /*isUnsigned=*/true),
  239. aggregate_type_id);
  240. return SemIR::ConstantId::Error;
  241. }
  242. }
  243. }
  244. if (!aggregate_id.is_valid()) {
  245. return MakeNonConstantResult(phase);
  246. }
  247. auto aggregate =
  248. context.insts().TryGetAs<SemIR::AnyAggregateValue>(aggregate_id);
  249. if (!aggregate) {
  250. CARBON_CHECK(phase != Phase::Template)
  251. << "Unexpected representation for template constant aggregate";
  252. return MakeNonConstantResult(phase);
  253. }
  254. auto elements = context.inst_blocks().Get(aggregate->elements_id);
  255. // We checked this for the array case above.
  256. CARBON_CHECK(index_val.ult(elements.size()))
  257. << "Index out of bounds in tuple indexing";
  258. return context.constant_values().Get(elements[index_val.getZExtValue()]);
  259. }
  260. auto TryEvalInst(Context& context, SemIR::InstId inst_id, SemIR::Inst inst)
  261. -> SemIR::ConstantId {
  262. // TODO: Ensure we have test coverage for each of these cases that can result
  263. // in a constant, once those situations are all reachable.
  264. switch (inst.kind()) {
  265. // These cases are constants if their operands are.
  266. case SemIR::AddrOf::Kind:
  267. return RebuildIfFieldsAreConstant(context, inst,
  268. &SemIR::AddrOf::lvalue_id);
  269. case SemIR::ArrayType::Kind:
  270. return RebuildAndValidateIfFieldsAreConstant(
  271. context, inst,
  272. [&](SemIR::ArrayType result) {
  273. auto bound_id = inst.As<SemIR::ArrayType>().bound_id;
  274. auto int_bound =
  275. context.insts().TryGetAs<SemIR::IntLiteral>(result.bound_id);
  276. if (!int_bound) {
  277. // TODO: Permit symbolic array bounds. This will require fixing
  278. // callers of `GetArrayBoundValue`.
  279. context.TODO(bound_id, "symbolic array bound");
  280. return false;
  281. }
  282. // TODO: We should check that the size of the resulting array type
  283. // fits in 64 bits, not just that the bound does. Should we use a
  284. // 32-bit limit for 32-bit targets?
  285. // TODO: Also check for a negative bound, once that's something we
  286. // can represent.
  287. const auto& bound_val = context.ints().Get(int_bound->int_id);
  288. if (bound_val.getActiveBits() > 64) {
  289. CARBON_DIAGNOSTIC(ArrayBoundTooLarge, Error,
  290. "Array bound of {0} is too large.",
  291. llvm::APInt);
  292. context.emitter().Emit(bound_id, ArrayBoundTooLarge, bound_val);
  293. return false;
  294. }
  295. return true;
  296. },
  297. &SemIR::ArrayType::bound_id, &SemIR::ArrayType::element_type_id);
  298. case SemIR::AssociatedEntityType::Kind:
  299. return RebuildIfFieldsAreConstant(
  300. context, inst, &SemIR::AssociatedEntityType::entity_type_id);
  301. case SemIR::BoundMethod::Kind:
  302. return RebuildIfFieldsAreConstant(context, inst,
  303. &SemIR::BoundMethod::object_id,
  304. &SemIR::BoundMethod::function_id);
  305. case SemIR::InterfaceWitness::Kind:
  306. return RebuildIfFieldsAreConstant(context, inst,
  307. &SemIR::InterfaceWitness::elements_id);
  308. case SemIR::PointerType::Kind:
  309. return RebuildIfFieldsAreConstant(context, inst,
  310. &SemIR::PointerType::pointee_id);
  311. case SemIR::StructType::Kind:
  312. return RebuildIfFieldsAreConstant(context, inst,
  313. &SemIR::StructType::fields_id);
  314. case SemIR::StructTypeField::Kind:
  315. return RebuildIfFieldsAreConstant(context, inst,
  316. &SemIR::StructTypeField::field_type_id);
  317. case SemIR::StructValue::Kind:
  318. return RebuildIfFieldsAreConstant(context, inst,
  319. &SemIR::StructValue::elements_id);
  320. case SemIR::TupleType::Kind:
  321. return RebuildIfFieldsAreConstant(context, inst,
  322. &SemIR::TupleType::elements_id);
  323. case SemIR::TupleValue::Kind:
  324. return RebuildIfFieldsAreConstant(context, inst,
  325. &SemIR::TupleValue::elements_id);
  326. case SemIR::UnboundElementType::Kind:
  327. return RebuildIfFieldsAreConstant(
  328. context, inst, &SemIR::UnboundElementType::class_type_id,
  329. &SemIR::UnboundElementType::element_type_id);
  330. // Initializers evaluate to a value of the object representation.
  331. case SemIR::ArrayInit::Kind:
  332. // TODO: Add an `ArrayValue` to represent a constant array object
  333. // representation instead of using a `TupleValue`.
  334. return RebuildInitAsValue(context, inst, SemIR::TupleValue::Kind);
  335. case SemIR::ClassInit::Kind:
  336. // TODO: Add a `ClassValue` to represent a constant class object
  337. // representation instead of using a `StructValue`.
  338. return RebuildInitAsValue(context, inst, SemIR::StructValue::Kind);
  339. case SemIR::StructInit::Kind:
  340. return RebuildInitAsValue(context, inst, SemIR::StructValue::Kind);
  341. case SemIR::TupleInit::Kind:
  342. return RebuildInitAsValue(context, inst, SemIR::TupleValue::Kind);
  343. case SemIR::AssociatedEntity::Kind:
  344. case SemIR::Builtin::Kind:
  345. // Builtins are always template constants.
  346. return MakeConstantResult(context, inst, Phase::Template);
  347. case SemIR::ClassDecl::Kind:
  348. // TODO: Once classes have generic arguments, handle them.
  349. return MakeConstantResult(
  350. context,
  351. SemIR::ClassType{SemIR::TypeId::TypeType,
  352. inst.As<SemIR::ClassDecl>().class_id},
  353. Phase::Template);
  354. case SemIR::InterfaceDecl::Kind:
  355. // TODO: Once interfaces have generic arguments, handle them.
  356. return MakeConstantResult(
  357. context,
  358. SemIR::InterfaceType{SemIR::TypeId::TypeType,
  359. inst.As<SemIR::InterfaceDecl>().interface_id},
  360. Phase::Template);
  361. case SemIR::ClassType::Kind:
  362. case SemIR::InterfaceType::Kind:
  363. CARBON_FATAL() << inst.kind()
  364. << " is only created during corresponding Decl handling.";
  365. // These cases are treated as being the unique canonical definition of the
  366. // corresponding constant value.
  367. // TODO: This doesn't properly handle redeclarations. Consider adding a
  368. // corresponding `Value` inst for each of these cases.
  369. case SemIR::AssociatedConstantDecl::Kind:
  370. case SemIR::BaseDecl::Kind:
  371. case SemIR::FieldDecl::Kind:
  372. case SemIR::FunctionDecl::Kind:
  373. case SemIR::Namespace::Kind:
  374. return SemIR::ConstantId::ForTemplateConstant(inst_id);
  375. case SemIR::BoolLiteral::Kind:
  376. case SemIR::IntLiteral::Kind:
  377. case SemIR::RealLiteral::Kind:
  378. case SemIR::StringLiteral::Kind:
  379. // Promote literals to the constant block.
  380. // TODO: Convert literals into a canonical form. Currently we can form two
  381. // different `i32` constants with the same value if they are represented
  382. // by `APInt`s with different bit widths.
  383. return MakeConstantResult(context, inst, Phase::Template);
  384. // The elements of a constant aggregate can be accessed.
  385. case SemIR::ClassElementAccess::Kind:
  386. case SemIR::InterfaceWitnessAccess::Kind:
  387. case SemIR::StructAccess::Kind:
  388. case SemIR::TupleAccess::Kind:
  389. return PerformAggregateAccess(context, inst);
  390. case SemIR::ArrayIndex::Kind:
  391. case SemIR::TupleIndex::Kind:
  392. return PerformAggregateIndex(context, inst);
  393. // TODO: These need special handling.
  394. case SemIR::BindValue::Kind:
  395. case SemIR::Call::Kind:
  396. case SemIR::Deref::Kind:
  397. case SemIR::ImportRefUsed::Kind:
  398. case SemIR::Temporary::Kind:
  399. case SemIR::TemporaryStorage::Kind:
  400. case SemIR::ValueAsRef::Kind:
  401. break;
  402. case SemIR::BindSymbolicName::Kind:
  403. // TODO: Consider forming a constant value here using a de Bruijn index or
  404. // similar, so that corresponding symbolic parameters in redeclarations
  405. // are treated as the same value.
  406. return SemIR::ConstantId::ForSymbolicConstant(inst_id);
  407. // These semantic wrappers don't change the constant value.
  408. case SemIR::BindAlias::Kind:
  409. return context.constant_values().Get(
  410. inst.As<SemIR::BindAlias>().value_id);
  411. case SemIR::NameRef::Kind:
  412. return context.constant_values().Get(inst.As<SemIR::NameRef>().value_id);
  413. case SemIR::Converted::Kind:
  414. return context.constant_values().Get(
  415. inst.As<SemIR::Converted>().result_id);
  416. case SemIR::InitializeFrom::Kind:
  417. return context.constant_values().Get(
  418. inst.As<SemIR::InitializeFrom>().src_id);
  419. case SemIR::SpliceBlock::Kind:
  420. return context.constant_values().Get(
  421. inst.As<SemIR::SpliceBlock>().result_id);
  422. case SemIR::ValueOfInitializer::Kind:
  423. return context.constant_values().Get(
  424. inst.As<SemIR::ValueOfInitializer>().init_id);
  425. case SemIR::FacetTypeAccess::Kind:
  426. // TODO: Once we start tracking the witness in the facet value, remove it
  427. // here. For now, we model a facet value as just a type.
  428. return context.constant_values().Get(
  429. inst.As<SemIR::FacetTypeAccess>().facet_id);
  430. // `not true` -> `false`, `not false` -> `true`.
  431. // All other uses of unary `not` are non-constant.
  432. case SemIR::UnaryOperatorNot::Kind: {
  433. auto const_id = context.constant_values().Get(
  434. inst.As<SemIR::UnaryOperatorNot>().operand_id);
  435. auto phase = GetPhase(const_id);
  436. if (phase == Phase::Template) {
  437. auto value =
  438. context.insts().GetAs<SemIR::BoolLiteral>(const_id.inst_id());
  439. value.value =
  440. (value.value == SemIR::BoolValue::False ? SemIR::BoolValue::True
  441. : SemIR::BoolValue::False);
  442. return MakeConstantResult(context, value, Phase::Template);
  443. }
  444. if (phase == Phase::UnknownDueToError) {
  445. return SemIR::ConstantId::Error;
  446. }
  447. break;
  448. }
  449. // `const (const T)` evaluates to `const T`. Otherwise, `const T` evaluates
  450. // to itself.
  451. case SemIR::ConstType::Kind: {
  452. auto inner_id = context.constant_values().Get(
  453. context.types().GetInstId(inst.As<SemIR::ConstType>().inner_id));
  454. if (inner_id.is_constant() &&
  455. context.insts().Get(inner_id.inst_id()).Is<SemIR::ConstType>()) {
  456. return inner_id;
  457. }
  458. return MakeConstantResult(context, inst, GetPhase(inner_id));
  459. }
  460. // These cases are either not expressions or not constant.
  461. case SemIR::AddrPattern::Kind:
  462. case SemIR::Assign::Kind:
  463. case SemIR::BindName::Kind:
  464. case SemIR::BlockArg::Kind:
  465. case SemIR::Branch::Kind:
  466. case SemIR::BranchIf::Kind:
  467. case SemIR::BranchWithArg::Kind:
  468. case SemIR::ImplDecl::Kind:
  469. case SemIR::Param::Kind:
  470. case SemIR::ReturnExpr::Kind:
  471. case SemIR::Return::Kind:
  472. case SemIR::StructLiteral::Kind:
  473. case SemIR::TupleLiteral::Kind:
  474. case SemIR::VarStorage::Kind:
  475. break;
  476. case SemIR::ImportRefUnused::Kind:
  477. CARBON_FATAL() << "ImportRefUnused should transform to ImportRefUsed "
  478. "before TryEvalInst.";
  479. }
  480. return SemIR::ConstantId::NotConstant;
  481. }
  482. } // namespace Carbon::Check