Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/name_scope_test.cpp (C++)

Code before:
    auto [added, entry_id] =
        name_scope.LookupOrAdd(entry2.name_id, entry2.result.target_inst_id(),
                               entry2.result.access_kind());
    EXPECT_FALSE(added);
    EXPECT_EQ(name_scope.GetEntry(entry_id), entry2);
  }

  {
    auto [added, entry_id] =
        name_scope.LookupOrAdd(entry3.name_id, entry3.result.target_inst_id(),
                               entry3.result.access_kind());
    EXPECT_FALSE(added);
    EXPECT_EQ(name_scope.GetEntry(entry_id), entry3);
  }
}

TEST(NameScope, Poison) {
  int id = 0;

  InstId scope_inst_id(++id);
  NameId scope_name_id(++id);
  NameScopeId parent_scope_id(++id);
  NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);

  NameId poison1(++id);
  LocId poisoning1(++id);
  EXPECT_EQ(name_scope.LookupOrPoison(poisoning1, poison1), std::nullopt);
  EXPECT_THAT(name_scope.entries(),
              ElementsAre(NameScope::Entry(
                  {.name_id = poison1,
                   .result = ScopeLookupResult::MakePoisoned(poisoning1)})));

  NameId poison2(++id);
  LocId poisoning2(++id);
  EXPECT_EQ(name_scope.LookupOrPoison(poisoning2, poison2), std::nullopt);
  EXPECT_THAT(
      name_scope.entries(),
      ElementsAre(NameScope::Entry(
                      {.name_id = poison1,
                       .result = ScopeLookupResult::MakePoisoned(poisoning1)}),
                  NameScope::Entry({.name_id = poison2,
                                    .result = ScopeLookupResult::MakePoisoned(
                                        poisoning2)})));

  auto lookup = name_scope.Lookup(poison1);
  ASSERT_NE(lookup, std::nullopt);
  EXPECT_THAT(name_scope.GetEntry(*lookup),
              NameScope::Entry(
                  {.name_id = poison1,
                   .result = ScopeLookupResult::MakePoisoned(poisoning1)}));
}

Review comment (suggestion):
I feel like this would be more clear and consistent:
```suggestion
  LocId poisoning_loc1(++id);
```
or
```suggestion
  LocId poisoning_loc_id1(++id);
```

Code after:
    auto [added, entry_id] =
        name_scope.LookupOrAdd(entry2.name_id, entry2.result.target_inst_id(),
                               entry2.result.access_kind());
    EXPECT_FALSE(added);
    EXPECT_EQ(name_scope.GetEntry(entry_id), entry2);
  }

  {
    auto [added, entry_id] =
        name_scope.LookupOrAdd(entry3.name_id, entry3.result.target_inst_id(),
                               entry3.result.access_kind());
    EXPECT_FALSE(added);
    EXPECT_EQ(name_scope.GetEntry(entry_id), entry3);
  }
}

TEST(NameScope, Poison) {
  int id = 0;

  InstId scope_inst_id(++id);
  NameId scope_name_id(++id);
  NameScopeId parent_scope_id(++id);
  NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);

  NameId poison1(++id);
  LocId poisoning_loc1(++id);
  EXPECT_EQ(name_scope.LookupOrPoison(poisoning_loc1, poison1), std::nullopt);
  EXPECT_THAT(
      name_scope.entries(),
      ElementsAre(NameScope::Entry(
          {.name_id = poison1,
           .result = ScopeLookupResult::MakePoisoned(poisoning_loc1)})));

  NameId poison2(++id);
  LocId poisoning_loc2(++id);
  EXPECT_EQ(name_scope.LookupOrPoison(poisoning_loc2, poison2), std::nullopt);
  EXPECT_THAT(
      name_scope.entries(),
      ElementsAre(
          NameScope::Entry(
              {.name_id = poison1,
               .result = ScopeLookupResult::MakePoisoned(poisoning_loc1)}),
          NameScope::Entry(
              {.name_id = poison2,
               .result = ScopeLookupResult::MakePoisoned(poisoning_loc2)})));

  auto lookup = name_scope.Lookup(poison1);
  ASSERT_NE(lookup, std::nullopt);
  EXPECT_THAT(name_scope.GetEntry(*lookup),
              NameScope::Entry(
                  {.name_id = poison1,