Skip to content

Commit e1bd9b3

Browse files
authored
Rollup merge of #67795 - Mark-Simulacrum:fmt-argument, r=dtolnay
Cleanup formatting code This removes a few leftover positional enum variants that were no longer used. All details that are changed are unstable (and `#[doc(hidden)]`), so this should not impact downstream code.
2 parents ec7f209 + a804a45 commit e1bd9b3

File tree

5 files changed

+47
-86
lines changed

5 files changed

+47
-86
lines changed

src/libcore/fmt/mod.rs

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::mem;
1010
use crate::num::flt2dec;
1111
use crate::ops::Deref;
1212
use crate::result;
13-
use crate::slice;
1413
use crate::str;
1514

1615
mod builders;
@@ -234,8 +233,6 @@ pub struct Formatter<'a> {
234233
precision: Option<usize>,
235234

236235
buf: &'a mut (dyn Write + 'a),
237-
curarg: slice::Iter<'a, ArgumentV1<'a>>,
238-
args: &'a [ArgumentV1<'a>],
239236
}
240237

241238
// NB. Argument is essentially an optimized partially applied formatting function,
@@ -1043,8 +1040,6 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
10431040
buf: output,
10441041
align: rt::v1::Alignment::Unknown,
10451042
fill: ' ',
1046-
args: args.args,
1047-
curarg: args.args.iter(),
10481043
};
10491044

10501045
let mut idx = 0;
@@ -1063,7 +1058,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
10631058
// a string piece.
10641059
for (arg, piece) in fmt.iter().zip(args.pieces.iter()) {
10651060
formatter.buf.write_str(*piece)?;
1066-
formatter.run(arg)?;
1061+
run(&mut formatter, arg, &args.args)?;
10671062
idx += 1;
10681063
}
10691064
}
@@ -1077,6 +1072,39 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
10771072
Ok(())
10781073
}
10791074

1075+
fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV1<'_>]) -> Result {
1076+
fmt.fill = arg.format.fill;
1077+
fmt.align = arg.format.align;
1078+
fmt.flags = arg.format.flags;
1079+
fmt.width = getcount(args, &arg.format.width);
1080+
fmt.precision = getcount(args, &arg.format.precision);
1081+
1082+
// Extract the correct argument
1083+
let value = {
1084+
#[cfg(bootstrap)]
1085+
{
1086+
match arg.position {
1087+
rt::v1::Position::At(i) => args[i],
1088+
}
1089+
}
1090+
#[cfg(not(bootstrap))]
1091+
{
1092+
args[arg.position]
1093+
}
1094+
};
1095+
1096+
// Then actually do some printing
1097+
(value.formatter)(value.value, fmt)
1098+
}
1099+
1100+
fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> {
1101+
match *cnt {
1102+
rt::v1::Count::Is(n) => Some(n),
1103+
rt::v1::Count::Implied => None,
1104+
rt::v1::Count::Param(i) => args[i].as_usize(),
1105+
}
1106+
}
1107+
10801108
/// Padding after the end of something. Returned by `Formatter::padding`.
10811109
#[must_use = "don't forget to write the post padding"]
10821110
struct PostPadding {
@@ -1114,41 +1142,6 @@ impl<'a> Formatter<'a> {
11141142
align: self.align,
11151143
width: self.width,
11161144
precision: self.precision,
1117-
1118-
// These only exist in the struct for the `run` method,
1119-
// which won’t be used together with this method.
1120-
curarg: self.curarg.clone(),
1121-
args: self.args,
1122-
}
1123-
}
1124-
1125-
// First up is the collection of functions used to execute a format string
1126-
// at runtime. This consumes all of the compile-time statics generated by
1127-
// the format! syntax extension.
1128-
fn run(&mut self, arg: &rt::v1::Argument) -> Result {
1129-
// Fill in the format parameters into the formatter
1130-
self.fill = arg.format.fill;
1131-
self.align = arg.format.align;
1132-
self.flags = arg.format.flags;
1133-
self.width = self.getcount(&arg.format.width);
1134-
self.precision = self.getcount(&arg.format.precision);
1135-
1136-
// Extract the correct argument
1137-
let value = match arg.position {
1138-
rt::v1::Position::Next => *self.curarg.next().unwrap(),
1139-
rt::v1::Position::At(i) => self.args[i],
1140-
};
1141-
1142-
// Then actually do some printing
1143-
(value.formatter)(value.value, self)
1144-
}
1145-
1146-
fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<usize> {
1147-
match *cnt {
1148-
rt::v1::Count::Is(n) => Some(n),
1149-
rt::v1::Count::Implied => None,
1150-
rt::v1::Count::Param(i) => self.args[i].as_usize(),
1151-
rt::v1::Count::NextParam => self.curarg.next()?.as_usize(),
11521145
}
11531146
}
11541147

