Skip to content

Commit aa79a7c

Browse files
committed
fix: adjust formatting macros to support implicit named arguments
Removes the single-argument specialization rules because they can include implicit placeholders. A format string passed to `format_args!`, if it includes implicit placeholders, can't be formed by `concat!` as this would cause [a macro hygiene issue][1]. This commit re-implements the `*ln!` macros as two internal function calls to work around this restriction. [1]: https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html#generated-format-strings
1 parent 682d748 commit aa79a7c

File tree

3 files changed

+18
-46
lines changed

3 files changed

+18
-46
lines changed

src/arm_semihosting/src/macros.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ macro_rules! syscall1 {
3434
/// This macro returns a `Result<(), ()>` value
3535
#[macro_export]
3636
macro_rules! hprint {
37-
($s:expr) => {
38-
$crate::export::hstdout_str($s)
39-
};
4037
($($tt:tt)*) => {
4138
$crate::export::hstdout_fmt(format_args!($($tt)*))
4239
};
@@ -47,14 +44,11 @@ macro_rules! hprint {
4744
/// This macro returns a `Result<(), ()>` value
4845
#[macro_export]
4946
macro_rules! hprintln {
50-
() => {
51-
$crate::export::hstdout_str("\n")
52-
};
53-
($s:expr) => {
54-
$crate::export::hstdout_str(concat!($s, "\n"))
55-
};
56-
($s:expr, $($tt:tt)*) => {
57-
$crate::export::hstdout_fmt(format_args!(concat!($s, "\n"), $($tt)*))
47+
($($tt:tt)*) => {
48+
match $crate::export::hstdout_fmt(format_args!($($tt)*)) {
49+
Ok(()) => $crate::export::hstdout_str("\n"),
50+
Err(()) => Err(()),
51+
}
5852
};
5953
}
6054

@@ -63,9 +57,6 @@ macro_rules! hprintln {
6357
/// This macro returns a `Result<(), ()>` value
6458
#[macro_export]
6559
macro_rules! heprint {
66-
($s:expr) => {
67-
$crate::export::hstderr_str($s)
68-
};
6960
($($tt:tt)*) => {
7061
$crate::export::hstderr_fmt(format_args!($($tt)*))
7162
};
@@ -76,14 +67,11 @@ macro_rules! heprint {
7667
/// This macro returns a `Result<(), ()>` value
7768
#[macro_export]
7869
macro_rules! heprintln {
79-
() => {
80-
$crate::export::hstderr_str("\n")
81-
};
82-
($s:expr) => {
83-
$crate::export::hstderr_str(concat!($s, "\n"))
84-
};
85-
($s:expr, $($tt:tt)*) => {
86-
$crate::export::hstderr_fmt(format_args!(concat!($s, "\n"), $($tt)*))
70+
($($tt:tt)*) => {
71+
match $crate::export::hstderr_fmt(format_args!($($tt)*)) {
72+
Ok(()) => $crate::export::hstderr_str("\n"),
73+
Err(()) => Err(()),
74+
}
8775
};
8876
}
8977

src/r3_support_rp2040/src/stdout.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ pub fn write_fmt(args: fmt::Arguments<'_>) {
105105
/// Macro for printing to the serial standard output
106106
#[macro_export]
107107
macro_rules! sprint {
108-
($s:expr) => {
109-
$crate::stdout::write_str($s)
110-
};
111108
($($tt:tt)*) => {
112109
$crate::stdout::write_fmt(format_args!($($tt)*))
113110
};
@@ -116,13 +113,8 @@ macro_rules! sprint {
116113
/// Macro for printing to the serial standard output, with a newline.
117114
#[macro_export]
118115
macro_rules! sprintln {
119-
() => {
120-
$crate::stdout::write_str("\n")
121-
};
122-
($s:expr) => {
123-
$crate::stdout::write_str(concat!($s, "\n"))
124-
};
125-
($s:expr, $($tt:tt)*) => {
126-
$crate::stdout::write_fmt(format_args!(concat!($s, "\n"), $($tt)*))
127-
};
116+
($($tt:tt)*) => {{
117+
$crate::stdout::write_fmt(format_args!($($tt)*));
118+
$crate::stdout::write_str("\n");
119+
}};
128120
}

src/r3_support_rza1/src/stdout.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ pub fn write_fmt(args: fmt::Arguments<'_>) {
101101
/// Macro for printing to the serial standard output
102102
#[macro_export]
103103
macro_rules! sprint {
104-
($s:expr) => {
105-
$crate::stdout::write_str($s)
106-
};
107104
($($tt:tt)*) => {
108105
$crate::stdout::write_fmt(format_args!($($tt)*))
109106
};
@@ -112,13 +109,8 @@ macro_rules! sprint {
112109
/// Macro for printing to the serial standard output, with a newline.
113110
#[macro_export]
114111
macro_rules! sprintln {
115-
() => {
116-
$crate::stdout::write_str("\n")
117-
};
118-
($s:expr) => {
119-
$crate::stdout::write_str(concat!($s, "\n"))
120-
};
121-
($s:expr, $($tt:tt)*) => {
122-
$crate::stdout::write_fmt(format_args!(concat!($s, "\n"), $($tt)*))
123-
};
112+
($($tt:tt)*) => {{
113+
$crate::stdout::write_fmt(format_args!($($tt)*));
114+
$crate::stdout::write_str("\n");
115+
}};
124116
}

0 commit comments

Comments
 (0)