intellij-rust icon indicating copy to clipboard operation
intellij-rust copied to clipboard

Tracking issue for procedural macro support

Open Undin opened this issue 5 years ago • 24 comments

Procedural macro support feature depends on the following (experimental) features:

  • org.rust.cargo.evaluate.build.scripts - enables building and collecting build artifacts including proc-macro libraries during importing of project structure
  • org.rust.macros.proc - enables expansion of procedural macros

To enable an experimental feature, type Experimental feature in the dialog of Help | Find Action action, enable the corresponding items and reload the project model via Refresh Cargo Projects action in Cargo tool window

image
  • [x] introduce proc macro server to evaluate procedural macro functions #6658
  • [x] compile only "host" code of project: build scripts and proc-macro targets when org.rust.cargo.evaluate.build.scripts experimental feature is enabled #6735
  • [x] support Apple silicon machines (aarch64-apple-darwin target) #7307
  • [x] expand function-like macros #6564 Fixes #4707 Fixes #6367 Fixes #6983 Fixes #7090 Fixes #7242 Fixes #7608 Fixes #7699 Fixes #8049 Fixes #8785 Fixes #9266
  • [x] expand custom derive macros (impl only) #6992 Fixes #4629 Fixes #5468 Fixes #5478 Fixes #5728 Fixes #5954 Fixes #6036 Fixes #6489 Fixes #6880 Fixes #7158 (requires #7184) Fixes #7848 Fixes #8193 Fixes #8415 Fixes #8581 Fixes #8610 Fixes #8847 Fixes #8856 Fixes #9133 Fixes #9216
  • [x] take into account all items generated by custom derive macros #7725 Fixes #1786 Fixes #7365 Fixes #7901 Fixes #7997
  • [x] expand attribute macros #7194 Fixes #5104 Fixes #5239 Fixes #5748 Fixes #5896 Fixes #5975 (requires changes in the corresponding inspection) Fixes #6006 Fixes #6091 Fixes #6093 Fixes #6162 (requires changes in the annotator) Fixes #6482 Fixes #6938 Fixes #7610 Fixes #7863 Fixes #8350 Fixes #8407 FIxes #8640 Fixes #9384
  • [ ] enable org.rust.cargo.evaluate.build.scripts experimental feature by default
  • [ ] enable org.rust.macros.proc experimental feature by default
  • [ ] Other Fixes #7170

Undin avatar Mar 01 '21 08:03 Undin

