summaryrefslogtreecommitdiffstats
path: root/tests/ui-fulldeps/pprust-expr-roundtrip.rs
blob: 7a91dcf0dad35b9808f64c5b61f5859fc78fb9a0 (plain)
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
// run-pass
// ignore-cross-compile

// The general idea of this test is to enumerate all "interesting" expressions and check that
// `parse(print(e)) == e` for all `e`. Here's what's interesting, for the purposes of this test:
//
// 1. The test focuses on expression nesting, because interactions between different expression
//    types are harder to test manually than single expression types in isolation.
//
// 2. The test only considers expressions of at most two nontrivial nodes. So it will check `x +
//    x` and `x + (x - x)` but not `(x * x) + (x - x)`. The assumption here is that the correct
//    handling of an expression might depend on the expression's parent, but doesn't depend on its
//    siblings or any more distant ancestors.
//
// 3. The test only checks certain expression kinds. The assumption is that similar expression
//    types, such as `if` and `while` or `+` and `-`, will be handled identically in the printer
//    and parser. So if all combinations of exprs involving `if` work correctly, then combinations
//    using `while`, `if let`, and so on will likely work as well.

#![feature(rustc_private)]

extern crate rustc_ast;
extern crate rustc_ast_pretty;
extern crate rustc_data_structures;
extern crate rustc_parse;
extern crate rustc_session;
extern crate rustc_span;
extern crate thin_vec;

// Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta
// files.
#[allow(unused_extern_crates)]
extern crate rustc_driver;

use rustc_ast::mut_visit::{self, visit_clobber, MutVisitor};
use rustc_ast::ptr::P;
use rustc_ast::*;
use rustc_ast_pretty::pprust;
use rustc_parse::new_parser_from_source_str;
use rustc_session::parse::ParseSess;
use rustc_span::source_map::FilePathMapping;
use rustc_span::source_map::{FileName, Spanned, DUMMY_SP};
use rustc_span::symbol::Ident;
use thin_vec::thin_vec;

fn parse_expr(ps: &ParseSess, src: &str) -> Option<P<Expr>> {
    let src_as_string = src.to_string();

    let mut p =
        new_parser_from_source_str(ps, FileName::Custom(src_as_string.clone()), src_as_string);
    p.parse_expr().map_err(|e| e.cancel()).ok()
}

// Helper functions for building exprs
fn expr(kind: ExprKind) -> P<Expr> {
    P(Expr { id: DUMMY_NODE_ID, kind, span: DUMMY_SP, attrs: AttrVec::new(), tokens: None })
}

fn make_x() -> P<Expr> {
    let seg = PathSegment::from_ident(Ident::from_str("x"));
    let path = Path { segments: thin_vec![seg], span: DUMMY_SP, tokens: None };
    expr(ExprKind::Path(None, path))
}

