Skip to content

Commit a6d6589

Browse files
committed
Remove match-less special case in format_args!() expansion.
1 parent 5659310 commit a6d6589

File tree

4 files changed

+19
-76
lines changed

4 files changed

+19
-76
lines changed

compiler/rustc_ast_lowering/src/format.rs

+7-66
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use super::LoweringContext;
2-
use rustc_ast as ast;
3-
use rustc_ast::visit::{self, Visitor};
42
use rustc_ast::*;
53
use rustc_data_structures::fx::FxIndexSet;
64
use rustc_hir as hir;
@@ -190,37 +188,10 @@ fn expand_format_args<'hir>(
190188
ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements))
191189
});
192190

193-
let arguments = fmt.arguments.all_args();
194-
195-
// If the args array contains exactly all the original arguments once,
196-
// in order, we can use a simple array instead of a `match` construction.
197-
// However, if there's a yield point in any argument except the first one,
198-
// we don't do this, because an ArgumentV1 cannot be kept across yield points.
199-
let use_simple_array = argmap.len() == arguments.len()
200-
&& argmap.iter().enumerate().all(|(i, &(j, _))| i == j)
201-
&& arguments.iter().skip(1).all(|arg| !may_contain_yield_point(&arg.expr));
202-
203-
let args = if use_simple_array {
191+
let args = if fmt.arguments.all_args().is_empty() {
204192
// Generate:
205-
// &[
206-
// ::core::fmt::ArgumentV1::new_display(&arg0),
207-
// ::core::fmt::ArgumentV1::new_lower_hex(&arg1),
208-
// ::core::fmt::ArgumentV1::new_debug(&arg2),
209-
// ]
210-
let elements: Vec<_> = arguments
211-
.iter()
212-
.zip(argmap)
213-
.map(|(arg, (_, ty))| {
214-
let sp = arg.expr.span.with_ctxt(macsp.ctxt());
215-
let arg = ctx.lower_expr(&arg.expr);
216-
let ref_arg = ctx.arena.alloc(ctx.expr(
217-
sp,
218-
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg),
219-
));
220-
make_argument(ctx, sp, ref_arg, ty)
221-
})
222-
.collect();
223-
ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements))
193+
// &[]
194+
ctx.expr_array_ref(macsp, &[])
224195
} else {
225196
// Generate:
226197
// &match (&arg0, &arg1, &arg2) {
@@ -233,7 +204,7 @@ fn expand_format_args<'hir>(
233204
let args_ident = Ident::new(sym::args, macsp);
234205
let (args_pat, args_hir_id) = ctx.pat_ident(macsp, args_ident);
235206
let args = ctx.arena.alloc_from_iter(argmap.iter().map(|&(arg_index, ty)| {
236-
if let Some(arg) = arguments.get(arg_index) {
207+
if let Some(arg) = fmt.arguments.all_args().get(arg_index) {
237208
let sp = arg.expr.span.with_ctxt(macsp.ctxt());
238209
let args_ident_expr = ctx.expr_ident(macsp, args_ident, args_hir_id);
239210
let arg = ctx.arena.alloc(ctx.expr(
@@ -248,7 +219,9 @@ fn expand_format_args<'hir>(
248219
ctx.expr(macsp, hir::ExprKind::Err)
249220
}
250221
}));
251-
let elements: Vec<_> = arguments
222+
let elements: Vec<_> = fmt
223+
.arguments
224+
.all_args()
252225
.iter()
253226
.map(|arg| {
254227
let arg_expr = ctx.lower_expr(&arg.expr);
@@ -320,35 +293,3 @@ fn expand_format_args<'hir>(
320293
hir::ExprKind::Call(new_v1, new_args)
321294
}
322295
}
323-
324-
fn may_contain_yield_point(e: &ast::Expr) -> bool {
325-
struct MayContainYieldPoint(bool);
326-
327-
impl Visitor<'_> for MayContainYieldPoint {
328-
fn visit_expr(&mut self, e: &ast::Expr) {
329-
if let ast::ExprKind::Await(_) | ast::ExprKind::Yield(_) = e.kind {
330-
self.0 = true;
331-
} else {
332-
visit::walk_expr(self, e);
333-
}
334-
}
335-
336-
fn visit_mac_call(&mut self, _: &ast::MacCall) {
337-
self.0 = true;
338-
}
339-
340-
fn visit_attribute(&mut self, _: &ast::Attribute) {
341-
// Conservatively assume this may be a proc macro attribute in
342-
// expression position.
343-
self.0 = true;
344-
}
345-
346-
fn visit_item(&mut self, _: &ast::Item) {
347-
// Do not recurse into nested items.
348-
}
349-
}
350-
351-
let mut visitor = MayContainYieldPoint(false);
352-
visitor.visit_expr(e);
353-
visitor.0
354-
}

tests/ui/closures/print/closure-print-generic-trim-off-verbose-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let c1 : () = c;
99
| expected due to this
1010
|
1111
= note: expected unit type `()`
12-
found closure `[mod1::f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#16t, extern "rust-call" fn(()), _#15t]]`
12+
found closure `[mod1::f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#19t, extern "rust-call" fn(()), _#18t]]`
1313
help: use parentheses to call this closure
1414
|
1515
LL | let c1 : () = c();

tests/ui/closures/print/closure-print-generic-verbose-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let c1 : () = c;
99
| expected due to this
1010
|
1111
= note: expected unit type `()`
12-
found closure `[f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#16t, extern "rust-call" fn(()), _#15t]]`
12+
found closure `[f<T>::{closure#0} closure_substs=(unavailable) substs=[T, _#19t, extern "rust-call" fn(()), _#18t]]`
1313
help: use parentheses to call this closure
1414
|
1515
LL | let c1 : () = c();

tests/ui/issues/issue-69455.stderr

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
error[E0282]: type annotations needed
2-
--> $DIR/issue-69455.rs:29:20
1+
error[E0284]: type annotations needed
2+
--> $DIR/issue-69455.rs:29:41
33
|
44
LL | println!("{}", 23u64.test(xs.iter().sum()));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the associated function `new_display`
5+
| ---- ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
6+
| |
7+
| type must be known at this point
68
|
7-
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
9+
= note: cannot satisfy `<u64 as Test<_>>::Output == _`
810
help: consider specifying the generic argument
911
|
10-
LL | println!("{}", 23u64.test(xs.iter().sum())::<T>);
11-
| +++++
12+
LL | println!("{}", 23u64.test(xs.iter().sum::<S>()));
13+
| +++++
1214

1315
error[E0283]: type annotations needed
1416
--> $DIR/issue-69455.rs:29:41
@@ -33,5 +35,5 @@ LL | println!("{}", 23u64.test(xs.iter().sum::<S>()));
3335

3436
error: aborting due to 2 previous errors
3537

36-
Some errors have detailed explanations: E0282, E0283.
37-
For more information about an error, try `rustc --explain E0282`.
38+
Some errors have detailed explanations: E0283, E0284.
39+
For more information about an error, try `rustc --explain E0283`.

0 commit comments

Comments
 (0)