handle_binding_pattern.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 <utility>
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/facet_type.h"
  9. #include "toolchain/check/handle.h"
  10. #include "toolchain/check/inst.h"
  11. #include "toolchain/check/interface.h"
  12. #include "toolchain/check/name_lookup.h"
  13. #include "toolchain/check/pattern.h"
  14. #include "toolchain/check/return.h"
  15. #include "toolchain/check/type.h"
  16. #include "toolchain/check/type_completion.h"
  17. #include "toolchain/check/unused.h"
  18. #include "toolchain/diagnostics/format_providers.h"
  19. #include "toolchain/parse/node_ids.h"
  20. #include "toolchain/sem_ir/ids.h"
  21. #include "toolchain/sem_ir/inst.h"
  22. #include "toolchain/sem_ir/pattern.h"
  23. #include "toolchain/sem_ir/typed_insts.h"
  24. namespace Carbon::Check {
  25. auto HandleParseNode(Context& context, Parse::UnderscoreNameId node_id)
  26. -> bool {
  27. context.node_stack().Push(node_id, SemIR::NameId::Underscore);
  28. return true;
  29. }
  30. // Returns the `InstKind` corresponding to the pattern's `NodeKind`.
  31. static auto GetPatternInstKind(Parse::NodeKind node_kind, bool is_ref)
  32. -> SemIR::InstKind {
  33. switch (node_kind) {
  34. case Parse::NodeKind::CompileTimeBindingPattern:
  35. return SemIR::InstKind::SymbolicBindingPattern;
  36. case Parse::NodeKind::LetBindingPattern:
  37. return is_ref ? SemIR::InstKind::RefBindingPattern
  38. : SemIR::InstKind::ValueBindingPattern;
  39. case Parse::NodeKind::VarBindingPattern:
  40. return SemIR::InstKind::RefBindingPattern;
  41. case Parse::NodeKind::FormBindingPattern:
  42. return SemIR::InstKind::FormBindingPattern;
  43. default:
  44. CARBON_FATAL("Unexpected node kind: {0}", node_kind);
  45. }
  46. }
  47. // Returns true if a parameter is valid in the given `introducer_kind`.
  48. static auto IsValidParamForIntroducer(Context& context, Parse::NodeId node_id,
  49. SemIR::NameId name_id,
  50. Lex::TokenKind introducer_kind,
  51. bool is_generic) -> bool {
  52. switch (introducer_kind) {
  53. case Lex::TokenKind::Fn: {
  54. if (context.full_pattern_stack().CurrentKind() ==
  55. FullPatternStack::Kind::ImplicitParamList &&
  56. !(is_generic || name_id == SemIR::NameId::SelfValue)) {
  57. CARBON_DIAGNOSTIC(
  58. ImplictParamMustBeConstant, Error,
  59. "implicit parameters of functions must be constant or `self`");
  60. context.emitter().Emit(node_id, ImplictParamMustBeConstant);
  61. return false;
  62. }
  63. // Parameters can have incomplete types in a function declaration, but not
  64. // in a function definition. We don't know which kind we have here, so
  65. // don't validate it.
  66. return true;
  67. }
  68. case Lex::TokenKind::Choice:
  69. if (context.scope_stack().PeekInstId().has_value()) {
  70. // We are building a pattern for a choice alternative, not the
  71. // choice type itself.
  72. // Implicit param lists are prevented during parse.
  73. CARBON_CHECK(context.full_pattern_stack().CurrentKind() !=
  74. FullPatternStack::Kind::ImplicitParamList,
  75. "choice alternative with implicit parameters");
  76. // Don't fall through to the `Class` logic for choice alternatives.
  77. return true;
  78. }
  79. [[fallthrough]];
  80. case Lex::TokenKind::Class:
  81. case Lex::TokenKind::Impl:
  82. case Lex::TokenKind::Interface: {
  83. if (name_id == SemIR::NameId::SelfValue) {
  84. CARBON_DIAGNOSTIC(SelfParameterNotAllowed, Error,
  85. "`self` parameter only allowed on functions");
  86. context.emitter().Emit(node_id, SelfParameterNotAllowed);
  87. return false;
  88. }
  89. if (!is_generic) {
  90. CARBON_DIAGNOSTIC(GenericParamMustBeConstant, Error,
  91. "parameters of generic types must be constant");
  92. context.emitter().Emit(node_id, GenericParamMustBeConstant);
  93. return false;
  94. }
  95. return true;
  96. }
  97. default:
  98. return true;
  99. }
  100. }
  101. namespace {
  102. // Information about the expression in the type position of a binding pattern,
  103. // i.e. the position following the `:`/`:?`/`:!` separator. Note that this
  104. // expression may be interpreted as a type or a form, depending on the binding
  105. // kind.
  106. struct BindingPatternTypeInfo {
  107. // The parse node representing the expression.
  108. Parse::AnyExprId node_id;
  109. // The inst representing the converted value of that expression. For a `:?`
  110. // binding the expression is converted to type `Core.Form`; otherwise it is
  111. // converted to type `type`.
  112. SemIR::InstId inst_id;
  113. // For a `:?` binding this is the type component of the form denoted by
  114. // `inst_id`. Otherwise this is the type denoted by `inst_id`.
  115. SemIR::TypeId type_component_id;
  116. };
  117. } // namespace
  118. // Handle the type position of a binding pattern.
  119. static auto HandleAnyBindingPatternType(Context& context,
  120. Parse::NodeKind node_kind)
  121. -> BindingPatternTypeInfo {
  122. auto [node_id, original_inst_id] = context.node_stack().PopExprWithNodeId();
  123. if (node_kind == Parse::FormBindingPattern::Kind) {
  124. auto as_form = FormExprAsForm(context, node_id, original_inst_id);
  125. return {.node_id = node_id,
  126. .inst_id = as_form.form_inst_id,
  127. .type_component_id = as_form.type_component_id};
  128. } else {
  129. auto as_type = ExprAsType(context, node_id, original_inst_id);
  130. return {.node_id = node_id,
  131. .inst_id = as_type.inst_id,
  132. .type_component_id = as_type.type_id};
  133. }
  134. }
  135. // TODO: make this function shorter by factoring pieces out.
  136. static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
  137. Parse::NodeKind node_kind,
  138. bool is_unused = false) -> bool {
  139. auto type_expr = HandleAnyBindingPatternType(context, node_kind);
  140. if (context.types()
  141. .GetAsInst(type_expr.type_component_id)
  142. .Is<SemIR::TypeComponentOf>()) {
  143. return context.TODO(node_id, "Support symbolic form bindings");
  144. }
  145. SemIR::ExprRegionId type_expr_region_id =
  146. EndSubpatternAsExpr(context, type_expr.inst_id);
  147. // The name in a generic binding may be wrapped in `template`.
  148. bool is_generic = node_kind == Parse::NodeKind::CompileTimeBindingPattern;
  149. bool is_template =
  150. context.node_stack()
  151. .PopAndDiscardSoloNodeIdIf<Parse::NodeKind::TemplateBindingName>();
  152. // A non-generic template binding is diagnosed by the parser.
  153. is_template &= is_generic;
  154. // The name in a runtime binding may be wrapped in `ref`.
  155. bool is_ref =
  156. context.node_stack()
  157. .PopAndDiscardSoloNodeIdIf<Parse::NodeKind::RefBindingName>();
  158. SemIR::InstKind pattern_inst_kind = GetPatternInstKind(node_kind, is_ref);
  159. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  160. const DeclIntroducerState& introducer =
  161. context.decl_introducer_state_stack().innermost();
  162. auto make_binding_pattern = [&]() -> SemIR::InstId {
  163. // TODO: Eventually the name will need to support associations with other
  164. // scopes, but right now we don't support qualified names here.
  165. auto binding = AddBindingPattern(
  166. context, name_node, name_id, type_expr.type_component_id,
  167. context.constant_values().Get(type_expr.inst_id), type_expr_region_id,
  168. pattern_inst_kind, is_template, is_unused);
  169. // TODO: If `is_generic`, then `binding.bind_id is a SymbolicBinding. Subst
  170. // the `.Self` of type `type` in the `cast_type_id` type (a `FacetType`)
  171. // with the `binding.bind_id` itself, and build a new pattern with that.
  172. // This is kind of cyclical. So we need to reuse the EntityNameId, which
  173. // will also reuse the CompileTimeBinding for the new SymbolicBinding.
  174. if (name_id != SemIR::NameId::Underscore) {
  175. // Add name to lookup immediately, so it can be used in the rest of the
  176. // enclosing pattern.
  177. auto name_context =
  178. context.decl_name_stack().MakeUnqualifiedName(name_node, name_id);
  179. context.decl_name_stack().AddNameOrDiagnose(
  180. name_context, binding.bind_id,
  181. introducer.modifier_set.GetAccessKind());
  182. context.full_pattern_stack().AddBindName(name_id);
  183. }
  184. return binding.pattern_id;
  185. };
  186. auto abstract_diagnostic_context = [&](auto& builder) {
  187. CARBON_DIAGNOSTIC(AbstractTypeInVarPattern, Context,
  188. "binding pattern has abstract type {0} in `var` "
  189. "pattern",
  190. SemIR::TypeId);
  191. builder.Context(type_expr.node_id, AbstractTypeInVarPattern,
  192. type_expr.type_component_id);
  193. };
  194. // A `self` binding can only appear in an implicit parameter list.
  195. if (name_id == SemIR::NameId::SelfValue &&
  196. !context.node_stack().PeekIs(Parse::NodeKind::ImplicitParamListStart)) {
  197. CARBON_DIAGNOSTIC(
  198. SelfOutsideImplicitParamList, Error,
  199. "`self` can only be declared in an implicit parameter list");
  200. context.emitter().Emit(node_id, SelfOutsideImplicitParamList);
  201. }
  202. if (node_kind == Parse::NodeKind::CompileTimeBindingPattern &&
  203. introducer.kind == Lex::TokenKind::Let) {
  204. // TODO: We should re-evaluate the contents of the eval block in a
  205. // synthesized specific to form these values, in order to propagate the
  206. // values.
  207. return context.TODO(node_id,
  208. "local `let :!` bindings are currently unsupported");
  209. }
  210. // Allocate an instruction of the appropriate kind, linked to the name for
  211. // error locations.
  212. switch (context.full_pattern_stack().CurrentKind()) {
  213. case FullPatternStack::Kind::ImplicitParamList:
  214. case FullPatternStack::Kind::ExplicitParamList: {
  215. if (!IsValidParamForIntroducer(context, node_id, name_id, introducer.kind,
  216. is_generic)) {
  217. if (name_id != SemIR::NameId::Underscore) {
  218. AddNameToLookup(context, name_id, SemIR::ErrorInst::InstId);
  219. }
  220. // Replace the parameter with `ErrorInst` so that we don't try
  221. // constructing a generic based on it.
  222. context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
  223. break;
  224. }
  225. // Using `AsConcreteType` here causes `fn F[var self: Self]();`
  226. // to fail since `Self` is an incomplete type.
  227. if (node_kind == Parse::NodeKind::VarBindingPattern) {
  228. auto [unqualified_type_id, qualifiers] =
  229. context.types().GetUnqualifiedTypeAndQualifiers(
  230. type_expr.type_component_id);
  231. if ((qualifiers & SemIR::TypeQualifiers::Partial) !=
  232. SemIR::TypeQualifiers::Partial &&
  233. context.types().Is<SemIR::ClassType>(unqualified_type_id)) {
  234. auto class_type =
  235. context.types().GetAs<SemIR::ClassType>(unqualified_type_id);
  236. auto& class_info = context.classes().Get(class_type.class_id);
  237. if (class_info.inheritance_kind ==
  238. SemIR::Class::InheritanceKind::Abstract) {
  239. Diagnostics::ContextScope scope(&context.emitter(),
  240. abstract_diagnostic_context);
  241. DiagnoseAbstractClass(context, class_type.class_id,
  242. /*direct_use=*/true);
  243. type_expr.type_component_id = SemIR::ErrorInst::TypeId;
  244. }
  245. }
  246. }
  247. auto result_inst_id = make_binding_pattern();
  248. // A binding pattern in a function signature is a `Call` parameter
  249. // unless it's nested inside a `var` pattern (because then the
  250. // enclosing `var` pattern is), or it's a compile-time binding pattern
  251. // (because then it's not passed to the `Call` inst).
  252. if (node_kind == Parse::NodeKind::LetBindingPattern ||
  253. node_kind == Parse::NodeKind::FormBindingPattern) {
  254. auto type_id = context.insts().GetAttachedType(result_inst_id);
  255. if (is_ref) {
  256. result_inst_id = AddPatternInst<SemIR::RefParamPattern>(
  257. context, node_id,
  258. {.type_id = type_id, .subpattern_id = result_inst_id});
  259. } else if (node_kind == Parse::NodeKind::FormBindingPattern) {
  260. result_inst_id = AddPatternInst<SemIR::FormParamPattern>(
  261. context, node_id,
  262. {.type_id = type_id, .subpattern_id = result_inst_id});
  263. } else {
  264. result_inst_id = AddPatternInst<SemIR::ValueParamPattern>(
  265. context, node_id,
  266. {.type_id = type_id, .subpattern_id = result_inst_id});
  267. }
  268. }
  269. context.node_stack().Push(node_id, result_inst_id);
  270. break;
  271. }
  272. case FullPatternStack::Kind::NameBindingDecl: {
  273. auto incomplete_diagnostic_context = [&](auto& builder) {
  274. CARBON_DIAGNOSTIC(IncompleteTypeInBindingDecl, Context,
  275. "binding pattern has incomplete type {0} in name "
  276. "binding declaration",
  277. InstIdAsType);
  278. builder.Context(type_expr.node_id, IncompleteTypeInBindingDecl,
  279. type_expr.inst_id);
  280. };
  281. if (node_kind == Parse::NodeKind::VarBindingPattern) {
  282. if (!RequireConcreteType(
  283. context, type_expr.type_component_id, type_expr.node_id,
  284. incomplete_diagnostic_context, abstract_diagnostic_context)) {
  285. type_expr.type_component_id = SemIR::ErrorInst::TypeId;
  286. }
  287. } else {
  288. if (!RequireCompleteType(context, type_expr.type_component_id,
  289. type_expr.node_id,
  290. incomplete_diagnostic_context)) {
  291. type_expr.type_component_id = SemIR::ErrorInst::TypeId;
  292. }
  293. }
  294. auto binding_pattern_id = make_binding_pattern();
  295. if (node_kind == Parse::NodeKind::VarBindingPattern) {
  296. CARBON_CHECK(!is_generic);
  297. if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Returned)) {
  298. // TODO: Should we check this for the `var` as a whole, rather than
  299. // for the name binding?
  300. auto bind_id = context.bind_name_map()
  301. .Lookup(binding_pattern_id)
  302. .value()
  303. .bind_name_id;
  304. RegisterReturnedVar(
  305. context, introducer.modifier_node_id(ModifierOrder::Decl),
  306. type_expr.node_id, type_expr.type_component_id, bind_id, name_id);
  307. }
  308. }
  309. context.node_stack().Push(node_id, binding_pattern_id);
  310. break;
  311. }
  312. case FullPatternStack::Kind::NotInEitherParamList:
  313. CARBON_FATAL("Unreachable");
  314. }
  315. return true;
  316. }
  317. auto HandleParseNode(Context& context, Parse::LetBindingPatternId node_id)
  318. -> bool {
  319. return HandleAnyBindingPattern(context, node_id,
  320. Parse::NodeKind::LetBindingPattern);
  321. }
  322. auto HandleParseNode(Context& context, Parse::VarBindingPatternId node_id)
  323. -> bool {
  324. return HandleAnyBindingPattern(context, node_id,
  325. Parse::NodeKind::VarBindingPattern);
  326. }
  327. auto HandleParseNode(Context& context, Parse::FormBindingPatternId node_id)
  328. -> bool {
  329. return HandleAnyBindingPattern(context, node_id,
  330. Parse::NodeKind::FormBindingPattern);
  331. }
  332. auto HandleParseNode(Context& context,
  333. Parse::CompileTimeBindingPatternStartId /*node_id*/)
  334. -> bool {
  335. // Make a scope to contain the `.Self` facet value for use in the type of the
  336. // compile time binding. This is popped when handling the
  337. // CompileTimeBindingPatternId.
  338. context.scope_stack().PushForSameRegion();
  339. // The `.Self` must have a type of `FacetType`, so that it gets wrapped in
  340. // `FacetAccessType` when used in a type position, such as in `U:! I(.Self)`.
  341. // This allows substitution with other facet values without requiring an
  342. // additional `FacetAccessType` to be inserted.
  343. auto type_id = GetEmptyFacetType(context);
  344. MakePeriodSelfFacetValue(context, type_id);
  345. return true;
  346. }
  347. auto HandleParseNode(Context& context,
  348. Parse::CompileTimeBindingPatternId node_id) -> bool {
  349. // Pop the `.Self` facet value name introduced by the
  350. // CompileTimeBindingPatternStart.
  351. context.scope_stack().Pop(/*check_unused=*/true);
  352. auto node_kind = Parse::NodeKind::CompileTimeBindingPattern;
  353. const DeclIntroducerState& introducer =
  354. context.decl_introducer_state_stack().innermost();
  355. if (introducer.kind == Lex::TokenKind::Let) {
  356. // Disallow `let` outside of function and interface definitions.
  357. // TODO: Find a less brittle way of doing this. A `scope_inst_id` of `None`
  358. // can represent a block scope, but is also used for other kinds of scopes
  359. // that aren't necessarily part of a function decl.
  360. // We don't need to check if the scope is an interface here as this is
  361. // already caught in the parse phase by the separated associated constant
  362. // logic.
  363. auto scope_inst_id = context.scope_stack().PeekInstId();
  364. if (scope_inst_id.has_value()) {
  365. auto scope_inst = context.insts().Get(scope_inst_id);
  366. if (!scope_inst.Is<SemIR::FunctionDecl>()) {
  367. context.TODO(
  368. node_id,
  369. "`let` compile time binding outside function or interface");
  370. node_kind = Parse::NodeKind::LetBindingPattern;
  371. }
  372. }
  373. }
  374. return HandleAnyBindingPattern(context, node_id, node_kind);
  375. }
  376. auto HandleParseNode(Context& context,
  377. Parse::AssociatedConstantNameAndTypeId node_id) -> bool {
  378. auto [type_node, parsed_type_id] = context.node_stack().PopExprWithNodeId();
  379. auto [cast_type_inst_id, cast_type_id] =
  380. ExprAsType(context, type_node, parsed_type_id);
  381. EndSubpatternAsExpr(context, cast_type_inst_id);
  382. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  383. if (name_id == SemIR::NameId::Underscore) {
  384. // The action item here may be to document this as not allowed, and
  385. // add a proper diagnostic.
  386. context.TODO(node_id, "_ used as associated constant name");
  387. }
  388. SemIR::AssociatedConstantDecl assoc_const_decl = {
  389. .type_id = cast_type_id,
  390. .assoc_const_id = SemIR::AssociatedConstantId::None,
  391. .decl_block_id = SemIR::InstBlockId::None};
  392. auto decl_id =
  393. AddPlaceholderInstInNoBlock(context, node_id, assoc_const_decl);
  394. assoc_const_decl.assoc_const_id = context.associated_constants().Add(
  395. {.name_id = name_id,
  396. .parent_scope_id = context.scope_stack().PeekNameScopeId(),
  397. .decl_id = decl_id,
  398. .default_value_id = SemIR::InstId::None});
  399. ReplaceInstBeforeConstantUse(context, decl_id, assoc_const_decl);
  400. context.node_stack().Push(node_id, decl_id);
  401. return true;
  402. }
  403. auto HandleParseNode(Context& context, Parse::FieldNameAndTypeId node_id)
  404. -> bool {
  405. auto [type_node, parsed_type_id] = context.node_stack().PopExprWithNodeId();
  406. auto [cast_type_inst_id, cast_type_id] =
  407. ExprAsType(context, type_node, parsed_type_id);
  408. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  409. auto parent_class_decl =
  410. context.scope_stack().TryGetCurrentScopeAs<SemIR::ClassDecl>();
  411. CARBON_CHECK(parent_class_decl);
  412. if (!RequireConcreteType(
  413. context, cast_type_id, type_node,
  414. [&](auto& builder) {
  415. CARBON_DIAGNOSTIC(IncompleteTypeInFieldDecl, Context,
  416. "field has incomplete type {0}", SemIR::TypeId);
  417. builder.Context(type_node, IncompleteTypeInFieldDecl, cast_type_id);
  418. },
  419. [&](auto& builder) {
  420. CARBON_DIAGNOSTIC(AbstractTypeInFieldDecl, Context,
  421. "field has abstract type {0}", SemIR::TypeId);
  422. builder.Context(type_node, AbstractTypeInFieldDecl, cast_type_id);
  423. })) {
  424. cast_type_id = SemIR::ErrorInst::TypeId;
  425. }
  426. if (cast_type_id == SemIR::ErrorInst::TypeId) {
  427. cast_type_inst_id = SemIR::ErrorInst::TypeInstId;
  428. }
  429. auto& class_info = context.classes().Get(parent_class_decl->class_id);
  430. auto field_type_id = GetUnboundElementType(
  431. context, context.types().GetTypeInstId(class_info.self_type_id),
  432. cast_type_inst_id);
  433. auto field_id =
  434. AddInst<SemIR::FieldDecl>(context, node_id,
  435. {.type_id = field_type_id,
  436. .name_id = name_id,
  437. .index = SemIR::ElementIndex::None});
  438. context.field_decls_stack().AppendToTop(field_id);
  439. auto name_context =
  440. context.decl_name_stack().MakeUnqualifiedName(node_id, name_id);
  441. context.decl_name_stack().AddNameOrDiagnose(
  442. name_context, field_id,
  443. context.decl_introducer_state_stack()
  444. .innermost()
  445. .modifier_set.GetAccessKind());
  446. return true;
  447. }
  448. auto HandleParseNode(Context& context, Parse::RefBindingNameId node_id)
  449. -> bool {
  450. context.node_stack().Push(node_id);
  451. return true;
  452. }
  453. auto HandleParseNode(Context& context, Parse::TemplateBindingNameId node_id)
  454. -> bool {
  455. context.node_stack().Push(node_id);
  456. return true;
  457. }
  458. // Within a pattern with an unused modifier, sets the is_unused on all
  459. // entity names and also returns whether any names were found. The result
  460. // is needed to emit a diagnostic when the unused modifier is
  461. // unnecessary.
  462. static auto MarkPatternUnused(Context& context, SemIR::InstId inst_id) -> bool {
  463. bool found_name = false;
  464. llvm::SmallVector<SemIR::InstId> worklist;
  465. worklist.push_back(inst_id);
  466. while (!worklist.empty()) {
  467. auto current_inst_id = worklist.pop_back_val();
  468. auto inst = context.insts().Get(current_inst_id);
  469. CARBON_KIND_SWITCH(inst) {
  470. case CARBON_KIND_ANY(SemIR::AnyParamPattern, param): {
  471. worklist.push_back(param.subpattern_id);
  472. break;
  473. }
  474. case CARBON_KIND_ANY(SemIR::AnyBindingPattern, bind): {
  475. auto& name = context.entity_names().Get(bind.entity_name_id);
  476. name.is_unused = true;
  477. // We treat `_` as not marking the pattern as unused for the purpose of
  478. // deciding whether to issue a warning for `unused` on a pattern that
  479. // doesn't contain any bindings. `_` is implicitly unused, so marking it
  480. // `unused` is redundant but harmless.
  481. if (name.name_id != SemIR::NameId::Underscore) {
  482. found_name = true;
  483. }
  484. break;
  485. }
  486. case CARBON_KIND(SemIR::TuplePattern tuple): {
  487. for (auto elem_id : context.inst_blocks().Get(tuple.elements_id)) {
  488. worklist.push_back(elem_id);
  489. }
  490. break;
  491. }
  492. case CARBON_KIND(SemIR::VarPattern var): {
  493. worklist.push_back(var.subpattern_id);
  494. break;
  495. }
  496. default:
  497. break;
  498. }
  499. }
  500. return found_name;
  501. }
  502. auto HandleParseNode(Context& context, Parse::UnusedPatternId node_id) -> bool {
  503. auto [child_node, child_inst_id] =
  504. context.node_stack().PopPatternWithNodeId();
  505. if (!MarkPatternUnused(context, child_inst_id)) {
  506. CARBON_DIAGNOSTIC(UnusedPatternNoBindings, Warning,
  507. "`unused` modifier on pattern without bindings");
  508. context.emitter().Emit(node_id, UnusedPatternNoBindings);
  509. }
  510. context.node_stack().Push(node_id, child_inst_id);
  511. return true;
  512. }
  513. } // namespace Carbon::Check