Skip to content
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

refactor suggest_traits_to_import #71888

Merged
merged 1 commit into from
May 7, 2020
Merged
Changes from all commits
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
104 changes: 49 additions & 55 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,65 +947,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// this isn't perfect (that is, there are cases when
// implementing a trait would be legal but is rejected
// here).
!unsatisfied_predicates.iter().any(|(p, _)| match p {
// Hide traits if they are present in predicates as they can be fixed without
// having to implement them.
ty::Predicate::Trait(t, _) => t.def_id() != info.def_id,
ty::Predicate::Projection(p) => p.item_def_id() != info.def_id,
_ => true,
}) && (type_is_local || info.def_id.is_local())
&& self
.associated_item(info.def_id, item_name, Namespace::ValueNS)
.filter(|item| {
if let ty::AssocKind::Fn = item.kind {
let id = item.def_id.as_local().map(|def_id| {
self.tcx.hir().as_local_hir_id(def_id)
});
if let Some(hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(fn_sig, method),
..
})) = id.map(|id| self.tcx.hir().get(id))
unsatisfied_predicates.iter().all(|(p, _)| match p {
// Hide traits if they are present in predicates as they can be fixed without
// having to implement them.
ty::Predicate::Trait(t, _) => t.def_id() == info.def_id,
ty::Predicate::Projection(p) => p.item_def_id() == info.def_id,
_ => false,
}) && (type_is_local || info.def_id.is_local())
&& self
.associated_item(info.def_id, item_name, Namespace::ValueNS)
.filter(|item| {
if let ty::AssocKind::Fn = item.kind {
let id = item
.def_id
.as_local()
.map(|def_id| self.tcx.hir().as_local_hir_id(def_id));
if let Some(hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(fn_sig, method),
..
})) = id.map(|id| self.tcx.hir().get(id))
{
let self_first_arg = match method {
hir::TraitFn::Required([ident, ..]) => {
ident.name == kw::SelfLower
}
hir::TraitFn::Provided(body_id) => {
self.tcx.hir().body(*body_id).params.first().map_or(
false,
|param| {
matches!(
param.pat.kind,
hir::PatKind::Binding(_, _, ident, _)
if ident.name == kw::SelfLower
)
},
)
}
_ => false,
};

if !fn_sig.decl.implicit_self.has_implicit_self()
&& self_first_arg
{
let self_first_arg = match method {
hir::TraitFn::Required([ident, ..]) => {
ident.name == kw::SelfLower
}
hir::TraitFn::Provided(body_id) => {
match &self.tcx.hir().body(*body_id).params[..] {
[hir::Param {
pat:
hir::Pat {
kind:
hir::PatKind::Binding(
_,
_,
ident,
..,
),
..
},
..
}, ..] => ident.name == kw::SelfLower,
_ => false,
}
}
_ => false,
};

if !fn_sig.decl.implicit_self.has_implicit_self()
&& self_first_arg
{
if let Some(ty) = fn_sig.decl.inputs.get(0) {
arbitrary_rcvr.push(ty.span);
}
return false;
if let Some(ty) = fn_sig.decl.inputs.get(0) {
arbitrary_rcvr.push(ty.span);
}
return false;
}
}
// We only want to suggest public or local traits (#45781).
item.vis == ty::Visibility::Public || info.def_id.is_local()
})
.is_some()
}
// We only want to suggest public or local traits (#45781).
item.vis == ty::Visibility::Public || info.def_id.is_local()
})
.is_some()
})
.collect::<Vec<_>>();
for span in &arbitrary_rcvr {
Expand Down