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

imp: use global paths in generated code by ibc-derive #1017

Merged
merged 2 commits into from
Dec 22, 2023

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Dec 21, 2023

Because the generated code uses paths which start with ibc::, they
are first resolved from the local module’s namespace. That is, if an
ibc module is defined, the derives create code which does not
compile. This is demonstrated by the following fragment:

use ::ibc::core::client::context::consensus_state::ConsensusState;
use ::ibc::core::commitment_types::commitment::CommitmentRoot;

mod ibc {}

#[derive(ConsensusState)]
enum AnyConsensusState { Foo(Foo) }

struct Foo;

impl ConsensusState for Foo {
    fn root(&self) -> &CommitmentRoot { todo!() }
    fn timestamp(&self) -> ::ibc::primitives::Timestamp { todo!() }
    fn encode_vec(self) -> Vec<u8> { todo!() }
}

The compilation fails with ‘failed to resolve: could not find core
in ibc’ errors.

To solve this, use global paths (i.e. ones starting with
::ibc::). Starting from 2018 Rust edition such paths resolve from
the extern prelude and thus ::ibc points at the ibc crate.

Note however that in 2015 edition global paths are anchored at the
crate root so ::ibc will attempt to look for crate::ibc
module. This means that this change is breaking for code using ancient
Rust editions.


PR author checklist:

  • Added changelog entry, using unclog.
  • Added tests.
  • Linked to GitHub issue.
  • Updated code comments and documentation (e.g., docs/).
  • Tagged one reviewer who will be the one responsible for shepherding this PR.

Reviewer checklist:

  • Reviewed Files changed in the GitHub PR explorer.
  • Manually tested (in case integration/unit/mock tests are absent).

Because the generated code uses paths which start with `ibc::`, they
are first resolved from the local module’s namespace.  That is, if
an `ibc` module is defined, the derives create code which does not
compile.  This is demonstrated by the following fragment:

    use ::ibc::core::client::context::consensus_state::ConsensusState;
    use ::ibc::core::commitment_types::commitment::CommitmentRoot;

    mod ibc {}

    #[derive(ConsensusState)]
    enum AnyConsensusState { Foo(Foo) }

    struct Foo;

    impl ConsensusState for Foo {
        fn root(&self) -> &CommitmentRoot { todo!() }
        fn timestamp(&self) -> ::ibc::primitives::Timestamp { todo!() }
        fn encode_vec(self) -> Vec<u8> { todo!() }
    }

The compilation fails with ‘failed to resolve: could not find `core`
in `ibc`’ errors.

To solve this, use global paths (i.e. ones starting with `::ibc::`).
Starting from 2018 Rust edition such paths resolve from the extern
prelude and thus `::ibc` points at the `ibc` crate.

Note however that in 2015 edition global paths are anchored at the
crate root so `::ibc` will attempt to look for `crate::ibc` module.
This means that this change is breaking for code using ancient Rust
editions.
Copy link
Member

@Farhad-Shabani Farhad-Shabani left a comment

Choose a reason for hiding this comment

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

I don’t think we have any users on edition 2015. Let’s include it. Thanks!

@Farhad-Shabani Farhad-Shabani changed the title ibc-derive: use global paths in generated code imp: use global paths in generated code by ibc-derive Dec 22, 2023
@Farhad-Shabani Farhad-Shabani added this to the v0.49.0 milestone Dec 22, 2023
@Farhad-Shabani Farhad-Shabani merged commit c020e51 into cosmos:main Dec 22, 2023
8 checks passed
@mina86 mina86 deleted the d branch December 22, 2023 04:47
Farhad-Shabani added a commit that referenced this pull request Sep 9, 2024
* ibc-derive: use global paths in generated code

Because the generated code uses paths which start with `ibc::`, they
are first resolved from the local module’s namespace.  That is, if
an `ibc` module is defined, the derives create code which does not
compile.  This is demonstrated by the following fragment:

    use ::ibc::core::client::context::consensus_state::ConsensusState;
    use ::ibc::core::commitment_types::commitment::CommitmentRoot;

    mod ibc {}

    #[derive(ConsensusState)]
    enum AnyConsensusState { Foo(Foo) }

    struct Foo;

    impl ConsensusState for Foo {
        fn root(&self) -> &CommitmentRoot { todo!() }
        fn timestamp(&self) -> ::ibc::primitives::Timestamp { todo!() }
        fn encode_vec(self) -> Vec<u8> { todo!() }
    }

The compilation fails with ‘failed to resolve: could not find `core`
in `ibc`’ errors.

To solve this, use global paths (i.e. ones starting with `::ibc::`).
Starting from 2018 Rust edition such paths resolve from the extern
prelude and thus `::ibc` points at the `ibc` crate.

Note however that in 2015 edition global paths are anchored at the
crate root so `::ibc` will attempt to look for `crate::ibc` module.
This means that this change is breaking for code using ancient Rust
editions.

* chore: unclog

---------

Co-authored-by: Farhad Shabani <farhad.shabani@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants