Repository: rust
Issue: Incorrect "can't find crate" error for `--extern` library with transitive dependency
Labels: A-linkage,A-diagnostics,T-compiler

In short, when compiling with rustc, `--extern` on its own is insufficient to point rustc at transitive dependency rlibs.

### The bug

Direct dependencies can be specified with `--extern direct_dep=/path/to/libdirect_dep.rlib`, and this allows `use`ing items from the specified crate. However, if this crate also has its own dependencies, these will not be found even if `--extern transitive_dep=/path/to/libtransitive_dep.rlib` is passed for them. In this case, instead of rustc complaining about not being able to find the transitive dependency, it will complain about not being able to find the direct dependency.

It appears possible to work around this in two ways:
1. by passing `-L` to the directory containing the transitive dependencies
2. by passing `--extern transitive_dep=/path/to/libdep.rlib` *and* adding a `extern crate transitive_dep;` line to the depending crate's sources. All the docs I can find claim that these two should have the same effect, but they do not as they are both necessary here.

At the very least, the error message produced here is wrong, as it claims that `direct_dep` is missing when it is not.
In addition, the documentation around `--extern` does not seem to explain how it interacts with `extern crate` completely. It's very surprising that using both of these together produces different (successfully compiling) behavior than using one or neither. I also gave `-Z unstable-options` and `--extern force:transitive_dep=...`, but it behaves identically to non-`force` `--extern` here.

### Reproducing

A minimal reproduction of this bug is as follows:

`unicode_ident.rs`:
```rust
pub mod ui {}
```
`proc_macro2.rs`:
```rust
pub use unicode_ident::ui;
pub mod pm {}
```
`quote.rs`:
```rust
pub use proc_macro2::pm;
```

Compile these to rlibs with the following commands:
```
rustc --edition 2021 --crate-type=rlib unicode_ident.rs -o libunicode_ident.rlib
rustc --edition 2021 --crate-type=rlib --extern unicode_ident=libunicode_ident.rlib proc_macro2.rs -o libproc_macro2.rlib
# always works
rustc --edition 2021 --crate-type=rlib -L . --extern proc_macro2=libproc_macro2.rlib quote.rs -o libquote.rlib
# works only if `extern crate unicode_ident;` line is added
rustc --edition 2021 --crate-type=rlib --extern unicode_ident=libunicode_ident.rlib --extern proc_macro2=libproc_macro2.rlib quote.rs -o libquote.rlib
# does not work even with extern crate line
rustc --edition 2021 --crate-type=rlib --extern proc_macro2=libproc_macro2.rlib quote.rs -o libquote.rlib
```

#### I expected to see this happen:
the third and fourth rustc invocations succeed, without the presence of absence of `extern crate unicode_ident` changing whether the crate is found, and the fifth rustc invocation produces an error message indicating that `unicode_ident` could not be found, not one erroneously indicating that `proc_macro2` could not be found (current behavior):
```
error[E0463]: can't find crate for `proc_macro2`
 --> quote.rs:2:9
  |
2 | pub use proc_macro2::pm;
  |         ^^^^^^^^^^^ can't find crate
```

### Meta
I've reproduced this with 
rustc 1.77.0-nightly (d5fd09972 2024-01-22)
as well as stable rustc 1.73.0.