Skip to content

Commit 97915ab

Browse files
author
Lukas Markeffsky
committed
Add note for mismatched types with circular dependencies
1 parent 7650bd1 commit 97915ab

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
613613
}
614614

615615
let report_path_match = |err: &mut Diagnostic, did1: DefId, did2: DefId| {
616-
// Only external crates, if either is from a local
617-
// module we could have false positives
618-
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
616+
// Only report definitions from different crates. If both definitions
617+
// are from a local module we could have false positives, e.g.
618+
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
619+
if did1.krate != did2.krate {
619620
let abs_path =
620621
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
621622

@@ -627,10 +628,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
627628
};
628629
if same_path().unwrap_or(false) {
629630
let crate_name = self.tcx.crate_name(did1.krate);
630-
err.note(&format!(
631-
"perhaps two different versions of crate `{}` are being used?",
632-
crate_name
633-
));
631+
let msg = if did1.is_local() || did2.is_local() {
632+
format!(
633+
"the crate `{crate_name}` is compiled multiple times, possibly with different configurations"
634+
)
635+
} else {
636+
format!(
637+
"perhaps two different versions of crate `{crate_name}` are being used?"
638+
)
639+
};
640+
err.note(msg);
634641
}
635642
}
636643
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// edition: 2021
2+
// compile-flags: --crate-type lib --extern circular_dependencies={{build-base}}/circular-dependencies/libcircular_dependencies.rmeta --emit dep-info,metadata
3+
4+
use circular_dependencies::Foo;
5+
6+
pub fn consume_foo(_: Foo) {}
7+
8+
pub fn produce_foo() -> Foo {
9+
Foo
10+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ignore-tidy-linelength
2+
// revisions: cpass1 cfail2
3+
// edition: 2021
4+
// [cpass1] compile-flags: --crate-type lib --emit dep-info,metadata
5+
// [cfail2] aux-build: circular-dependencies-aux.rs
6+
// [cfail2] compile-flags: --test --extern aux={{build-base}}/circular-dependencies/auxiliary/libcircular_dependencies_aux.rmeta -L dependency={{build-base}}/circular-dependencies
7+
8+
pub struct Foo;
9+
//[cfail2]~^ NOTE `Foo` is defined in the current crate
10+
//[cfail2]~| NOTE `Foo` is defined in the current crate
11+
//[cfail2]~| NOTE `circular_dependencies::Foo` is defined in crate `circular_dependencies`
12+
//[cfail2]~| NOTE `circular_dependencies::Foo` is defined in crate `circular_dependencies`
13+
14+
pub fn consume_foo(_: Foo) {}
15+
//[cfail2]~^ NOTE function defined here
16+
17+
pub fn produce_foo() -> Foo {
18+
Foo
19+
}
20+
21+
#[test]
22+
fn test() {
23+
aux::consume_foo(produce_foo());
24+
//[cfail2]~^ ERROR mismatched types [E0308]
25+
//[cfail2]~| NOTE expected `circular_dependencies::Foo`, found `Foo`
26+
//[cfail2]~| NOTE arguments to this function are incorrect
27+
//[cfail2]~| NOTE `Foo` and `circular_dependencies::Foo` have similar names, but are actually distinct types
28+
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
29+
//[cfail2]~| NOTE function defined here
30+
31+
consume_foo(aux::produce_foo());
32+
//[cfail2]~^ ERROR mismatched types [E0308]
33+
//[cfail2]~| NOTE expected `Foo`, found `circular_dependencies::Foo`
34+
//[cfail2]~| NOTE arguments to this function are incorrect
35+
//[cfail2]~| NOTE `circular_dependencies::Foo` and `Foo` have similar names, but are actually distinct types
36+
//[cfail2]~| NOTE the crate `circular_dependencies` is compiled multiple times, possibly with different configurations
37+
}

0 commit comments

Comments
 (0)