-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Support matching interpolated types in macro invocation syntax #6659
Comments
Hmm, I'm not sure if this is actually a error. Rust macros work on token-trees, not text. Since As for a workaround, you could add another clause to The only thing I could see is maybe adding a bit of logic to allow specialisation on tokens. So you could say, "match a type token that is |
Yes, this is more like a feature request than a bug report :). In my case, it would have been nice to be able to match on types. I guess I tried to add some kind of polymorphism to the c_to_rust! macro with it (which I'd find useful to be supported) Adding a clause that matches any type wouldn't help since I needed different types to be handled differently. The only way I found to work around it is to define a trait |
@Aatch you raise an interesting point there: it seems like this is a place where the macro system is falling down. In your suggestion to add logic for specialization on tokens, was there any particular syntax you were thinking of? Off the top of my head, I was thinking perhaps something like:
where the trailing Another idea for a syntax is:
which might be an easier syntax to both use and also implement. |
(bug triage) No updates here. |
Triage: macro stuff has been stabilized, anything more would need an RFC. If this is worth porting over to an RFC repo issue, please let me know. |
Fix let_and_return false positive The issue: See this Rust playground link: https://play.rust-lang.org/?edition=2018&gist=12cb5d1e7527f8c37743b87fc4a53748 Run the above with clippy to see the following warning: ``` warning: returning the result of a `let` binding from a block --> src/main.rs:24:5 | 23 | let value = Foo::new(&x).value(); | --------------------------------- unnecessary `let` binding 24 | value | ^^^^^ | = note: `#[warn(clippy::let_and_return)]` on by default = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return help: return the expression directly | 23 | 24 | Foo::new(&x).value() | ``` Implementing the suggested fix, removing the temporary let binding, yields a compiler error: ``` error[E0597]: `x` does not live long enough --> src/main.rs:23:14 | 23 | Foo::new(&x).value() | ---------^^- | | | | | borrowed value does not live long enough | a temporary with access to the borrow is created here ... 24 | } | - | | | `x` dropped here while still borrowed | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `Foo` | = note: the temporary is part of an expression at the end of a block; consider forcing this temporary to be dropped sooner, before the block's local variables are dropped help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block | 23 | let x = Foo::new(&x).value(); x | ^^^^^^^ ^^^ ``` The fix: Of course, clippy looks like it should already handle this edge case; however, it appears `utils::fn_def_id` is not returning a `DefId` for `Foo::new`. Changing the `qpath_res` lookup to use the child Path `hir_id` instead of the parent Call `hir_id` fixes the issue. changelog: none
It would be nice if macros could be invoked using an interpolated type from a transcription of another macro.
This is my macro to convert C typed values from an external library to Rust types:
It works fine when invoked like
c_to_rust!(i: c_int)
, but it can't be used from within another macro using an interpolated type likec_to_rust!($v: $ty)
(where the macro's invocation contains$v:ident, $ty:ty
) - probably because interpolated types cannot be matched literally in the invocation syntax.Here's my other macro which fails to use the above one. For bindings to an external library, I need to provide a lot of callback functions. So I'm using a macro to define them, since they only differ in name and parameter types. But I want the forwarding function to convert the parameter to Rust types before passing them along, which unfortunately doesn't work. (Any hints on how to work around this are welcome, I didn't find a suitable way without repeating a lot of code)
The text was updated successfully, but these errors were encountered: