-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Additional span info for E0053 #35765
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -12,10 +12,9 @@ use middle::free_region::FreeRegionMap; | |
use rustc::infer::{self, InferOk, TypeOrigin}; | ||
use rustc::ty; | ||
use rustc::traits::{self, Reveal}; | ||
use rustc::ty::error::ExpectedFound; | ||
use rustc::ty::error::{ExpectedFound, TypeError}; | ||
use rustc::ty::subst::{Subst, Substs}; | ||
use rustc::hir::map::Node; | ||
use rustc::hir::{ImplItemKind, TraitItem_}; | ||
use rustc::hir::{ImplItemKind, TraitItem_, Ty_}; | ||
|
||
use syntax::ast; | ||
use syntax_pos::Span; | ||
|
@@ -300,6 +299,7 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, | |
impl_m_span, | ||
impl_m_body_id, | ||
&impl_sig); | ||
let impl_args = impl_sig.inputs.clone(); | ||
let impl_fty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy { | ||
unsafety: impl_m.fty.unsafety, | ||
abi: impl_m.fty.abi, | ||
|
@@ -318,6 +318,7 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, | |
impl_m_span, | ||
impl_m_body_id, | ||
&trait_sig); | ||
let trait_args = trait_sig.inputs.clone(); | ||
let trait_fty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy { | ||
unsafety: trait_m.fty.unsafety, | ||
abi: trait_m.fty.abi, | ||
|
@@ -331,16 +332,82 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, | |
impl_fty, | ||
trait_fty); | ||
|
||
let impl_m_iter = match tcx.map.expect_impl_item(impl_m_node_id).node { | ||
ImplItemKind::Method(ref impl_m_sig, _) => impl_m_sig.decl.inputs.iter(), | ||
_ => bug!("{:?} is not a method", impl_m) | ||
}; | ||
|
||
let (impl_err_span, trait_err_span) = match terr { | ||
TypeError::Mutability => { | ||
if let Some(trait_m_node_id) = tcx.map.as_local_node_id(trait_m.def_id) { | ||
let trait_m_iter = match tcx.map.expect_trait_item(trait_m_node_id).node { | ||
TraitItem_::MethodTraitItem(ref trait_m_sig, _) => | ||
trait_m_sig.decl.inputs.iter(), | ||
_ => bug!("{:?} is not a MethodTraitItem", trait_m) | ||
}; | ||
|
||
impl_m_iter.zip(trait_m_iter).find(|&(ref impl_arg, ref trait_arg)| { | ||
match (&impl_arg.ty.node, &trait_arg.ty.node) { | ||
(&Ty_::TyRptr(_, ref impl_mt), &Ty_::TyRptr(_, ref trait_mt)) | | ||
(&Ty_::TyPtr(ref impl_mt), &Ty_::TyPtr(ref trait_mt)) => | ||
impl_mt.mutbl != trait_mt.mutbl, | ||
_ => false | ||
} | ||
}).map(|(ref impl_arg, ref trait_arg)| { | ||
match (impl_arg.to_self(), trait_arg.to_self()) { | ||
(Some(impl_self), Some(trait_self)) => | ||
(impl_self.span, Some(trait_self.span)), | ||
(None, None) => (impl_arg.ty.span, Some(trait_arg.ty.span)), | ||
_ => bug!("impl and trait fns have different first args, \ | ||
impl: {:?}, trait: {:?}", impl_arg, trait_arg) | ||
} | ||
}).unwrap_or((origin.span(), tcx.map.span_if_local(trait_m.def_id))) | ||
} else { | ||
(origin.span(), tcx.map.span_if_local(trait_m.def_id)) | ||
} | ||
} | ||
TypeError::Sorts(ExpectedFound { expected, found }) => { | ||
if let Some(trait_m_node_id) = tcx.map.as_local_node_id(trait_m.def_id) { | ||
let trait_m_iter = match tcx.map.expect_trait_item(trait_m_node_id).node { | ||
TraitItem_::MethodTraitItem(ref trait_m_sig, _) => | ||
trait_m_sig.decl.inputs.iter(), | ||
_ => bug!("{:?} is not a MethodTraitItem", trait_m) | ||
}; | ||
let impl_iter = impl_args.iter(); | ||
let trait_iter = trait_args.iter(); | ||
let arg_idx = impl_iter.zip(trait_iter) | ||
.position(|(impl_arg_ty, trait_arg_ty)| { | ||
*impl_arg_ty == found && *trait_arg_ty == expected | ||
}).unwrap(); | ||
impl_m_iter.zip(trait_m_iter) | ||
.nth(arg_idx) | ||
.map(|(impl_arg, trait_arg)| | ||
(impl_arg.ty.span, Some(trait_arg.ty.span))) | ||
.unwrap_or( | ||
(origin.span(), tcx.map.span_if_local(trait_m.def_id))) | ||
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. I ended up not using |
||
} else { | ||
(origin.span(), tcx.map.span_if_local(trait_m.def_id)) | ||
} | ||
} | ||
_ => (origin.span(), tcx.map.span_if_local(trait_m.def_id)) | ||
}; | ||
|
||
let origin = TypeOrigin::MethodCompatCheck(impl_err_span); | ||
|
||
let mut diag = struct_span_err!( | ||
tcx.sess, origin.span(), E0053, | ||
"method `{}` has an incompatible type for trait", trait_m.name | ||
); | ||
|
||
infcx.note_type_err( | ||
&mut diag, origin, | ||
&mut diag, | ||
origin, | ||
trait_err_span.map(|sp| (sp, format!("original trait requirement"))), | ||
Some(infer::ValuePairs::Types(ExpectedFound { | ||
expected: trait_fty, | ||
found: impl_fty | ||
})), &terr | ||
expected: trait_fty, | ||
found: impl_fty | ||
})), | ||
&terr | ||
); | ||
diag.emit(); | ||
return | ||
|
@@ -487,12 +554,9 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, | |
trait_ty); | ||
|
||
// Locate the Span containing just the type of the offending impl | ||
if let Some(impl_trait_node) = tcx.map.get_if_local(impl_c.def_id) { | ||
if let Node::NodeImplItem(impl_trait_item) = impl_trait_node { | ||
if let ImplItemKind::Const(ref ty, _) = impl_trait_item.node { | ||
origin = TypeOrigin::Misc(ty.span); | ||
} | ||
} | ||
match tcx.map.expect_impl_item(impl_c_node_id).node { | ||
ImplItemKind::Const(ref ty, _) => origin = TypeOrigin::Misc(ty.span), | ||
_ => bug!("{:?} is not a impl const", impl_c) | ||
} | ||
|
||
let mut diag = struct_span_err!( | ||
|
@@ -502,16 +566,16 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, | |
); | ||
|
||
// Add a label to the Span containing just the type of the item | ||
if let Some(orig_trait_node) = tcx.map.get_if_local(trait_c.def_id) { | ||
if let Node::NodeTraitItem(orig_trait_item) = orig_trait_node { | ||
if let TraitItem_::ConstTraitItem(ref ty, _) = orig_trait_item.node { | ||
diag.span_label(ty.span, &format!("original trait requirement")); | ||
} | ||
} | ||
} | ||
let trait_c_node_id = tcx.map.as_local_node_id(trait_c.def_id).unwrap(); | ||
let trait_c_span = match tcx.map.expect_trait_item(trait_c_node_id).node { | ||
TraitItem_::ConstTraitItem(ref ty, _) => ty.span, | ||
_ => bug!("{:?} is not a trait const", trait_c) | ||
}; | ||
|
||
infcx.note_type_err( | ||
&mut diag, origin, | ||
&mut diag, | ||
origin, | ||
Some((trait_c_span, format!("original trait requirement"))), | ||
Some(infer::ValuePairs::Types(ExpectedFound { | ||
expected: trait_ty, | ||
found: impl_ty | ||
|
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
src/test/ui/mismatched_types/trait-impl-fn-incompatibility.rs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// rustc-env:RUST_NEW_ERROR_FORMAT | ||
|
||
trait Foo { | ||
fn foo(x: u16); | ||
fn bar(&mut self, bar: &mut Bar); | ||
} | ||
|
||
struct Bar; | ||
|
||
impl Foo for Bar { | ||
fn foo(x: i16) { } | ||
fn bar(&mut self, bar: &Bar) { } | ||
} | ||
|
||
fn main() { | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error[E0053]: method `foo` has an incompatible type for trait | ||
--> $DIR/trait-impl-fn-incompatibility.rs:21:15 | ||
| | ||
14 | fn foo(x: u16); | ||
| --- original trait requirement | ||
... | ||
21 | fn foo(x: i16) { } | ||
| ^^^ expected u16, found i16 | ||
|
||
error[E0053]: method `bar` has an incompatible type for trait | ||
--> $DIR/trait-impl-fn-incompatibility.rs:22:28 | ||
| | ||
15 | fn bar(&mut self, bar: &mut Bar); | ||
| -------- original trait requirement | ||
... | ||
22 | fn bar(&mut self, bar: &Bar) { } | ||
| ^^^^ values differ in mutability | ||
| | ||
= note: expected type `fn(&mut Bar, &mut Bar)` | ||
= note: found type `fn(&mut Bar, &Bar)` | ||
|
||
error: aborting due to 2 previous errors | ||
|
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.
This
unwrap
is probably the cause of the ICE / regression #35869.