Skip to content

Commit 7cd7bbc

Browse files
committed
WIP
1 parent b17736a commit 7cd7bbc

File tree

7 files changed

+404
-50
lines changed

7 files changed

+404
-50
lines changed

compiler/rustc_builtin_macros/src/assert.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,18 @@ struct Assert {
103103
custom_message: Option<TokenStream>,
104104
}
105105

106-
/// `if !{ ... } { ... }`
106+
/// `match <cond> { true => {} _ => <then> }`
107107
fn expr_if_not(cx: &ExtCtxt<'_>, span: Span, cond: P<Expr>, then: P<Expr>) -> P<Expr> {
108-
// Instead of expanding to `if !<cond> { <then> }`, we expand to `if <cond> {} else { <then> }`.
108+
// Instead of expanding to `if !<cond> { <then> }`, we expand to
109+
// `match <cond> { true => {} _ <then> }`.
109110
// This allows us to always complain about mismatched types instead of "cannot apply unary
110-
// operator `!` to type `X`" when passing an invalid `<cond>`.
111+
// operator `!` to type `X`" when passing an invalid `<cond>`, while also allowing `<cond>` to
112+
// be `&true`.
111113
let els = cx.expr_block(cx.block(span, thin_vec![]));
112-
cx.expr_if(span, cond, els, Some(then))
114+
let mut arms = thin_vec![];
115+
arms.push(cx.arm(span, cx.pat_lit(span, cx.expr_bool(span, true)), els));
116+
arms.push(cx.arm(span, cx.pat_wild(span), then));
117+
cx.expr_match(span, cond, arms)
113118
}
114119

115120
fn parse_assert<'a>(cx: &ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PResult<'a, Assert> {

0 commit comments

Comments
 (0)