2019-12-03 17:55:18 -08:00
|
|
|
[package]
|
|
|
|
name = "solana-sdk-macro"
|
2021-03-12 23:31:17 -08:00
|
|
|
version = "1.7.0"
|
2019-12-03 17:55:18 -08:00
|
|
|
description = "Solana SDK Macro"
|
2020-06-13 14:41:05 -07:00
|
|
|
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
2019-12-03 17:55:18 -08:00
|
|
|
repository = "https://github.com/solana-labs/solana"
|
|
|
|
homepage = "https://solana.com/"
|
2021-03-10 12:46:17 -08:00
|
|
|
documentation = "https://docs.rs/solana-sdk-macro"
|
2019-12-03 17:55:18 -08:00
|
|
|
license = "Apache-2.0"
|
|
|
|
edition = "2018"
|
|
|
|
|
|
|
|
[lib]
|
|
|
|
proc-macro = true
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
bs58 = "0.3.0"
|
2020-08-02 14:21:07 -07:00
|
|
|
proc-macro2 = "1.0.19"
|
2019-12-03 17:55:18 -08:00
|
|
|
quote = "1.0"
|
|
|
|
syn = { version = "1.0", features = ["full", "extra-traits"] }
|
Fix hygiene issues in `declare_program!` and `declare_loader!`
The `declare_program!` and `declare_loader!` macros both expand to
new macro definitions (based on the `$name` argument). These 'inner'
macros make use of the special `$crate` metavariable to access items in
the crate where the 'inner' macros is defined.
However, this only works due to a bug in rustc. When a macro is
expanded, all `$crate` tokens in its output are 'marked' as being
resolved in the defining crate of that macro. An inner macro (including
the body of its arms) is 'just' another set of tokens that appears in
the body of the outer macro, so any `$crate` identifiers used there are
resolved relative to the 'outer' macro.
For example, consider the following code:
```rust
macro_rules! outer {
() => {
macro_rules! inner {
() => {
$crate::Foo
}
}
}
}
```
The path `$crate::Foo` will be resolved relative to the crate that defines `outer`,
**not** the crate which defines `inner`.
However, rustc currently loses this extra resolution information
(referred to as 'hygiene' information) when a crate is serialized.
In the above example, this means that the macro `inner` (which gets
defined in whatever crate invokes `outer!`) will behave differently
depending on which crate it is invoked from:
When `inner` is invoked from the same crate in which it is defined,
the hygiene information will still be available,
which will cause `$crate::Foo` to be resolved in the crate which defines 'outer'.
When `inner` is invoked from a different crate, it will be loaded from
the metadata of the crate which defines 'inner'. Since the hygiene
information is currently lost, rust will 'forget' that `$crate::Foo` is
supposed to be resolved in the context of 'outer'. Instead, it will be
resolved relative to the crate which defines 'inner', which can cause
incorrect code to compile.
This bug will soon be fixed in rust (see https://github.com/rust-lang/rust/pull/72121),
which will break `declare_program!` and `declare_loader!`. Fortunately,
it's possible to obtain the desired behavior (`$crate` resolving in the
context of the 'inner' macro) by use of a procedural macro.
This commit adds a `respan!` proc-macro to the `sdk/macro` crate.
Using the newly-stabilized (on Nightly) `Span::resolved_at` method,
the `$crate` identifier can be made to be resolved in the context of the
proper crate.
Since `Span::resolved_at` is only stable on the latest nightly,
referencing it on an earlier version of Rust will cause a compilation error.
This requires the `rustversion` crate to be used, which allows conditionally
compiling code epending on the Rust compiler version in use. Since this method is already
stabilized in the latest nightly, there will never be a situation where
the hygiene bug is fixed (e.g. https://github.com/rust-lang/rust/pull/72121)
is merged but we are unable to call `Span::resolved_at`.
2020-06-19 22:42:11 -07:00
|
|
|
rustversion = "1.0.3"
|
2020-04-16 18:18:28 -07:00
|
|
|
|
|
|
|
[package.metadata.docs.rs]
|
|
|
|
targets = ["x86_64-unknown-linux-gnu"]
|