diff --git a/toolchain/check/context.h b/toolchain/check/context.h index 6043f716fc2b..0573522fb8d8 100644 --- a/toolchain/check/context.h +++ b/toolchain/check/context.h @@ -348,12 +348,9 @@ class Context { // Directly expose SemIR::File data accessors for brevity in calls. - // Use `lexical_lookup().AddIdentifier` to add entries. `identifiers()` is - // const to discourage misuse. - auto identifiers() -> const StringStoreWrapper& { + auto identifiers() -> StringStoreWrapper& { return sem_ir().identifiers(); } - auto ints() -> ValueStore& { return sem_ir().ints(); } auto reals() -> ValueStore& { return sem_ir().reals(); } auto string_literal_values() -> StringStoreWrapper& { diff --git a/toolchain/check/import.cpp b/toolchain/check/import.cpp index b285c72cbef1..ff49ef46f937 100644 --- a/toolchain/check/import.cpp +++ b/toolchain/check/import.cpp @@ -57,8 +57,7 @@ static auto CopyNameFromImportIR(Context& context, if (auto import_identifier_id = import_name_id.AsIdentifierId(); import_identifier_id.is_valid()) { auto name = import_sem_ir.identifiers().Get(import_identifier_id); - return SemIR::NameId::ForIdentifier( - context.lexical_lookup().AddIdentifier(name)); + return SemIR::NameId::ForIdentifier(context.identifiers().Add(name)); } return import_name_id; } diff --git a/toolchain/check/lexical_lookup.h b/toolchain/check/lexical_lookup.h index 1b8c395ade1b..09ab07e1862f 100644 --- a/toolchain/check/lexical_lookup.h +++ b/toolchain/check/lexical_lookup.h @@ -29,36 +29,23 @@ class LexicalLookup { ScopeIndex scope_index; }; - explicit LexicalLookup(StringStoreWrapper& identifiers) - : identifiers_(&identifiers), - lookup_(identifiers_->size() + SemIR::NameId::NonIndexValueCount) {} - - ~LexicalLookup() { - CARBON_CHECK(lookup_.size() == - identifiers_->size() + SemIR::NameId::NonIndexValueCount) - << lookup_.size() << " must match " << identifiers_->size() << " + " - << SemIR::NameId::NonIndexValueCount - << "; something may have been added incorrectly"; - } - - // Handles both adding the identifier and resizing lookup_ to accommodate the - // new entry. `identifiers().Add` must not be called directly once checking - // has begun. - auto AddIdentifier(llvm::StringRef name) -> IdentifierId { - auto id = identifiers_->Add(name); - // Bear in mind that Add was not guaranteed to actually change the size. - lookup_.resize(identifiers_->size() + SemIR::NameId::NonIndexValueCount); - return id; - } + explicit LexicalLookup(const StringStoreWrapper& identifiers) + : lookup_(identifiers.size() + SemIR::NameId::NonIndexValueCount) {} // Returns the lexical lookup results for a name. auto Get(SemIR::NameId name_id) -> llvm::SmallVector& { - return lookup_[name_id.index + SemIR::NameId::NonIndexValueCount]; + size_t index = name_id.index + SemIR::NameId::NonIndexValueCount; + CARBON_CHECK(index < lookup_.size()) + << "An identifier was added after the Context was initialized. " + "Currently, we expect that new identifiers will never be used with " + "lexical lookup (they're added for things like detecting name " + "collisions in imports). That might change with metaprogramming: if " + "it does, we may need to start resizing `lookup_`, either on each " + "identifier addition or in Get` where this CHECK currently fires."; + return lookup_[index]; } private: - StringStoreWrapper* identifiers_; - // Maps identifiers to name lookup results. // TODO: Consider TinyPtrVector or similar. For now, use a small size // of 2 to cover the common case.