Introduce a CheckedSaveAndRestore like llvm::SaveAndRestore (#7965)

Sets a variable to a new value, saving a copy of that new value. Then
CHECKs that the variable is still set to that new value before restoring
the original value over top of it. This is meant to catch data loss
where the variable holds some change that would be overwritten by the
restore. In particular, this would catch bad bracketing of sets and
restores.
This commit is contained in:
Dana Jansens authored and GitHub committed 2026-10-13 22:47:19 +00:00
1 parent 4ab46d448c
commit d4f1ec203d
5 files changed
+72 -20

No files matched your search

+6
View File
@@ -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"],
+46
View File
@@ -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 <utility>
#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 <typename T>
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 <typename T>
CheckedSaveAndRestore(T&, const T&) -> CheckedSaveAndRestore<T>;
template <typename T>
CheckedSaveAndRestore(T&, T&&) -> CheckedSaveAndRestore<T>;
} // namespace Carbon
#endif // CARBON_COMMON_CHECKED_SAVE_AND_RESTORE_H_
+1
View File
@@ -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",
+1
View File
@@ -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<DeclaringImplDecl>& {
return declaring_impl_decls_;
+18 -20
View File
@@ -9,10 +9,10 @@
#include <optional>
#include <utility>
#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<SemIR::FunctionDecl>(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