/// Iterate over exprs of depth up to `depth`. The goal is to explore all "interesting"
/// combinations of expression nesting. For example, we explore combinations using `if`, but not
/// `while` or `match`, since those should print and parse in much the same way as `if`.
fn iter_exprs(depth: usize, f: &mut dyn FnMut(P<Expr>)) {
    if depth == 0 {
        f(make_x());
        return;
    }

    let mut g = |e| f(expr(e));

    for kind in 0..=19 {
        match kind {
            0 => iter_exprs(depth - 1, &mut |e| g(ExprKind::Box(e))),
            1 => iter_exprs(depth - 1, &mut |e| g(ExprKind::Call(e, vec![]))),
            2 => {
                let seg = PathSegment::from_ident(Ident::from_str("x"));
                iter_exprs(depth - 1, &mut |e| {
                    g(ExprKind::MethodCall(Box::new(MethodCall {
                        seg: seg.clone(), receiver: e, args: vec![make_x()], span: DUMMY_SP
                    }))
                )});
                iter_exprs(depth - 1, &mut |e| {
                    g(ExprKind::MethodCall(Box::new(MethodCall {
                        seg: seg.clone(), receiver: make_x(), args: vec![e], span: DUMMY_SP
                    }))
                )});
            }
            3..=8 => {
                let op = Spanned {
                    span: DUMMY_SP,
                    node: match kind {
                        3 => BinOpKind::Add,
                        4 => BinOpKind::Mul,
                        5 => BinOpKind::Shl,
                        6 => BinOpKind::And,
                        7 => BinOpKind::Or,
                        8 => BinOpKind::Lt,
                        _ => unreachable!(),
                    },
                };
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Binary(op, e, make_x())));
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Binary(op, make_x(), e)));
            }
            9 => {
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Unary(UnOp::Deref, e)));
            }
            10 => {
                let block = P(Block {
                    stmts: Vec::new(),
                    id: DUMMY_NODE_ID,
                    rules: BlockCheckMode::Default,
                    span: DUMMY_SP,
                    tokens: None,
                    could_be_bare_literal: false,
                });
                iter_exprs(depth - 1, &mut |e| g(ExprKind::If(e, block.clone(), None)));
            }
            11 => {
                let decl = P(FnDecl { inputs: vec![], output: FnRetTy::Default(DUMMY_SP) });
                iter_exprs(depth - 1, &mut |e| {
                    g(ExprKind::Closure(Box::new(Closure {
                        binder: ClosureBinder::NotPresent,
                        capture_clause: CaptureBy::Value,
                        constness: Const::No,
                        asyncness: Async::No,
                        movability: Movability::Movable,
                        fn_decl: decl.clone(),
                        body: e,
                        fn_decl_span: DUMMY_SP,
                        fn_arg_span: DUMMY_SP,
                    })))
                });
            }
            12 => {
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Assign(e, make_x(), DUMMY_SP)));
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Assign(make_x(), e, DUMMY_SP)));
            }
            13 => {
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Field(e, Ident::from_str("f"))));
            }
            14 => {
                iter_exprs(depth - 1, &mut |e| {
                    g(ExprKind::Range(Some(e), Some(make_x()), RangeLimits::HalfOpen))
                });
                iter_exprs(depth - 1, &mut |e| {
                    g(ExprKind::Range(Some(make_x()), Some(e), RangeLimits::HalfOpen))
                });
            }
            15 => {
                iter_exprs(depth - 1, &mut |e| {
                    g(ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, e))
                });
            }
            16 => {
                g(ExprKind::Ret(None));
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Ret(Some(e))));
            }
            17 => {
                let path = Path::from_ident(Ident::from_str("S"));
                g(ExprKind::Struct(P(StructExpr {
                    qself: None,
                    path,
                    fields: vec![],
                    rest: StructRest::Base(make_x()),
                })));
            }
            18 => {
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Try(e)));
            }
            19 => {
                let pat =
                    P(Pat { id: DUMMY_NODE_ID, kind: PatKind::Wild, span: DUMMY_SP, tokens: None });
                iter_exprs(depth - 1, &mut |e| g(ExprKind::Let(pat.clone(), e, DUMMY_SP)))
            }
            _ => panic!("bad counter value in iter_exprs"),
        }
    }
}

// Folders for manipulating the placement of `Paren` nodes. See below for why this is needed.

/// `MutVisitor` that removes all `ExprKind::Paren` nodes.
struct RemoveParens;

impl MutVisitor for RemoveParens {
    fn visit_expr(&mut self, e: &mut P<Expr>) {
        match e.kind.clone() {
            ExprKind::Paren(inner) => *e = inner,
            _ => {}
        };
        mut_visit::noop_visit_expr(e, self);
    }
}

/// `MutVisitor` that inserts `ExprKind::Paren` nodes around every `Expr`.
struct AddParens;

impl MutVisitor for AddParens {
    fn visit_expr(&mut self, e: &mut P<Expr>) {
        mut_visit::noop_visit_expr(e, self);
        visit_clobber(e, |e| {
            P(Expr {
                id: DUMMY_NODE_ID,
                kind: ExprKind::Paren(e),
                span: DUMMY_SP,
                attrs: AttrVec::new(),
                tokens: None,
            })
        });
    }
}

fn main() {
    rustc_span::create_default_session_globals_then(|| run());
}

fn run() {
    let ps = ParseSess::new(FilePathMapping::empty());

    iter_exprs(2, &mut |mut e| {
        // If the pretty printer is correct, then `parse(print(e))` should be identical to `e`,
        // modulo placement of `Paren` nodes.
        let printed = pprust::expr_to_string(&e);
        println!("printed: {}", printed);

        // Ignore expressions with chained comparisons that fail to parse
        if let Some(mut parsed) = parse_expr(&ps, &printed) {
            // We want to know if `parsed` is structurally identical to `e`, ignoring trivial
            // differences like placement of `Paren`s or the exact ranges of node spans.
            // Unfortunately, there is no easy way to make this comparison. Instead, we add `Paren`s
            // everywhere we can, then pretty-print. This should give an unambiguous representation
            // of each `Expr`, and it bypasses nearly all of the parenthesization logic, so we
            // aren't relying on the correctness of the very thing we're testing.
            RemoveParens.visit_expr(&mut e);
            AddParens.visit_expr(&mut e);
            let text1 = pprust::expr_to_string(&e);
            RemoveParens.visit_expr(&mut parsed);
            AddParens.visit_expr(&mut parsed);
            let text2 = pprust::expr_to_string(&parsed);
            assert!(
                text1 == text2,
                "exprs are not equal:\n  e =      {:?}\n  parsed = {:?}",
                text1,
                text2
            );
        }
    });
}