suggest to add a constraint except asyn-fn without explicit output

This commit is contained in:
csmoe 2019-10-24 01:28:27 +08:00
parent b380d35849
commit dcc14c40ee
6 changed files with 24 additions and 18 deletions

View File

@ -43,7 +43,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
///
/// It will later be extended to trait objects.
pub(super) fn try_report_anon_anon_conflict(&self) -> Option<ErrorReported> {
let (span, sub, sup) = self.get_regions();
let (span, sub, sup) = self.regions();
// Determine whether the sub and sup consist of both anonymous (elided) regions.
let anon_reg_sup = self.tcx().is_suitable_region(sup)?;

View File

@ -77,7 +77,7 @@ pub fn try_report(&self) -> Option<ErrorReported> {
.or_else(|| self.try_report_impl_not_conforming_to_trait())
}
pub fn get_regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
pub fn regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
match (&self.error, self.regions) {
(Some(ConcreteFailure(origin, sub, sup)), None) => (origin.span(), sub, sup),
(Some(SubSupConflict(_, _, origin, sub, _, sup)), None) => (origin.span(), sub, sup),

View File

@ -9,7 +9,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// When given a `ConcreteFailure` for a function with parameters containing a named region and
/// an anonymous region, emit an descriptive diagnostic error.
pub(super) fn try_report_named_anon_conflict(&self) -> Option<DiagnosticBuilder<'a>> {
let (span, sub, sup) = self.get_regions();
let (span, sub, sup) = self.regions();
debug!(
"try_report_named_anon_conflict(sub={:?}, sup={:?}, error={:?})",

View File

@ -20,8 +20,9 @@ pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> {
) = error.clone()
{
let anon_reg_sup = self.tcx().is_suitable_region(sup_r)?;
let return_ty = self.tcx().return_type_impl_trait(anon_reg_sup.def_id);
if sub_r == &RegionKind::ReStatic &&
self.tcx().return_type_impl_trait(anon_reg_sup.def_id).is_some()
return_ty.is_some()
{
let sp = var_origin.span();
let return_sp = sub_origin.span();
@ -53,16 +54,19 @@ pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> {
_ => "'_".to_owned(),
};
if let Ok(snippet) = self.tcx().sess.source_map().span_to_snippet(return_sp) {
err.span_suggestion(
return_sp,
&format!(
"you can add a constraint to the return type to make it last \
// only apply this suggestion onto non-async fnunctions
if !return_ty.unwrap().1 {
err.span_suggestion(
return_sp,
&format!(
"you can add a constraint to the return type to make it last \
less than `'static` and match {}",
lifetime,
),
format!("{} + {}", snippet, lifetime_name),
Applicability::Unspecified,
);
lifetime,
),
format!("{} + {}", snippet, lifetime_name),
Applicability::Unspecified,
);
}
}
err.emit();
return Some(ErrorReported);

View File

@ -1552,14 +1552,14 @@ pub fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo>
return Some(FreeRegionInfo {
def_id: suitable_region_binding_scope,
boundregion: bound_region,
is_impl_item: is_impl_item,
is_impl_item,
});
}
pub fn return_type_impl_trait(
&self,
scope_def_id: DefId,
) -> Option<Ty<'tcx>> {
) -> Option<(Ty<'tcx>, bool)> {
// HACK: `type_of_def_id()` will fail on these (#55796), so return `None`.
let hir_id = self.hir().as_local_hir_id(scope_def_id).unwrap();
match self.hir().get(hir_id) {
@ -1579,8 +1579,10 @@ pub fn return_type_impl_trait(
ty::FnDef(_, _) => {
let sig = ret_ty.fn_sig(*self);
let output = self.erase_late_bound_regions(&sig.output());
let is_async_fn =
hir::IsAsync::Async == self.asyncness(scope_def_id);
if output.is_impl_trait() {
Some(output)
Some((output, is_async_fn))
} else {
None
}

View File

@ -698,10 +698,10 @@ fn add_static_impl_trait_suggestion(
if let (Some(f), Some(ty::RegionKind::ReStatic)) =
(self.to_error_region(fr), self.to_error_region(outlived_fr))
{
if let Some(ty::TyS {
if let Some((ty::TyS {
kind: ty::Opaque(did, substs),
..
}) = infcx
}, _)) = infcx
.tcx
.is_suitable_region(f)
.map(|r| r.def_id)