Skip to main content

rustc_builtin_macros/assert/
context.rs

1use rustc_ast::token::{self, Delimiter, IdentIsRaw};
2use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
3use rustc_ast::{
4    BinOpKind, BorrowKind, DUMMY_NODE_ID, DelimArgs, Expr, ExprKind, ItemKind, MacCall, MethodCall,
5    Mutability, Path, PathSegment, Stmt, StructRest, UnOp, UseTree, UseTreeKind,
6};
7use rustc_ast_pretty::pprust;
8use rustc_data_structures::fx::FxHashSet;
9use rustc_expand::base::ExtCtxt;
10use rustc_span::{Ident, Span, Symbol, sym};
11use thin_vec::{ThinVec, thin_vec};
12
13pub(super) struct Context<'cx, 'a> {
14    // An optimization.
15    //
16    // Elements that aren't consumed (PartialEq, PartialOrd, ...) can be copied **after** the
17    // `assert!` expression fails rather than copied on-the-fly.
18    best_case_captures: Vec<Stmt>,
19    // Top-level `let captureN = Capture::new()` statements
20    capture_decls: Vec<Capture>,
21    cx: &'cx ExtCtxt<'a>,
22    // Formatting string used for debugging
23    fmt_string: String,
24    // If the current expression being visited consumes itself. Used to construct
25    // `best_case_captures`.
26    is_consumed: bool,
27    // Top-level `let __local_bindN = &expr` statements
28    local_bind_decls: Vec<Stmt>,
29    // Used to avoid capturing duplicated paths
30    //
31    // ```rust
32    // let a = 1i32;
33    // assert!(add(a, a) == 3);
34    // ```
35    paths: FxHashSet<Ident>,
36    span: Span,
37}
38
39impl<'cx, 'a> Context<'cx, 'a> {
40    pub(super) fn new(cx: &'cx ExtCtxt<'a>, span: Span) -> Self {
41        Self {
42            best_case_captures: <_>::default(),
43            capture_decls: <_>::default(),
44            cx,
45            fmt_string: <_>::default(),
46            is_consumed: true,
47            local_bind_decls: <_>::default(),
48            paths: <_>::default(),
49            span,
50        }
51    }
52
53    /// Builds the whole `assert!` expression. For example, `let elem = 1; assert!(elem == 1);` expands to:
54    ///
55    /// ```rust
56    /// let elem = 1;
57    /// {
58    ///   #[allow(unused_imports)]
59    ///   use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
60    ///   let mut __capture0 = ::core::asserting::Capture::new();
61    ///   let __local_bind0 = &elem;
62    ///   if !(
63    ///     *{
64    ///       (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
65    ///       __local_bind0
66    ///     } == 1
67    ///   ) {
68    ///     panic!("Assertion failed: elem == 1\nWith captures:\n  elem = {:?}", __capture0)
69    ///   }
70    /// }
71    /// ```
72    pub(super) fn build(mut self, mut cond_expr: Box<Expr>, panic_path: Path) -> Box<Expr> {
73        let expr_str = pprust::expr_to_string(&cond_expr);
74        self.manage_cond_expr(&mut cond_expr);
75        let initial_imports = self.build_initial_imports();
76        let panic = self.build_panic(&expr_str, panic_path);
77        let cond_expr_with_unlikely = self.build_unlikely(cond_expr);
78
79        let Self { best_case_captures, capture_decls, cx, local_bind_decls, span, .. } = self;
80
81        let mut assert_then_stmts = ThinVec::with_capacity(2);
82        assert_then_stmts.extend(best_case_captures);
83        assert_then_stmts.push(self.cx.stmt_expr(panic));
84        let assert_then = self.cx.block(span, assert_then_stmts);
85
86        let mut stmts = ThinVec::with_capacity(4);
87        stmts.push(initial_imports);
88        stmts.extend(capture_decls.into_iter().map(|c| c.decl));
89        stmts.extend(local_bind_decls);
90        stmts.push(
91            cx.stmt_expr(cx.expr(span, ExprKind::If(cond_expr_with_unlikely, assert_then, None))),
92        );
93        cx.expr_block(cx.block(span, stmts))
94    }
95
96    /// Initial **trait** imports
97    ///
98    /// use ::core::asserting::{ ... };
99    fn build_initial_imports(&self) -> Stmt {
100        let nested_tree = |this: &Self, sym| {
101            (
102                UseTree {
103                    prefix: this.cx.path(this.span, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [Ident::with_dummy_span(sym)]))vec![Ident::with_dummy_span(sym)]),
104                    kind: UseTreeKind::Simple(None),
105                },
106                DUMMY_NODE_ID,
107            )
108        };
109        self.cx.stmt_item(
110            self.span,
111            self.cx.item(
112                self.span,
113                {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(self.cx.attr_nested_word(sym::allow, sym::unused_imports,
            self.span));
    vec
}thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)],
114                ItemKind::Use(UseTree {
115                    prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
116                    kind: UseTreeKind::Nested {
117                        items: {
    let len = [(), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(nested_tree(self, sym::TryCaptureGeneric));
    vec.push(nested_tree(self, sym::TryCapturePrintable));
    vec
}thin_vec![
118                            nested_tree(self, sym::TryCaptureGeneric),
119                            nested_tree(self, sym::TryCapturePrintable),
120                        ],
121                        span: self.span,
122                    },
123                }),
124            ),
125        )
126    }
127
128    /// Takes the conditional expression of `assert!` and then wraps it inside `unlikely`
129    fn build_unlikely(&self, cond_expr: Box<Expr>) -> Box<Expr> {
130        let unlikely_path = self.cx.std_path(&[sym::intrinsics, sym::unlikely]);
131        self.cx.expr_call(
132            self.span,
133            self.cx.expr_path(self.cx.path(self.span, unlikely_path)),
134            {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(self.cx.expr(self.span, ExprKind::Unary(UnOp::Not, cond_expr)));
    vec
}thin_vec![self.cx.expr(self.span, ExprKind::Unary(UnOp::Not, cond_expr))],
135        )
136    }
137
138    /// The necessary custom `panic!(...)` expression.
139    ///
140    /// panic!(
141    ///     "Assertion failed: ... \n With expansion: ...",
142    ///     __capture0,
143    ///     ...
144    /// );
145    fn build_panic(&self, expr_str: &str, panic_path: Path) -> Box<Expr> {
146        let escaped_expr_str = escape_to_fmt(expr_str);
147        let initial = [
148            TokenTree::token_joint(
149                token::Literal(token::Lit {
150                    kind: token::LitKind::Str,
151                    symbol: Symbol::intern(&if self.fmt_string.is_empty() {
152                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Assertion failed: {0}",
                escaped_expr_str))
    })format!("Assertion failed: {escaped_expr_str}")
153                    } else {
154                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Assertion failed: {1}\nWith captures:\n{0}",
                &self.fmt_string, escaped_expr_str))
    })format!(
155                            "Assertion failed: {escaped_expr_str}\nWith captures:\n{}",
156                            &self.fmt_string
157                        )
158                    }),
159                    suffix: None,
160                }),
161                self.span,
162            ),
163            TokenTree::token_alone(token::Comma, self.span),
164        ];
165        let captures = self.capture_decls.iter().flat_map(|cap| {
166            [
167                TokenTree::token_joint(
168                    token::Ident(cap.ident.name, IdentIsRaw::No),
169                    cap.ident.span,
170                ),
171                TokenTree::token_alone(token::Comma, self.span),
172            ]
173        });
174        self.cx.expr(
175            self.span,
176            ExprKind::MacCall(Box::new(MacCall {
177                path: panic_path,
178                args: Box::new(DelimArgs {
179                    dspan: DelimSpan::from_single(self.span),
180                    delim: Delimiter::Parenthesis,
181                    tokens: initial.into_iter().chain(captures).collect::<TokenStream>(),
182                }),
183            })),
184        )
185    }
186
187    /// Recursive function called until `cond_expr` and `fmt_str` are fully modified.
188    ///
189    /// See [Self::manage_initial_capture] and [Self::manage_try_capture]
190    fn manage_cond_expr(&mut self, expr: &mut Box<Expr>) {
191        match &mut expr.kind {
192            ExprKind::AddrOf(_, mutability, local_expr) => {
193                self.with_is_consumed_management(#[allow(non_exhaustive_omitted_patterns)] match mutability {
    Mutability::Mut => true,
    _ => false,
}matches!(mutability, Mutability::Mut), |this| {
194                    this.manage_cond_expr(local_expr)
195                });
196            }
197            ExprKind::Array(local_exprs) => {
198                for local_expr in local_exprs {
199                    self.manage_cond_expr(local_expr);
200                }
201            }
202            ExprKind::Binary(op, lhs, rhs) => {
203                self.with_is_consumed_management(
204                    #[allow(non_exhaustive_omitted_patterns)] match op.node {
    BinOpKind::Add | BinOpKind::And | BinOpKind::BitAnd | BinOpKind::BitOr |
        BinOpKind::BitXor | BinOpKind::Div | BinOpKind::Mul | BinOpKind::Or |
        BinOpKind::Rem | BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub =>
        true,
    _ => false,
}matches!(
205                        op.node,
206                        BinOpKind::Add
207                            | BinOpKind::And
208                            | BinOpKind::BitAnd
209                            | BinOpKind::BitOr
210                            | BinOpKind::BitXor
211                            | BinOpKind::Div
212                            | BinOpKind::Mul
213                            | BinOpKind::Or
214                            | BinOpKind::Rem
215                            | BinOpKind::Shl
216                            | BinOpKind::Shr
217                            | BinOpKind::Sub
218                    ),
219                    |this| {
220                        this.manage_cond_expr(lhs);
221                        this.manage_cond_expr(rhs);
222                    },
223                );
224            }
225            ExprKind::Call(_, local_exprs) => {
226                for local_expr in local_exprs {
227                    self.manage_cond_expr(local_expr);
228                }
229            }
230            ExprKind::Cast(local_expr, _) => {
231                self.manage_cond_expr(local_expr);
232            }
233            ExprKind::If(local_expr, _, _) => {
234                self.manage_cond_expr(local_expr);
235            }
236            ExprKind::Index(prefix, suffix, _) => {
237                self.manage_cond_expr(prefix);
238                self.manage_cond_expr(suffix);
239            }
240            ExprKind::Let(_, local_expr, _, _) => {
241                self.manage_cond_expr(local_expr);
242            }
243            ExprKind::Match(local_expr, ..) => {
244                self.manage_cond_expr(local_expr);
245            }
246            ExprKind::MethodCall(call) => {
247                for arg in &mut call.args {
248                    self.manage_cond_expr(arg);
249                }
250            }
251            ExprKind::Move(local_expr, _) => {
252                self.manage_cond_expr(local_expr);
253            }
254            ExprKind::Path(_, Path { segments, .. }) if let [path_segment] = &segments[..] => {
255                let path_ident = path_segment.ident;
256                self.manage_initial_capture(expr, path_ident);
257            }
258            ExprKind::Paren(local_expr) => {
259                self.manage_cond_expr(local_expr);
260            }
261            ExprKind::Range(prefix, suffix, _) => {
262                if let Some(elem) = prefix {
263                    self.manage_cond_expr(elem);
264                }
265                if let Some(elem) = suffix {
266                    self.manage_cond_expr(elem);
267                }
268            }
269            ExprKind::Repeat(local_expr, elem) => {
270                self.manage_cond_expr(local_expr);
271                self.manage_cond_expr(&mut elem.value);
272            }
273            ExprKind::Struct(elem) => {
274                for field in &mut elem.fields {
275                    self.manage_cond_expr(&mut field.expr);
276                }
277                if let StructRest::Base(local_expr) = &mut elem.rest {
278                    self.manage_cond_expr(local_expr);
279                }
280            }
281            ExprKind::Tup(local_exprs) => {
282                for local_expr in local_exprs {
283                    self.manage_cond_expr(local_expr);
284                }
285            }
286            ExprKind::Unary(un_op, local_expr) => {
287                self.with_is_consumed_management(#[allow(non_exhaustive_omitted_patterns)] match un_op {
    UnOp::Neg | UnOp::Not => true,
    _ => false,
}matches!(un_op, UnOp::Neg | UnOp::Not), |this| {
288                    this.manage_cond_expr(local_expr)
289                });
290            }
291            // Expressions that are not worth or can not be captured.
292            //
293            // Full list instead of `_` to catch possible future inclusions and to
294            // sync with the `rfc-2011-nicer-assert-messages/all-expr-kinds.rs` test.
295            ExprKind::Assign(_, _, _)
296            | ExprKind::AssignOp(_, _, _)
297            | ExprKind::Gen(_, _, _, _)
298            | ExprKind::Await(_, _)
299            | ExprKind::Use(_, _)
300            | ExprKind::Block(_, _)
301            | ExprKind::Break(_, _)
302            | ExprKind::Closure(_)
303            | ExprKind::ConstBlock(_)
304            | ExprKind::Continue(_)
305            | ExprKind::Dummy
306            | ExprKind::Err(_)
307            | ExprKind::Field(_, _)
308            | ExprKind::ForLoop { .. }
309            | ExprKind::FormatArgs(_)
310            | ExprKind::IncludedBytes(..)
311            | ExprKind::InlineAsm(_)
312            | ExprKind::Lit(_)
313            | ExprKind::Loop(_, _, _)
314            | ExprKind::MacCall(_)
315            | ExprKind::OffsetOf(_, _)
316            | ExprKind::Path(_, _)
317            | ExprKind::Ret(_)
318            | ExprKind::Try(_)
319            | ExprKind::TryBlock(_, _)
320            | ExprKind::Type(_, _)
321            | ExprKind::Underscore
322            | ExprKind::While(_, _, _)
323            | ExprKind::Yeet(_)
324            | ExprKind::Become(_)
325            | ExprKind::Yield(_)
326            | ExprKind::UnsafeBinderCast(..) => {}
327        }
328    }
329
330    /// Pushes the top-level declarations and modifies `expr` to try capturing variables.
331    ///
332    /// `fmt_str`, the formatting string used for debugging, is constructed to show possible
333    /// captured variables.
334    fn manage_initial_capture(&mut self, expr: &mut Box<Expr>, path_ident: Ident) {
335        if self.paths.contains(&path_ident) {
336            return;
337        } else {
338            self.fmt_string.push_str("  ");
339            self.fmt_string.push_str(path_ident.as_str());
340            self.fmt_string.push_str(" = {:?}\n");
341            let _ = self.paths.insert(path_ident);
342        }
343        let curr_capture_idx = self.capture_decls.len();
344        let capture_string = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("__capture{0}", curr_capture_idx))
    })format!("__capture{curr_capture_idx}");
