name_scope_test.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/sem_ir/name_scope.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. namespace Carbon::SemIR {
  8. namespace {
  9. using ::testing::ElementsAre;
  10. using ::testing::Pair;
  11. TEST(ScopeLookupResult, MakeWrappedLookupResultUsingExistingInstId) {
  12. InstId inst_id(1);
  13. auto result = ScopeLookupResult::MakeWrappedLookupResult(
  14. inst_id, AccessKind::Protected);
  15. EXPECT_FALSE(result.is_poisoned());
  16. EXPECT_TRUE(result.is_found());
  17. EXPECT_EQ(result.target_inst_id(), inst_id);
  18. EXPECT_EQ(result.access_kind(), AccessKind::Protected);
  19. EXPECT_TRUE(result == result);
  20. }
  21. TEST(ScopeLookupResult, MakeWrappedLookupResultUsingNoneInstId) {
  22. auto result = ScopeLookupResult::MakeWrappedLookupResult(
  23. InstId::None, AccessKind::Protected);
  24. EXPECT_FALSE(result.is_poisoned());
  25. EXPECT_FALSE(result.is_found());
  26. EXPECT_EQ(result.access_kind(), AccessKind::Protected);
  27. EXPECT_TRUE(result == result);
  28. }
  29. TEST(ScopeLookupResult, MakeWrappedLookupResultUsingErrorInst) {
  30. auto result = ScopeLookupResult::MakeWrappedLookupResult(ErrorInst::InstId,
  31. AccessKind::Private);
  32. EXPECT_FALSE(result.is_poisoned());
  33. EXPECT_TRUE(result.is_found());
  34. EXPECT_EQ(result.target_inst_id(), ErrorInst::InstId);
  35. EXPECT_EQ(result.access_kind(), AccessKind::Private);
  36. EXPECT_TRUE(result == result);
  37. }
  38. TEST(ScopeLookupResult, MakeFoundExisting) {
  39. InstId inst_id(1);
  40. auto result = ScopeLookupResult::MakeFound(inst_id, AccessKind::Protected);
  41. EXPECT_FALSE(result.is_poisoned());
  42. EXPECT_TRUE(result.is_found());
  43. EXPECT_EQ(result.target_inst_id(), inst_id);
  44. EXPECT_EQ(result.access_kind(), AccessKind::Protected);
  45. EXPECT_TRUE(result == result);
  46. }
  47. TEST(ScopeLookupResult, MakeNotFound) {
  48. auto result = ScopeLookupResult::MakeNotFound();
  49. EXPECT_FALSE(result.is_poisoned());
  50. EXPECT_FALSE(result.is_found());
  51. EXPECT_EQ(result.access_kind(), AccessKind::Public);
  52. EXPECT_TRUE(result == result);
  53. }
  54. TEST(ScopeLookupResult, MakePoisoned) {
  55. LocId loc_id(1);
  56. auto result = ScopeLookupResult::MakePoisoned(loc_id);
  57. EXPECT_TRUE(result.is_poisoned());
  58. EXPECT_FALSE(result.is_found());
  59. EXPECT_EQ(result.poisoning_loc_id(), loc_id);
  60. EXPECT_EQ(result.access_kind(), AccessKind::Public);
  61. EXPECT_TRUE(result == result);
  62. }
  63. TEST(ScopeLookupResult, MakeError) {
  64. auto result = ScopeLookupResult::MakeError();
  65. EXPECT_FALSE(result.is_poisoned());
  66. EXPECT_TRUE(result.is_found());
  67. EXPECT_EQ(result.target_inst_id(), ErrorInst::InstId);
  68. EXPECT_EQ(result.access_kind(), AccessKind::Public);
  69. EXPECT_TRUE(result == result);
  70. }
  71. TEST(ScopeLookupResult, EqualityPoisonedDifferent) {
  72. EXPECT_FALSE(ScopeLookupResult::MakePoisoned(LocId(1)) ==
  73. ScopeLookupResult::MakeNotFound());
  74. EXPECT_FALSE(ScopeLookupResult::MakeNotFound() ==
  75. ScopeLookupResult::MakePoisoned(LocId(1)));
  76. }
  77. TEST(ScopeLookupResult, EqualityPoisonedLocIdDifferent) {
  78. EXPECT_FALSE(ScopeLookupResult::MakePoisoned(LocId(1)) ==
  79. ScopeLookupResult::MakePoisoned(LocId(2)));
  80. }
  81. TEST(ScopeLookupResult, EqualityFoundDifferent) {
  82. EXPECT_FALSE(ScopeLookupResult::MakeFound(InstId(1), AccessKind::Public) ==
  83. ScopeLookupResult::MakeNotFound());
  84. EXPECT_FALSE(ScopeLookupResult::MakeNotFound() ==
  85. ScopeLookupResult::MakeFound(InstId(1), AccessKind::Public));
  86. }
  87. TEST(ScopeLookupResult, EqualityFoundTargetInstIdDifferent) {
  88. EXPECT_FALSE(ScopeLookupResult::MakeFound(InstId(1), AccessKind::Public) ==
  89. ScopeLookupResult::MakeFound(InstId(2), AccessKind::Public));
  90. }
  91. TEST(ScopeLookupResult, EqualityFoundAccessKindDifferent) {
  92. EXPECT_FALSE(ScopeLookupResult::MakeFound(InstId(1), AccessKind::Public) ==
  93. ScopeLookupResult::MakeFound(InstId(1), AccessKind::Protected));
  94. }
  95. TEST(ScopeLookupResult, EqualityErrorDifferent) {
  96. EXPECT_FALSE(ScopeLookupResult::MakeNotFound() ==
  97. ScopeLookupResult::MakeError());
  98. EXPECT_FALSE(ScopeLookupResult::MakeError() ==
  99. ScopeLookupResult::MakeNotFound());
  100. }
  101. TEST(NameScope, Empty) {
  102. int id = 0;
  103. InstId scope_inst_id(++id);
  104. NameId scope_name_id(++id);
  105. NameScopeId parent_scope_id(++id);
  106. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  107. EXPECT_THAT(name_scope.entries(), ElementsAre());
  108. EXPECT_THAT(name_scope.extended_scopes(), ElementsAre());
  109. EXPECT_EQ(name_scope.inst_id(), scope_inst_id);
  110. EXPECT_EQ(name_scope.name_id(), scope_name_id);
  111. EXPECT_EQ(name_scope.parent_scope_id(), parent_scope_id);
  112. EXPECT_FALSE(name_scope.has_error());
  113. EXPECT_FALSE(name_scope.is_closed_import());
  114. EXPECT_FALSE(name_scope.is_imported_package());
  115. EXPECT_THAT(name_scope.import_ir_scopes(), ElementsAre());
  116. }
  117. TEST(NameScope, Lookup) {
  118. int id = 0;
  119. InstId scope_inst_id(++id);
  120. NameId scope_name_id(++id);
  121. NameScopeId parent_scope_id(++id);
  122. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  123. NameScope::Entry entry1 = {
  124. .name_id = NameId(++id),
  125. .result = ScopeLookupResult::MakeFound(InstId(++id), AccessKind::Public)};
  126. name_scope.AddRequired(entry1);
  127. NameScope::Entry entry2 = {.name_id = NameId(++id),
  128. .result = ScopeLookupResult::MakeFound(
  129. InstId(++id), AccessKind::Protected)};
  130. name_scope.AddRequired(entry2);
  131. NameScope::Entry entry3 = {.name_id = NameId(++id),
  132. .result = ScopeLookupResult::MakeFound(
  133. InstId(++id), AccessKind::Private)};
  134. name_scope.AddRequired(entry3);
  135. auto lookup = name_scope.Lookup(entry1.name_id);
  136. ASSERT_NE(lookup, std::nullopt);
  137. EXPECT_EQ(static_cast<NameScope&>(name_scope).GetEntry(*lookup), entry1);
  138. EXPECT_EQ(static_cast<const NameScope&>(name_scope).GetEntry(*lookup),
  139. entry1);
  140. lookup = name_scope.Lookup(entry2.name_id);
  141. ASSERT_NE(lookup, std::nullopt);
  142. EXPECT_EQ(name_scope.GetEntry(*lookup), entry2);
  143. lookup = name_scope.Lookup(entry3.name_id);
  144. ASSERT_NE(lookup, std::nullopt);
  145. EXPECT_EQ(name_scope.GetEntry(*lookup), entry3);
  146. NameId unknown_name_id(++id);
  147. EXPECT_EQ(name_scope.Lookup(unknown_name_id), std::nullopt);
  148. // Check that this is different from LookupOrPoison() - doesn't get poisoned.
  149. EXPECT_EQ(name_scope.Lookup(unknown_name_id), std::nullopt);
  150. }
  151. TEST(NameScope, LookupOrPoison) {
  152. int id = 0;
  153. InstId scope_inst_id(++id);
  154. NameId scope_name_id(++id);
  155. NameScopeId parent_scope_id(++id);
  156. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  157. NameScope::Entry entry1 = {
  158. .name_id = NameId(++id),
  159. .result = ScopeLookupResult::MakeFound(InstId(++id), AccessKind::Public)};
  160. name_scope.AddRequired(entry1);
  161. NameScope::Entry entry2 = {.name_id = NameId(++id),
  162. .result = ScopeLookupResult::MakeFound(
  163. InstId(++id), AccessKind::Protected)};
  164. name_scope.AddRequired(entry2);
  165. NameScope::Entry entry3 = {.name_id = NameId(++id),
  166. .result = ScopeLookupResult::MakeFound(
  167. InstId(++id), AccessKind::Private)};
  168. name_scope.AddRequired(entry3);
  169. LocId poisoning_loc_id_known_entries(++id);
  170. auto lookup =
  171. name_scope.LookupOrPoison(poisoning_loc_id_known_entries, entry1.name_id);
  172. ASSERT_NE(lookup, std::nullopt);
  173. EXPECT_EQ(static_cast<NameScope&>(name_scope).GetEntry(*lookup), entry1);
  174. EXPECT_EQ(static_cast<const NameScope&>(name_scope).GetEntry(*lookup),
  175. entry1);
  176. lookup =
  177. name_scope.LookupOrPoison(poisoning_loc_id_known_entries, entry2.name_id);
  178. ASSERT_NE(lookup, std::nullopt);
  179. EXPECT_EQ(name_scope.GetEntry(*lookup), entry2);
  180. lookup =
  181. name_scope.LookupOrPoison(poisoning_loc_id_known_entries, entry3.name_id);
  182. ASSERT_NE(lookup, std::nullopt);
  183. EXPECT_EQ(name_scope.GetEntry(*lookup), entry3);
  184. NameId unknown_name_id(++id);
  185. LocId poisoning_loc_id_unknown_entry(++id);
  186. EXPECT_EQ(name_scope.LookupOrPoison(poisoning_loc_id_unknown_entry,
  187. unknown_name_id),
  188. std::nullopt);
  189. // Check that this is different from Lookup() - does get poisoned.
  190. lookup = name_scope.Lookup(unknown_name_id);
  191. ASSERT_NE(lookup, std::nullopt);
  192. EXPECT_EQ(name_scope.GetEntry(*lookup).result,
  193. ScopeLookupResult::MakePoisoned(poisoning_loc_id_unknown_entry));
  194. }
  195. TEST(NameScope, LookupOrPoisonNotIdentifier) {
  196. int id = 0;
  197. InstId scope_inst_id(++id);
  198. NameId scope_name_id(++id);
  199. NameScopeId parent_scope_id(++id);
  200. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  201. LocId poisoning_loc_id(++id);
  202. EXPECT_EQ(name_scope.LookupOrPoison(poisoning_loc_id, NameId::SelfType),
  203. std::nullopt);
  204. // Check that this is different from the identifier use case - doesn't get
  205. // poisoned.
  206. EXPECT_EQ(name_scope.Lookup(NameId::SelfType), std::nullopt);
  207. }
  208. TEST(NameScope, LookupOrAdd) {
  209. int id = 0;
  210. InstId scope_inst_id(++id);
  211. NameId scope_name_id(++id);
  212. NameScopeId parent_scope_id(++id);
  213. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  214. NameScope::Entry entry1 = {
  215. .name_id = NameId(++id),
  216. .result = ScopeLookupResult::MakeFound(InstId(++id), AccessKind::Public)};
  217. {
  218. auto [added, entry_id] =
  219. name_scope.LookupOrAdd(entry1.name_id, entry1.result.target_inst_id(),
  220. entry1.result.access_kind());
  221. EXPECT_TRUE(added);
  222. EXPECT_EQ(name_scope.GetEntry(entry_id), entry1);
  223. }
  224. NameScope::Entry entry2 = {.name_id = NameId(++id),
  225. .result = ScopeLookupResult::MakeFound(
  226. InstId(++id), AccessKind::Protected)};
  227. {
  228. auto [added, entry_id] =
  229. name_scope.LookupOrAdd(entry2.name_id, entry2.result.target_inst_id(),
  230. entry2.result.access_kind());
  231. EXPECT_TRUE(added);
  232. EXPECT_EQ(name_scope.GetEntry(entry_id), entry2);
  233. }
  234. NameScope::Entry entry3 = {.name_id = NameId(++id),
  235. .result = ScopeLookupResult::MakeFound(
  236. InstId(++id), AccessKind::Private)};
  237. {
  238. auto [added, entry_id] =
  239. name_scope.LookupOrAdd(entry3.name_id, entry3.result.target_inst_id(),
  240. entry3.result.access_kind());
  241. EXPECT_TRUE(added);
  242. EXPECT_EQ(name_scope.GetEntry(entry_id), entry3);
  243. }
  244. {
  245. auto [added, entry_id] =
  246. name_scope.LookupOrAdd(entry1.name_id, entry1.result.target_inst_id(),
  247. entry1.result.access_kind());
  248. EXPECT_FALSE(added);
  249. EXPECT_EQ(name_scope.GetEntry(entry_id), entry1);
  250. }
  251. {
  252. auto [added, entry_id] =
  253. name_scope.LookupOrAdd(entry2.name_id, entry2.result.target_inst_id(),
  254. entry2.result.access_kind());
  255. EXPECT_FALSE(added);
  256. EXPECT_EQ(name_scope.GetEntry(entry_id), entry2);
  257. }
  258. {
  259. auto [added, entry_id] =
  260. name_scope.LookupOrAdd(entry3.name_id, entry3.result.target_inst_id(),
  261. entry3.result.access_kind());
  262. EXPECT_FALSE(added);
  263. EXPECT_EQ(name_scope.GetEntry(entry_id), entry3);
  264. }
  265. }
  266. TEST(NameScope, Poison) {
  267. int id = 0;
  268. InstId scope_inst_id(++id);
  269. NameId scope_name_id(++id);
  270. NameScopeId parent_scope_id(++id);
  271. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  272. NameId poison1(++id);
  273. LocId poisoning_loc1(++id);
  274. EXPECT_EQ(name_scope.LookupOrPoison(poisoning_loc1, poison1), std::nullopt);
  275. EXPECT_THAT(
  276. name_scope.entries(),
  277. ElementsAre(NameScope::Entry(
  278. {.name_id = poison1,
  279. .result = ScopeLookupResult::MakePoisoned(poisoning_loc1)})));
  280. NameId poison2(++id);
  281. LocId poisoning_loc2(++id);
  282. EXPECT_EQ(name_scope.LookupOrPoison(poisoning_loc2, poison2), std::nullopt);
  283. EXPECT_THAT(
  284. name_scope.entries(),
  285. ElementsAre(
  286. NameScope::Entry(
  287. {.name_id = poison1,
  288. .result = ScopeLookupResult::MakePoisoned(poisoning_loc1)}),
  289. NameScope::Entry(
  290. {.name_id = poison2,
  291. .result = ScopeLookupResult::MakePoisoned(poisoning_loc2)})));
  292. auto lookup = name_scope.Lookup(poison1);
  293. ASSERT_NE(lookup, std::nullopt);
  294. EXPECT_THAT(name_scope.GetEntry(*lookup),
  295. NameScope::Entry(
  296. {.name_id = poison1,
  297. .result = ScopeLookupResult::MakePoisoned(poisoning_loc1)}));
  298. }
  299. TEST(NameScope, AddRequiredAfterPoison) {
  300. int id = 0;
  301. InstId scope_inst_id(++id);
  302. NameId scope_name_id(++id);
  303. NameScopeId parent_scope_id(++id);
  304. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  305. NameId name_id(++id);
  306. InstId inst_id(++id);
  307. LocId poisoning_loc_id(++id);
  308. EXPECT_EQ(name_scope.LookupOrPoison(poisoning_loc_id, name_id), std::nullopt);
  309. EXPECT_THAT(
  310. name_scope.entries(),
  311. ElementsAre(NameScope::Entry(
  312. {.name_id = name_id,
  313. .result = ScopeLookupResult::MakePoisoned(poisoning_loc_id)})));
  314. NameScope::Entry entry = {
  315. .name_id = name_id,
  316. .result = ScopeLookupResult::MakeFound(inst_id, AccessKind::Private)};
  317. name_scope.AddRequired(entry);
  318. auto lookup = name_scope.LookupOrPoison(poisoning_loc_id, name_id);
  319. ASSERT_NE(lookup, std::nullopt);
  320. EXPECT_EQ(name_scope.GetEntry(*lookup),
  321. NameScope::Entry({.name_id = name_id,
  322. .result = ScopeLookupResult::MakeFound(
  323. inst_id, AccessKind::Private)}));
  324. }
  325. TEST(NameScope, ExtendedScopes) {
  326. int id = 0;
  327. InstId scope_inst_id(++id);
  328. NameId scope_name_id(++id);
  329. NameScopeId parent_scope_id = NameScopeId::Package;
  330. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  331. EXPECT_THAT(name_scope.extended_scopes(), ElementsAre());
  332. InstId extended_scope1(++id);
  333. name_scope.AddExtendedScope(extended_scope1);
  334. EXPECT_THAT(name_scope.extended_scopes(), ElementsAre(extended_scope1));
  335. InstId extended_scope2(++id);
  336. name_scope.AddExtendedScope(extended_scope2);
  337. EXPECT_THAT(name_scope.extended_scopes(),
  338. ElementsAre(extended_scope1, extended_scope2));
  339. }
  340. TEST(NameScope, HasError) {
  341. int id = 0;
  342. InstId scope_inst_id(++id);
  343. NameId scope_name_id(++id);
  344. NameScopeId parent_scope_id(++id);
  345. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  346. EXPECT_FALSE(name_scope.has_error());
  347. name_scope.set_has_error();
  348. EXPECT_TRUE(name_scope.has_error());
  349. name_scope.set_has_error();
  350. EXPECT_TRUE(name_scope.has_error());
  351. }
  352. TEST(NameScope, IsClosedImport) {
  353. int id = 0;
  354. InstId scope_inst_id(++id);
  355. NameId scope_name_id(++id);
  356. NameScopeId parent_scope_id(++id);
  357. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  358. EXPECT_FALSE(name_scope.is_closed_import());
  359. name_scope.set_is_closed_import(true);
  360. EXPECT_TRUE(name_scope.is_closed_import());
  361. name_scope.set_is_closed_import(false);
  362. EXPECT_FALSE(name_scope.is_closed_import());
  363. }
  364. TEST(NameScope, IsImportedPackageParentNonPackageScope) {
  365. int id = 0;
  366. InstId scope_inst_id(++id);
  367. NameId scope_name_id(++id);
  368. NameScopeId parent_scope_id(++id);
  369. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  370. EXPECT_FALSE(name_scope.is_imported_package());
  371. name_scope.set_is_closed_import(true);
  372. EXPECT_FALSE(name_scope.is_imported_package());
  373. name_scope.set_is_closed_import(false);
  374. EXPECT_FALSE(name_scope.is_imported_package());
  375. }
  376. TEST(NameScope, IsImportedPackageParentPackageScope) {
  377. int id = 0;
  378. InstId scope_inst_id(++id);
  379. NameId scope_name_id(++id);
  380. NameScopeId parent_scope_id = NameScopeId::Package;
  381. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  382. EXPECT_FALSE(name_scope.is_imported_package());
  383. name_scope.set_is_closed_import(true);
  384. EXPECT_TRUE(name_scope.is_imported_package());
  385. name_scope.set_is_closed_import(false);
  386. EXPECT_FALSE(name_scope.is_imported_package());
  387. }
  388. TEST(NameScope, ImportIRScopes) {
  389. int id = 0;
  390. InstId scope_inst_id(++id);
  391. NameId scope_name_id(++id);
  392. NameScopeId parent_scope_id = NameScopeId::Package;
  393. NameScope name_scope(scope_inst_id, scope_name_id, parent_scope_id);
  394. EXPECT_THAT(name_scope.import_ir_scopes(), ElementsAre());
  395. ImportIRId import_ir_id1(++id);
  396. NameScopeId import_name_scope_id1(++id);
  397. name_scope.AddImportIRScope({import_ir_id1, import_name_scope_id1});
  398. EXPECT_THAT(name_scope.import_ir_scopes(),
  399. ElementsAre(Pair(import_ir_id1, import_name_scope_id1)));
  400. ImportIRId import_ir_id2(++id);
  401. NameScopeId import_name_scope_id2(++id);
  402. name_scope.AddImportIRScope({import_ir_id2, import_name_scope_id2});
  403. EXPECT_THAT(name_scope.import_ir_scopes(),
  404. ElementsAre(Pair(import_ir_id1, import_name_scope_id1),
  405. Pair(import_ir_id2, import_name_scope_id2)));
  406. }
  407. } // namespace
  408. } // namespace Carbon::SemIR