-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sp-api:
impl_runtime_apis!
replace the use of Self
as a type argu…
…ment (#4012) closes #1890 ### Overview Introduces similar checker struct to `CheckTraitDecls` in `decl_runtime_apis!` - `CheckTraitImpls`. Overrides `visit::visit_type_path` to detect usage of `Self` as a type argument within the scope of `impl_runtime_apis!`. **Note**: only prevents the usage of `Self` as a type argument in an angle bracket `<>`, as it is the only use case that fails to compile. For example, the code [below](https://github.com/paritytech/polkadot-sdk/blob/master/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs#L1002) compiles fine: ```rs impl BridgeMessagesConfig<WithBridgeHubRococoMessagesInstance> for Runtime { fn is_relayer_rewarded(relayer: &Self::AccountId) -> bool { let bench_lane_id = <Self as BridgeMessagesConfig<WithBridgeHubRococoMessagesInstance>>::bench_lane_id(); // ... ``` ### Result Given a block of code like this: ```rs impl_runtime_apis! { impl apis::Core<Block> for Runtime { fn initialize_block(header: &HeaderFor<Self>) -> ExtrinsicInclusionMode { let _: HeaderFor<Self> = header.clone(); RuntimeExecutive::initialize_block(header) } // ... } // ... ``` <details open> <summary>Output:</summary> ```bash $ cargo build --release -p minimal-template-node error: `Self` can not be used as type argument in the scope of `impl_runtime_apis!`. Use `Runtime` instead. --> /polkadot-sdk/templates/minimal/runtime/src/lib.rs:133:11 | 133 | let _: HeaderFor<Self> = header.clone(); | ^^^^^^^^^^^^^^^ error: `Self` can not be used as type argument in the scope of `impl_runtime_apis!`. Use `Runtime` instead. --> /polkadot-sdk/templates/minimal/runtime/src/lib.rs:132:32 | 132 | fn initialize_block(header: &HeaderFor<Self>) -> ExtrinsicInclusionMode { ``` </details> --------- Co-authored-by: Pavlo Khrystenko <p.khrystenko@gmail.com> Co-authored-by: Pavlo Khrystenko <45178695+pkhry@users.noreply.github.com> Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Co-authored-by: Bastian Köcher <git@kchr.de>
- Loading branch information
1 parent
67394cd
commit a1aa71e
Showing
3 changed files
with
139 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 | ||
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json | ||
|
||
title: "`impl_runtime_apis!`: replace the use of `Self` with `Runtime`" | ||
|
||
doc: | ||
- audience: Runtime Dev | ||
description: | | ||
Currently, if there is a type alias similar to `type HeaderFor<T>` in the scope, it makes sense to expect that | ||
`HeaderFor<Runtime>` and `HeaderFor<Self>` are equivalent. However, this is not the case. It currently leads to | ||
a compilation error that `Self is not in scope`, which is confusing. This PR introduces a visitor, similar to | ||
`CheckTraitDecl` in `decl_runtime_apis!`, `ReplaceSelfImpl`. It identifies usage of `Self` as a type argument in | ||
`impl_runtime_apis!` and replaces `Self` with an explicit `Runtime` type. | ||
|
||
For example, the following example code will be transformed before expansion: | ||
```rust | ||
impl apis::Core<Block> for Runtime { | ||
fn initialize_block(header: &HeaderFor<Self>) -> ExtrinsicInclusionMode { | ||
let _: HeaderFor<Self> = header.clone(); | ||
RuntimeExecutive::initialize_block(header) | ||
} | ||
} | ||
``` | ||
Instead, it will be passed to macro as: | ||
```rust | ||
impl apis::Core<Block> for Runtime { | ||
fn initialize_block(header: &HeaderFor<Runtime>) -> ExtrinsicInclusionMode { | ||
let _: HeaderFor<Runtime> = header.clone(); | ||
RuntimeExecutive::initialize_block(header) | ||
} | ||
} | ||
``` | ||
crates: | ||
- name: sp-api | ||
bump: none | ||
- name: sp-api-proc-macro | ||
bump: none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters