Skip to content

Suggest to shorten temporary lifetime during method call inside generator #68212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let target_span = tables
.generator_interior_types
.iter()
.find(|ty::GeneratorInteriorTypeCause { ty, .. }| {
.find(|(ty::GeneratorInteriorTypeCause { ty, .. }, _)| {
// Careful: the regions for types that appear in the
// generator interior are not generally known, so we
// want to erase them when comparing (and anyway,
Expand All @@ -2479,19 +2479,21 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
);
eq
})
.map(|ty::GeneratorInteriorTypeCause { span, scope_span, .. }| {
(span, source_map.span_to_snippet(*span), scope_span)
.map(|(ty::GeneratorInteriorTypeCause { span, scope_span, .. }, expr)| {
(span, source_map.span_to_snippet(*span), scope_span, expr)
});

debug!(
"maybe_note_obligation_cause_for_async_await: target_ty={:?} \
generator_interior_types={:?} target_span={:?}",
target_ty, tables.generator_interior_types, target_span
);
if let Some((target_span, Ok(snippet), scope_span)) = target_span {
if let Some((target_span, Ok(snippet), scope_span, expr)) = target_span {
self.note_obligation_cause_for_async_await(
err,
*target_span,
scope_span,
*expr,
snippet,
generator_did,
last_generator,
Expand All @@ -2514,6 +2516,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
err: &mut DiagnosticBuilder<'_>,
target_span: Span,
scope_span: &Option<Span>,
expr: Option<hir::HirId>,
snippet: String,
first_generator: DefId,
last_generator: Option<DefId>,
Expand Down Expand Up @@ -2549,6 +2552,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// not implemented.
let is_send = self.tcx.is_diagnostic_item(sym::send_trait, trait_ref.def_id);
let is_sync = self.tcx.is_diagnostic_item(sym::sync_trait, trait_ref.def_id);
let hir = self.tcx.hir();
let trait_explanation = if is_send || is_sync {
let (trait_name, trait_verb) =
if is_send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
Expand All @@ -2564,8 +2568,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

let message = if let Some(name) = last_generator
.and_then(|generator_did| self.tcx.parent(generator_did))
.and_then(|parent_did| self.tcx.hir().as_local_hir_id(parent_did))
.and_then(|parent_hir_id| self.tcx.hir().opt_name(parent_hir_id))
.and_then(|parent_did| hir.as_local_hir_id(parent_did))
.and_then(|parent_hir_id| hir.opt_name(parent_hir_id))
{
format!("future returned by `{}` is not {}", name, trait_name)
} else {
Expand All @@ -2581,7 +2585,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
};

// Look at the last interior type to get a span for the `.await`.
let await_span = tables.generator_interior_types.iter().map(|i| i.span).last().unwrap();
let await_span =
tables.generator_interior_types.iter().map(|(i, _)| i.span).last().unwrap();
let mut span = MultiSpan::from_span(await_span);
span.push_span_label(
await_span,
Expand All @@ -2606,6 +2611,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
),
);

if let Some(expr_id) = expr {
let expr = hir.expect_expr(expr_id);
let is_ref = tables.expr_adjustments(expr).iter().any(|adj| adj.is_region_borrow());
let parent = hir.get_parent_node(expr_id);
if let Some(hir::Node::Expr(e)) = hir.find(parent) {
let method_span = hir.span(parent);
if tables.is_method_call(e) && is_ref {
err.span_help(
method_span,
"consider moving this method call into a `let` \
binding to create a shorter lived borrow",
);
}
}
}

// Add a note for the item obligation that remains - normally a note pointing to the
// bound that introduced the obligation (e.g. `T: Send`).
debug!("note_obligation_cause_for_async_await: next_code={:?}", next_code);
Expand Down
9 changes: 9 additions & 0 deletions src/librustc/ty/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ pub struct Adjustment<'tcx> {
pub target: Ty<'tcx>,
}

impl Adjustment<'tcx> {
pub fn is_region_borrow(&self) -> bool {
match self.kind {
Adjust::Borrow(AutoBorrow::Ref(..)) => true,
_ => false,
}
}
}

#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
pub enum Adjust<'tcx> {
/// Go from ! to any type.
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ pub struct TypeckTables<'tcx> {
/// entire variable.
pub upvar_list: ty::UpvarListMap,

/// Stores the type, span and optional scope span of all types
/// Stores the type, expression, span and optional scope span of all types
/// that are live across the yield of this generator (if a generator).
pub generator_interior_types: Vec<GeneratorInteriorTypeCause<'tcx>>,
pub generator_interior_types: Vec<(GeneratorInteriorTypeCause<'tcx>, Option<hir::HirId>)>,
}

impl<'tcx> TypeckTables<'tcx> {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_typeck/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_span::Span;
struct InteriorVisitor<'a, 'tcx> {
fcx: &'a FnCtxt<'a, 'tcx>,
types: FxHashMap<ty::GeneratorInteriorTypeCause<'tcx>, usize>,
exprs: Vec<Option<hir::HirId>>,
region_scope_tree: &'tcx region::ScopeTree,
expr_count: usize,
kind: hir::GeneratorKind,
Expand Down Expand Up @@ -99,6 +100,7 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
scope_span,
})
.or_insert(entries);
self.exprs.push(expr.map(|e| e.hir_id));
}
} else {
debug!(
Expand Down Expand Up @@ -136,6 +138,7 @@ pub fn resolve_interior<'a, 'tcx>(
expr_count: 0,
kind,
prev_unresolved_span: None,
exprs: vec![],
};
intravisit::walk_body(&mut visitor, body);

Expand Down Expand Up @@ -170,7 +173,8 @@ pub fn resolve_interior<'a, 'tcx>(
});

// Store the generator types and spans into the tables for this generator.
let interior_types = types.iter().map(|t| t.0.clone()).collect::<Vec<_>>();
let interior_types =
types.iter().zip(visitor.exprs).map(|(t, e)| (t.0.clone(), e)).collect::<Vec<_>>();
visitor.fcx.inh.tables.borrow_mut().generator_interior_types = interior_types;

// Extract type components
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/async-await/issue-64130-4-async-move.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ LL | let _x = get().await;
...
LL | }
| - `client` is later dropped here
help: consider moving this method call into a `let` binding to create a shorter lived borrow
--> $DIR/issue-64130-4-async-move.rs:19:15
|
LL | match client.status() {
| ^^^^^^^^^^^^^^^
= note: the return type of a function must have a statically known size

error: aborting due to previous error
Expand Down