Skip to content

Commit b9351ef

Browse files
authored
Rollup merge of rust-lang#65730 - csmoe:return-lifetime, r=nikomatsakis
Suggest to add lifetime constraint at explicit ouput of functions Closes rust-lang#62097
2 parents 9e34664 + 1adb26b commit b9351ef

File tree

11 files changed

+74
-29
lines changed

11 files changed

+74
-29
lines changed

src/librustc/hir/lowering.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,16 @@ impl<'a> LoweringContext<'a> {
21202120
impl_trait_return_allow: bool,
21212121
make_ret_async: Option<NodeId>,
21222122
) -> P<hir::FnDecl> {
2123+
debug!("lower_fn_decl(\
2124+
fn_decl: {:?}, \
2125+
in_band_ty_params: {:?}, \
2126+
impl_trait_return_allow: {}, \
2127+
make_ret_async: {:?})",
2128+
decl,
2129+
in_band_ty_params,
2130+
impl_trait_return_allow,
2131+
make_ret_async,
2132+
);
21232133
let lt_mode = if make_ret_async.is_some() {
21242134
// In `async fn`, argument-position elided lifetimes
21252135
// must be transformed into fresh generic parameters so that
@@ -2412,7 +2422,7 @@ impl<'a> LoweringContext<'a> {
24122422

24132423
hir::FunctionRetTy::Return(P(hir::Ty {
24142424
kind: opaque_ty_ref,
2415-
span,
2425+
span: opaque_ty_span,
24162426
hir_id: self.next_id(),
24172427
}))
24182428
}
@@ -2522,7 +2532,7 @@ impl<'a> LoweringContext<'a> {
25222532
hir::Lifetime {
25232533
hir_id: self.lower_node_id(id),
25242534
span,
2525-
name: name,
2535+
name,
25262536
}
25272537
}
25282538

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 11 deletions
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();
@@ -52,17 +53,23 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5253
}) => name.to_string(),
5354
_ => "'_".to_owned(),
5455
};
55-
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 \
56+
let fn_return_span = return_ty.unwrap().1;
57+
if let Ok(snippet) =
58+
self.tcx().sess.source_map().span_to_snippet(fn_return_span) {
59+
// only apply this suggestion onto functions with
60+
// explicit non-desugar'able return.
61+
if fn_return_span.desugaring_kind().is_none() {
62+
err.span_suggestion(
63+
fn_return_span,
64+
&format!(
65+
"you can add a constraint to the return type to make it last \
6066
less than `'static` and match {}",
61-
lifetime,
62-
),
63-
format!("{} + {}", snippet, lifetime_name),
64-
Applicability::Unspecified,
65-
);
67+
lifetime,
68+
),
69+
format!("{} + {}", snippet, lifetime_name),
70+
Applicability::Unspecified,
71+
);
72+
}
6673
}
6774
err.emit();
6875
return Some(ErrorReported);

src/librustc/ty/context.rs

Lines changed: 4 additions & 3 deletions
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>, Span)> {
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) {
@@ -1580,7 +1580,8 @@ impl<'tcx> TyCtxt<'tcx> {
15801580
let sig = ret_ty.fn_sig(*self);
15811581
let output = self.erase_late_bound_regions(&sig.output());
15821582
if output.is_impl_trait() {
1583-
Some(output)
1583+
let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap();
1584+
Some((output, fn_decl.output.span()))
15841585
} else {
15851586
None
15861587
}

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

Lines changed: 2 additions & 2 deletions
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)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// edition:2018
2+
async fn foo<F>(fun: F)
3+
where
4+
F: FnOnce() + 'static
5+
{
6+
fun()
7+
}
8+
9+
struct Struct;
10+
11+
impl Struct {
12+
pub async fn run_dummy_fn(&self) { //~ ERROR cannot infer
13+
foo(|| self.bar()).await;
14+
}
15+
16+
pub fn bar(&self) {}
17+
}
18+
19+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: cannot infer an appropriate lifetime
2+
--> $DIR/issue-62097.rs:12:31
3+
|
4+
LL | pub async fn run_dummy_fn(&self) {
5+
| ^^^^^ ...but this borrow...
6+
LL | foo(|| self.bar()).await;
7+
| --- this return type evaluates to the `'static` lifetime...
8+
|
9+
note: ...can't outlive the lifetime `'_` as defined on the method body at 12:31
10+
--> $DIR/issue-62097.rs:12:31
11+
|
12+
LL | pub async fn run_dummy_fn(&self) {
13+
| ^
14+
15+
error: aborting due to previous error
16+

src/test/ui/async-await/issues/issue-63388-2.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ note: ...can't outlive the lifetime `'_` as defined on the method body at 11:14
2020
|
2121
LL | foo: &dyn Foo, bar: &'a dyn Foo
2222
| ^
23-
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime `'_` as defined on the method body at 11:14
24-
|
25-
LL | foo + '_
26-
|
2723

2824
error: aborting due to 2 previous errors
2925

src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ note: ...can't outlive the lifetime `'_` as defined on the method body at 8:26
1111
|
1212
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
1313
| ^
14-
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime `'_` as defined on the method body at 8:26
15-
|
16-
LL | async fn f(self: Pin<&Self>) -> impl Clone + '_ { self }
17-
| ^^^^^^^^^^^^^^^
1814

1915
error: aborting due to previous error
2016

0 commit comments

Comments
 (0)