-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add note for possible crate mismatches in type errors #28300
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b16eb8
Add note for when a type error comes from similarly named objects fro…
Manishearth 89af153
Handle trait objects
Manishearth b48ffa0
Use 'a different' for trait object mismatches too
Manishearth a3f05f6
Fix fixme, add crate check
Manishearth 00e7005
Add test
Manishearth c65d338
Print correct crate name
Manishearth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -226,6 +227,8 @@ pub trait ErrorReporting<'tcx> { | |
|
||
fn report_type_error(&self, trace: TypeTrace<'tcx>, terr: &ty::TypeError<'tcx>); | ||
|
||
fn check_and_note_conflicting_crates(&self, terr: &ty::TypeError<'tcx>, sp: Span); | ||
|
||
fn report_and_explain_type_error(&self, | ||
trace: TypeTrace<'tcx>, | ||
terr: &ty::TypeError<'tcx>); | ||
|
@@ -484,13 +487,58 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> { | |
expected_found_str, | ||
terr); | ||
|
||
self.check_and_note_conflicting_crates(terr, trace.origin.span()); | ||
|
||
match trace.origin { | ||
infer::MatchExpressionArm(_, arm_span) => | ||
self.tcx.sess.span_note(arm_span, "match arm with an incompatible type"), | ||
_ => () | ||
} | ||
} | ||
|
||
/// 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()) { | ||
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 | ||
// due to different versions of the same crate | ||
match (&exp_found.expected.sty, &exp_found.found.sty) { | ||
(&ty::TyEnum(ref exp_adt, _), &ty::TyEnum(ref found_adt, _)) | | ||
(&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, _)) => { | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC all FIXMEs should have an associated issue There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
} | ||
} | ||
|
||
fn report_and_explain_type_error(&self, | ||
trace: TypeTrace<'tcx>, | ||
terr: &ty::TypeError<'tcx>) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
The error in the above code will see the path "main::Foo" for both of the two different types.