345        let ident = Ident::new(Symbol::intern(&capture_string), self.span);
346        let init_std_path = self.cx.std_path(&[sym::asserting, sym::Capture, sym::new]);
347        let init = self.cx.expr_call(
348            self.span,
349            self.cx.expr_path(self.cx.path(self.span, init_std_path)),
350            ThinVec::new(),
351        );
352        let capture = Capture { decl: self.cx.stmt_let(self.span, true, ident, init), ident };
353        self.capture_decls.push(capture);
354        self.manage_try_capture(ident, curr_capture_idx, expr);
355    }
356
357    /// Tries to copy `__local_bindN` into `__captureN`.
358    ///
359    /// *{
360    ///    (&Wrapper(__local_bindN)).try_capture(&mut __captureN);
361    ///    __local_bindN
362    /// }
363    fn manage_try_capture(
364        &mut self,
365        capture: Ident,
366        curr_capture_idx: usize,
367        expr: &mut Box<Expr>,
368    ) {
369        let local_bind_string = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("__local_bind{0}",
                curr_capture_idx))
    })format!("__local_bind{curr_capture_idx}");
370        let local_bind = Ident::new(Symbol::intern(&local_bind_string), self.span);
371        self.local_bind_decls.push(self.cx.stmt_let(
372            self.span,
373            false,
374            local_bind,
375            self.cx.expr_addr_of(self.span, expr.clone()),
376        ));
377        let wrapper = self.cx.expr_call(
378            self.span,
379            self.cx.expr_path(
380                self.cx.path(self.span, self.cx.std_path(&[sym::asserting, sym::Wrapper])),
381            ),
382            {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(self.cx.expr_path(Path::from_ident(local_bind)));
    vec
}thin_vec![self.cx.expr_path(Path::from_ident(local_bind))],
383        );
384        let try_capture_call = self
385            .cx
386            .stmt_expr(expr_method_call(
387                self.cx,
388                PathSegment {
389                    args: None,
390                    id: DUMMY_NODE_ID,
391                    ident: Ident::new(sym::try_capture, self.span),
392                },
393                expr_paren(self.cx, self.span, self.cx.expr_addr_of(self.span, wrapper)),
394                {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(expr_addr_of_mut(self.cx, self.span,
            self.cx.expr_path(Path::from_ident(capture))));
    vec
}thin_vec![expr_addr_of_mut(
395                    self.cx,
396                    self.span,
397                    self.cx.expr_path(Path::from_ident(capture)),
398                )],
399                self.span,
400            ))
401            .add_trailing_semicolon();
402        let local_bind_path = self.cx.expr_path(Path::from_ident(local_bind));
403        let rslt = if self.is_consumed {
404            let ret = self.cx.stmt_expr(local_bind_path);
405            self.cx.expr_block(self.cx.block(self.span, {
    let len = [(), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(try_capture_call);
    vec.push(ret);
    vec
}thin_vec![try_capture_call, ret]))
406        } else {
407            self.best_case_captures.push(try_capture_call);
408            local_bind_path
409        };
410        *expr = self.cx.expr_deref(self.span, rslt);
411    }
412
413    // Calls `f` with the internal `is_consumed` set to `curr_is_consumed` and then
414    // sets the internal `is_consumed` back to its original value.
415    fn with_is_consumed_management(&mut self, curr_is_consumed: bool, f: impl FnOnce(&mut Self)) {
416        let prev_is_consumed = self.is_consumed;
417        self.is_consumed = curr_is_consumed;
418        f(self);
419        self.is_consumed = prev_is_consumed;
420    }
421}
422
423/// Information about a captured element.
424#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Capture {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "Capture",
            "decl", &self.decl, "ident", &&self.ident)
    }
}Debug)]
425struct Capture {
426    // Generated indexed `Capture` statement.
427    //
428    // `let __capture{} = Capture::new();`
429    decl: Stmt,
430    // The name of the generated indexed `Capture` variable.
431    //
432    // `__capture{}`
433    ident: Ident,
434}
435
436/// Escapes to use as a formatting string.
437fn escape_to_fmt(s: &str) -> String {
438    let mut rslt = String::with_capacity(s.len());
439    for c in s.chars() {
440        rslt.extend(c.escape_debug());
441        match c {
442            '{' | '}' => rslt.push(c),
443            _ => {}
444        }
445    }
446    rslt
447}
448
449fn expr_addr_of_mut(cx: &ExtCtxt<'_>, sp: Span, e: Box<Expr>) -> Box<Expr> {
450    cx.expr(sp, ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, e))
451}
452
453fn expr_method_call(
454    cx: &ExtCtxt<'_>,
455    seg: PathSegment,
456    receiver: Box<Expr>,
457    args: ThinVec<Box<Expr>>,
458    span: Span,
459) -> Box<Expr> {
460    cx.expr(span, ExprKind::MethodCall(Box::new(MethodCall { seg, receiver, args, span })))
461}
462
463fn expr_paren(cx: &ExtCtxt<'_>, sp: Span, e: Box<Expr>) -> Box<Expr> {
464    cx.expr(sp, ExprKind::Paren(e))
465}