improve comments for unsized suggestions

This commit is contained in:
Taylor Yu 2021-06-02 16:06:50 -05:00
parent 69f0dc69a4
commit 3252432c27

View file

@ -1811,6 +1811,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
.iter() .iter()
.filter(|param| param.span == span) .filter(|param| param.span == span)
.filter(|param| { .filter(|param| {
// Check that none of the explicit trait bounds is `Sized`. Assume that an explicit
// `Sized` bound is there intentionally and we don't need to suggest relaxing it.
param param
.bounds .bounds
.iter() .iter()
@ -1827,6 +1829,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
item item
@ @
hir::Item { hir::Item {
// Only suggest indirection for uses of type parameters in ADTs.
kind: kind:
hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..), hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..),
.. ..
@ -1838,6 +1841,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
} }
_ => {} _ => {}
}; };
// Didn't add an indirection suggestion, so add a general suggestion to relax `Sized`.
let (span, separator) = match param.bounds { let (span, separator) = match param.bounds {
[] => (span.shrink_to_hi(), ":"), [] => (span.shrink_to_hi(), ":"),
[.., bound] => (bound.span().shrink_to_hi(), " +"), [.., bound] => (bound.span().shrink_to_hi(), " +"),
@ -1858,7 +1862,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
) -> bool { ) -> bool {
// Suggesting `T: ?Sized` is only valid in an ADT if `T` is only used in a // Suggesting `T: ?Sized` is only valid in an ADT if `T` is only used in a
// borrow. `struct S<'a, T: ?Sized>(&'a T);` is valid, `struct S<T: ?Sized>(T);` // borrow. `struct S<'a, T: ?Sized>(&'a T);` is valid, `struct S<T: ?Sized>(T);`
// is not. // is not. Look for invalid "bare" parameter uses, and suggest using indirection.
let mut visitor = let mut visitor =
FindTypeParam { param: param.name.ident().name, invalid_spans: vec![], nested: false }; FindTypeParam { param: param.name.ident().name, invalid_spans: vec![], nested: false };
visitor.visit_item(item); visitor.visit_item(item);