Skip to content

Trait method cannot be imported from package when pulling dependency from git repository #128569

Closed
@edusporto

Description

@edusporto

I'm writing code for a programming language that uses two libraries: one for a (very simple) parser, and another for a lower level byte code, which also uses the same parser library.

The parser library (TSPL) defines the Parser trait, with the byte code library (HVM) defining a CoreParser that implements that trait through a macro from TSPL.

Then, a third library does something like this, where index is a method from that trait:

use TSPL::Parser;
use hvm::ast::CoreParser;

pub fn func(parser: &mut CoreParser) {
    *parser.index() = 1;
}

This works as expected if the dependencies in Cargo.toml are defined like this:

[dependencies]
TSPL = "0.0.12"
hvm = "2.0.21"

If we change TSPL to use the version from the latest git commit like this:

[dependencies]
TSPL = { git = "https://github.com/HigherOrderCO/TSPL.git" }
hvm = "2.0.21"

Then the code does not compile anymore, even though the version in the main branch in git has the exact same code as the published version 0.0.12. We get an error and a warning:

warning: unused import: `TSPL::Parser`
 --> src/lib.rs:1:5
  |
1 | use TSPL::Parser;
  |     ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0599]: no method named `index` found for mutable reference `&mut CoreParser<'_>` in the current scope
 --> src/lib.rs:5:13
  |
5 |     *parser.index() = 1;
  |             ^^^^^ private field, not a method
  |
  = help: items from traits can only be used if the trait is in scope
help: trait `Parser` which provides `index` is implemented but not in scope; perhaps you want to import it
  |
1 + use TSPL::Parser;

Notice that the compiler error tells us the function index has been found from the TSPL package, and we do as the error message tells us (use TSPL::Parser), but the compiler does not recognize the trait is being used even so.

While writing this issue, I figured out the problem: hvm uses the version from crates-io and our library doesn't and, even though they have the exact same code, the two versions of TSPL clash and the compiler does not compile. If we do something like this it works as expected:

[dependencies]
TSPL = "0.0.12"
hvm = "2.0.21"

[patch.crates-io]
TSPL = { git = "https://github.com/HigherOrderCO/TSPL.git" }

I still want to open this issue because I believe the error message could be more descriptive: in other cases where dependencies have clashing versions of other dependencies, usually Rust specifies it as such. The current error message is not very descriptive - the solution it gives to the problem does not work at all and may push users into the wrong direction.

Meta

rustc --version --verbose:

rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: aarch64-apple-darwin
release: 1.80.0
LLVM version: 18.1.7
Backtrace

error[E0599]: no method named `index` found for mutable reference `&mut CoreParser<'_>` in the current scope
 --> src/lib.rs:5:13
  |
5 |     *parser.index() = 1;
  |             ^^^^^ private field, not a method
  |
  = help: items from traits can only be used if the trait is in scope
help: trait `Parser` which provides `index` is implemented but not in scope; perhaps you want to import it
  |
1 + use TSPL::Parser;
  |

warning: unused import: `TSPL::Parser`
 --> src/lib.rs:1:5
  |
1 | use TSPL::Parser;
  |     ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0599`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-crate-version-mismatchDiagnostics: Errors or lints caused be the use of two different crate versions.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions