egg_recursive 0.2.0

A recursive interface for egg: e-graphs good without S-expresion!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! This crate provides a recursive interface to [`egg`].
//!
//! [`egg`] alrady comes with S-expression-based interface, but it has the following drawbacks:
//!
//! - It uses [`std::str::FromStr`] and [`std::fmt::Display`] to parse/format textual representation of ASTs.
//!     + These CAN be used for other purposes and this can cause a conflict with other applications.
//! - Parser favours the first clause for terminal variants with the same parameter.
//!     + This can result in unexpected behaviour under the existence of ambiguity.
//! - ALL textual representation of ASTs fed to [`egg::rewrite`] is checked at RUNTIME.
//!     + This means we can't see syntax error until compilation;
//!     + This further complicates the debugging process.
//! - S-expressions get harder and harder to be parsed by human eyes
//!
//! This crate alleviates these problems by introducing a recursive interface to [`egg`].
//!
//! ## Abstraction over Language
//!
//! You can define a recursive language as a ordinary `Self`-referencing enum,
//! containing [`Box`]es, [`[Self; N]`], or [`Vec`]s or other terminal other types.
//! Such language AST must implement [`Recursive`] trait and have a [`Signature`] type synonym.
//! This also provides [`Pat<L>`] for pattern matching, which can be converted to/from [`Pattern<AsLanguage<L>>`].
//! We are providing a [`Language`] derive macro for automatically derive such types and impls.
//!
//! ```rust
//! use egg::*;
//! use egg_recursive::*;
//!
//! #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Language)]
//! pub enum Arith {
//!     Num(i32),
//!     Neg(Box<Self>),
//!     Add([Box<Self>; 2]),
//!     Mul([Box<Self>; 2]),
//! }
//! ```
//!
//! This generates the following entities:
//!
//! 1. `Recursive` impl for `Arith`;
//!     - You can convert from/to [`RecExpr<Signature<Arith, Id>>`] with [`From`] instances or [`Recursive::into_rec_expr`]/[`Recursive::from_rec_expr`].
//! 2. `EggArith` type alias for `Signature<Arith, Id>`, which implements [`egg::Language`];
//! 3. `ArithPat` type for recursive pattern ASTs.
//!     + It already implements [`egg::Searcher`] and [`egg::Applier`].
//!     + This is indeed a newtype wrapper of [`Pat<AsLanguage<Arith>>`].
//!         - We provide this as a newtype to allow (otherwise ophan) impls of utility traits for [`Pat`]s, (e.g. [`std::ops::Add`], [`std::ops::Mul`], etc).
//!     + This can be converted to/from [`egg::Pattern<EggArith>`] with [`From`] instances.
//! 4. `ArithCons` trait, which provides smart constructors for `Arith` and `ArithPat`s.
//!     + `ArithCons` has trait methods like `num(i64)`, `add([Self; 2])`, `mul([Self; 2])`, `neg(Self)`, etc. It has just snake_cased variant of the original enum variants. If the snake_case version is reserved keyword, suffixed with `_` (e.g. `If` becomes `if_()`).
//!
//! An `enum` passed to [`self::Language`] should satisfy the following conditions:
//!
//! 1. It MUST NOT have any generic parameters.
//! 2. Recursive variants MUST have only one field, which is one of the following (in any case, the type itself MUST be referenced by `Self`, not a enum name itself):
//!    + `Box<Self>`,
//!    + `[Box<Self>; N]`,
//!    + `Vec<Self>`,
//!    + `T<Box<Self>>` for some [`IntoLanguageChildren`] type `T` (described in the next section).
//! 3. Arguments to the non-recursive variants MUST implement at least [`Eq`], [`Ord`], [`Hash`], and [`Clone`].
//!
//! ## Helper Types for [`egg::LanguageChildren`]
//!
//! Sometimes, the more the [`LanguageChildren`] has a components, the harder to get memorise the order/semantics of components.
//! To alleviates this situation, we introduce [`IntoLanguageChildren`] trait, which abstracts over view types of [`egg::LanguageChildren`].
//!
//! You don't have to write it manually and it can generally be derived by [`self::LanguageChildren`] derive macro.
//!
//! Suppose we want to add boolean operations and if-expressions:
//!
//! ```rust
//! use egg::*;
//! use egg_recursive::*;
//!
//! #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, LanguageChildren)]
//! pub struct IfThenElse<T> {
//!     pub cond: T,
//!     pub then_branch: T,
//!     pub else_branch: T,
//! }
//!
//! #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Language)]
//! pub enum Arith {
//!     Num(i32),
//!     Bool(bool),
//!     Neg(Box<Self>),
//!     Add([Box<Self>; 2]),
//!     Mul([Box<Self>; 2]),
//!     IsZero(Box<Self>),
//!     If(IfThenElse<Box<Self>>),
//! }
//! ```
//!
//! This generates `RawIfThenElse` and [`IntoLanguageChildren`] instance to convert it to [`LanguageChildren`].
//! You can call either `IfThenElse::view` or `RawIfThenElse::unview` to convert it to/from `RawIfThenElse`.
//!
//! [`egg::LanguageChildren`] macro accepts types with the following conditions:
//!
//! 1. It MUST be a struct with exactly one generic parameter.
//! 2. Every field must be one of the following (where `T` its only parameter):
//!     + `T`,
//!     + [`[T; N]`], or
//!     + [`Vec<T>`],
//! 3. AT MOST ONE [`Vec<T>`] is allowed in its field.
//!
//! ## Writing Rewrite Rules
//!
//! `egg_recursive` provides a redefined [`rewrite!`] macro to write rewrite rules.
//! This is almost the same as the original [`egg::rewrite!`] macro, but it allows you to write conditions in a more readable way.
//! Due to the ambiguity issue, it doesn't support bidirectional rules (`<=>`) for the time being.
//!
//! The following illustrates the usage of the macro:
//!
//! ```ignore
//! pub fn make_rules() -> Vec<Rewrite<EggArith, ConstantFold>> {
//!     let v = |s: &str| ArithPat::pat_var(s);
//!     let x = || v("x");
//!     let y = || v("y");
//!     let x_var: Var = "?x".parse().unwrap();
//!     vec![
//!         rewrite!("add-comm"; x() + y() => y() + x()),
//!         rewrite!("mul-comm"; x() * y() => y() * x()),
//!         rewrite!("add-zero"; x() + ArithPat::float(0.0.into()) => x()),
//!         rewrite!("add-zero"; x() => x() + ArithPat::float(0.0.into())),
//!         rewrite!("mul-inv";
//!             x() * x().inv() => ArithPat::float(1.0.into())
//!         ;   if is_non_zero(x_var.clone())
//!         ),
//!     ]
//! }
//! ```
//!
//! In the above, `+` and `*` comes from `std::ops::Add` and `std::ops::Mul`  instances defined manually,
//! which is possible because `ArithPat` is a newtype, but not an alias.
//!
use std::borrow::Cow;

