Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify what was missing in binding pattern errors #4314

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/declaration/no_prelude/fail_pattern_in_signature.carbon

// CHECK:STDERR: fail_pattern_in_signature.carbon:[[@LINE+7]]:6: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_pattern_in_signature.carbon:[[@LINE+7]]:6: ERROR: Expected name in binding pattern.
// CHECK:STDERR: fn F((a: {}, b: {}), c: {});
// CHECK:STDERR: ^
// CHECK:STDERR:
Expand Down
12 changes: 7 additions & 5 deletions toolchain/parse/handle_binding_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ auto HandleBindingPattern(Context& context) -> void {
}

// Handle an invalid pattern introducer for parameters and variables.
auto on_error = [&]() {
auto on_error = [&](llvm::StringLiteral expected) {
if (!state.has_error) {
CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
"Expected binding pattern.");
context.emitter().Emit(*context.position(), ExpectedBindingPattern);
"Expected {0} in binding pattern.",
llvm::StringLiteral);
context.emitter().Emit(*context.position(), ExpectedBindingPattern,
expected);
state.has_error = true;
}
};
Expand All @@ -50,7 +52,7 @@ auto HandleBindingPattern(Context& context) -> void {
// Add a placeholder for the name.
context.AddLeafNode(NodeKind::IdentifierName, *context.position(),
/*has_error=*/true);
on_error();
on_error("name");
}

