Skip to content
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

sp-api: impl_runtime_apis! replace the use of Self as a type argument #4012

Merged
merged 33 commits into from
Nov 6, 2024

Conversation

dastansam
Copy link
Contributor

@dastansam dastansam commented Apr 5, 2024

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 compiles fine:

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:

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)
		}
		// ...
         }
// ...
Output:
$ 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 {

@dastansam dastansam changed the title impl_runtime_apis! prevent use of Self impl_runtime_apis! prevent the use of Self Apr 5, 2024
@dastansam dastansam changed the title impl_runtime_apis! prevent the use of Self WIP: impl_runtime_apis! prevent the use of Self Apr 5, 2024
@paritytech-cicd-pr
Copy link

The CI pipeline was cancelled due to failure one of the required jobs.
Job name: cargo-clippy
Logs: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/5811316

@dastansam dastansam marked this pull request as ready for review April 7, 2024 15:23
@dastansam dastansam requested a review from ggwpez April 7, 2024 15:23
@dastansam dastansam changed the title WIP: impl_runtime_apis! prevent the use of Self sp-api: impl_runtime_apis! prevent the use of Self as a type argument Apr 7, 2024
@dastansam
Copy link
Contributor Author

@kianenigma just pinging here :)

@bkchr
Copy link
Member

bkchr commented Jun 24, 2024

IMO, it would make more sense to replace Self with the impl type of the trait. So, the user can just use Self and doesn't see an error.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be happy with the existing approach, but maybe we should explore @bkchr's suggestion? at least see how much harder it is?

in any case, this is great and we will submit a tip for it, thank you!

@dastansam
Copy link
Contributor Author

I would be happy with the existing approach, but maybe we should explore @bkchr's suggestion? at least see how much harder it is?

in any case, this is great and we will submit a tip for it, thank you!

sure, I will give it a try

@kianenigma
Copy link
Contributor

any updates?

@dastansam
Copy link
Contributor Author

any updates?

working on it

@kianenigma
Copy link
Contributor

again? :)

@dastansam
Copy link
Contributor Author

again? :)

will try to push it through this weekend, if not, I am ok if someone takes over)

@pkhry pkhry self-assigned this Oct 21, 2024
prdoc/pr_4012.prdoc Outdated Show resolved Hide resolved
impl client::Core<Block> for Runtime {
fn initialize_block(header: &HeaderFor<Runtime>) -> Output<Runtime> {
let _: HeaderFor<Runtime> = header.clone();
example_fn::<Runtime>(header)
Copy link
Contributor

@jsdw jsdw Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ie would it currently work if I had something like this?:

let foo: Self = ...

Or is that sort of thing nonsensical anyways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd keep things conservative in this regard

@jsdw
Copy link
Contributor

jsdw commented Oct 31, 2024

Looks good to me but I wonder if we can simplify the code a touch and whether we should replace Self more generally everywhere or else tweak the comment :)

@pkhry
Copy link
Contributor

pkhry commented Oct 31, 2024

/cmd fmt

Copy link

Command "fmt" has started 🚀 See logs here

Copy link

Command "fmt" has finished ✅ See logs here

@pkhry pkhry requested review from pkhry, jsdw and lexnv October 31, 2024 15:17
@pkhry
Copy link
Contributor

pkhry commented Nov 6, 2024

/cmd fmt

Copy link

github-actions bot commented Nov 6, 2024

Command "fmt" has started 🚀 See logs here

Copy link

github-actions bot commented Nov 6, 2024

Command "fmt" has finished ✅ See logs here

@pkhry pkhry requested a review from bkchr November 6, 2024 12:35
@pkhry pkhry added this pull request to the merge queue Nov 6, 2024
Merged via the queue into paritytech:master with commit a1aa71e Nov 6, 2024
192 of 196 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D1-medium Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase. R0-silent Changes should not be mentioned in any release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

impl_runtime_apis! {}: prevent the use of Self
8 participants