use egg::*;
pub use egg_recursive_derive::*;
pub use functo_rs::data::Functor;
use functo_rs::data::{ArrayFunctor, Identity, UndetVec};

/// A type synonym for `L::Container<T>`,
/// which corresponds to primitive terms of `L` with parameters in `T`.
pub type Signature<L, T> = <L as Functor>::Container<T>;

/// A type synonym for [`Signature<L, Id>`], which should be an egg [`Language`].
pub type AsLanguage<L> = <L as Functor>::Container<Id>;

/// A recursive language, which can be converted to and from a [`RecExpr`].
///
/// In this case, `<Self as Functor>::Container<T>` plays a role of a language signature of language `L`, that is, primitive terms with parameters `T`
/// (hence the synonym [`Signature<L, T>`]).
/// Corresponding [`Language`] in egg must be [`AsLanguage<Self>`] (e.g. [`Signature<L, Id>`]).
pub trait Recursive: Functor + Sized
where
    AsLanguage<Self>: Language,
{
    /// Unwraps a recursion by one-level.
    fn unwrap(self) -> Signature<Self, Self>;

    /// Wraps a recursion by one-level.
    fn wrap(inner: Signature<Self, Self>) -> Self;

    /// Structural clone
    fn sclone<T: Clone>(sig: &Signature<Self, T>) -> Signature<Self, T>;

    /// Converts a reference to a signature into a signature of references.
    fn sig_each_ref<T>(refs: &Signature<Self, T>) -> Signature<Self, &T>;

    /// Adds into an existing [`RecExpr`] and returns the id of the root node.
    fn add_into_rec_expr(self, expr: &mut RecExpr<Signature<Self, Id>>) -> Id
where {
        let graph = Self::fmap(|e| e.add_into_rec_expr(expr), self.unwrap());
        expr.add(graph)
    }

    /// Converts into a [`RecExpr`].
    fn into_rec_expr(self) -> (RecExpr<Signature<Self, Id>>, Id) {
        let mut expr = RecExpr::default();
        let id = self.add_into_rec_expr(&mut expr);
        (expr, id)
    }

    /// Converts from a [`RecExpr`].
    fn from_rec_expr(expr: &RecExpr<Signature<Self, Id>>, id: Id) -> Self {
        Self::wrap(Self::fmap(
            |e| Self::from_rec_expr(expr, e),
            expr[id].clone(),
        ))
    }
}

/// Recursive AST for patterns in language `L`.
/// This can be converted to/from [`Pattern<AsLanguage<L>>`].
///
/// User can also define [`From<L> for Pat<L>`] for concrete `L` with [`Self::wrap`].
///
/// [`Language`] derive macro will genrate a newtype wrapper of this type, which implements [`egg::Searcher`] and [`egg::Applier`] too, and it can have (otherwise orphan) custom instances.
pub enum Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    /// Variable in pattern.
    PatVar(Var),
    /// Recursive pattern.
    Wrap(Signature<L, Box<Pat<L>>>),
}

impl<L> Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn add_vars_to(&self, vars: &mut Vec<Var>) {
        match self {
            Self::PatVar(v) => {
                if !vars.contains(v) {
                    vars.push(*v)
                }
            }
            Self::Wrap(w) => {
                L::fmap(|e| e.add_vars_to(vars), L::sig_each_ref(w));
            }
        }
    }
}

impl<L, N> egg::Searcher<AsLanguage<L>, N> for Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
    N: Analysis<AsLanguage<L>>,
{
    fn search_eclass_with_limit(
        &self,
        egraph: &EGraph<AsLanguage<L>, N>,
        eclass: Id,
        limit: usize,
    ) -> Option<::egg::SearchMatches<'_, AsLanguage<L>>> {
        use ::egg::*;
        let pat: Pattern<AsLanguage<L>> = Pattern::from(self);
        let SearchMatches {
            eclass,
            substs,
            ast,
        } = pat.search_eclass_with_limit(egraph, eclass, limit)?;
        Some(SearchMatches {
            eclass,
            substs,
            ast: ast.map(|cow| Cow::Owned(cow.into_owned())),
        })
    }

    fn vars(&self) -> Vec<Var> {
        let mut vars = Vec::new();
        self.add_vars_to(&mut vars);
        vars
    }
}

impl<L, N> egg::Applier<AsLanguage<L>, N> for Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
    N: Analysis<AsLanguage<L>>,
{
    fn apply_one(
        &self,
        egraph: &mut EGraph<AsLanguage<L>, N>,
        eclass: Id,
        subst: &Subst,
        searcher_ast: Option<&PatternAst<AsLanguage<L>>>,
        rule_name: Symbol,
    ) -> Vec<Id> {
        Pattern::from(self).apply_one(egraph, eclass, subst, searcher_ast, rule_name)
    }
}

impl<L> Clone for Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn clone(&self) -> Self {
        match self {
            Self::PatVar(arg0) => Self::PatVar(*arg0),
            Self::Wrap(arg0) => Self::Wrap(L::sclone(arg0)),
        }
    }
}

impl<L> Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    /// Smart constructor for a variable.
    /// NOTE: this DOES NOT require `?`-prefix before the identifier.
    pub fn pat_var<'a, S: Into<Cow<'a, str>>>(v: S) -> Self {
        Self::PatVar(format!("?{}", v.into()).parse().unwrap())
    }

    /// Unwraps the pattern recursion by one layer.
    pub fn unwrap(self) -> ENodeOrVar<Signature<L, Self>> {
        match self {
            Pat::PatVar(v) => ENodeOrVar::Var(v),
            Pat::Wrap(w) => ENodeOrVar::ENode(L::fmap(|e| *e, w)),
        }
    }

    /// Wraps the pattern recursion by one layer.
    pub fn wrap(inner: Signature<L, Self>) -> Self {
        Pat::Wrap(L::fmap(Box::new, inner))
    }
}

impl<L> From<Var> for Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: Var) -> Self {
        Self::PatVar(value)
    }
}

