Enforce full match inside a group. If not inside a group, treating the patten as it has a trailing wildcard.
PiperOrigin-RevId: 650651899
Change-Id: I0348588209479c9748aa62035d5e513e27827908
diff --git a/bazel/llvm.bzl b/bazel/llvm.bzl
index b809d55..70663b7 100644
--- a/bazel/llvm.bzl
+++ b/bazel/llvm.bzl
@@ -53,7 +53,7 @@
executable = False,
)
-LLVM_COMMIT_SHA = "f002558883dbc32d939e9ab9f7a2296459bf47cc"
+LLVM_COMMIT_SHA = "2abe53a17f486a055a3715f19a37e3e91b4415fc"
def llvm_loader_repository_dependencies():
# This *declares* the dependency, but it won't actually be *downloaded* unless it's used.
diff --git a/common/BUILD b/common/BUILD
index b552c2f..71aa63c 100644
--- a/common/BUILD
+++ b/common/BUILD
@@ -174,7 +174,6 @@
":token_stream_printer",
"@crate_index//:anyhow",
"@crate_index//:proc-macro2",
- "@crate_index//:quote",
],
)
diff --git a/common/token_stream_matchers.rs b/common/token_stream_matchers.rs
index c3ecde0..39f2f90 100644
--- a/common/token_stream_matchers.rs
+++ b/common/token_stream_matchers.rs
@@ -99,7 +99,6 @@
use anyhow::{anyhow, Result};
pub use proc_macro2::TokenStream;
use proc_macro2::TokenTree;
- use quote::quote;
use std::iter;
pub use token_stream_printer::{
cc_tokens_to_formatted_string_for_tests, rs_tokens_to_formatted_string,
@@ -165,16 +164,11 @@
let iter = input.clone().into_iter();
let mut best_mismatch = Mismatch::for_no_partial_match();
- let preprocessed_pattern = if !format!("{}", pattern).ends_with("...") {
- quote! { #pattern ... }
- } else {
- pattern.clone()
- };
let mut stack = vec![iter];
while let Some(mut iter) = stack.pop() {
loop {
- match match_prefix(iter.clone(), preprocessed_pattern.clone()) {
+ match match_prefix(iter.clone(), pattern.clone(), false) {
MatchInfo::Match { input_suffix: _ } => return Ok(()),
MatchInfo::Mismatch(mismatch) => {
if best_mismatch.match_length < mismatch.match_length {
@@ -230,6 +224,7 @@
fn match_prefix(
input: impl Iterator<Item = TokenTree> + Clone,
pattern: TokenStream,
+ match_inside_group: bool,
) -> MatchInfo {
let mut input_iter = input.clone();
let mut pattern_iter = pattern.clone().into_iter().peekable();
@@ -251,6 +246,7 @@
reinsert_token(input_iter.clone(), actual_token).into_iter(),
input.clone(),
skip_wildcard(pattern_iter.clone()),
+ match_inside_group,
) {
MatchInfo::Mismatch(mut mismatch) => {
mismatch.match_length += match_counter;
@@ -275,10 +271,14 @@
mismatch.match_length += match_counter;
return MatchInfo::Mismatch(mismatch);
}
- } else {
+ match_counter += 1;
+ } else if match_inside_group {
return MatchInfo::Match { input_suffix: reinsert_token(input_iter, actual_token) };
+ } else {
+ // If we are not inside a group, seeing the end of the pattern means that we
+ // have matched the entire pattern.
+ return MatchInfo::Match { input_suffix: TokenStream::new() };
}
- match_counter += 1;
}
if pattern_iter.peek().is_none() {
@@ -300,8 +300,9 @@
input_iter: impl Iterator<Item = TokenTree> + Clone,
input: impl Iterator<Item = TokenTree> + Clone,
pattern: TokenStream,
+ match_inside_group: bool,
) -> MatchInfo {
- match match_prefix(input_iter.clone(), pattern.clone()) {
+ match match_prefix(input_iter.clone(), pattern.clone(), match_inside_group) {
MatchInfo::Match { input_suffix } if input_suffix.is_empty() => {
MatchInfo::Match { input_suffix }
}
@@ -363,7 +364,7 @@
});
}
let match_info =
- match_prefix(actual_group.stream().into_iter(), pattern_group.stream());
+ match_prefix(actual_group.stream().into_iter(), pattern_group.stream(), true);
match match_info {
MatchInfo::Match { input_suffix } => {
if input_suffix
@@ -510,7 +511,7 @@
r#"expected 'B' but got 'A'
Caused by:
- 0: expected 'struct B ...' got 'struct A { int a ; int b ; } ;'
+ 0: expected 'struct B' got 'struct A { int a ; int b ; } ;'
1: input:
```
@@ -537,7 +538,7 @@
"expected 'B' but got 'A'
Caused by:
- 0: expected 'struct B ...' got 'struct A { a : i64 , b : i64 }'
+ 0: expected 'struct B' got 'struct A { a : i64 , b : i64 }'
1: input:\n \n ```
struct A {
a: i64,
@@ -558,7 +559,7 @@
)
.expect_err("unexpected match")
),
- r#"expected 'struct X { } ...' but the input already ended: expected 'fn foo () { } struct X { } ...' got 'fn foo () { }': input:
+ r#"expected 'struct X { }' but the input already ended: expected 'fn foo () { } struct X { }' got 'fn foo () { }': input:
```
fn foo() {}
@@ -579,7 +580,7 @@
)
.expect_err("unexpected match")
),
- r#"expected delimiter Parenthesis for group '()' but got Brace for group '{ }': expected 'fn foo () () ...' got 'fn foo () { }': input:
+ r#"expected delimiter Parenthesis for group '()' but got Brace for group '{ }': expected 'fn foo () ()' got 'fn foo () { }': input:
```
fn foo() {}
@@ -602,7 +603,7 @@
),
"expected 'c' but got 'b': \
expected 'let a = 1 ; let c = 2 ;' got 'let a = 1 ; let b = 2 ;': \
- expected 'fn foo () { let a = 1 ; let c = 2 ; } ...' \
+ expected 'fn foo () { let a = 1 ; let c = 2 ; }' \
got 'fn foo () { let a = 1 ; let b = 2 ; }': \
input:\n\n```\nfn foo() {\n let a = 1;\n let b = 2;\n}\n\n```"
);
@@ -646,7 +647,7 @@
)
.expect_err("unexpected match")
),
- r#"matched the entire pattern but the input still contained 'drop_impl () ;': expected 'fn drop (& mut self) { } ...' got 'fn drop (& mut self) { drop_impl () ; }': input:
+ r#"matched the entire pattern but the input still contained 'drop_impl () ;': expected 'fn drop (& mut self) { }' got 'fn drop (& mut self) { drop_impl () ; }': input:
```
impl Drop {
@@ -667,7 +668,7 @@
)
.expect_err("unexpected match")
),
- r#"matched the entire pattern but the input still contained 'drop_impl2 () ;': expected 'fn drop (& mut self) { drop_impl1 () ; } ...' got 'fn drop (& mut self) { drop_impl1 () ; drop_impl2 () ; }': input:
+ r#"matched the entire pattern but the input still contained 'drop_impl2 () ;': expected 'fn drop (& mut self) { drop_impl1 () ; }' got 'fn drop (& mut self) { drop_impl1 () ; drop_impl2 () ; }': input:
```
impl Drop {
@@ -741,7 +742,7 @@
// the error message shows "longer match" with more tokens consumed by the wildcard
"expected 'c' but got 'b': \
expected 'c' got 'b b': \
- expected '[a ... c] ...' got '[a b b]': \
+ expected '[a ... c]' got '[a b b]': \
input:\n\n```\n[a b b]\n```"
);
assert_eq!(
@@ -757,7 +758,7 @@
// the error message shows "longer match" with branching off the wildcard earlier
"expected 'c' but got 'b': \
expected 'b c' got 'b b': \
- expected '[a ... b c] ...' got '[a b b]': \
+ expected '[a ... b c]' got '[a b b]': \
input:\n\n```\n[a b b]\n```"
);
}
@@ -802,4 +803,17 @@
}
);
}
+
+ #[test]
+ #[should_panic]
+ fn test_assert_rs_matches_no_trailing_wildcard_inside_group() {
+ assert_rs_matches!(
+ quote! {
+ fn f() -> f32 { return 1.0; }
+ },
+ quote! {
+ fn f() -> f32 {}
+ }
+ );
+ }
}