Suggest impl Trait
return type
Address #85991 Suggest the `impl Trait` return type syntax if the user tried to return a generic parameter and we get a type mismatch The suggestion is not emitted if the param appears in the function parameters, and only get the bounds that actually involve `T: ` directly It also checks whether the generic param is contained in any where bound (where it isn't the self type), and if one is found (like `Option<T>: Send`), it is not suggested. This also adds `TyS::contains`, which recursively vistits the type and looks if the other type is contained anywhere
This commit is contained in:
parent
b8c56fa8c3
commit
4bed7485da
7 changed files with 321 additions and 3 deletions
|
@ -8,8 +8,12 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
|
|||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{CtorOf, DefKind};
|
||||
use rustc_hir::lang_items::LangItem;
|
||||
use rustc_hir::{Expr, ExprKind, ItemKind, Node, Path, QPath, Stmt, StmtKind, TyKind};
|
||||
use rustc_hir::{
|
||||
Expr, ExprKind, GenericBound, ItemKind, Node, Path, QPath, Stmt, StmtKind, TyKind,
|
||||
WherePredicate,
|
||||
};
|
||||
use rustc_infer::infer::{self, TyCtxtInferExt};
|
||||
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use rustc_middle::ty::{self, Binder, Ty};
|
||||
use rustc_span::symbol::{kw, sym};
|
||||
|
@ -559,6 +563,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let ty = self.tcx.erase_late_bound_regions(ty);
|
||||
if self.can_coerce(expected, ty) {
|
||||
err.span_label(sp, format!("expected `{}` because of return type", expected));
|
||||
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
|
||||
return true;
|
||||
}
|
||||
false
|
||||
|
@ -566,6 +571,115 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
/// check whether the return type is a generic type with a trait bound
|
||||
/// only suggest this if the generic param is not present in the arguments
|
||||
/// if this is true, hint them towards changing the return type to `impl Trait`
|
||||
/// ```
|
||||
/// fn cant_name_it<T: Fn() -> u32>() -> T {
|
||||
/// || 3
|
||||
/// }
|
||||
/// ```
|
||||
fn try_suggest_return_impl_trait(
|
||||
&self,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
expected: Ty<'tcx>,
|
||||
found: Ty<'tcx>,
|
||||
fn_id: hir::HirId,
|
||||
) {
|
||||
// Only apply the suggestion if:
|
||||
// - the return type is a generic parameter
|
||||
// - the generic param is not used as a fn param
|
||||
// - the generic param has at least one bound
|
||||
// - the generic param doesn't appear in any other bounds where it's not the Self type
|
||||
// Suggest:
|
||||
// - Changing the return type to be `impl <all bounds>`
|
||||
|
||||
debug!("try_suggest_return_impl_trait, expected = {:?}, found = {:?}", expected, found);
|
||||
|
||||
let ty::Param(expected_ty_as_param) = expected.kind() else { return };
|
||||
|
||||
let fn_node = self.tcx.hir().find(fn_id);
|
||||
|
||||
let Some(hir::Node::Item(hir::Item {
|
||||
kind:
|
||||
hir::ItemKind::Fn(
|
||||
hir::FnSig { decl: hir::FnDecl { inputs: fn_parameters, output: fn_return, .. }, .. },
|
||||
hir::Generics { params, where_clause, .. },
|
||||
_body_id,
|
||||
),
|
||||
..
|
||||
})) = fn_node else { return };
|
||||
|
||||
let Some(expected_generic_param) = params.get(expected_ty_as_param.index as usize) else { return };
|
||||
|
||||
// get all where BoundPredicates here, because they are used in to cases below
|
||||
let where_predicates = where_clause
|
||||
.predicates
|
||||
.iter()
|
||||
.filter_map(|p| match p {
|
||||
WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
|
||||
bounds,
|
||||
bounded_ty,
|
||||
..
|
||||
}) => {
|
||||
// FIXME: Maybe these calls to `ast_ty_to_ty` can be removed (and the ones below)
|
||||
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, bounded_ty);
|
||||
Some((ty, bounds))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.map(|(ty, bounds)| match ty.kind() {
|
||||
ty::Param(param_ty) if param_ty == expected_ty_as_param => Ok(Some(bounds)),
|
||||
// check whether there is any predicate that contains our `T`, like `Option<T>: Send`
|
||||
_ => match ty.contains(expected) {
|
||||
true => Err(()),
|
||||
false => Ok(None),
|
||||
},
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>();
|
||||
|
||||
let Ok(where_predicates) = where_predicates else { return };
|
||||
|
||||
// now get all predicates in the same types as the where bounds, so we can chain them
|
||||
let predicates_from_where =
|
||||
where_predicates.iter().flatten().map(|bounds| bounds.iter()).flatten();
|
||||
|
||||
// extract all bounds from the source code using their spans
|
||||
let all_matching_bounds_strs = expected_generic_param
|
||||
.bounds
|
||||
.iter()
|
||||
.chain(predicates_from_where)
|
||||
.filter_map(|bound| match bound {
|
||||
GenericBound::Trait(_, _) => {
|
||||
self.tcx.sess.source_map().span_to_snippet(bound.span()).ok()
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
if all_matching_bounds_strs.len() == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let all_bounds_str = all_matching_bounds_strs.join(" + ");
|
||||
|
||||
let ty_param_used_in_fn_params = fn_parameters.iter().any(|param| {
|
||||
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, param);
|
||||
matches!(ty.kind(), ty::Param(fn_param_ty_param) if expected_ty_as_param == fn_param_ty_param)
|
||||
});
|
||||
|
||||
if ty_param_used_in_fn_params {
|
||||
return;
|
||||
}
|
||||
|
||||
err.span_suggestion(
|
||||
fn_return.span(),
|
||||
"consider using an impl return type",
|
||||
format!("impl {}", all_bounds_str),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
|
||||
pub(in super::super) fn suggest_missing_break_or_return_expr(
|
||||
&self,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue