Files
carbon-lang/toolchain/parse/handle_unused.cpp
T
Burak EmirandDana Jansens fec6ce2f9f Implement "unused pattern bindings" p2022 - parsing (#6460)
This implements proposal #2022, with changes from #3763 and leads
answers on #6448

Detection of unusedness is happening in
~~`toolchain/check/dataflow_analysis.cpp`~~ next PR. See
[here](https://github.com/burakemir/carbon-lang/tree/unused_pattern_bindings_p2022_impl_part2)
for preview.

~~All test cases with unused bindings were updated in order to avoid
polluting test output.~~

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
2025-12-17 15:40:20 +00:00

37 lines
1.2 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/parse/context.h"
#include "toolchain/parse/handle.h"
namespace Carbon::Parse {
auto HandleUnusedPattern(Context& context) -> void {
auto state = context.PopState();
if (state.in_unused_pattern) {
CARBON_DIAGNOSTIC(NestedUnused, Error,
"`unused` nested within another `unused`");
context.emitter().Emit(*context.position(), NestedUnused);
state.has_error = true;
}
context.PushState(StateKind::FinishUnusedPattern);
context.ConsumeChecked(Lex::TokenKind::Unused);
context.PushStateForPattern(StateKind::Pattern, state.in_var_pattern,
/*in_unused_pattern=*/true);
}
auto HandleFinishUnusedPattern(Context& context) -> void {
auto state = context.PopState();
context.AddNode(NodeKind::UnusedPattern, state.token, state.has_error);
// Propagate errors to the parent state so that they can take different
// actions on invalid patterns.
if (state.has_error) {
context.ReturnErrorOnState();
}
}
} // namespace Carbon::Parse