Stream: t-compiler/rust-analyzer

Topic: proc-macro no ast-node


Edwin Cheng (Dec 16 2020 at 11:17, on Zulip):

I opened PR #6901 which temp fix some panics due to current design :

We don't have ast-node for proc-macro such that HasSource::source fail.
So how would we handle this problem properly ?

matklad (Dec 16 2020 at 11:31, on Zulip):

Oh

matklad (Dec 16 2020 at 11:31, on Zulip):

oh oh oh oh no

matklad (Dec 16 2020 at 11:37, on Zulip):

This seems to be one of that cases where innocuous-looking design decision early on would lead to a lot of pain down the road.

I think, fundamentally, HasSource needs to return an Option, for two reasons.

bjorn3 (Dec 16 2020 at 11:38, on Zulip):

.rlib's do contains Span's from which you could get the original source in most cases.

matklad (Dec 16 2020 at 11:39, on Zulip):

@Edwin Cheng how hard would it be to split hir::MacroDef into separate hir::ProcMacro and hir::DeclMacro, such that only the latter implements HasSource?

Jonas Schievink [he/him] (Dec 16 2020 at 11:40, on Zulip):

Technically HasSource could point you to the function marked as #[proc_macro], since they now go through hir_def name resolution

matklad (Dec 16 2020 at 11:41, on Zulip):

@bjorn3 that's true, but there's a difference between "source is an immanent property of an object" and "you can get source in 99.99% of the time". Although the user-visible experience would be almost identiical, the two situations need fundamentally different code in the implementation.

And I think it is true that source is not an inherent property of the things we have in code-model, there can be various synthetic things which behave as if they were real, but lack the source nontheless.

Edwin Cheng (Dec 16 2020 at 11:45, on Zulip):

Not that hard i think , but @Jonas Schievink is refactoring for macro 2.0. I would prefer doing it after that

Edwin Cheng (Dec 16 2020 at 11:48, on Zulip):

Technically HasSource could point you to the function marked as #[proc_macro], since they now go through hir_def name resolution

Yes, and goto def in following cases works for now:

use foo; // foo is proc macro
matklad (Dec 16 2020 at 12:08, on Zulip):

But, as usual, the ground truth is the .so with the macro, and not the source COde

Joshua Nelson (Dec 16 2020 at 13:28, on Zulip):

matklad said:

bjorn3 that's true, but there's a difference between "source is an immanent property of an object" and "you can get source in 99.99% of the time".

I learned a new word today! :laughing:

Last update: Jul 27 2021 at 21:45UTC