name_lookup.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/check/name_lookup.h"
  5. #include "toolchain/check/generic.h"
  6. #include "toolchain/check/import.h"
  7. #include "toolchain/check/import_cpp.h"
  8. #include "toolchain/check/import_ref.h"
  9. #include "toolchain/check/type_completion.h"
  10. #include "toolchain/diagnostics/format_providers.h"
  11. #include "toolchain/sem_ir/name_scope.h"
  12. namespace Carbon::Check {
  13. auto AddNameToLookup(Context& context, SemIR::NameId name_id,
  14. SemIR::InstId target_id, ScopeIndex scope_index) -> void {
  15. if (auto existing = context.scope_stack().LookupOrAddName(name_id, target_id,
  16. scope_index);
  17. existing.has_value()) {
  18. // TODO: Add coverage to this use case and use the location of the name
  19. // instead of the target.
  20. DiagnoseDuplicateName(context, name_id, target_id, existing);
  21. }
  22. }
  23. auto LookupNameInDecl(Context& context, SemIR::LocId loc_id,
  24. SemIR::NameId name_id, SemIR::NameScopeId scope_id,
  25. ScopeIndex scope_index) -> SemIR::ScopeLookupResult {
  26. if (!scope_id.has_value()) {
  27. // Look for a name in the specified scope or a scope nested within it only.
  28. // There are two cases where the name would be in an outer scope:
  29. //
  30. // - The name is the sole component of the declared name:
  31. //
  32. // class A;
  33. // fn F() {
  34. // class A;
  35. // }
  36. //
  37. // In this case, the inner A is not the same class as the outer A, so
  38. // lookup should not find the outer A.
  39. //
  40. // - The name is a qualifier of some larger declared name:
  41. //
  42. // class A { class B; }
  43. // fn F() {
  44. // class A.B {}
  45. // }
  46. //
  47. // In this case, we're not in the correct scope to define a member of
  48. // class A, so we should reject, and we achieve this by not finding the
  49. // name A from the outer scope.
  50. //
  51. // There is also one case where the name would be in an inner scope:
  52. //
  53. // - The name is redeclared by a parameter of the same entity:
  54. //
  55. // fn F() {
  56. // class C(C:! type);
  57. // }
  58. //
  59. // In this case, the class C is not a redeclaration of its parameter, but
  60. // we find the parameter in order to diagnose a redeclaration error.
  61. return SemIR::ScopeLookupResult::MakeWrappedLookupResult(
  62. context.scope_stack().LookupInLexicalScopesWithin(name_id, scope_index),
  63. SemIR::AccessKind::Public);
  64. } else {
  65. // We do not look into `extend`ed scopes here. A qualified name in a
  66. // declaration must specify the exact scope in which the name was originally
  67. // introduced:
  68. //
  69. // base class A { fn F(); }
  70. // class B { extend base: A; }
  71. //
  72. // // Error, no `F` in `B`.
  73. // fn B.F() {}
  74. return LookupNameInExactScope(context, loc_id, name_id, scope_id,
  75. context.name_scopes().Get(scope_id),
  76. /*is_being_declared=*/true);
  77. }
  78. }
  79. auto LookupUnqualifiedName(Context& context, Parse::NodeId node_id,
  80. SemIR::NameId name_id, bool required)
  81. -> LookupResult {
  82. // TODO: Check for shadowed lookup results.
  83. // Find the results from ancestor lexical scopes. These will be combined with
  84. // results from non-lexical scopes such as namespaces and classes.
  85. auto [lexical_result, non_lexical_scopes] =
  86. context.scope_stack().LookupInLexicalScopes(name_id);
  87. // Walk the non-lexical scopes and perform lookups into each of them.
  88. for (auto [index, lookup_scope_id, specific_id] :
  89. llvm::reverse(non_lexical_scopes)) {
  90. if (auto non_lexical_result =
  91. LookupQualifiedName(context, node_id, name_id,
  92. LookupScope{.name_scope_id = lookup_scope_id,
  93. .specific_id = specific_id},
  94. /*required=*/false);
  95. non_lexical_result.scope_result.is_found()) {
  96. return non_lexical_result;
  97. }
  98. }
  99. if (lexical_result == SemIR::InstId::InitTombstone) {
  100. CARBON_DIAGNOSTIC(UsedBeforeInitialization, Error,
  101. "`{0}` used before initialization", SemIR::NameId);
  102. context.emitter().Emit(node_id, UsedBeforeInitialization, name_id);
  103. return {.specific_id = SemIR::SpecificId::None,
  104. .scope_result = SemIR::ScopeLookupResult::MakeError()};
  105. }
  106. if (lexical_result.has_value()) {
  107. // A lexical scope never needs an associated specific. If there's a
  108. // lexically enclosing generic, then it also encloses the point of use of
  109. // the name.
  110. return {.specific_id = SemIR::SpecificId::None,
  111. .scope_result = SemIR::ScopeLookupResult::MakeFound(
  112. lexical_result, SemIR::AccessKind::Public)};
  113. }
  114. // We didn't find anything at all.
  115. if (required) {
  116. DiagnoseNameNotFound(context, node_id, name_id);
  117. }
  118. return {.specific_id = SemIR::SpecificId::None,
  119. .scope_result = SemIR::ScopeLookupResult::MakeError()};
  120. }
  121. auto LookupNameInExactScope(Context& context, SemIR::LocId loc_id,
  122. SemIR::NameId name_id, SemIR::NameScopeId scope_id,
  123. SemIR::NameScope& scope, bool is_being_declared)
  124. -> SemIR::ScopeLookupResult {
  125. if (auto entry_id = is_being_declared
  126. ? scope.Lookup(name_id)
  127. : scope.LookupOrPoison(loc_id, name_id)) {
  128. auto lookup_result = scope.GetEntry(*entry_id).result;
  129. if (!lookup_result.is_poisoned()) {
  130. LoadImportRef(context, lookup_result.target_inst_id());
  131. }
  132. return lookup_result;
  133. }
  134. if (!scope.import_ir_scopes().empty()) {
  135. // TODO: Enforce other access modifiers for imports.
  136. return SemIR::ScopeLookupResult::MakeWrappedLookupResult(
  137. ImportNameFromOtherPackage(context, loc_id, scope_id,
  138. scope.import_ir_scopes(), name_id),
  139. SemIR::AccessKind::Public);
  140. }
  141. if (scope.is_cpp_scope()) {
  142. SemIR::InstId imported_inst_id =
  143. ImportNameFromCpp(context, loc_id, scope_id, name_id);
  144. if (imported_inst_id.has_value()) {
  145. SemIR::ScopeLookupResult result = SemIR::ScopeLookupResult::MakeFound(
  146. imported_inst_id, SemIR::AccessKind::Public);
  147. scope.AddRequired({.name_id = name_id, .result = result});
  148. return result;
  149. }
  150. }
  151. return SemIR::ScopeLookupResult::MakeNotFound();
  152. }
  153. // Prints diagnostics on invalid qualified name access.
  154. static auto DiagnoseInvalidQualifiedNameAccess(Context& context, SemIRLoc loc,
  155. SemIR::InstId scope_result_id,
  156. SemIR::NameId name_id,
  157. SemIR::AccessKind access_kind,
  158. bool is_parent_access,
  159. AccessInfo access_info) -> void {
  160. auto class_type = context.insts().TryGetAs<SemIR::ClassType>(
  161. context.constant_values().GetInstId(access_info.constant_id));
  162. if (!class_type) {
  163. return;
  164. }
  165. // TODO: Support scoped entities other than just classes.
  166. const auto& class_info = context.classes().Get(class_type->class_id);
  167. auto parent_type_id = class_info.self_type_id;
  168. if (access_kind == SemIR::AccessKind::Private && is_parent_access) {
  169. if (auto base_type_id =
  170. class_info.GetBaseType(context.sem_ir(), class_type->specific_id);
  171. base_type_id.has_value()) {
  172. parent_type_id = base_type_id;
  173. } else if (auto adapted_type_id = class_info.GetAdaptedType(
  174. context.sem_ir(), class_type->specific_id);
  175. adapted_type_id.has_value()) {
  176. parent_type_id = adapted_type_id;
  177. } else {
  178. CARBON_FATAL("Expected parent for parent access");
  179. }
  180. }
  181. CARBON_DIAGNOSTIC(
  182. ClassInvalidMemberAccess, Error,
  183. "cannot access {0:private|protected} member `{1}` of type {2}",
  184. BoolAsSelect, SemIR::NameId, SemIR::TypeId);
  185. CARBON_DIAGNOSTIC(ClassMemberDeclaration, Note, "declared here");
  186. context.emitter()
  187. .Build(loc, ClassInvalidMemberAccess,
  188. access_kind == SemIR::AccessKind::Private, name_id, parent_type_id)
  189. .Note(scope_result_id, ClassMemberDeclaration)
  190. .Emit();
  191. }
  192. // Returns whether the access is prohibited by the access modifiers.
  193. static auto IsAccessProhibited(std::optional<AccessInfo> access_info,
  194. SemIR::AccessKind access_kind,
  195. bool is_parent_access) -> bool {
  196. if (!access_info) {
  197. return false;
  198. }
  199. switch (access_kind) {
  200. case SemIR::AccessKind::Public:
  201. return false;
  202. case SemIR::AccessKind::Protected:
  203. return access_info->highest_allowed_access == SemIR::AccessKind::Public;
  204. case SemIR::AccessKind::Private:
  205. return access_info->highest_allowed_access !=
  206. SemIR::AccessKind::Private ||
  207. is_parent_access;
  208. }
  209. }
  210. // Information regarding a prohibited access.
  211. struct ProhibitedAccessInfo {
  212. // The resulting inst of the lookup.
  213. SemIR::InstId scope_result_id;
  214. // The access kind of the lookup.
  215. SemIR::AccessKind access_kind;
  216. // If the lookup is from an extended scope. For example, if this is a base
  217. // class member access from a class that extends it.
  218. bool is_parent_access;
  219. };
  220. auto AppendLookupScopesForConstant(Context& context, SemIR::LocId loc_id,
  221. SemIR::ConstantId base_const_id,
  222. llvm::SmallVector<LookupScope>* scopes)
  223. -> bool {
  224. auto base_id = context.constant_values().GetInstId(base_const_id);
  225. auto base = context.insts().Get(base_id);
  226. if (auto base_as_facet_access_type = base.TryAs<SemIR::FacetAccessType>()) {
  227. // Move from the symbolic facet value up in typish-ness to its FacetType to
  228. // find a lookup scope.
  229. auto facet_type_type_id =
  230. context.insts()
  231. .Get(base_as_facet_access_type->facet_value_inst_id)
  232. .type_id();
  233. base_const_id = context.types().GetConstantId(facet_type_type_id);
  234. base_id = context.constant_values().GetInstId(base_const_id);
  235. base = context.insts().Get(base_id);
  236. }
  237. if (auto base_as_namespace = base.TryAs<SemIR::Namespace>()) {
  238. scopes->push_back(
  239. LookupScope{.name_scope_id = base_as_namespace->name_scope_id,
  240. .specific_id = SemIR::SpecificId::None});
  241. return true;
  242. }
  243. if (auto base_as_class = base.TryAs<SemIR::ClassType>()) {
  244. RequireCompleteType(
  245. context, context.types().GetTypeIdForTypeConstantId(base_const_id),
  246. loc_id, [&] {
  247. CARBON_DIAGNOSTIC(QualifiedExprInIncompleteClassScope, Error,
  248. "member access into incomplete class {0}",
  249. InstIdAsType);
  250. return context.emitter().Build(
  251. loc_id, QualifiedExprInIncompleteClassScope, base_id);
  252. });
  253. auto& class_info = context.classes().Get(base_as_class->class_id);
  254. scopes->push_back(LookupScope{.name_scope_id = class_info.scope_id,
  255. .specific_id = base_as_class->specific_id});
  256. return true;
  257. }
  258. if (auto base_as_facet_type = base.TryAs<SemIR::FacetType>()) {
  259. auto complete_id = RequireCompleteFacetType(
  260. context, context.types().GetTypeIdForTypeConstantId(base_const_id),
  261. loc_id, *base_as_facet_type, [&] {
  262. CARBON_DIAGNOSTIC(QualifiedExprInIncompleteFacetTypeScope, Error,
  263. "member access into incomplete facet type {0}",
  264. InstIdAsType);
  265. return context.emitter().Build(
  266. loc_id, QualifiedExprInIncompleteFacetTypeScope, base_id);
  267. });
  268. if (complete_id.has_value()) {
  269. const auto& resolved = context.complete_facet_types().Get(complete_id);
  270. for (const auto& interface : resolved.required_interfaces) {
  271. auto& interface_info = context.interfaces().Get(interface.interface_id);
  272. scopes->push_back({.name_scope_id = interface_info.scope_id,
  273. .specific_id = interface.specific_id});
  274. }
  275. } else {
  276. // Lookup into this scope should fail without producing an error since
  277. // `RequireCompleteFacetType` has already issued a diagnostic.
  278. scopes->push_back(LookupScope{.name_scope_id = SemIR::NameScopeId::None,
  279. .specific_id = SemIR::SpecificId::None});
  280. }
  281. return true;
  282. }
  283. if (base_const_id == SemIR::ErrorInst::SingletonConstantId) {
  284. // Lookup into this scope should fail without producing an error.
  285. scopes->push_back(LookupScope{.name_scope_id = SemIR::NameScopeId::None,
  286. .specific_id = SemIR::SpecificId::None});
  287. return true;
  288. }
  289. // TODO: Per the design, if `base_id` is any kind of type, then lookup should
  290. // treat it as a name scope, even if it doesn't have members. For example,
  291. // `(i32*).X` should fail because there's no name `X` in `i32*`, not because
  292. // there's no name `X` in `type`.
  293. return false;
  294. }
  295. // Prints a diagnostic for a missing qualified name.
  296. static auto DiagnoseMemberNameNotFound(
  297. Context& context, SemIRLoc loc, SemIR::NameId name_id,
  298. llvm::ArrayRef<LookupScope> lookup_scopes) -> void {
  299. if (lookup_scopes.size() == 1 &&
  300. lookup_scopes.front().name_scope_id.has_value()) {
  301. auto specific_id = lookup_scopes.front().specific_id;
  302. auto scope_inst_id = specific_id.has_value()
  303. ? GetInstForSpecific(context, specific_id)
  304. : context.name_scopes()
  305. .Get(lookup_scopes.front().name_scope_id)
  306. .inst_id();
  307. CARBON_DIAGNOSTIC(MemberNameNotFoundInScope, Error,
  308. "member name `{0}` not found in {1}", SemIR::NameId,
  309. InstIdAsType);
  310. context.emitter().Emit(loc, MemberNameNotFoundInScope, name_id,
  311. scope_inst_id);
  312. return;
  313. }
  314. CARBON_DIAGNOSTIC(MemberNameNotFound, Error, "member name `{0}` not found",
  315. SemIR::NameId);
  316. context.emitter().Emit(loc, MemberNameNotFound, name_id);
  317. }
  318. auto LookupQualifiedName(Context& context, SemIR::LocId loc_id,
  319. SemIR::NameId name_id,
  320. llvm::ArrayRef<LookupScope> lookup_scopes,
  321. bool required, std::optional<AccessInfo> access_info)
  322. -> LookupResult {
  323. llvm::SmallVector<LookupScope> scopes(lookup_scopes);
  324. // TODO: Support reporting of multiple prohibited access.
  325. llvm::SmallVector<ProhibitedAccessInfo> prohibited_accesses;
  326. LookupResult result = {
  327. .specific_id = SemIR::SpecificId::None,
  328. .scope_result = SemIR::ScopeLookupResult::MakeNotFound()};
  329. bool has_error = false;
  330. bool is_parent_access = false;
  331. // Walk this scope and, if nothing is found here, the scopes it extends.
  332. while (!scopes.empty()) {
  333. auto [scope_id, specific_id] = scopes.pop_back_val();
  334. if (!scope_id.has_value()) {
  335. has_error = true;
  336. continue;
  337. }
  338. auto& name_scope = context.name_scopes().Get(scope_id);
  339. has_error |= name_scope.has_error();
  340. const SemIR::ScopeLookupResult scope_result =
  341. LookupNameInExactScope(context, loc_id, name_id, scope_id, name_scope);
  342. SemIR::AccessKind access_kind = scope_result.access_kind();
  343. auto is_access_prohibited =
  344. IsAccessProhibited(access_info, access_kind, is_parent_access);
  345. // Keep track of prohibited accesses, this will be useful for reporting
  346. // multiple prohibited accesses if we can't find a suitable lookup.
  347. if (is_access_prohibited) {
  348. prohibited_accesses.push_back({
  349. .scope_result_id = scope_result.target_inst_id(),
  350. .access_kind = access_kind,
  351. .is_parent_access = is_parent_access,
  352. });
  353. }
  354. if (!scope_result.is_found() || is_access_prohibited) {
  355. // If nothing is found in this scope or if we encountered an invalid
  356. // access, look in its extended scopes.
  357. const auto& extended = name_scope.extended_scopes();
  358. scopes.reserve(scopes.size() + extended.size());
  359. for (auto extended_id : llvm::reverse(extended)) {
  360. // Substitute into the constant describing the extended scope to
  361. // determine its corresponding specific.
  362. CARBON_CHECK(extended_id.has_value());
  363. LoadImportRef(context, extended_id);
  364. SemIR::ConstantId const_id = GetConstantValueInSpecific(
  365. context.sem_ir(), specific_id, extended_id);
  366. DiagnosticAnnotationScope annotate_diagnostics(
  367. &context.emitter(), [&](auto& builder) {
  368. CARBON_DIAGNOSTIC(FromExtendHere, Note,
  369. "declared as an extended scope here");
  370. builder.Note(extended_id, FromExtendHere);
  371. });
  372. if (!AppendLookupScopesForConstant(context, loc_id, const_id,
  373. &scopes)) {
  374. // TODO: Handle case where we have a symbolic type and instead should
  375. // look in its type.
  376. }
  377. }
  378. is_parent_access |= !extended.empty();
  379. continue;
  380. }
  381. // If this is our second lookup result, diagnose an ambiguity.
  382. if (result.scope_result.is_found()) {
  383. CARBON_DIAGNOSTIC(
  384. NameAmbiguousDueToExtend, Error,
  385. "ambiguous use of name `{0}` found in multiple extended scopes",
  386. SemIR::NameId);
  387. context.emitter().Emit(loc_id, NameAmbiguousDueToExtend, name_id);
  388. // TODO: Add notes pointing to the scopes.
  389. return {.specific_id = SemIR::SpecificId::None,
  390. .scope_result = SemIR::ScopeLookupResult::MakeError()};
  391. }
  392. result.scope_result = scope_result;
  393. result.specific_id = specific_id;
  394. }
  395. if (required && !result.scope_result.is_found()) {
  396. if (!has_error) {
  397. if (prohibited_accesses.empty()) {
  398. DiagnoseMemberNameNotFound(context, loc_id, name_id, lookup_scopes);
  399. } else {
  400. // TODO: We should report multiple prohibited accesses in case we don't
  401. // find a valid lookup. Reporting the last one should suffice for now.
  402. auto [scope_result_id, access_kind, is_parent_access] =
  403. prohibited_accesses.back();
  404. // Note, `access_info` is guaranteed to have a value here, since
  405. // `prohibited_accesses` is non-empty.
  406. DiagnoseInvalidQualifiedNameAccess(context, loc_id, scope_result_id,
  407. name_id, access_kind,
  408. is_parent_access, *access_info);
  409. }
  410. }
  411. CARBON_CHECK(!result.scope_result.is_poisoned());
  412. return {.specific_id = SemIR::SpecificId::None,
  413. .scope_result = SemIR::ScopeLookupResult::MakeError()};
  414. }
  415. return result;
  416. }
  417. // Returns the scope of the Core package, or `None` if it's not found.
  418. //
  419. // TODO: Consider tracking the Core package in SemIR so we don't need to use
  420. // name lookup to find it.
  421. static auto GetCorePackage(Context& context, SemIR::LocId loc_id,
  422. llvm::StringRef name) -> SemIR::NameScopeId {
  423. auto packaging = context.parse_tree().packaging_decl();
  424. if (packaging && packaging->names.package_id == PackageNameId::Core) {
  425. return SemIR::NameScopeId::Package;
  426. }
  427. auto core_name_id = SemIR::NameId::Core;
  428. // Look up `package.Core`.
  429. auto core_scope_result = LookupNameInExactScope(
  430. context, loc_id, core_name_id, SemIR::NameScopeId::Package,
  431. context.name_scopes().Get(SemIR::NameScopeId::Package));
  432. if (core_scope_result.is_found()) {
  433. // We expect it to be a namespace.
  434. if (auto namespace_inst = context.insts().TryGetAs<SemIR::Namespace>(
  435. core_scope_result.target_inst_id())) {
  436. // TODO: Decide whether to allow the case where `Core` is not a package.
  437. return namespace_inst->name_scope_id;
  438. }
  439. }
  440. CARBON_DIAGNOSTIC(
  441. CoreNotFound, Error,
  442. "`Core.{0}` implicitly referenced here, but package `Core` not found",
  443. std::string);
  444. context.emitter().Emit(loc_id, CoreNotFound, name.str());
  445. return SemIR::NameScopeId::None;
  446. }
  447. auto LookupNameInCore(Context& context, SemIR::LocId loc_id,
  448. llvm::StringRef name) -> SemIR::InstId {
  449. auto core_package_id = GetCorePackage(context, loc_id, name);
  450. if (!core_package_id.has_value()) {
  451. return SemIR::ErrorInst::SingletonInstId;
  452. }
  453. auto name_id = SemIR::NameId::ForIdentifier(context.identifiers().Add(name));
  454. auto scope_result =
  455. LookupNameInExactScope(context, loc_id, name_id, core_package_id,
  456. context.name_scopes().Get(core_package_id));
  457. if (!scope_result.is_found()) {
  458. CARBON_DIAGNOSTIC(
  459. CoreNameNotFound, Error,
  460. "name `Core.{0}` implicitly referenced here, but not found",
  461. SemIR::NameId);
  462. context.emitter().Emit(loc_id, CoreNameNotFound, name_id);
  463. return SemIR::ErrorInst::SingletonInstId;
  464. }
  465. // Look through import_refs and aliases.
  466. return context.constant_values().GetConstantInstId(
  467. scope_result.target_inst_id());
  468. }
  469. auto DiagnoseDuplicateName(Context& context, SemIR::NameId name_id,
  470. SemIRLoc dup_def, SemIRLoc prev_def) -> void {
  471. CARBON_DIAGNOSTIC(NameDeclDuplicate, Error,
  472. "duplicate name `{0}` being declared in the same scope",
  473. SemIR::NameId);
  474. CARBON_DIAGNOSTIC(NameDeclPrevious, Note, "name is previously declared here");
  475. context.emitter()
  476. .Build(dup_def, NameDeclDuplicate, name_id)
  477. .Note(prev_def, NameDeclPrevious)
  478. .Emit();
  479. }
  480. auto DiagnosePoisonedName(Context& context, SemIR::NameId name_id,
  481. SemIR::LocId poisoning_loc_id,
  482. SemIR::LocId decl_name_loc_id) -> void {
  483. CARBON_CHECK(poisoning_loc_id.has_value(),
  484. "Trying to diagnose poisoned name with no poisoning location");
  485. CARBON_DIAGNOSTIC(NameUseBeforeDecl, Error,
  486. "name `{0}` used before it was declared", SemIR::NameId);
  487. CARBON_DIAGNOSTIC(NameUseBeforeDeclNote, Note, "declared here");
  488. context.emitter()
  489. .Build(poisoning_loc_id, NameUseBeforeDecl, name_id)
  490. .Note(decl_name_loc_id, NameUseBeforeDeclNote)
  491. .Emit();
  492. }
  493. auto DiagnoseNameNotFound(Context& context, SemIRLoc loc, SemIR::NameId name_id)
  494. -> void {
  495. CARBON_DIAGNOSTIC(NameNotFound, Error, "name `{0}` not found", SemIR::NameId);
  496. context.emitter().Emit(loc, NameNotFound, name_id);
  497. }
  498. } // namespace Carbon::Check