mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
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>
37 lines
1.2 KiB
C++
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
|