about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2021-01-22 14:30:17 +0000
committerGitHub <noreply@github.com>2021-01-22 14:30:17 +0000
commit81a60b7aa8dca83e2c9543390cf116016192871a (patch)
treee12d9cae433f85c9956060f2380fba91b0bf9bfc
parent2ceee724270f2186e5e85acff49acd35bf8a652a (diff)
parentd0c1405564024d109ec22b130584d74fcb82d153 (diff)
downloadrust-81a60b7aa8dca83e2c9543390cf116016192871a.tar.gz
rust-81a60b7aa8dca83e2c9543390cf116016192871a.zip
Rollup merge of #81233 - lzutao:dbg, r=KodrAus
Document why not use concat! in dbg! macro

Original title: Reduce code generated by `dbg!` macro
The expanded code before/after: <https://rust.godbolt.org/z/hE3j95>.

---

We cannot use `concat!` since `file!` could contains `{` or the expression is a block (`{ .. }`).
Using it will generated malformed format strings.
So let's document this reason why we don't use `concat!` macro at all.
-rw-r--r--library/std/src/macros.rs4
1 files changed, 4 insertions, 0 deletions
diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs
index 5a70aa070e8..e466f315152 100644
--- a/library/std/src/macros.rs
+++ b/library/std/src/macros.rs
@@ -282,6 +282,10 @@ macro_rules! eprintln {
 #[macro_export]
 #[stable(feature = "dbg_macro", since = "1.32.0")]
 macro_rules! dbg {
+    // NOTE: We cannot use `concat!` to make a static string as a format argument
+    // of `eprintln!` because `file!` could contain a `{` or
+    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
+    // will be malformed.
     () => {
         $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
     };