1
Fork 0

check where clause before suggesting unsized

This commit is contained in:
Taylor Yu 2021-06-18 14:13:12 -05:00
parent b053550847
commit c9fcbda389
3 changed files with 45 additions and 0 deletions

View file

@ -14,6 +14,7 @@ use crate::infer::{self, InferCtxt, TyCtxtInferExt};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor;
use rustc_hir::GenericParam;
@ -2009,6 +2010,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
Some(param) => param,
_ => return,
};
let param_def_id = self.tcx.hir().local_def_id(param.hir_id).to_def_id();
let preds = generics.where_clause.predicates.iter();
let explicitly_sized = preds
.filter_map(|pred| match pred {
hir::WherePredicate::BoundPredicate(bp) => Some(bp),
_ => None,
})
.flat_map(|bp| match bp.bounded_ty.kind {
hir::TyKind::Path(hir::QPath::Resolved(
None,
&hir::Path { res: Res::Def(DefKind::TyParam, def_id), .. },
)) if def_id == param_def_id => bp.bounds,
_ => &[][..],
})
.any(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) == sized_trait);
if explicitly_sized {
return;
}
debug!("maybe_suggest_unsized_generics: param={:?}", param);
match node {
hir::Node::Item(