rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.98k stars 12.54k forks source link

`#[doc = format!(...)]` silently fails to produce output instead of producing compilation error #99377

Closed JohnScience closed 2 years ago

JohnScience commented 2 years ago

In order to keep the doc test for evcxr fresh, I wanted to use

/// Blah-blah-blah
#[doc = format!(":dep nalgebra_latex = {{ version = \"{}\", features = [\"lin_sys\", \"evcxr\"] }}", env!("CARGO_PKG_VERSION"))]
/// blah-blah-blah

The code was in a plain library crate, not in a cell of Jupyter Notebook.

I expected to see a compile error at least when I would run cargo doc.

Instead, it silently produced an empty line.

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (1c7b36d4d 2022-07-12)
binary: rustc
commit-hash: 1c7b36d4db582cb47513a6c7176baaec1c3346ab
commit-date: 2022-07-12
host: x86_64-pc-windows-msvc
release: 1.64.0-nightly
LLVM version: 14.0.6
Backtrace

``` ```

GuillaumeGomez commented 2 years ago

I guess not all macros should be allowed in attributes. cc @petrochenkov (I think you were the one who implemented this feature?)

petrochenkov commented 2 years ago

If the output of a macro is not a string literal, then it should produce an error. Not sure why the error is not produced in this case, someone (probably not me) needs to investigate.

GuillaumeGomez commented 2 years ago

When I run it I get:

error: unexpected expression: `{
           let res =
               ::alloc::fmt::format(::core::fmt::Arguments::new_v1(&[":dep nalgebra_latex = { version = \"",
                                   "\", features = [\"lin_sys\", \"evcxr\"] }"],
                       &[::core::fmt::ArgumentV1::new_display(&"1")]));
           res
       }`
 --> foo.rs:1:9
  |
1 | ... = format!(":dep nalgebra_latex = {{ version = \"{}\", features = [\"lin_sys\", \"evcxr\"] }}", env!("LOL"...
  |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
LoganDark commented 2 years ago

FWIW, concat! does work in #[doc] attributes, so that is the preferred way to do this. I do agree this should be a compile error though.

JohnScience commented 2 years ago

I cannot reproduce it. So I guess it's worth closing.