Suggest assoc ty bound on bare dyn trait in eq constraint
This commit is contained in:
parent
d3d77a8733
commit
22b9e960d9
3 changed files with 159 additions and 0 deletions
|
@ -64,10 +64,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
if self_ty.span.can_be_used_for_suggestions()
|
||||
&& !self.maybe_suggest_impl_trait(self_ty, &mut diag)
|
||||
{
|
||||
// FIXME: Only emit this suggestion if the trait is object safe.
|
||||
diag.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable);
|
||||
}
|
||||
// Check if the impl trait that we are considering is an impl of a local trait.
|
||||
self.maybe_suggest_blanket_trait_impl(self_ty, &mut diag);
|
||||
self.maybe_suggest_assoc_ty_bound(self_ty, &mut diag);
|
||||
diag.stash(self_ty.span, StashKey::TraitMissingMethod);
|
||||
} else {
|
||||
let msg = "trait objects without an explicit `dyn` are deprecated";
|
||||
|
@ -153,6 +155,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
fn maybe_suggest_impl_trait(&self, self_ty: &hir::Ty<'_>, diag: &mut Diag<'_>) -> bool {
|
||||
let tcx = self.tcx();
|
||||
let parent_id = tcx.hir().get_parent_item(self_ty.hir_id).def_id;
|
||||
// FIXME: If `type_alias_impl_trait` is enabled, also look for `Trait0<Ty = Trait1>`
|
||||
// and suggest `Trait0<Ty = impl Trait1>`.
|
||||
let (sig, generics, owner) = match tcx.hir_node_by_def_id(parent_id) {
|
||||
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, generics, _), .. }) => {
|
||||
(sig, generics, None)
|
||||
|
@ -260,4 +264,38 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn maybe_suggest_assoc_ty_bound(&self, self_ty: &hir::Ty<'_>, diag: &mut Diag<'_>) {
|
||||
let mut parents = self.tcx().hir().parent_iter(self_ty.hir_id);
|
||||
|
||||
if let Some((_, hir::Node::TypeBinding(binding))) = parents.next()
|
||||
&& let hir::TypeBindingKind::Equality { term: hir::Term::Ty(obj_ty) } = binding.kind
|
||||
{
|
||||
if let Some((_, hir::Node::TraitRef(..))) = parents.next()
|
||||
&& let Some((_, hir::Node::Ty(ty))) = parents.next()
|
||||
&& let hir::TyKind::TraitObject(..) = ty.kind
|
||||
{
|
||||
// Assoc ty bounds aren't permitted inside trait object types.
|
||||
return;
|
||||
}
|
||||
|
||||
let lo = if binding.gen_args.span_ext.is_dummy() {
|
||||
binding.ident.span
|
||||
} else {
|
||||
binding.gen_args.span_ext
|
||||
};
|
||||
let hi = obj_ty.span;
|
||||
|
||||
if !lo.eq_ctxt(hi) {
|
||||
return;
|
||||
}
|
||||
|
||||
diag.span_suggestion_verbose(
|
||||
lo.between(hi),
|
||||
"you might have meant to write a bound here",
|
||||
": ",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue