Skip to content

Add note for possible crate mismatches in type errors #28300

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

Merged
merged 6 commits into from
Sep 9, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handle trait objects
  • Loading branch information
Manishearth committed Sep 8, 2015
commit 89af15322dbca73b098e55bbd283a2d8a254571b
41 changes: 26 additions & 15 deletions src/librustc/middle/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use rustc_front::hir;
use rustc_front::print::pprust;

use middle::def;
use middle::def_id::DefId;
use middle::infer;
use middle::region;
use middle::subst;
Expand Down Expand Up @@ -497,6 +498,25 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {

/// Adds a note if the types come from similarly named crates
fn check_and_note_conflicting_crates(&self, terr: &ty::TypeError<'tcx>, sp: Span) {
let report_path_match = |did1: DefId, did2: DefId| {
// Only external crates, if either is from a local
// module we could have false positives
if !(did1.is_local() || did2.is_local()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check that the crates are different - for example:

fn main() {
    let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
}

The error in the above code will see the path "main::Foo" for both of the two different types.

let exp_path = self.tcx.with_path(did1,
|p| p.map(|x| x.to_string())
.collect::<Vec<_>>());
let found_path = self.tcx.with_path(did2,
|p| p.map(|x| x.to_string())
.collect::<Vec<_>>());
// We compare strings because PathMod and PathName can be different
// for imported and non-imported crates
if exp_path == found_path {
self.tcx.sess.span_note(sp, &format!("Perhaps two different versions \
of crate `{}` are being used?",
exp_path[0]));
}
}
};
match *terr {
ty::TypeError::Sorts(ref exp_found) => {
// if they are both "path types", there's a chance of ambiguity
Expand All @@ -506,24 +526,15 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
(&ty::TyStruct(ref exp_adt, _), &ty::TyStruct(ref found_adt, _)) |
(&ty::TyEnum(ref exp_adt, _), &ty::TyStruct(ref found_adt, _)) |
(&ty::TyStruct(ref exp_adt, _), &ty::TyEnum(ref found_adt, _)) => {
// Only external crates, if either is from a local
// module we could have false positives
if exp_adt.did.is_local() || found_adt.did.is_local() {
return
}
let exp_path = self.tcx.with_path(exp_adt.did,
|p| p.collect::<Vec<_>>());
let found_path = self.tcx.with_path(exp_adt.did,
|p| p.collect::<Vec<_>>());
if exp_path == found_path {
self.tcx.sess.span_note(sp, &format!("Perhaps two different versions \
of crate `{}` are being used?",
exp_path[0]));
}
report_path_match(exp_adt.did, found_adt.did);
},
_ => ()
}
}
},
ty::TypeError::Traits(ref exp_found) => {
self.tcx.sess.note("errrr0");
report_path_match(exp_found.expected, exp_found.found);
},
_ => () // FIXME(Manishearth) handle traits and stuff
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC all FIXMEs should have an associated issue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, it's the same bug (since this is only a partial fix)

}
}
Expand Down