From e9d8d06f9299f07be05b5b38c2ff7d5e13847f1d Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Mon, 26 Oct 2026 16:35:11 +0000 Subject: [PATCH] Add lifetime comments to CanonicalValueStore and IdentifierStore (#8037) Followup to https://github.com/carbon-language/carbon-lang/pull/8025. It's easy to accidentally put a string into the `IdentifierStore` that doesn't live long enough. Add some comments to document this pitfall. --- toolchain/base/canonical_value_store.h | 4 +++- toolchain/base/shared_value_stores.h | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/toolchain/base/canonical_value_store.h b/toolchain/base/canonical_value_store.h index 9fb8c33f5e94..966086196c2b 100644 --- a/toolchain/base/canonical_value_store.h +++ b/toolchain/base/canonical_value_store.h @@ -17,7 +17,9 @@ namespace Carbon { // A wrapper for accumulating immutable values with deduplication, providing IDs // to later retrieve the value. // -// `ValueT` represents the type being stored. +// `ValueT` represents the type being stored. Be careful storing non-owned +// values (e.g. `llvm::StringRef`), as the store will not prevent the +// underlying data from being freed. // // `KeyT` can optionally be different from `ValueT`, and if so is used for the // argument to `Lookup`. In this case, `ValueT` must provide a `GetAsKey` member diff --git a/toolchain/base/shared_value_stores.h b/toolchain/base/shared_value_stores.h index 328f4ef6ab42..9c52dbdd2446 100644 --- a/toolchain/base/shared_value_stores.h +++ b/toolchain/base/shared_value_stores.h @@ -25,6 +25,9 @@ class SharedValueStores : public Yaml::Printable { using IntStore = IntStore; using RealStore = ValueStore; using FloatStore = CanonicalValueStore; + // Note that this stores string references. The underlying string data must + // be stored somewhere more durable (e.g. a string literal or + // `clang::ASTContext::Idents`). using IdentifierStore = CanonicalValueStore; using StringLiteralStore = CanonicalValueStore;