Skip to content

Commit

Permalink
Name return type in free region messages
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Aug 6, 2018
1 parent f72b8a4 commit b13e3f8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
})
.or_else(|| {
self.give_name_if_anonymous_region_appears_in_output(
infcx.tcx, mir, mir_def_id, fr, counter, diag)
infcx, mir, mir_def_id, fr, counter, diag)
})
.unwrap_or_else(|| span_bug!(mir.span, "can't make a name for free region {:?}", fr))
}
Expand Down Expand Up @@ -577,38 +577,51 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// or be early bound (named, not in argument).
fn give_name_if_anonymous_region_appears_in_output(
&self,
tcx: TyCtxt<'_, '_, 'tcx>,
infcx: &InferCtxt<'_, '_, 'tcx>,
mir: &Mir<'tcx>,
mir_def_id: DefId,
fr: RegionVid,
counter: &mut usize,
diag: &mut DiagnosticBuilder<'_>,
) -> Option<InternedString> {
let tcx = infcx.tcx;

let return_ty = self.universal_regions.unnormalized_output_ty;
debug!(
"give_name_if_anonymous_region_appears_in_output: return_ty = {:?}",
return_ty
);
if !tcx.any_free_region_meets(&return_ty, |r| r.to_region_vid() == fr) {
if !infcx.tcx.any_free_region_meets(&return_ty, |r| r.to_region_vid() == fr) {
return None;
}

let mir_node_id = tcx.hir.as_local_node_id(mir_def_id).expect("non-local mir");
let args_span = if let hir::ExprKind::Closure(_, _, _, span, _)
let type_name = with_highlight_region(fr, *counter, || {
infcx.extract_type_name(&return_ty)
});

let mir_node_id = tcx.hir.as_local_node_id(mir_def_id).expect("non-local mir");

let (return_span, mir_description) = if let hir::ExprKind::Closure(_, _, _, span, gen_move)
= tcx.hir.expect_expr(mir_node_id).node
{
span
(
tcx.sess.codemap().end_point(span),
if gen_move.is_some() { " of generator" } else { " of closure" }
)
} else {
mir.span
// unreachable?
(mir.span, "")
};

let region_name = self.synthesize_region_name(counter);
diag.span_label(
args_span,
format!("lifetime `{}` appears in return type", region_name),
return_span,
format!("return type{} is {}", mir_description, type_name),
);

Some(region_name)
// This counter value will already have been used, so this function will increment it
// so the next value will be used next and return the region name that would have been
// used.
Some(self.synthesize_region_name(counter))
}

/// Create a synthetic region named `'1`, incrementing the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ error: unsatisfied lifetime constraints
--> $DIR/E0621-does-not-trigger-for-closures.rs:25:26
|
LL | invoke(&x, |a, b| if a > b { a } else { b }); //~ ERROR E0495
| ------ ^^^^^ requires that `'1` must outlive `'2`
| | |
| | has type `&'1 i32`
| lifetime `'2` appears in return type
| -- ^^^^^ requires that `'1` must outlive `'2`
| ||
| |return type of closure is &'2 i32
| has type `&'1 i32`

error: aborting due to previous error

4 changes: 2 additions & 2 deletions src/test/ui/issue-40510-1.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
|
LL | || {
| --
| |
| ||
| |return type of closure is &'2 mut std::boxed::Box<()>
| lifetime `'1` represents this closure's body
| lifetime `'2` appears in return type
LL | &mut x
| ^^^^^^ return requires that `'1` must outlive `'2`
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issue-40510-3.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
|
LL | || {
| --
| |
| ||
| |return type of closure is [closure@$DIR/issue-40510-3.rs:18:9: 20:10 x:&'2 mut std::vec::Vec<()>]
| lifetime `'1` represents this closure's body
| lifetime `'2` appears in return type
LL | / || {
LL | | x.push(())
LL | | }
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issue-49824.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
|
LL | || {
| --
| |
| ||
| |return type of closure is [closure@$DIR/issue-49824.rs:22:9: 24:10 x:&'2 mut i32]
| lifetime `'1` represents this closure's body
| lifetime `'2` appears in return type
LL | / || {
LL | | let _y = &mut x;
LL | | }
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/nll/issue-48238.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ error: unsatisfied lifetime constraints
|
LL | move || use_val(&orig); //~ ERROR
| ------- ^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`
| |
| | |
| | return type of closure is &'2 u8
| lifetime `'1` represents this closure's body
| lifetime `'2` appears in return type
|
= note: closure implements `Fn`, so references to captured variables can't escape the closure

Expand Down

0 comments on commit b13e3f8

Please sign in to comment.