From 5f379da544bec9fe1ac64ea162041e7e3f6c3ddb Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Fri, 3 Jan 2025 22:31:50 -0500 Subject: [PATCH] fix(lib): prevent wildcards from incorrectly marking child patterns as infallible When a pattern appears under a wildcard parent (like "(_ (expr))"), we were incorrectly marking it as infallible. The parent_pattern_guaranteed flag only means the pattern will match after finding the right wildcard parent, not that any wildcard parent will work. --- cli/src/tests/query_test.rs | 30 ++++++++++++++++++++++++++++++ lib/src/query.c | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 6e722aef..b6b76cd1 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -5520,3 +5520,33 @@ fn f() { ); assert_eq!(matches, flipped_matches); } + +#[test] +fn test_wildcard_parent_allows_fallible_child_patterns() { + let language = get_language("javascript"); + let mut parser = Parser::new(); + parser.set_language(&language).unwrap(); + + let source_code = r#" +function foo() { + "bar" +} + "#; + + let query = Query::new( + &language, + "(function_declaration + (_ + (expression_statement) + ) + ) @part", + ) + .unwrap(); + + assert_query_matches( + &language, + &query, + source_code, + &[(0, vec![("part", "function foo() {\n \"bar\"\n}")])], + ); +} diff --git a/lib/src/query.c b/lib/src/query.c index a594342f..12829a20 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -3004,7 +3004,7 @@ bool ts_query__step_is_fallible( return ( next_step->depth != PATTERN_DONE_MARKER && next_step->depth > step->depth && - !next_step->parent_pattern_guaranteed + (!next_step->parent_pattern_guaranteed || step->symbol == WILDCARD_SYMBOL) ); }