From 2b6cad8fdc36169d760bb3c6e07f576f56a81d3e Mon Sep 17 00:00:00 2001 From: Josh L Date: Fri, 13 Sep 2024 21:30:16 +0000 Subject: [PATCH 1/2] Clarify what was missing in binding pattern errors --- toolchain/parse/handle_binding_pattern.cpp | 12 +++++++----- .../basics/fail_paren_match_regression.carbon | 2 +- .../declaration/fail_with_identifier_as_param.carbon | 2 +- .../parse/testdata/generics/impl/fail_impl.carbon | 2 +- .../generics/interface/fail_self_param_syntax.carbon | 4 ++-- toolchain/parse/testdata/let/fail_bad_name.carbon | 2 +- toolchain/parse/testdata/let/fail_empty.carbon | 2 +- .../parse/testdata/let/fail_missing_name.carbon | 4 ++-- .../parse/testdata/let/fail_missing_type.carbon | 2 +- toolchain/parse/testdata/let/fail_no_semi.carbon | 2 +- .../testdata/match/fail_missing_case_pattern.carbon | 2 +- .../parse/testdata/package_expr/fail_in_name.carbon | 2 +- toolchain/parse/testdata/var/fail_bad_name.carbon | 2 +- toolchain/parse/testdata/var/fail_empty.carbon | 2 +- toolchain/parse/testdata/var/fail_no_semi.carbon | 2 +- 15 files changed, 23 insertions(+), 21 deletions(-) diff --git a/toolchain/parse/handle_binding_pattern.cpp b/toolchain/parse/handle_binding_pattern.cpp index a0a43e7563e44..8a50ce7d29f9f 100644 --- a/toolchain/parse/handle_binding_pattern.cpp +++ b/toolchain/parse/handle_binding_pattern.cpp @@ -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; } }; @@ -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(); @@ -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); diff --git a/toolchain/parse/testdata/basics/fail_paren_match_regression.carbon b/toolchain/parse/testdata/basics/fail_paren_match_regression.carbon index acf3c05bb2425..f7d687cdd407c 100644 --- a/toolchain/parse/testdata/basics/fail_paren_match_regression.carbon +++ b/toolchain/parse/testdata/basics/fail_paren_match_regression.carbon @@ -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: diff --git a/toolchain/parse/testdata/function/declaration/fail_with_identifier_as_param.carbon b/toolchain/parse/testdata/function/declaration/fail_with_identifier_as_param.carbon index 6ef9eab377331..4271e80a04c94 100644 --- a/toolchain/parse/testdata/function/declaration/fail_with_identifier_as_param.carbon +++ b/toolchain/parse/testdata/function/declaration/fail_with_identifier_as_param.carbon @@ -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); diff --git a/toolchain/parse/testdata/generics/impl/fail_impl.carbon b/toolchain/parse/testdata/generics/impl/fail_impl.carbon index 985843770e3f2..a78cab279fa7b 100644 --- a/toolchain/parse/testdata/generics/impl/fail_impl.carbon +++ b/toolchain/parse/testdata/generics/impl/fail_impl.carbon @@ -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: diff --git a/toolchain/parse/testdata/generics/interface/fail_self_param_syntax.carbon b/toolchain/parse/testdata/generics/interface/fail_self_param_syntax.carbon index a2e5ce63e27e9..c80522c7f12d6 100644 --- a/toolchain/parse/testdata/generics/interface/fail_self_param_syntax.carbon +++ b/toolchain/parse/testdata/generics/interface/fail_self_param_syntax.carbon @@ -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; diff --git a/toolchain/parse/testdata/let/fail_bad_name.carbon b/toolchain/parse/testdata/let/fail_bad_name.carbon index 4c3f2801db4d4..77a8f92c64956 100644 --- a/toolchain/parse/testdata/let/fail_bad_name.carbon +++ b/toolchain/parse/testdata/let/fail_bad_name.carbon @@ -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; diff --git a/toolchain/parse/testdata/let/fail_empty.carbon b/toolchain/parse/testdata/let/fail_empty.carbon index 6ae07ffc5d35e..d19a83dc5391d 100644 --- a/toolchain/parse/testdata/let/fail_empty.carbon +++ b/toolchain/parse/testdata/let/fail_empty.carbon @@ -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; diff --git a/toolchain/parse/testdata/let/fail_missing_name.carbon b/toolchain/parse/testdata/let/fail_missing_name.carbon index 533fe6df1c4ff..c5c232d64daf9 100644 --- a/toolchain/parse/testdata/let/fail_missing_name.carbon +++ b/toolchain/parse/testdata/let/fail_missing_name.carbon @@ -10,7 +10,7 @@ // --- 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: @@ -18,7 +18,7 @@ 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; diff --git a/toolchain/parse/testdata/let/fail_missing_type.carbon b/toolchain/parse/testdata/let/fail_missing_type.carbon index bd37327255db9..5084a7282e439 100644 --- a/toolchain/parse/testdata/let/fail_missing_type.carbon +++ b/toolchain/parse/testdata/let/fail_missing_type.carbon @@ -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; diff --git a/toolchain/parse/testdata/let/fail_no_semi.carbon b/toolchain/parse/testdata/let/fail_no_semi.carbon index 96ff4139dce51..b67d63d329197 100644 --- a/toolchain/parse/testdata/let/fail_no_semi.carbon +++ b/toolchain/parse/testdata/let/fail_no_semi.carbon @@ -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: diff --git a/toolchain/parse/testdata/match/fail_missing_case_pattern.carbon b/toolchain/parse/testdata/match/fail_missing_case_pattern.carbon index 3d9d5fe4e60ad..ee71d4cd95a53 100644 --- a/toolchain/parse/testdata/match/fail_missing_case_pattern.carbon +++ b/toolchain/parse/testdata/match/fail_missing_case_pattern.carbon @@ -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; } diff --git a/toolchain/parse/testdata/package_expr/fail_in_name.carbon b/toolchain/parse/testdata/package_expr/fail_in_name.carbon index 5573067499e6e..6119893486947 100644 --- a/toolchain/parse/testdata/package_expr/fail_in_name.carbon +++ b/toolchain/parse/testdata/package_expr/fail_in_name.carbon @@ -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: diff --git a/toolchain/parse/testdata/var/fail_bad_name.carbon b/toolchain/parse/testdata/var/fail_bad_name.carbon index 37197c60c0ac3..0610bf5639b48 100644 --- a/toolchain/parse/testdata/var/fail_bad_name.carbon +++ b/toolchain/parse/testdata/var/fail_bad_name.carbon @@ -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 *; diff --git a/toolchain/parse/testdata/var/fail_empty.carbon b/toolchain/parse/testdata/var/fail_empty.carbon index bf9f729acb484..99402577d6a3d 100644 --- a/toolchain/parse/testdata/var/fail_empty.carbon +++ b/toolchain/parse/testdata/var/fail_empty.carbon @@ -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; diff --git a/toolchain/parse/testdata/var/fail_no_semi.carbon b/toolchain/parse/testdata/var/fail_no_semi.carbon index 1d6211710549f..8daed5a8732e3 100644 --- a/toolchain/parse/testdata/var/fail_no_semi.carbon +++ b/toolchain/parse/testdata/var/fail_no_semi.carbon @@ -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: From e6abbcd692c3367271dc9ac9c87acd8193953886 Mon Sep 17 00:00:00 2001 From: Josh L Date: Sat, 14 Sep 2024 03:25:20 +0000 Subject: [PATCH 2/2] Update test goldens --- .../declaration/no_prelude/fail_pattern_in_signature.carbon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchain/check/testdata/function/declaration/no_prelude/fail_pattern_in_signature.carbon b/toolchain/check/testdata/function/declaration/no_prelude/fail_pattern_in_signature.carbon index 484b39e6fbf5f..422034455004b 100644 --- a/toolchain/check/testdata/function/declaration/no_prelude/fail_pattern_in_signature.carbon +++ b/toolchain/check/testdata/function/declaration/no_prelude/fail_pattern_in_signature.carbon @@ -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: