Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Cannot use stringify! within paste::item! in a #[doc] attribute #29

Closed
ilyvion opened this issue May 24, 2020 · 4 comments · Fixed by #30
Closed

Cannot use stringify! within paste::item! in a #[doc] attribute #29

ilyvion opened this issue May 24, 2020 · 4 comments · Fixed by #30

Comments

@ilyvion
Copy link

ilyvion commented May 24, 2020

This may be an odd edge case, but I want to have this bit of code within a macro:

paste::item! {
    /// The error type returned when a checked
    #[doc = $name_str]
    /// type conversion fails.
    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    pub enum [< TryFrom $name Error >] {
        /// The error type returned when a checked integral type conversion fails.
        TryFromIntError(TryFromIntError),
        /// The error type returned when a floating point value is outside the range
        /// of the integer type it needs to be converted to.
        FloatToInt,
    }
    impl From<TryFromIntError> for [< TryFrom $name Error >] {
        fn from(e: TryFromIntError) -> Self {
            Self::TryFromIntError(e)
        }
    }
}

It gives this compiler error:

error: unexpected token: `stringify`
   --> src\base\def_macro.rs:714:9
    |
714 | /         paste::item! {
715 | |             /// The error type returned when a checked
716 | |             #[doc = $name_str]
717 | |             /// type conversion fails.
...   |
730 | |             }
731 | |         }
    | |_________^
    | 
   ::: src\base.rs:40:1

The $name_str is passed into this invocation as stringify!($name). This works just fine if done outside of a paste::item!, so there must be something going on in the interaction between paste::item!, stringify! and/or #[doc].

It does not work with #[doc = stringify!($name)] either (same exact error), but #[doc = "a regular string"] works just fine.

@dtolnay
Copy link
Owner

dtolnay commented May 24, 2020

I published 0.1.13 with a fix.

@ilyvion
Copy link
Author

ilyvion commented May 24, 2020

Thanks, that was fast! And your fix works like a charm. 😄

@ilyvion
Copy link
Author

ilyvion commented Mar 13, 2021

It seems like this issue (or at least, a related one) has reared its ugly head again in the latest Rust 1.50. You can see it in action on this playground.

Related discussion @ rust-lang/rust#78835.

@ilyvion
Copy link
Author

ilyvion commented Mar 13, 2021

Actually, some more in-depth research tells me that this seems entirely unrelated to that Rust issue/version. I just noticed that the issue goes away if I go back to paste v0.1.14, but is present in all versions from v0.1.15 onwards except in 1.0.3, where it works, but then 1.0.4 breaks it again.

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

Successfully merging a pull request may close this issue.

2 participants