impl<L> From<L> for Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: L) -> Self {
        Self::Wrap(L::fmap(|e| Box::new(e.into()), value.unwrap()))
    }
}

impl<L> Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    /// Convert a recursive pattern into a [`PatternAst<L>`].
    /// See also: [`Self::to_pattern_ast`].
    pub fn into_pattern_ast(self) -> PatternAst<AsLanguage<L>> {
        let mut ast = PatternAst::default();
        self.add_into_pattern_ast(&mut ast);
        ast
    }

    /// Convert a borrowed recursive pattern to a [`PatternAst<L>`].
    /// See also: [`Self::into_pattern_ast`].
    pub fn to_pattern_ast(&self) -> PatternAst<AsLanguage<L>> {
        let mut ast = PatternAst::default();
        self.add_to_pattern_ast(&mut ast);
        ast
    }

    /// Convert a [`PatternAst<L>`] into a recursive pattern.
    pub fn from_pattern_ast(ast: &PatternAst<AsLanguage<L>>, id: Id) -> Self {
        match &ast[id] {
            ENodeOrVar::Var(v) => Pat::PatVar(*v),
            ENodeOrVar::ENode(e) => Pat::Wrap(L::fmap(
                |e| Box::new(Pat::<L>::from_pattern_ast(ast, e)),
                e.clone(),
            )),
        }
    }

    /// Adds the pattern into an existing [`PatternAst`] and returns the id of the root node. See also: [`Self::into_pattern_ast`], [`Self::add_to_pattern_ast`].
    pub fn add_into_pattern_ast(self, ast: &mut PatternAst<AsLanguage<L>>) -> Id {
        match self {
            Pat::PatVar(v) => ast.add(ENodeOrVar::Var(v)),
            Pat::Wrap(w) => {
                let graph = L::fmap(|e| e.add_into_pattern_ast(ast), w);
                ast.add(ENodeOrVar::ENode(graph))
            }
        }
    }

    /// Adds the borrowed pattern into an existing [`PatternAst`] and returns the id of the root node. See also: [`Self::to_pattern_ast`], [`Self::add_into_pattern_ast`].
    pub fn add_to_pattern_ast(&self, ast: &mut PatternAst<AsLanguage<L>>) -> Id {
        match self {
            Pat::PatVar(v) => ast.add(ENodeOrVar::Var(*v)),
            Pat::Wrap(w) => {
                let graph = L::fmap(|e| e.add_to_pattern_ast(ast), L::sig_each_ref(w));
                ast.add(ENodeOrVar::ENode(graph))
            }
        }
    }
}

impl<L> From<Pat<L>> for PatternAst<AsLanguage<L>>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: Pat<L>) -> Self {
        value.into_pattern_ast()
    }
}

impl<L> From<&Pat<L>> for PatternAst<AsLanguage<L>>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: &Pat<L>) -> Self {
        value.to_pattern_ast()
    }
}

impl<L> From<&Pat<L>> for Pattern<AsLanguage<L>>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: &Pat<L>) -> Self {
        Pattern::new(value.to_pattern_ast())
    }
}

impl<L> From<PatternAst<AsLanguage<L>>> for Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: PatternAst<AsLanguage<L>>) -> Self {
        let root = value.root();
        Pat::from_pattern_ast(&value, root)
    }
}

impl<L> From<Pat<L>> for Pattern<AsLanguage<L>>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: Pat<L>) -> Self {
        Pattern::new(value.into_pattern_ast())
    }
}

impl<L> From<Pattern<AsLanguage<L>>> for Pat<L>
where
    L: Recursive,
    AsLanguage<L>: Language,
{
    fn from(value: Pattern<AsLanguage<L>>) -> Self {
        Pat::from(value.ast)
    }
}

pub type LangChildren<T> = <<T as IntoLanguageChildren>::RawData as Functor>::Container<Id>;

pub type RawData<T, U> = <<T as IntoLanguageChildren>::RawData as Functor>::Container<U>;

pub type View<T, U> = <<T as IntoLanguageChildren>::View as Functor>::Container<U>;