if (auto kind = context.PositionKind();
Expand All @@ -63,7 +65,7 @@ auto HandleBindingPattern(Context& context) -> void {
context.PushState(state);
context.PushStateForExpr(PrecedenceGroup::ForType());
} else {
on_error();
on_error("`:` or `:!`");
// Add a placeholder for the type.
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
/*has_error=*/true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/basics/fail_paren_match_regression.carbon

// CHECK:STDERR: fail_paren_match_regression.carbon:[[@LINE+8]]:5: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_paren_match_regression.carbon:[[@LINE+8]]:5: ERROR: Expected name in binding pattern.
// CHECK:STDERR: var = (foo {})
// CHECK:STDERR: ^
// CHECK:STDERR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/function/declaration/fail_with_identifier_as_param.carbon

// CHECK:STDERR: fail_with_identifier_as_param.carbon:[[@LINE+3]]:11: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_with_identifier_as_param.carbon:[[@LINE+3]]:11: ERROR: Expected `:` or `:!` in binding pattern.
// CHECK:STDERR: fn foo(bar);
// CHECK:STDERR: ^
fn foo(bar);
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/generics/impl/fail_impl.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl forall f32;
// CHECK:STDERR:
impl forall [] u32;

// CHECK:STDERR: fail_impl.carbon:[[@LINE+8]]:21: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_impl.carbon:[[@LINE+8]]:21: ERROR: Expected `:` or `:!` in binding pattern.
// CHECK:STDERR: impl forall [invalid] i8;
// CHECK:STDERR: ^
// CHECK:STDERR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/generics/interface/fail_self_param_syntax.carbon

interface Foo {
// CHECK:STDERR: fail_self_param_syntax.carbon:[[@LINE+4]]:13: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_self_param_syntax.carbon:[[@LINE+4]]:13: ERROR: Expected `:` or `:!` in binding pattern.
// CHECK:STDERR: fn Sub[me Self](b: Self) -> Self;
// CHECK:STDERR: ^~~~
// CHECK:STDERR:
fn Sub[me Self](b: Self) -> Self;

// CHECK:STDERR: fail_self_param_syntax.carbon:[[@LINE+3]]:10: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_self_param_syntax.carbon:[[@LINE+3]]:10: ERROR: Expected name in binding pattern.
// CHECK:STDERR: fn Mul[Self](b: Self) -> Self;
// CHECK:STDERR: ^~~~
fn Mul[Self](b: Self) -> Self;
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/let/fail_bad_name.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/let/fail_bad_name.carbon

// CHECK:STDERR: fail_bad_name.carbon:[[@LINE+3]]:5: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_bad_name.carbon:[[@LINE+3]]:5: ERROR: Expected name in binding pattern.
// CHECK:STDERR: let ? = 4;
// CHECK:STDERR: ^
let ? = 4;
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/let/fail_empty.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/let/fail_empty.carbon

// CHECK:STDERR: fail_empty.carbon:[[@LINE+3]]:4: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_empty.carbon:[[@LINE+3]]:4: ERROR: Expected name in binding pattern.
// CHECK:STDERR: let;
// CHECK:STDERR: ^
let;
Expand Down
4 changes: 2 additions & 2 deletions toolchain/parse/testdata/let/fail_missing_name.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

// --- fail_runtime_binding.carbon

// CHECK:STDERR: fail_runtime_binding.carbon:[[@LINE+4]]:5: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_runtime_binding.carbon:[[@LINE+4]]:5: ERROR: Expected name in binding pattern.
// CHECK:STDERR: let : i32 = 4;
// CHECK:STDERR: ^
// CHECK:STDERR:
let : i32 = 4;

// --- fail_complietime_binding.carbon

// CHECK:STDERR: fail_complietime_binding.carbon:[[@LINE+3]]:5: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_complietime_binding.carbon:[[@LINE+3]]:5: ERROR: Expected name in binding pattern.
// CHECK:STDERR: let :! bool = true;
// CHECK:STDERR: ^~
let :! bool = true;
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/let/fail_missing_type.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/let/fail_missing_type.carbon

// CHECK:STDERR: fail_missing_type.carbon:[[@LINE+3]]:7: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_missing_type.carbon:[[@LINE+3]]:7: ERROR: Expected `:` or `:!` in binding pattern.
// CHECK:STDERR: let a = 4;
// CHECK:STDERR: ^
let a = 4;
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/let/fail_no_semi.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

let

// CHECK:STDERR: fail_no_semi.carbon:[[@LINE+16]]:21: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_no_semi.carbon:[[@LINE+16]]:21: ERROR: Expected name in binding pattern.
// CHECK:STDERR: // CHECK:STDOUT: ]
// CHECK:STDERR: ^
// CHECK:STDERR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

fn f() -> i32 {
match (3) {
// CHECK:STDERR: fail_missing_case_pattern.carbon:[[@LINE+3]]:10: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_missing_case_pattern.carbon:[[@LINE+3]]:10: ERROR: Expected name in binding pattern.
// CHECK:STDERR: case => { return 2; }
// CHECK:STDERR: ^~
case => { return 2; }
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/package_expr/fail_in_name.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/package_expr/fail_in_name.carbon

// CHECK:STDERR: fail_in_name.carbon:[[@LINE+4]]:5: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_in_name.carbon:[[@LINE+4]]:5: ERROR: Expected name in binding pattern.
// CHECK:STDERR: var package.val: i32;
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/var/fail_bad_name.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/var/fail_bad_name.carbon

// CHECK:STDERR: fail_bad_name.carbon:[[@LINE+3]]:5: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_bad_name.carbon:[[@LINE+3]]:5: ERROR: Expected name in binding pattern.
// CHECK:STDERR: var *;
// CHECK:STDERR: ^
var *;
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/var/fail_empty.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/var/fail_empty.carbon

// CHECK:STDERR: fail_empty.carbon:[[@LINE+3]]:4: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_empty.carbon:[[@LINE+3]]:4: ERROR: Expected name in binding pattern.
// CHECK:STDERR: var;
// CHECK:STDERR: ^
var;
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/var/fail_no_semi.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var

// CHECK:STDERR: fail_no_semi.carbon:[[@LINE+16]]:21: ERROR: Expected binding pattern.
// CHECK:STDERR: fail_no_semi.carbon:[[@LINE+16]]:21: ERROR: Expected name in binding pattern.
// CHECK:STDERR: // CHECK:STDOUT: ]
// CHECK:STDERR: ^
// CHECK:STDERR:
Expand Down
Loading