-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add utility macros to help with writing tests. #10453
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
Conversation
r? @dswij (rustbot has picked a reviewer for you, use r? to override) |
I'm not sure about |
I'm going to see if rewriting it to use the same escaping syntax |
And done. The description has been updated for the new syntax. |
Haven't tested it but an alternative mechanism for declare_inline_macro!(inline);
fn main() {
inline!(5 + 5 / 10);
} Where // `inline_impl` being similar to `with_span`
macro_rules! inline {
($($tt:tt)*) => proc_macros::inline_impl!(inline $($tt)*),
} May or may not be preferable to the attribute macro, I'm assuming proc macros can expand to |
I'm not sure how to make that work. The tokens inside |
Ah right of course, it's not enough to just get a span from somewhere else in the file |
☔ The latest upstream changes (presumably #10467) made this pull request unmergeable. Please resolve the merge conflicts. |
b2590f3
to
a541584
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, personally I find the changes makes things easier to read. One thing that'd be good will be having documentation on using these. There were a couple of confusions on writing tests for macro in the past, think it would be a good time to add it now.
r=me if we're adding the documentation in a separate PR.
52990ce
to
364a61e
Compare
Minor implementation changes. #[inline_macros]
fn foo<T: Into<inline!(u32)>>(x: inline!(u32)) -> inline!(u32) { ... } The escape character for I don't think this needs to be held back on adding it to the book. There isn't anything on testing macros to start with, and there are outstanding PRs to rewrite the documentation anyways. |
@bors r=dswij |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Adds two utility macros to help with testing:
external
expands to it's argument tokens, but makes them appear to come from an external macro. Helps make tests forin_external_macro
much more readable.inline_macros
is an attribute macro which allows the use of a pseudoinline!
macro which expands to it's argument tokens, but makes them appear to be from a crate-local macro expansion. This removes the need to writemacro_rules
boilerplate when testing how lints interact with macros.external
's usage is simple.external!(struct Foo { x: u32});
will make the struct appear as though it came from an external macro. Individual tokens can be escaped if needed.external!($x + 0 / 10)
will make everything exceptx
appear as though it came from an external macro. Can also use$literal
and$(tokens...)
as well.inline_macros
is more complicated due to compiler constraints. Given:inline!(5 + 5 / 10)
will be replace with a call to a generated macro which expands to the contained tokens.Tokens can be escaped by prefixing them with
$
:This will pass
x
as anident
argument and10
as aliteral
argument.Token sequences can also be passed with
$(...)
:This will pass
x >= 5
astt
arguments, andx
as anident
argument.Not 100% sure
inline_macros
is actually worth having. It does make the tests a little easier to read once you're used to it and it becomes more useful once there are multiple macro tests. The verbosity of declaring single use macros starts to hurt at that point.changelog: None