/// A trait for a type that can be converted from/to [`egg::LanguageChildren`].
/// [`Self::RawData`] must be a [`egg::LanguageChildren`].
///
/// This can be derived by [`self::LanguageChildren`] derive macro.
pub trait IntoLanguageChildren: Sized
where
    Self::View: Functor<Container<Self::Param> = Self>,
    Self::RawData: Functor,
    <Self::RawData as Functor>::Container<Id>: LanguageChildren,
{
    /// Parameter type (e.g. `T` for `Vec<T>`)
    type Param;
    /// View type, which has a descriptive field name.
    type View;
    /// Raw [`egg::LanguageChildren`] type, which is typiacally a fixed-length array,
    /// [`Vec`], [`egg::Id`], or their newtype-wrapper.
    type RawData;

    /// Converts [`egg::LanguageChildren`] into a view type.
    fn view<T>(children: RawData<Self, T>) -> View<Self, T>;

    /// Converts a view into [`egg::LanguageChildren`].
    fn unview<T>(children: View<Self, T>) -> RawData<Self, T>;

    /// Maps a view by a function.
    fn map<U>(self, f: impl FnMut(Self::Param) -> U) -> View<Self, U> {
        <Self::View as Functor>::fmap(f, self)
    }

    /// Converts a reference to a [`egg::LanguageChildren`] into a reference to a view.
    fn raw_as_refs<T>(refs: &RawData<Self, T>) -> RawData<Self, &T>;
}

impl<T> IntoLanguageChildren for Vec<T> {
    type View = UndetVec;
    type Param = T;
    type RawData = UndetVec;

    #[inline(always)]
    fn view<U>(children: Vec<U>) -> Vec<U> {
        children
    }

    #[inline(always)]
    fn unview<U>(children: Vec<U>) -> Vec<U> {
        children
    }

    #[inline(always)]
    fn raw_as_refs<U>(refs: &Vec<U>) -> Vec<&U> {
        refs.iter().collect()
    }
}

impl<T, const N: usize> IntoLanguageChildren for [T; N] {
    type View = ArrayFunctor<N>;
    type Param = T;
    type RawData = ArrayFunctor<N>;

    #[inline(always)]
    fn view<U>(children: [U; N]) -> [U; N] {
        children
    }

    #[inline(always)]
    fn unview<U>(children: [U; N]) -> [U; N] {
        children
    }

    #[inline(always)]
    fn raw_as_refs<U>(refs: &[U; N]) -> [&U; N] {
        refs.each_ref()
    }
}

impl IntoLanguageChildren for Id {
    type View = Identity;
    type Param = Id;
    type RawData = Identity;

    #[inline(always)]
    fn view<T>(children: T) -> T {
        children
    }

    #[inline(always)]
    fn unview<T>(children: T) -> T {
        children
    }

    #[inline(always)]
    fn raw_as_refs<T>(refs: &T) -> &T {
        refs
    }
}

#[macro_export]
macro_rules! rewrite {
    (
        $name:expr;
        $lhs:expr => $rhs:expr
    )  => {{
        let searcher = ::egg::Pattern::from($lhs);
        let applier = ::egg::Pattern::from($rhs);
        ::egg::Rewrite::new($name.to_string(), searcher, applier).unwrap()
    }};
    (
        $name:expr;
        $lhs:expr => $rhs:expr;
        $(if $cond:expr)*
    )  => {{
        let searcher = ::egg::Pattern::from($lhs);
        let core_applier = ::egg::Pattern::from($rhs);
        let applier = ::egg::__rewrite!(@applier core_applier; $($cond,)*);
        ::egg::Rewrite::new($name.to_string(), searcher, applier).unwrap()
    }};
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::Path;

    #[test]
    fn test_invalid_language_derivations() {
        let t = trybuild::TestCases::new();
        let mut chs = fs::read_dir(Path::new("tests").join("invalid").join("language"))
            .unwrap()
            .flatten()
            .filter(|p| p.path().extension() == Some("rs".as_ref()))
            .collect::<Vec<_>>();
        chs.sort_by_key(|p| p.file_name());
        for entry in chs {
            t.compile_fail(entry.path());
        }
    }

    #[test]
    fn test_success() {
        let t = trybuild::TestCases::new();
        let mut chs = fs::read_dir(Path::new("tests").join("success"))
            .unwrap()
            .flatten()
            .filter(|p| p.path().extension() == Some("rs".as_ref()))
            .collect::<Vec<_>>();
        chs.sort_by_key(|p| p.file_name());
        for entry in chs {
            t.pass(entry.path());
        }
    }
}