Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indoc 0.2 doesn't work with format!() anymore #12

Closed
Boscop opened this issue Sep 19, 2017 · 6 comments
Closed

indoc 0.2 doesn't work with format!() anymore #12

Boscop opened this issue Sep 19, 2017 · 6 comments
Labels

Comments

@Boscop
Copy link

Boscop commented Sep 19, 2017

I was using indoc 0.1.15 for this function:

fn send_pretty(user: &User, subject: &str, body: &str) -> Result<(), &'static str> {
    use std::iter::repeat;

    send_email(&user.email, subject, &format!(indoc!("
        Hello {username}
        ======{underline}

        {body}
    "),
        username = user.name,
        underline = repeat('=').take(user.name.len()).collect::<String>(),
        body = body
    ))
}

Now that I updated to indoc 0.2 it doesn't work anymore:

error: src\mail.rs:39: format argument must be a string literal.

It seems that indoc!() doesn't return a string literal anymore.

@dtolnay dtolnay added the bug label Sep 19, 2017
@dtolnay
Copy link
Owner

dtolnay commented Sep 19, 2017

Thanks for reporting this! I didn't think of this case. I don't think there is a way to make it work on the stable compiler but I published 0.2.1 with an optional unstable feature to use Macros 2.0 in a way that format! should work.

[dependencies]
indoc = { version = "0.2.1", features = ["unstable"] }
#![feature(proc_macro)]

extern crate indoc;
use indoc::indoc;

fn main() {
    let s = format!(indoc!("{}"), true);
    assert_eq!(s, "true");
}

@Boscop
Copy link
Author

Boscop commented Sep 19, 2017

Hm, I still get the same error when I use #![feature(proc_macro)] and extern crate indoc;
(Before, with version 0.1.15, I had #![plugin(indoc)] and no extern crate)
When I write #[macro_use] extern crate indoc; I get:

error: src\mail.rs:10: procedural macros cannot be imported with #[macro_use]
help: src\mail.rs:10: instead, import the procedural macro like any other item: use indoc_impl::indoc;

@dtolnay
Copy link
Owner

dtolnay commented Sep 19, 2017

Macros 2.0 macros are imported differently. Check out the code in my previous comment.

@dtolnay
Copy link
Owner

dtolnay commented Sep 19, 2017

If you get the same "format argument must be a string literal" error with indoc 0.2.1 with the unstable feature enabled and imported with use indoc::indoc, could you share the full nonworking code?

@Boscop
Copy link
Author

Boscop commented Sep 20, 2017

Ah, I hadn't seen use indoc::indoc;. Now it works, thanks :)
Does that mean the indoc macro will never work with format!() on stable Rust from now on?

@Boscop Boscop closed this as completed Sep 20, 2017
@dtolnay
Copy link
Owner

dtolnay commented Sep 20, 2017

The indoc macro has never worked as a format string on stable Rust in the past, and it will continue to not work as a format string on stable Rust until the Macros 2.0 feature is stabilized. Other uses of indoc will work on stable Rust >=1.15.0 as of indoc 0.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants