Skip to content

Commit eb44c89

Browse files
committed
Auto merge of #45946 - estebank:crate-conflict-diag, r=arielb1
Use multiline text for crate conflict diagnostics After: ``` error[E0464]: multiple matching crates for `libc` --> /checkout/src/rustc/dlmalloc_shim/../../dlmalloc/src/linux.rs:1:1 | 1 | extern crate libc; | ^^^^^^^^^^^^^^^^^^ | = note: candidates: crate `libc`: /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-658d35794c10b003.rlib crate `libc`: /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-f32a17a3111b01aa.rlib ``` Before: ``` error[E0464]: multiple matching crates for `libc` --> /checkout/src/rustc/dlmalloc_shim/../../dlmalloc/src/linux.rs:1:1 | 1 | extern crate libc; | ^^^^^^^^^^^^^^^^^^ | = note: candidates: = note: path: /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-658d35794c10b003.rlib = note: crate name: libc = note: path: /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-f32a17a3111b01aa.rlib = note: crate name: libc ```
2 parents 9342661 + 5eb5e91 commit eb44c89

File tree

10 files changed

+95
-103
lines changed

10 files changed

+95
-103
lines changed

src/librustc_metadata/locator.rs

+85-84
Original file line numberDiff line numberDiff line change
@@ -311,98 +311,96 @@ impl<'a> Context<'a> {
311311
&None => String::new(),
312312
&Some(ref r) => format!(" which `{}` depends on", r.ident),
313313
};
314+
let mut msg = "the following crate versions were found:".to_string();
314315
let mut err = if !self.rejected_via_hash.is_empty() {
315-
struct_span_err!(self.sess,
316-
self.span,
317-
E0460,
318-
"found possibly newer version of crate `{}`{}",
319-
self.ident,
320-
add)
321-
} else if !self.rejected_via_triple.is_empty() {
322-
struct_span_err!(self.sess,
323-
self.span,
324-
E0461,
325-
"couldn't find crate `{}` with expected target triple {}{}",
326-
self.ident,
327-
self.triple,
328-
add)
329-
} else if !self.rejected_via_kind.is_empty() {
330-
struct_span_err!(self.sess,
331-
self.span,
332-
E0462,
333-
"found staticlib `{}` instead of rlib or dylib{}",
334-
self.ident,
335-
add)
336-
} else if !self.rejected_via_version.is_empty() {
337-
struct_span_err!(self.sess,
338-
self.span,
339-
E0514,
340-
"found crate `{}` compiled by an incompatible version of rustc{}",
341-
self.ident,
342-
add)
343-
} else {
344316
let mut err = struct_span_err!(self.sess,
345317
self.span,
346-
E0463,
347-
"can't find crate for `{}`{}",
318+
E0460,
319+
"found possibly newer version of crate `{}`{}",
348320
self.ident,
349321
add);
350-
351-
if (self.ident == "std" || self.ident == "core")
352-
&& self.triple != config::host_triple() {
353-
err.note(&format!("the `{}` target may not be installed", self.triple));
354-
}
355-
err.span_label(self.span, "can't find crate");
356-
err
357-
};
358-
359-
if !self.rejected_via_triple.is_empty() {
360-
let mismatches = self.rejected_via_triple.iter();
361-
for (i, &CrateMismatch { ref path, ref got }) in mismatches.enumerate() {
362-
err.note(&format!("crate `{}`, path #{}, triple {}: {}",
363-
self.ident,
364-
i + 1,
365-
got,
366-
path.display()));
367-
}
368-
}
369-
if !self.rejected_via_hash.is_empty() {
370322
err.note("perhaps that crate needs to be recompiled?");
371323
let mismatches = self.rejected_via_hash.iter();
372-
for (i, &CrateMismatch { ref path, .. }) in mismatches.enumerate() {
373-
err.note(&format!("crate `{}` path #{}: {}", self.ident, i + 1, path.display()));
324+
for &CrateMismatch { ref path, .. } in mismatches {
325+
msg.push_str(&format!("\ncrate `{}`: {}", self.ident, path.display()));
374326
}
375327
match self.root {
376328
&None => {}
377329
&Some(ref r) => {
378-
for (i, path) in r.paths().iter().enumerate() {
379-
err.note(&format!("crate `{}` path #{}: {}",
380-
r.ident,
381-
i + 1,
382-
path.display()));
330+
for path in r.paths().iter() {
331+
msg.push_str(&format!("\ncrate `{}`: {}", r.ident, path.display()));
383332
}
384333
}
385334
}
386-
}
387-
if !self.rejected_via_kind.is_empty() {
335+
err.note(&msg);
336+
err
337+
} else if !self.rejected_via_triple.is_empty() {
338+
let mut err = struct_span_err!(self.sess,
339+
self.span,
340+
E0461,
341+
"couldn't find crate `{}` \
342+
with expected target triple {}{}",
343+
self.ident,
344+
self.triple,
345+
add);
346+
let mismatches = self.rejected_via_triple.iter();
347+
for &CrateMismatch { ref path, ref got } in mismatches {
348+
msg.push_str(&format!("\ncrate `{}`, target triple {}: {}",
349+
self.ident,
350+
got,
351+
path.display()));
352+
}
353+
err.note(&msg);
354+
err
355+
} else if !self.rejected_via_kind.is_empty() {
356+
let mut err = struct_span_err!(self.sess,
357+
self.span,
358+
E0462,
359+
"found staticlib `{}` instead of rlib or dylib{}",
360+
self.ident,
361+
add);
388362
err.help("please recompile that crate using --crate-type lib");
389363
let mismatches = self.rejected_via_kind.iter();
390-
for (i, &CrateMismatch { ref path, .. }) in mismatches.enumerate() {
391-
err.note(&format!("crate `{}` path #{}: {}", self.ident, i + 1, path.display()));
364+
for &CrateMismatch { ref path, .. } in mismatches {
365+
msg.push_str(&format!("\ncrate `{}`: {}", self.ident, path.display()));
392366
}
393-
}
394-
if !self.rejected_via_version.is_empty() {
367+
err.note(&msg);
368+
err
369+
} else if !self.rejected_via_version.is_empty() {
370+
let mut err = struct_span_err!(self.sess,
371+
self.span,
372+
E0514,
373+
"found crate `{}` compiled by an incompatible version \
374+
of rustc{}",
375+
self.ident,
376+
add);
395377
err.help(&format!("please recompile that crate using this compiler ({})",
396378
rustc_version()));
397379
let mismatches = self.rejected_via_version.iter();
398-
for (i, &CrateMismatch { ref path, ref got }) in mismatches.enumerate() {
399-
err.note(&format!("crate `{}` path #{}: {} compiled by {:?}",
400-
self.ident,
401-
i + 1,
402-
path.display(),
403-
got));
380+
for &CrateMismatch { ref path, ref got } in mismatches {
381+
msg.push_str(&format!("\ncrate `{}` compiled by {}: {}",
382+
self.ident,
383+
got,
384+
path.display()));
404385
}
405-
}
386+
err.note(&msg);
387+
err
388+
} else {
389+
let mut err = struct_span_err!(self.sess,
390+
self.span,
391+
E0463,
392+
"can't find crate for `{}`{}",
393+
self.ident,
394+
add);
395+
396+
if (self.ident == "std" || self.ident == "core")
397+
&& self.triple != config::host_triple() {
398+
err.note(&format!("the `{}` target may not be installed", self.triple));
399+
}
400+
err.span_label(self.span, "can't find crate");
401+
err
402+
};
403+
406404
if !self.rejected_via_filename.is_empty() {
407405
let dylibname = self.dylibname();
408406
let mismatches = self.rejected_via_filename.iter();
@@ -534,16 +532,23 @@ impl<'a> Context<'a> {
534532
E0464,
535533
"multiple matching crates for `{}`",
536534
self.crate_name);
537-
err.note("candidates:");
538-
for (_, lib) in libraries {
539-
if let Some((ref p, _)) = lib.dylib {
540-
err.note(&format!("path: {}", p.display()));
541-
}
542-
if let Some((ref p, _)) = lib.rlib {
543-
err.note(&format!("path: {}", p.display()));
535+
let candidates = libraries.iter().filter_map(|(_, lib)| {
536+
let crate_name = &lib.metadata.get_root().name.as_str();
537+
match &(&lib.dylib, &lib.rlib) {
538+
&(&Some((ref pd, _)), &Some((ref pr, _))) => {
539+
Some(format!("\ncrate `{}`: {}\n{:>padding$}",
540+
crate_name,
541+
pd.display(),
542+
pr.display(),
543+
padding=8 + crate_name.len()))
544+
}
545+
&(&Some((ref p, _)), &None) | &(&None, &Some((ref p, _))) => {
546+
Some(format!("\ncrate `{}`: {}", crate_name, p.display()))
547+
}
548+
&(&None, &None) => None,
544549
}
545-
note_crate_name(&mut err, &lib.metadata.get_root().name.as_str());
546-
}
550+
}).collect::<String>();
551+
err.note(&format!("candidates:{}", candidates));
547552
err.emit();
548553
None
549554
}
@@ -815,10 +820,6 @@ impl<'a> Context<'a> {
815820
}
816821
}
817822

818-
pub fn note_crate_name(err: &mut DiagnosticBuilder, name: &str) {
819-
err.note(&format!("crate name: {}", name));
820-
}
821-
822823
// Just a small wrapper to time how long reading metadata takes.
823824
fn get_metadata_section(target: &Target,
824825
flavor: CrateFlavor,

src/test/compile-fail/changing-crates.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
extern crate a;
1919
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
20+
//~| NOTE: the following crate versions were found
2021
//~| NOTE: perhaps that crate needs to be recompiled
21-
//~| NOTE: crate `a` path #1:
22-
//~| NOTE: crate `b` path #1:
2322

2423
fn main() {}

src/test/compile-fail/svh-change-lit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
extern crate a;
1919
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
2020
//~| NOTE: perhaps that crate needs to be recompiled
21-
//~| NOTE: crate `a` path #1:
22-
//~| NOTE: crate `b` path #1:
21+
//~| NOTE: the following crate versions were found:
2322

2423
fn main() {
2524
b::foo()

src/test/compile-fail/svh-change-significant-cfg.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
extern crate a;
1919
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
2020
//~| NOTE: perhaps that crate needs to be recompiled
21-
//~| NOTE: crate `a` path #1:
22-
//~| NOTE: crate `b` path #1:
21+
//~| NOTE: the following crate versions were found:
2322

2423
fn main() {
2524
b::foo()

src/test/compile-fail/svh-change-trait-bound.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
extern crate a;
1919
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
2020
//~| NOTE: perhaps that crate needs to be recompiled
21-
//~| NOTE: crate `a` path #1:
22-
//~| NOTE: crate `b` path #1:
21+
//~| NOTE: the following crate versions were found:
2322

2423
fn main() {
2524
b::foo()

src/test/compile-fail/svh-change-type-arg.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
extern crate a;
1919
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
2020
//~| NOTE: perhaps that crate needs to be recompiled
21-
//~| NOTE: crate `a` path #1:
22-
//~| NOTE: crate `b` path #1:
21+
//~| NOTE: the following crate versions were found:
2322

2423
fn main() {
2524
b::foo()

src/test/compile-fail/svh-change-type-ret.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
extern crate a;
1919
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
2020
//~| NOTE: perhaps that crate needs to be recompiled
21-
//~| NOTE: crate `a` path #1:
22-
//~| NOTE: crate `b` path #1:
21+
//~| NOTE: the following crate versions were found:
2322

2423
fn main() {
2524
b::foo()

src/test/compile-fail/svh-change-type-static.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
extern crate a;
1919
extern crate b; //~ ERROR: found possibly newer version of crate `a` which `b` depends on
2020
//~| NOTE: perhaps that crate needs to be recompiled
21-
//~| NOTE: crate `a` path #1:
22-
//~| NOTE: crate `b` path #1:
21+
//~| NOTE: the following crate versions were found:
2322

2423
fn main() {
2524
b::foo()

src/test/compile-fail/svh-use-trait.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
extern crate uta;
2424
extern crate utb; //~ ERROR: found possibly newer version of crate `uta` which `utb` depends
2525
//~| NOTE: perhaps that crate needs to be recompiled?
26-
//~| NOTE: crate `uta` path #1:
27-
//~| NOTE: crate `utb` path #1:
26+
//~| NOTE: the following crate versions were found:
2827

2928
fn main() {
3029
utb::foo()

src/test/run-make/many-crates-but-no-match/Makefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ all:
2929
$(RUSTC) -L $(A2) -L $(A3) crateC.rs >$(LOG) 2>&1 || true
3030
grep "found possibly newer version of crate \`crateA\` which \`crateB\` depends on" $(LOG)
3131
grep "note: perhaps that crate needs to be recompiled?" $(LOG)
32-
grep "note: crate \`crateA\` path #1:" $(LOG)
33-
grep "note: crate \`crateA\` path #2:" $(LOG)
34-
grep "note: crate \`crateB\` path #1:" $(LOG)
32+
grep "crate \`crateA\`:" $(LOG) # this will match two entries
33+
grep "crate \`crateB\`:" $(LOG)

0 commit comments

Comments
 (0)