Does this mean that a subset of derive macro code is currently considered (e.g. for code completion and missing-symbol highlighting?

take into account all items generated by custom derive macros

For example, the Snafu derive macro produces structs which intellij-rust seems to be unaware of.

Would a minimal use case example of this and tracking issue be of any benefit?

webern avatar Jun 03 '21 17:06 webern

@webern

Does this mean that a subset of derive macro code is currently considered (e.g. for code completion and missing-symbol highlighting?

Yep. Currently, the plugin takes into account only impl blocks generated by derive proc macro

Would a minimal use case example of this and tracking issue be of any benefit?

I don't think that we need additional tracking issue only for that case because the current issue already contains a similar item: "take into account all items generated by custom derive macros". But an additional use-case can be useful indeed to check it during the corresponding implementation

Undin avatar Jun 04 '21 11:06 Undin

additional use-case can be useful indeed to check it during the corresponding implementation

Here is the use-case:

Cargo.toml:

[package]
name = "snafu-jetbrains"
version = "0.1.0"
edition = "2018"

[dependencies]
snafu = "0.6"

main.rs

use snafu::Snafu;

/// The error type for this library.
#[derive(Debug, Snafu)]
enum Error {
    #[snafu(display("This is always an error:: {}", message))]
    AlwaysError { message: String },
}

fn hello_world() -> Result<(), Error> {
    AlwaysError {
        message: "this is a struct created by snafu",
    }
    .fail()
}

fn main() {
    hello_world().unwrap()
}

Problem:

image

Desired behavior: code completion should be available for the struct AlwaysError:

image

Desired behavior: context actions should provide the ability to fill in missing struct fields for the AlwaysError struct.

image

Thank you! 🚀

webern avatar Jun 14 '21 19:06 webern

@webern Thank you for your example. I've filed a separate issue #7365 with it

Undin avatar Jun 18 '21 20:06 Undin

How are we coming along on take into account all items generated by custom derive macros and #7365? This is suuuuuch a huge feature for me. Is at the top of the intellij-rust priority list?

Right now I have to open Sublime Text using Rust Analyzer to discover generated structs and functions! Help, I'm lost without my IDE!

webern avatar Jul 27 '21 02:07 webern

@webern #7725

vlad20012 avatar Aug 23 '21 09:08 vlad20012

What's blocking https://github.com/intellij-rust/intellij-rust/issues/6732? I don't see that issue mentioned here. I'm experiencing the same problem and would love if that is fixed.

ranile avatar Aug 24 '21 12:08 ranile

@hamza1311, I suppose it's #7725, but you can use the latest yew version from GitHub (not from crates.io) that works well right now.

vlad20012 avatar Aug 24 '21 12:08 vlad20012

@vlad20012, I see. It seems that specific case is fixed but there are other cases where the bug still exists. For example, the following code still gives the same message:
lib.rs:

use yew::prelude::*;

fn _test() -> Html {
    use_effect(|| {
        || {}
    });

    html! {

    }
}

Cargo.toml:

[package]
name = "yew-html-macro-bug-repro"
version = "0.1.0"
edition = "2018"

[dependencies]
yew = { git = "https://github.com/yewstack/yew/" }

Screenshot: image

It is also worth mentioning that the warning disappears if I do this: image

ranile avatar Aug 24 '21 13:08 ranile

EDIT: Fixed with #8238 and #8236

Can't seem to get this working with the sqlx::query_as! macro. I have enabled both org.rust.cargo.evaluate.build.scripts and org.rust.macros.proc

I get no methods on sqlx::query_as!(). see image below

https://youtrack.jetbrains.com/api/files/74-1179280?sign=MTYzNDUxNTIwMDAwMHwxMS02Mjg0MTF8NzQtMTE3OTI4MHxDTUUwRmRSSmRZUnRYakVienVnTHZz%0D%0AcEdxT2pDNnQyaDdOeGI5ZzNmSnpRDQo%0D%0A&updated=1634324447434

longfellowone avatar Oct 15 '21 19:10 longfellowone

@longfellowone could you, please, post the image here itself or provide another link? I see only 404 with this one

Undin avatar Oct 18 '21 19:10 Undin

Actual

image

Expected (this is what rust-analyzer shows in vscode) - Note correct methods showing such as fetch_all() https://docs.rs/sqlx/0.5.9/sqlx/query/struct.QueryAs.html

expect )

longfellowone avatar Oct 18 '21 20:10 longfellowone

I've noted that for sqlx I need to

  • set SQLX_OFFLINE=true in my project's root .env
  • run cargo sqlx prepare

for intellij-rust to be able to expand query! macros. (both documented in https://github.com/launchbadge/sqlx/tree/ce801b933006c422ada464d0663c68dcf6c680d4/sqlx-cli#force-building-in-offline-mode)

Setting DATABASE_URL in this .env does not work. I suspect the macro expander can't reach the database for some reason.

hectorj avatar Nov 09 '21 20:11 hectorj

I think https://github.com/intellij-rust/intellij-rust/issues/8221 could be part of this tracking issue.

tisonkun avatar Dec 04 '21 08:12 tisonkun

With #7194 and with all experimental features enabled, wonder why code highlighting for functions is different (i.e. half-disabled) when applying even a dummy attribute macro like this one?

// do nothing, just return the input back
#[proc_macro_attribute]
pub fn dummy(_args: TokenStream, input: TokenStream) -> TokenStream {
    input
}

and then:

image

Can anything be done from the proc macro authors standpoint so that it would be properly recognized by intellij-rust (or is it a bug of some sort and it should already work)? (e.g. in my particular case, I know that the input to a proc macro must be ItemFn)

aldanor avatar Dec 04 '21 22:12 aldanor

@aldanor Most likely you need to update the IntelliJ Rust plugin

vlad20012 avatar Dec 07 '21 09:12 vlad20012

I've noted that for sqlx I need to

  • set SQLX_OFFLINE=true in my project's root .env
  • run cargo sqlx prepare

for intellij-rust to be able to expand query! macros. (both documented in https://github.com/launchbadge/sqlx/tree/ce801b933006c422ada464d0663c68dcf6c680d4/sqlx-cli#force-building-in-offline-mode)

Setting DATABASE_URL in this .env does not work. I suspect the macro expander can't reach the database for some reason.

@hectorj

Fixed: #8238 and #8236

longfellowone avatar Dec 14 '21 18:12 longfellowone

Little confused about the status of #1786 -- the issue is shown with a checkmark here and a merged fix back in August, but the functionality doesn't work for me on the latest 0.4.162.4321-213 plugin build with org.rust.macros.proc enabled. Still waiting on something else?

thurn avatar Dec 23 '21 02:12 thurn

@thurn could you provide an example where the plugin doesn't work with proc macros as expected, please?

Undin avatar Dec 23 '21 05:12 Undin

reported as #8328

thurn avatar Dec 23 '21 15:12 thurn

Having issues with proc macros again #8423

longfellowone avatar Jan 31 '22 03:01 longfellowone

Is there any work going on for reporting errors generated by function-like macros? Right now, they just fail silently, forcing user to run cargo check to see what happened

ranile avatar Feb 22 '22 19:02 ranile

I tried enabling this, refreshed a cargo project and it just worked. Unfortunately, it only worked once. All changes since the initial refresh don't show up. I tried refreshing, I disabled and reenabled the functionality but nothing worked.

My code compiles fine even though IntelliJ now shows it as "red".

Before I open a new ticket I thought I'd ask if I am maybe missing something: Do I need to do anything to manually update proc macro details and/or can I delete a cache somewhere? A cargo clean followed by a refresh works. Is this intended?

lfrancke avatar Aug 15 '22 16:08 lfrancke

Had a issue with Sycamore (originally reported it for Yew) but these experimental features solved it for me. Also added screenshots and a repo to reproduce it: https://github.com/intellij-rust/intellij-rust/issues/6732#issuecomment-1248386202

johansmitsnl avatar Sep 15 '22 17:09 johansmitsnl

Hi I wanted to activate org.rust.macros.proc as an experimental feature, but I do not see it in there, what can I do to make it available? Thanks

CristianKerr avatar Oct 21 '22 09:10 CristianKerr

@CristianKerr follow this instruction

Undin avatar Oct 21 '22 09:10 Undin

@CristianKerr follow this instruction Its not there Screenshot from 2022-10-21 12-17-30

CristianKerr avatar Oct 21 '22 10:10 CristianKerr

@CristianKerr I suppose you use too old plugin version

Undin avatar Oct 21 '22 10:10 Undin

@CristianKerr I suppose you use too old plugin version

You are right, I have 0.3.140.3644-202, but I do not know why it doesn't show latest version for me (it looks like its up to date). Thanks!

CristianKerr avatar Oct 21 '22 10:10 CristianKerr

I do not know why it doesn't show latest version for me

Probably, you use old IDE version as well

Undin avatar Oct 21 '22 10:10 Undin