Skip to content

Commit dcc14c4

Browse files
committed
suggest to add a constraint except asyn-fn without explicit output
1 parent b380d35 commit dcc14c4

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4343
///
4444
/// It will later be extended to trait objects.
4545
pub(super) fn try_report_anon_anon_conflict(&self) -> Option<ErrorReported> {
46-
let (span, sub, sup) = self.get_regions();
46+
let (span, sub, sup) = self.regions();
4747

4848
// Determine whether the sub and sup consist of both anonymous (elided) regions.
4949
let anon_reg_sup = self.tcx().is_suitable_region(sup)?;

src/librustc/infer/error_reporting/nice_region_error/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
7777
.or_else(|| self.try_report_impl_not_conforming_to_trait())
7878
}
7979

80-
pub fn get_regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
80+
pub fn regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
8181
match (&self.error, self.regions) {
8282
(Some(ConcreteFailure(origin, sub, sup)), None) => (origin.span(), sub, sup),
8383
(Some(SubSupConflict(_, _, origin, sub, _, sup)), None) => (origin.span(), sub, sup),

src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
99
/// When given a `ConcreteFailure` for a function with parameters containing a named region and
1010
/// an anonymous region, emit an descriptive diagnostic error.
1111
pub(super) fn try_report_named_anon_conflict(&self) -> Option<DiagnosticBuilder<'a>> {
12-
let (span, sub, sup) = self.get_regions();
12+
let (span, sub, sup) = self.regions();
1313

1414
debug!(
1515
"try_report_named_anon_conflict(sub={:?}, sup={:?}, error={:?})",

src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2020
) = error.clone()
2121
{
2222
let anon_reg_sup = self.tcx().is_suitable_region(sup_r)?;
23+
let return_ty = self.tcx().return_type_impl_trait(anon_reg_sup.def_id);
2324
if sub_r == &RegionKind::ReStatic &&
24-
self.tcx().return_type_impl_trait(anon_reg_sup.def_id).is_some()
25+
return_ty.is_some()
2526
{
2627
let sp = var_origin.span();
2728
let return_sp = sub_origin.span();
@@ -53,16 +54,19 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5354
_ => "'_".to_owned(),
5455
};
5556
if let Ok(snippet) = self.tcx().sess.source_map().span_to_snippet(return_sp) {
56-
err.span_suggestion(
57-
return_sp,
58-
&format!(
59-
"you can add a constraint to the return type to make it last \
57+
// only apply this suggestion onto non-async fnunctions
58+
if !return_ty.unwrap().1 {
59+
err.span_suggestion(
60+
return_sp,
61+
&format!(
62+
"you can add a constraint to the return type to make it last \
6063
less than `'static` and match {}",
61-
lifetime,
62-
),
63-
format!("{} + {}", snippet, lifetime_name),
64-
Applicability::Unspecified,
65-
);
64+
lifetime,
65+
),
66+
format!("{} + {}", snippet, lifetime_name),
67+
Applicability::Unspecified,
68+
);
69+
}
6670
}
6771
err.emit();
6872
return Some(ErrorReported);

src/librustc/ty/context.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1552,14 +1552,14 @@ impl<'tcx> TyCtxt<'tcx> {
15521552
return Some(FreeRegionInfo {
15531553
def_id: suitable_region_binding_scope,
15541554
boundregion: bound_region,
1555-
is_impl_item: is_impl_item,
1555+
is_impl_item,
15561556
});
15571557
}
15581558

15591559
pub fn return_type_impl_trait(
15601560
&self,
15611561
scope_def_id: DefId,
1562-
) -> Option<Ty<'tcx>> {
1562+
) -> Option<(Ty<'tcx>, bool)> {
15631563
// HACK: `type_of_def_id()` will fail on these (#55796), so return `None`.
15641564
let hir_id = self.hir().as_local_hir_id(scope_def_id).unwrap();
15651565
match self.hir().get(hir_id) {
@@ -1579,8 +1579,10 @@ impl<'tcx> TyCtxt<'tcx> {
15791579
ty::FnDef(_, _) => {
15801580
let sig = ret_ty.fn_sig(*self);
15811581
let output = self.erase_late_bound_regions(&sig.output());
1582+
let is_async_fn =
1583+
hir::IsAsync::Async == self.asyncness(scope_def_id);
15821584
if output.is_impl_trait() {
1583-
Some(output)
1585+
Some((output, is_async_fn))
15841586
} else {
15851587
None
15861588
}

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
698698
if let (Some(f), Some(ty::RegionKind::ReStatic)) =
699699
(self.to_error_region(fr), self.to_error_region(outlived_fr))
700700
{
701-
if let Some(ty::TyS {
701+
if let Some((ty::TyS {
702702
kind: ty::Opaque(did, substs),
703703
..
704-
}) = infcx
704+
}, _)) = infcx
705705
.tcx
706706
.is_suitable_region(f)
707707
.map(|r| r.def_id)

0 commit comments

Comments
 (0)