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
//! `AstEquiv` trait for checking equivalence of two ASTs.
use rustc_target::spec::abi::Abi;
use std::rc::Rc;
use syntax::ast::*;
use syntax::parse::token::{DelimToken, Nonterminal, Token};
use syntax::ptr::P;
use syntax::source_map::{Span, Spanned};
use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
use syntax::ThinVec;
use syntax_pos::hygiene::SyntaxContext;

/// Trait for checking equivalence of AST nodes.  This is similar to `PartialEq`, but less strict,
/// as it ignores some fields that have no bearing on the semantics of the AST (particularly
/// `Span`s and `NodeId`s).
pub trait AstEquiv {
    fn ast_equiv(&self, other: &Self) -> bool;
}

impl<'a, T: AstEquiv> AstEquiv for &'a T {
    fn ast_equiv(&self, other: &&'a T) -> bool {
        <T as AstEquiv>::ast_equiv(*self, *other)
    }
}

impl<T: AstEquiv> AstEquiv for P<T> {
    fn ast_equiv(&self, other: &P<T>) -> bool {
        <T as AstEquiv>::ast_equiv(self, other)
    }
}

impl<T: AstEquiv> AstEquiv for Rc<T> {
    fn ast_equiv(&self, other: &Rc<T>) -> bool {
        <T as AstEquiv>::ast_equiv(self, other)
    }
}

impl<T: AstEquiv> AstEquiv for Spanned<T> {
    fn ast_equiv(&self, other: &Spanned<T>) -> bool {
        self.node.ast_equiv(&other.node)
    }
}

impl<T: AstEquiv> AstEquiv for [T] {
    fn ast_equiv(&self, other: &[T]) -> bool {
        for (l, r) in self.iter().zip(other.iter()) {
            if !l.ast_equiv(r) {
                return false;
            }
        }
        true
    }
}

impl<T: AstEquiv> AstEquiv for Vec<T> {
    fn ast_equiv(&self, other: &Vec<T>) -> bool {
        <[T] as AstEquiv>::ast_equiv(self, other)
    }
}

impl<T: AstEquiv> AstEquiv for ThinVec<T> {
    fn ast_equiv(&self, other: &ThinVec<T>) -> bool {
        <[T] as AstEquiv>::ast_equiv(self, other)
    }
}

impl<T: AstEquiv> AstEquiv for Option<T> {
    fn ast_equiv(&self, other: &Option<T>) -> bool {
        match (self, other) {
            (&Some(ref x), &Some(ref y)) => x.ast_equiv(y),
            (&None, &None) => true,
            (_, _) => false,
        }
    }
}

impl<A: AstEquiv, B: AstEquiv> AstEquiv for (A, B) {
    fn ast_equiv(&self, other: &Self) -> bool {
        self.0.ast_equiv(&other.0) && self.1.ast_equiv(&other.1)
    }
}

impl<A: AstEquiv, B: AstEquiv, C: AstEquiv> AstEquiv for (A, B, C) {
    fn ast_equiv(&self, other: &Self) -> bool {
        self.0.ast_equiv(&other.0) && self.1.ast_equiv(&other.1) && self.2.ast_equiv(&other.2)
    }
}

// Implementations for specific AST types are auto-generated.
include!(concat!(env!("OUT_DIR"), "/ast_equiv_gen.inc.rs"));