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

rustc: Gives inconsistent error messages if a dependency's dependency rlib has an unexpected filename #110460

Open
sdroege opened this issue Apr 17, 2023 · 4 comments
Labels
A-metadata Area: Crate metadata C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sdroege
Copy link
Contributor

sdroege commented Apr 17, 2023

Note that the following can only happen when calling rustc directly instead of going through cargo as cargo uses an rlib filename scheme that works and AFAIU doesn't allow overriding that anyway.

I tried this code:

  • a/lib.rs:
pub fn foo() -> i32 {
    123
}
  • b/lib.rs:
pub fn bar() -> i32 {
    some_a::foo() * 2
}
  • c/main.rs:
fn main() {
    //use b::bar;
    println!("{}", b::bar());
}

When compiling this as follows it will fail on the third rustc invocation.

mkdir -p builddir
rm -f builddir/liba.rlib
rm -f builddir/libb.rlib
rm -f builddir/c
rustc --crate-type rlib --edition=2021 --crate-name some_a --emit link -o builddir/liba.rlib a/lib.rs
rustc --crate-type rlib --edition=2021 --crate-name b --emit link -o builddir/libb.rlib --extern some_a=builddir/liba.rlib -L builddir b/lib.rs
rustc --crate-type bin --edition=2021 --crate-name c --emit link -o builddir/c --extern b=builddir/libb.rlib -L builddir c/main.rs

This gives the following error with latest stable (1.68.2) and also 660c966

error[E0463]: can't find crate for `some_a` which `b` depends on
 --> c/main.rs:3:20
  |
3 |     println!("{}", b::bar());
  |                    ^ can't find crate

error: aborting due to previous error

Note that the crate name of a is some_a while the rlib file is called liba.rlib. If changing either the crate name to a or the filename to libsome_a.rlib it works.

More confusingly, when using 660c966 and uncommenting the use b::foo line, it gives a different error

error[E0519]: the current crate is indistinguishable from one of its dependencies: it has the same crate-name `b` and was compiled with the same `-C metadata` arguments. This will result in symbol conflicts between the two.
 --> c/main.rs:2:9
  |
2 |     use b::bar;
  |         ^

error[E0519]: the current crate is indistinguishable from one of its dependencies: it has the same crate-name `b` and was compiled with the same `-C metadata` arguments. This will result in symbol conflicts between the two.
 --> c/main.rs:3:20
  |
3 |     println!("{}", b::bar());
  |                    ^

error: aborting due to 2 previous errors

I would expect

  1. both cases to give the same error message
  2. either using an "invalid" rlib name should fail directly (i.e. first rustc invocation above), or compilation of the above testcase should work fine
@sdroege sdroege added the C-bug Category: This is a bug. label Apr 17, 2023
@GuillaumeGomez GuillaumeGomez added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 17, 2023
sdroege added a commit to sdroege/meson that referenced this issue Apr 17, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 17, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 17, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 17, 2023
…ames

Previously the library names were directly mapped to filenames by meson
while the crate name gets spaces/dashes replaced by underscores. This
worked fine to a certain degree except that rustc expects a certain
filename scheme for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

So now we instead replace them with underscores directly in the target
names so consistent names are used everywhere, and also log a warning to
notify the user about this happening.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 17, 2023
…ames

Previously the library names were directly mapped to filenames by meson
while the crate name gets spaces/dashes replaced by underscores. This
worked fine to a certain degree except that rustc expects a certain
filename scheme for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

So now we instead replace them with underscores directly in the target
names so consistent names are used everywhere, and also log a warning to
notify the user about this happening.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 17, 2023
…ames

Previously the library names were directly mapped to filenames by meson
while the crate name gets spaces/dashes replaced by underscores. This
worked fine to a certain degree except that rustc expects a certain
filename scheme for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

So now we instead replace them with underscores directly in the target
names so consistent names are used everywhere, and also log a warning to
notify the user about this happening.

See rust-lang/rust#110460
@jyn514
Copy link
Member

jyn514 commented Apr 18, 2023

either using an "invalid" rlib name should fail directly (i.e. first rustc invocation above), or compilation of the above testcase should work fine

this doesn't error in the first example because you pass --extern some_a=liba.rlib. I think if you remove that flag it would give the same error, and that seems correct to me.

I don't have an explanation for why it errors trying to load b, that bit seems like a bug.

@jyn514 jyn514 added the A-metadata Area: Crate metadata label Apr 18, 2023
@sdroege
Copy link
Contributor Author

sdroege commented Apr 18, 2023

this doesn't error in the first example because you pass --extern some_a=liba.rlib. I think if you remove that flag it would give the same error, and that seems correct to me.

Yes then it fails directly as expected

I don't have an explanation for why it errors trying to load b, that bit seems like a bug.

Me neither but it behaves as if the rlib "remembers" the crate name but not the filename, and then it reconstructs the filename from the crate name and that doesn't work.

sdroege added a commit to sdroege/meson that referenced this issue Apr 18, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 18, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 18, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 18, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
sdroege added a commit to sdroege/meson that referenced this issue Apr 18, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
nirbheek pushed a commit to mesonbuild/meson that referenced this issue Apr 20, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
nirbheek pushed a commit to mesonbuild/meson that referenced this issue Apr 20, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
nirbheek pushed a commit to mesonbuild/meson that referenced this issue Apr 21, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
@Mark-Simulacrum
Copy link
Member

Note that the crate name of a is some_a while the rlib file is called liba.rlib. If changing either the crate name to a or the filename to libsome_a.rlib it works.

This is expected behavior given the current implementation. To reduce work we prune the list of files we attempt to load to get at the embedded crate name etc. (See https://github.com/rust-lang/rust/blob/master/compiler/rustc_metadata/src/locator.rs#L73-L84 for a summary of the steps).

The indistinguishable error seems separate though, not quite sure there.

@sdroege
Copy link
Contributor Author

sdroege commented May 9, 2023

That makes sense, thanks for checking. Considering that this is a constraint of the current implementation, it would probably make sense to check this during compilation of the first rlib (in this case) and fail directly as it won't be usable at a later step?

nirbheek pushed a commit to mesonbuild/meson that referenced this issue May 23, 2023
The library names are directly mapped to filenames by meson while the
crate name gets spaces/dashes replaced by underscores. This works fine
to a certain degree except that rustc expects a certain filename scheme
for rlibs that matches the crate name.

When using such a library as a dependency of a dependency compilation
will fail with a confusing error message.

See rust-lang/rust#110460
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-metadata Area: Crate metadata C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants