Skip to content

Commit 79b3ef8

Browse files
committed
Include rmeta candidates in "multiple matching crates" error
Only dylib and rlib candidates were included in the error. I think the reason is that at the time this error was originally implemented, rmeta crate sources were represented different from dylib and rlib sources. I wrote up more detailed analysis in [this comment][1]. The new version of the code is also a bit easier to read and should be more robust to future changes since it uses `CrateSources::paths()`. [1]: rust-lang#88675 (comment)
1 parent 62f4cc9 commit 79b3ef8

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

compiler/rustc_metadata/src/locator.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ use rustc_span::Span;
232232
use rustc_target::spec::{Target, TargetTriple};
233233

234234
use snap::read::FrameDecoder;
235+
use std::fmt::Write as _;
235236
use std::io::{Read, Result as IoResult, Write};
236237
use std::path::{Path, PathBuf};
237238
use std::{cmp, fmt, fs};
@@ -918,21 +919,22 @@ impl CrateError {
918919
libraries.sort_by_cached_key(|lib| lib.source.paths().next().unwrap().clone());
919920
let candidates = libraries
920921
.iter()
921-
.filter_map(|lib| {
922+
.map(|lib| {
922923
let crate_name = &lib.metadata.get_root().name().as_str();
923-
match (&lib.source.dylib, &lib.source.rlib) {
924-
(Some((pd, _)), Some((pr, _))) => Some(format!(
925-
"\ncrate `{}`: {}\n{:>padding$}",
926-
crate_name,
927-
pd.display(),
928-
pr.display(),
929-
padding = 8 + crate_name.len()
930-
)),
931-
(Some((p, _)), None) | (None, Some((p, _))) => {
932-
Some(format!("\ncrate `{}`: {}", crate_name, p.display()))
933-
}
934-
(None, None) => None,
924+
let mut paths = lib.source.paths();
925+
926+
// This `unwrap()` should be okay because there has to be at least one
927+
// source file. `CrateSource`'s docs confirm that too.
928+
let mut s = format!(
929+
"\ncrate `{}`: {}",
930+
crate_name,
931+
paths.next().unwrap().display()
932+
);
933+
let padding = 8 + crate_name.len();
934+
for path in paths {
935+
write!(s, "\n{:>padding$}", path.display(), padding = padding).unwrap();
935936
}
937+
s
936938
})
937939
.collect::<String>();
938940
err.note(&format!("candidates:{}", candidates));

src/test/ui/crate-loading/crateresolve2.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | extern crate crateresolve2;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: candidates:
8+
crate `crateresolve2`: $TEST_BUILD_DIR/crate-loading/crateresolve2/auxiliary/libcrateresolve2-1.rmeta
9+
crate `crateresolve2`: $TEST_BUILD_DIR/crate-loading/crateresolve2/auxiliary/libcrateresolve2-2.rmeta
10+
crate `crateresolve2`: $TEST_BUILD_DIR/crate-loading/crateresolve2/auxiliary/libcrateresolve2-3.rmeta
811

912
error: aborting due to previous error
1013

0 commit comments

Comments
 (0)