From 2459dbb4bad87c38284b0a2ba4f2d6d37db99627 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 7 Dec 2024 12:49:08 +0100 Subject: [PATCH] Address review comments --- compiler/rustc_ast_pretty/src/pprust/state.rs | 2 ++ .../rfc-3637-guard-patterns/macro-rules.rs | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/ui/pattern/rfc-3637-guard-patterns/macro-rules.rs diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 32eea2befbf..fd2e97a79ca 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1710,10 +1710,12 @@ impl<'a> State<'a> { } } PatKind::Guard(subpat, condition) => { + self.popen(); self.print_pat(subpat); self.space(); self.word_space("if"); self.print_expr(condition, FixupContext::default()); + self.pclose(); } PatKind::Slice(elts) => { self.word("["); diff --git a/tests/ui/pattern/rfc-3637-guard-patterns/macro-rules.rs b/tests/ui/pattern/rfc-3637-guard-patterns/macro-rules.rs new file mode 100644 index 00000000000..76681f45bb3 --- /dev/null +++ b/tests/ui/pattern/rfc-3637-guard-patterns/macro-rules.rs @@ -0,0 +1,20 @@ +//@ run-pass +//! Tests that the addition of guard patterns does not change the behavior of the `pat` macro +//! fragment. +#![feature(guard_patterns)] +#![allow(incomplete_features)] + +macro_rules! has_guard { + ($p:pat) => { + false + }; + ($p:pat if $e:expr) => { + true + }; +} + +fn main() { + assert_eq!(has_guard!(Some(_)), false); + assert_eq!(has_guard!(Some(_) if true), true); + assert_eq!(has_guard!((Some(_) if true)), false); +}