Skip to content

Commit 42dcf70

Browse files
committed
Auto merge of #98148 - c410-f3r:assert-compiler, r=oli-obk
[RFC 2011] Expand expressions where possible Tracking issue: #44838 Fourth step of #96496 Extends #97665 considering expressions that are good candidates for expansion. r? `@oli-obk`
2 parents 0887113 + 47b057a commit 42dcf70

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

compiler/rustc_builtin_macros/src/assert/context.rs

+90-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::{
55
token,
66
tokenstream::{DelimSpan, TokenStream, TokenTree},
77
BorrowKind, Expr, ExprKind, ItemKind, MacArgs, MacCall, MacDelimiter, Mutability, Path,
8-
PathSegment, Stmt, UseTree, UseTreeKind, DUMMY_NODE_ID,
8+
PathSegment, Stmt, StructRest, UseTree, UseTreeKind, DUMMY_NODE_ID,
99
};
1010
use rustc_ast_pretty::pprust;
1111
use rustc_data_structures::fx::FxHashSet;
@@ -167,15 +167,103 @@ impl<'cx, 'a> Context<'cx, 'a> {
167167
/// See [Self::manage_initial_capture] and [Self::manage_try_capture]
168168
fn manage_cond_expr(&mut self, expr: &mut P<Expr>) {
169169
match (*expr).kind {
170+
ExprKind::AddrOf(_, _, ref mut local_expr) => {
171+
self.manage_cond_expr(local_expr);
172+
}
173+
ExprKind::Array(ref mut local_exprs) => {
174+
for local_expr in local_exprs {
175+
self.manage_cond_expr(local_expr);
176+
}
177+
}
170178
ExprKind::Binary(_, ref mut lhs, ref mut rhs) => {
171179
self.manage_cond_expr(lhs);
172180
self.manage_cond_expr(rhs);
173181
}
182+
ExprKind::Call(_, ref mut local_exprs) => {
183+
for local_expr in local_exprs {
184+
self.manage_cond_expr(local_expr);
185+
}
186+
}
187+
ExprKind::Cast(ref mut local_expr, _) => {
188+
self.manage_cond_expr(local_expr);
189+
}
190+
ExprKind::Index(ref mut prefix, ref mut suffix) => {
191+
self.manage_cond_expr(prefix);
192+
self.manage_cond_expr(suffix);
193+
}
194+
ExprKind::MethodCall(_, ref mut local_exprs, _) => {
195+
for local_expr in local_exprs.iter_mut().skip(1) {
196+
self.manage_cond_expr(local_expr);
197+
}
198+
}
174199
ExprKind::Path(_, Path { ref segments, .. }) if let &[ref path_segment] = &segments[..] => {
175200
let path_ident = path_segment.ident;
176201
self.manage_initial_capture(expr, path_ident);
177202
}
178-
_ => {}
203+
ExprKind::Paren(ref mut local_expr) => {
204+
self.manage_cond_expr(local_expr);
205+
}
206+
ExprKind::Range(ref mut prefix, ref mut suffix, _) => {
207+
if let Some(ref mut elem) = prefix {
208+
self.manage_cond_expr(elem);
209+
}
210+
if let Some(ref mut elem) = suffix {
211+
self.manage_cond_expr(elem);
212+
}
213+
}
214+
ExprKind::Repeat(ref mut local_expr, ref mut elem) => {
215+
self.manage_cond_expr(local_expr);
216+
self.manage_cond_expr(&mut elem.value);
217+
}
218+
ExprKind::Struct(ref mut elem) => {
219+
for field in &mut elem.fields {
220+
self.manage_cond_expr(&mut field.expr);
221+
}
222+
if let StructRest::Base(ref mut local_expr) = elem.rest {
223+
self.manage_cond_expr(local_expr);
224+
}
225+
}
226+
ExprKind::Tup(ref mut local_exprs) => {
227+
for local_expr in local_exprs {
228+
self.manage_cond_expr(local_expr);
229+
}
230+
}
231+
ExprKind::Unary(_, ref mut local_expr) => {
232+
self.manage_cond_expr(local_expr);
233+
}
234+
// Expressions that are not worth or can not be captured.
235+
//
236+
// Full list instead of `_` to catch possible future inclusions and to
237+
// sync with the `rfc-2011-nicer-assert-messages/all-expr-kinds.rs` test.
238+
ExprKind::Assign(_, _, _)
239+
| ExprKind::AssignOp(_, _, _)
240+
| ExprKind::Async(_, _, _)
241+
| ExprKind::Await(_)
242+
| ExprKind::Block(_, _)
243+
| ExprKind::Box(_)
244+
| ExprKind::Break(_, _)
245+
| ExprKind::Closure(_, _, _, _, _, _)
246+
| ExprKind::ConstBlock(_)
247+
| ExprKind::Continue(_)
248+
| ExprKind::Err
249+
| ExprKind::Field(_, _)
250+
| ExprKind::ForLoop(_, _, _, _)
251+
| ExprKind::If(_, _, _)
252+
| ExprKind::InlineAsm(_)
253+
| ExprKind::Let(_, _, _)
254+
| ExprKind::Lit(_)
255+
| ExprKind::Loop(_, _)
256+
| ExprKind::MacCall(_)
257+
| ExprKind::Match(_, _)
258+
| ExprKind::Path(_, _)
259+
| ExprKind::Ret(_)
260+
| ExprKind::Try(_)
261+
| ExprKind::TryBlock(_)
262+
| ExprKind::Type(_, _)
263+
| ExprKind::Underscore
264+
| ExprKind::While(_, _, _)
265+
| ExprKind::Yeet(_)
266+
| ExprKind::Yield(_) => {}
179267
}
180268
}
181269

src/test/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs

+42
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,56 @@ struct Foo {
5555
bar: i32
5656
}
5757

58+
impl Foo {
59+
fn add(&self, a: i32, b: i32) -> i32 { a + b }
60+
}
61+
62+
fn add(a: i32, b: i32) -> i32 { a + b }
63+
5864
fn main() {
5965
// ***** Allowed *****
6066

6167
tests!(
6268
let mut elem = 1i32;
6369

70+
// addr of
71+
[ &elem == &3 ] => "Assertion failed: &elem == &3\nWith captures:\n elem = 1\n"
72+
73+
// array
74+
[ [elem][0] == 3 ] => "Assertion failed: [elem][0] == 3\nWith captures:\n elem = 1\n"
75+
6476
// binary
6577
[ elem + 1 == 3 ] => "Assertion failed: elem + 1 == 3\nWith captures:\n elem = 1\n"
78+
79+
// call
80+
[ add(elem, elem) == 3 ] => "Assertion failed: add(elem, elem) == 3\nWith captures:\n elem = 1\n"
81+
82+
// cast
83+
[ elem as i32 == 3 ] => "Assertion failed: elem as i32 == 3\nWith captures:\n elem = 1\n"
84+
85+
// index
86+
[ [1i32, 1][elem as usize] == 3 ] => "Assertion failed: [1i32, 1][elem as usize] == 3\nWith captures:\n elem = 1\n"
87+
88+
// method call
89+
[ FOO.add(elem, elem) == 3 ] => "Assertion failed: FOO.add(elem, elem) == 3\nWith captures:\n elem = 1\n"
90+
91+
// paren
92+
[ (elem) == 3 ] => "Assertion failed: (elem) == 3\nWith captures:\n elem = 1\n"
93+
94+
// range
95+
[ (0..elem) == (0..3) ] => "Assertion failed: (0..elem) == (0..3)\nWith captures:\n elem = 1\n"
96+
97+
// repeat
98+
[ [elem; 1] == [3; 1] ] => "Assertion failed: [elem; 1] == [3; 1]\nWith captures:\n elem = 1\n"
99+
100+
// struct
101+
[ Foo { bar: elem } == Foo { bar: 3 } ] => "Assertion failed: Foo { bar: elem } == Foo { bar: 3 }\nWith captures:\n elem = 1\n"
102+
103+
// tuple
104+
[ (elem, 1) == (3, 3) ] => "Assertion failed: (elem, 1) == (3, 3)\nWith captures:\n elem = 1\n"
105+
106+
// unary
107+
[ -elem == -3 ] => "Assertion failed: -elem == -3\nWith captures:\n elem = 1\n"
66108
);
67109

68110
// ***** Disallowed *****

0 commit comments

Comments
 (0)