Skip to content

Commit d5fb77f

Browse files
Merge pull request google#429 from google:specialization_bug
PiperOrigin-RevId: 654053132
2 parents 4cd2cae + 323a85f commit d5fb77f

File tree

7 files changed

+66
-44
lines changed

7 files changed

+66
-44
lines changed

googletest/src/matcher_support/auto_ref_eq.rs renamed to googletest/src/matcher_support/auto_eq.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#![doc(hidden)]
1616

17-
/// Auto-ref macro that wrap the expression with `eq(...)` if the expression is
17+
/// Macro that wraps the expression with `eq(...)` if the expression is
1818
/// not a matcher.
1919
///
2020
/// This is useful to let users pass expected value to macro matchers like
@@ -24,54 +24,57 @@
2424
/// If you are interested in using it in your matcher, please file an issue to
2525
/// stabilize this.
2626
#[macro_export]
27-
macro_rules! __auto_ref_eq {
27+
macro_rules! __auto_eq {
2828
($e:expr) => {{
2929
#[allow(unused_imports)]
30-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::{
31-
ExpectedKind, MatcherKind,
32-
};
30+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::ExpectedKind;
3331
match $e {
34-
expected => (&expected).kind().matcher(expected),
32+
expected => {
33+
$crate::matcher_support::__internal_unstable_do_not_depend_on_these::Wrapper(
34+
&expected,
35+
)
36+
.kind()
37+
.matcher(expected)
38+
}
3539
}
3640
}};
3741
}
3842

3943
// This reimplements the pattern presented in
40-
// https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md
44+
// https://github.com/dtolnay/case-studies/issues/14
4145
pub mod internal {
4246
use crate::{
4347
matcher::MatcherBase,
4448
matchers::{eq, EqMatcher},
4549
};
4650

47-
pub struct MatcherTag;
51+
pub struct Wrapper<T>(pub T);
4852

49-
pub trait MatcherKind {
53+
impl<'a, T: MatcherBase> Wrapper<&'a T> {
5054
#[inline]
51-
fn kind(&self) -> MatcherTag {
55+
pub fn kind(&self) -> MatcherTag {
5256
MatcherTag
5357
}
5458
}
5559

56-
impl<M: MatcherBase> MatcherKind for M {}
57-
58-
impl MatcherTag {
60+
pub trait ExpectedKind {
5961
#[inline]
60-
pub fn matcher<M>(self, matcher: M) -> M {
61-
matcher
62+
fn kind(&self) -> ExpectedTag {
63+
ExpectedTag
6264
}
6365
}
6466

65-
pub struct ExpectedTag;
67+
impl<T> ExpectedKind for Wrapper<T> {}
6668

67-
pub trait ExpectedKind {
69+
pub struct MatcherTag;
70+
71+
impl MatcherTag {
6872
#[inline]
69-
fn kind(&self) -> ExpectedTag {
70-
ExpectedTag
73+
pub fn matcher<M>(self, matcher: M) -> M {
74+
matcher
7175
}
7276
}
73-
74-
impl<T> ExpectedKind for &T {}
77+
pub struct ExpectedTag;
7578

7679
impl ExpectedTag {
7780
#[inline]
@@ -87,11 +90,17 @@ mod tests {
8790

8891
#[test]
8992
fn auto_ref_matcher() -> Result<()> {
90-
verify_that!(123, __auto_ref_eq!(ge(9)))
93+
verify_that!(123, __auto_eq!(ge(9)))
9194
}
9295

9396
#[test]
9497
fn auto_ref_expected() -> Result<()> {
95-
verify_that!(123, __auto_ref_eq!(123))
98+
verify_that!(123, __auto_eq!(123))
99+
}
100+
101+
#[test]
102+
fn auto_ref_on_ref_matcher() -> Result<()> {
103+
let matcher = eq(123);
104+
verify_that!(123, __auto_eq!(&matcher))
96105
}
97106
}

googletest/src/matcher_support/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
//! these facilities could be useful to downstream users writing custom
1919
//! matchers.
2020
21-
mod auto_ref_eq;
21+
mod auto_eq;
2222
pub(crate) mod count_elements;
2323
pub(crate) mod edit_distance;
2424
pub(crate) mod summarize_diff;
2525
pub(crate) mod zipped_iterator;
2626

2727
pub mod __internal_unstable_do_not_depend_on_these {
28-
pub use super::auto_ref_eq::internal::{ExpectedKind, MatcherKind};
29-
pub use crate::__auto_ref_eq as auto_ref_eq;
28+
pub use super::auto_eq::internal::{ExpectedKind, Wrapper};
29+
pub use crate::__auto_eq as auto_eq;
3030
}

googletest/src/matchers/field_matcher.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ macro_rules! __field {
198198
macro_rules! field_internal {
199199
(&$($t:ident)::+.$field:tt, ref $m:expr) => {{
200200
use $crate::matchers::__internal_unstable_do_not_depend_on_these::field_matcher;
201-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
201+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
202202
field_matcher(
203203
|o: &_| {
204204
match o {
@@ -211,11 +211,11 @@ macro_rules! field_internal {
211211
}
212212
},
213213
&stringify!($field),
214-
auto_ref_eq!($m))
214+
auto_eq!($m))
215215
}};
216216
(&$($t:ident)::+.$field:tt, $m:expr) => {{
217217
use $crate::matchers::__internal_unstable_do_not_depend_on_these::field_matcher;
218-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
218+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
219219
field_matcher(
220220
|o: &&_| {
221221
match o {
@@ -228,11 +228,11 @@ macro_rules! field_internal {
228228
}
229229
},
230230
&stringify!($field),
231-
auto_ref_eq!($m))
231+
auto_eq!($m))
232232
}};
233233
($($t:ident)::+.$field:tt, $m:expr) => {{
234234
use $crate::matchers::__internal_unstable_do_not_depend_on_these::field_matcher;
235-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
235+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
236236
field_matcher(
237237
|o: &_| {
238238
match o {
@@ -245,7 +245,7 @@ macro_rules! field_internal {
245245
}
246246
},
247247
&stringify!($field),
248-
auto_ref_eq!($m))
248+
auto_eq!($m))
249249
}};
250250
}
251251

googletest/src/matchers/property_matcher.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,35 +179,35 @@ macro_rules! property_internal {
179179

180180
(&$($t:ident)::+.$method:tt($($argument:tt),* $(,)?), ref $m:expr) => {{
181181
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_ref_matcher;
182-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
182+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
183183
property_ref_matcher(
184184
|o: &$($t)::+| $($t)::+::$method(o, $($argument),*),
185185
&stringify!($method($($argument),*)),
186-
auto_ref_eq!($m))
186+
auto_eq!($m))
187187
}};
188188
($($t:ident)::+.$method:tt($($argument:tt),* $(,)?), ref $m:expr) => {{
189189
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_ref_matcher;
190-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
190+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
191191
property_ref_matcher(
192192
|o: $($t)::+| $($t)::+::$method(o, $($argument),*),
193193
&stringify!($method($($argument),*)),
194-
auto_ref_eq!($m))
194+
auto_eq!($m))
195195
}};
196196
(& $($t:ident)::+.$method:tt($($argument:tt),* $(,)?), $m:expr) => {{
197197
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_matcher;
198-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
198+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
199199
property_matcher(
200200
|o: &&$($t)::+| o.$method($($argument),*),
201201
&stringify!($method($($argument),*)),
202-
auto_ref_eq!($m))
202+
auto_eq!($m))
203203
}};
204204
($($t:ident)::+.$method:tt($($argument:tt),* $(,)?), $m:expr) => {{
205205
use $crate::matchers::__internal_unstable_do_not_depend_on_these::property_matcher;
206-
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_ref_eq;
206+
use $crate::matcher_support::__internal_unstable_do_not_depend_on_these::auto_eq;
207207
property_matcher(
208208
|o: &$($t)::+| o.$method($($argument),*),
209209
&stringify!($method($($argument),*)),
210-
auto_ref_eq!($m))
210+
auto_eq!($m))
211211
}};
212212
}
213213

googletest/tests/field_matcher_test.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ fn matches_struct_ref_to_ref_binding_mode() -> Result<()> {
229229
}
230230

231231
#[test]
232-
fn matches_struct_with_auto_ref_eq() -> Result<()> {
232+
fn matches_struct_with_auto_eq() -> Result<()> {
233233
#[derive(Debug)]
234234
struct Strukt {
235235
a_field: String,
@@ -239,7 +239,7 @@ fn matches_struct_with_auto_ref_eq() -> Result<()> {
239239
}
240240

241241
#[test]
242-
fn matches_enum_with_auto_ref_eq() -> Result<()> {
242+
fn matches_enum_with_auto_eq() -> Result<()> {
243243
#[derive(Debug)]
244244
enum Enum {
245245
Str(String),
@@ -249,3 +249,16 @@ fn matches_enum_with_auto_ref_eq() -> Result<()> {
249249

250250
verify_that!(Enum::Str("32".into()), field!(Enum::Str.0, "32"))
251251
}
252+
253+
#[test]
254+
fn matches_enum_with_auto_eq_with_wrapper() -> Result<()> {
255+
#[derive(Debug)]
256+
struct Wrapper<I> {
257+
wrapped: I,
258+
}
259+
260+
verify_that!(
261+
Wrapper { wrapped: Wrapper { wrapped: 23 } },
262+
field!(Wrapper.wrapped, field!(Wrapper.wrapped, &23))
263+
)
264+
}

googletest/tests/matches_pattern_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ fn matches_copy_struct_property_non_copy() -> Result<()> {
18011801
}
18021802

18031803
#[test]
1804-
fn matches_struct_auto_ref_eq() -> Result<()> {
1804+
fn matches_struct_auto_eq() -> Result<()> {
18051805
#[derive(Debug, Clone)]
18061806
struct AStruct {
18071807
int: i32,

googletest/tests/property_matcher_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ fn matches_ref_to_ref_with_binding_mode() -> Result<()> {
285285
}
286286

287287
#[test]
288-
fn matches_property_auto_ref_eq() -> Result<()> {
288+
fn matches_property_auto_eq() -> Result<()> {
289289
#[derive(Debug)]
290290
struct Struct;
291291
impl Struct {

0 commit comments

Comments
 (0)