diff --git a/common/BUILD b/common/BUILD index 44d492e0ff2c..441c51f29a2b 100644 --- a/common/BUILD +++ b/common/BUILD @@ -113,6 +113,12 @@ cc_test( ], ) +cc_library( + name = "checked_save_and_restore", + hdrs = ["checked_save_and_restore.h"], + deps = [":check"], +) + cc_library( name = "concepts", hdrs = ["concepts.h"], diff --git a/common/checked_save_and_restore.h b/common/checked_save_and_restore.h new file mode 100644 index 000000000000..10ecfca1ed9e --- /dev/null +++ b/common/checked_save_and_restore.h @@ -0,0 +1,46 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef CARBON_COMMON_CHECKED_SAVE_AND_RESTORE_H_ +#define CARBON_COMMON_CHECKED_SAVE_AND_RESTORE_H_ + +#include + +#include "common/check.h" + +namespace Carbon { + +// Sets a variable to a new value, and restores the original value when +// the SetAndRestore is destroyed. CHECKs that the variable is still holding the +// new value when restoring the original value, to catch any data loss. +// +// The value must be copyable in order to compare with the new value at the time +// the SetAndRestore is destroyed. +template +class [[nodiscard]] CheckedSaveAndRestore { + public: + CheckedSaveAndRestore(T& var, const T& new_value) + : var_(&var), + new_value_(new_value), + orig_value_(std::exchange(var, new_value_)) {} + + ~CheckedSaveAndRestore() { + CARBON_CHECK(*var_ == new_value_); + *var_ = std::move(orig_value_); + } + + private: + T* var_; + T new_value_; + T orig_value_; +}; + +template +CheckedSaveAndRestore(T&, const T&) -> CheckedSaveAndRestore; +template +CheckedSaveAndRestore(T&, T&&) -> CheckedSaveAndRestore; + +} // namespace Carbon + +#endif // CARBON_COMMON_CHECKED_SAVE_AND_RESTORE_H_ diff --git a/toolchain/check/BUILD b/toolchain/check/BUILD index a21af9563113..3ff474ea6261 100644 --- a/toolchain/check/BUILD +++ b/toolchain/check/BUILD @@ -157,6 +157,7 @@ cc_library( ":node_stack", "//common:array_stack", "//common:check", + "//common:checked_save_and_restore", "//common:concepts", "//common:emplace_by_calling", "//common:enum_mask_base", diff --git a/toolchain/check/context.h b/toolchain/check/context.h index d4e1765caa1e..098d05252fad 100644 --- a/toolchain/check/context.h +++ b/toolchain/check/context.h @@ -314,6 +314,7 @@ class Context { struct DeclaringImplDecl { SemIR::ConstantId self_id; SemIR::SpecificInterface specific_interface; + auto operator==(const DeclaringImplDecl& rhs) const -> bool = default; }; auto declaring_impl_decls() -> llvm::SmallVector& { return declaring_impl_decls_; diff --git a/toolchain/check/eval.cpp b/toolchain/check/eval.cpp index df243a764233..bde7c86beba9 100644 --- a/toolchain/check/eval.cpp +++ b/toolchain/check/eval.cpp @@ -9,10 +9,10 @@ #include #include +#include "common/checked_save_and_restore.h" #include "common/raw_string_ostream.h" #include "llvm/ADT/APFloat.h" #include "llvm/Support/ConvertUTF.h" -#include "llvm/Support/SaveAndRestore.h" #include "toolchain/base/canonical_value_store.h" #include "toolchain/base/int.h" #include "toolchain/base/kind_switch.h" @@ -3540,20 +3540,21 @@ auto TryEvalInstUnsafe(Context& context, SemIR::InstId inst_id, return TryEvalInstInContext(eval_context, inst_id, inst); } -// Update `context.access_context` to the type of the innermost enclosing type -// scope of the generic. -static auto SetAccessContext(Context& context, const SemIR::Generic& generic) { +// Finds the scope to use for `context.access_context`, representing the +// innermost enclosing type scope of the generic. +static auto FindAccessContext(Context& context, const SemIR::Generic& generic) + -> SemIR::NameScopeId { auto function_decl = context.insts().TryGetAs(generic.decl_id); if (!function_decl || !function_decl->function_id.has_value()) { - return; + return context.access_context(); } const auto& function = context.functions().Get(function_decl->function_id); if (!function.parent_scope_id.has_value()) { - return; + return context.access_context(); } - context.access_context() = function.parent_scope_id; + return function.parent_scope_id; } auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id, @@ -3563,7 +3564,6 @@ auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id, const auto& generic = context.generics().Get(generic_id); auto eval_block_id = generic.GetEvalBlock(region); auto eval_block = context.inst_blocks().Get(eval_block_id); - llvm::SaveAndRestore access_context(context.access_context()); // Allocate the value block and store it back onto the specific, so that our // in-progress results are visible. @@ -3576,7 +3576,16 @@ auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id, } specific.SetValueBlock(region, value_block_id); - SetAccessContext(context, generic); + CheckedSaveAndRestore access_context(context.access_context(), + FindAccessContext(context, generic)); + + // The `declaring_impl_decls` is a global state that modifies the evaluation + // of LookupImplWitness. As a global state, it should only affect new + // instructions introduced during a given state. When we are evaluating for a + // specific, we are resolving existing instructions in the eval block for a + // generic. The global state should not apply to anything done while resolving + // a specific. + CheckedSaveAndRestore declaring(context.declaring_impl_decls(), {}); EvalContext eval_context(&context, loc_id, specific_id); @@ -3588,14 +3597,6 @@ auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id, builder.Context(loc_id, ResolvingSpecificHere, specific_id); }); - // The `declaring_impl_decls` is a global state that modifies the evaluation - // of LookupImplWitness. As a global state, it should only affect new - // instructions introduced during a given state. When we are evaluating for a - // specific, we are resolving existing instructions in the eval block for a - // generic. The global state should not apply to anything done while resolving - // a specific. - auto declaring = std::exchange(context.declaring_impl_decls(), {}); - for (auto [i, inst_id, result_id] : llvm::enumerate(eval_block, value_block)) { auto const_id = TryEvalInstInContext(eval_context, inst_id, @@ -3607,9 +3608,6 @@ auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id, } result_id = context.constant_values().GetInstId(const_id); } - - CARBON_CHECK(context.declaring_impl_decls().empty()); - context.declaring_impl_decls() = std::move(declaring); } // Information about the function call we are currently executing. Unlike