src/libcore/fmt/rt/v1.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
#[derive(Copy, Clone)]
99
pub struct Argument {
10+
#[cfg(bootstrap)]
1011
pub position: Position,
12+
#[cfg(not(bootstrap))]
13+
pub position: usize,
1114
pub format: FormatSpec,
1215
}
1316

@@ -37,12 +40,11 @@ pub enum Alignment {
3740
pub enum Count {
3841
Is(usize),
3942
Param(usize),
40-
NextParam,
4143
Implied,
4244
}
4345

46+
#[cfg(bootstrap)]
4447
#[derive(Copy, Clone)]
4548
pub enum Position {
46-
Next,
4749
At(usize),
4850
}

src/librustc_builtin_macros/format.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -590,17 +590,6 @@ impl<'a, 'b> Context<'a, 'b> {
590590
parse::NextArgument(ref arg) => {
591591
// Build the position
592592
let pos = {
593-
let pos = |c, arg| {
594-
let mut path = Context::rtpath(self.ecx, "Position");
595-
path.push(self.ecx.ident_of(c, sp));
596-
match arg {
597-
Some(i) => {
598-
let arg = self.ecx.expr_usize(sp, i);
599-
self.ecx.expr_call_global(sp, path, vec![arg])
600-
}
601-
None => self.ecx.expr_path(self.ecx.path_global(sp, path)),
602-
}
603-
};
604593
match arg.position {
605594
parse::ArgumentIs(i) | parse::ArgumentImplicitlyIs(i) => {
606595
// Map to index in final generated argument array
@@ -615,7 +604,7 @@ impl<'a, 'b> Context<'a, 'b> {
615604
arg_idx
616605
}
617606
};
618-
pos("At", Some(arg_idx))
607+
self.ecx.expr_usize(sp, arg_idx)
619608
}
620609

621610
// should never be the case, because names are already

src/test/ui/async-await/async-fn-nonsend.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// edition:2018
33
// compile-flags: --crate-type lib
44

5-
use std::{
6-
cell::RefCell,
7-
fmt::Debug,
8-
rc::Rc,
9-
};
5+
use std::{cell::RefCell, fmt::Debug, rc::Rc};
106

11-
fn non_sync() -> impl Debug { RefCell::new(()) }
7+
fn non_sync() -> impl Debug {
8+
RefCell::new(())
9+
}
1210

13-
fn non_send() -> impl Debug { Rc::new(()) }
11+
fn non_send() -> impl Debug {
12+
Rc::new(())
13+
}
1414

1515
fn take_ref<T>(_: &T) {}
1616

@@ -53,5 +53,4 @@ pub fn pass_assert() {
5353
//~^ ERROR future cannot be sent between threads safely
5454
assert_send(non_sync_with_method_call());
5555
//~^ ERROR future cannot be sent between threads safely
56-
//~^^ ERROR future cannot be sent between threads safely
5756
}

src/test/ui/async-await/async-fn-nonsend.stderr

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,5 @@ LL | }
6262
LL | }
6363
| - `f` is later dropped here
6464

65-
error: future cannot be sent between threads safely
66-
--> $DIR/async-fn-nonsend.rs:54:5
67-
|
68-
LL | fn assert_send(_: impl Send) {}
69-
| ----------- ---- required by this bound in `assert_send`
70-
...
71-
LL | assert_send(non_sync_with_method_call());
72-
| ^^^^^^^^^^^ future returned by `non_sync_with_method_call` is not `Send`
73-
|
74-
= help: within `std::fmt::ArgumentV1<'_>`, the trait `std::marker::Sync` is not implemented for `*mut (dyn std::ops::Fn() + 'static)`
75-
note: future is not `Send` as this value is used across an await
76-
--> $DIR/async-fn-nonsend.rs:43:9
77-
|
78-
LL | let f: &mut std::fmt::Formatter = panic!();
79-
| - has type `&mut std::fmt::Formatter<'_>`
80-
LL | if non_sync().fmt(f).unwrap() == () {
81-
LL | fut().await;
82-
| ^^^^^^^^^^^ await occurs here, with `f` maybe used later
83-
LL | }
84-
LL | }
85-
| - `f` is later dropped here
86-
87-
error: aborting due to 4 previous errors
65+
error: aborting due to 3 previous errors
8866

0 commit comments

Comments
 (0)