2022-08-26 10:32:59 +08:00
|
|
|
#![deny(rustc::untranslatable_diagnostic)]
|
|
|
|
#![deny(rustc::diagnostic_outside_of_impl)]
|
2019-11-29 19:48:29 -06:00
|
|
|
//! Error reporting machinery for lifetime errors.
|
|
|
|
|
2022-10-19 01:20:24 +00:00
|
|
|
use either::Either;
|
2022-07-08 18:06:18 +02:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2022-05-22 17:04:49 +02:00
|
|
|
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
|
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_hir::intravisit::Visitor;
|
|
|
|
use rustc_hir::{self as hir, Item, ItemKind, Node};
|
2020-01-06 23:21:41 +01:00
|
|
|
use rustc_infer::infer::{
|
2022-04-26 00:17:25 +02:00
|
|
|
error_reporting::nice_region_error::{
|
|
|
|
self, find_anon_type, find_param_with_region, suggest_adding_lifetime_params,
|
2022-05-22 17:04:49 +02:00
|
|
|
HirTraitObjectVisitor, NiceRegionError, TraitObjectVisitor,
|
2022-04-26 00:17:25 +02:00
|
|
|
},
|
2022-04-24 16:44:09 +02:00
|
|
|
error_reporting::unexpected_hidden_region_diagnostic,
|
|
|
|
NllRegionVariableOrigin, RelateParamBound,
|
2020-01-06 23:21:41 +01:00
|
|
|
};
|
2022-02-14 16:04:21 -06:00
|
|
|
use rustc_middle::hir::place::PlaceBase;
|
2022-10-19 01:20:24 +00:00
|
|
|
use rustc_middle::mir::{ConstraintCategory, ReturnConstraint, TerminatorKind};
|
2022-04-24 16:44:09 +02:00
|
|
|
use rustc_middle::ty::subst::InternalSubsts;
|
2022-05-22 17:04:49 +02:00
|
|
|
use rustc_middle::ty::Region;
|
|
|
|
use rustc_middle::ty::TypeVisitor;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::{self, RegionVid, Ty};
|
2022-07-17 04:09:20 +09:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident};
|
2022-04-24 16:44:09 +02:00
|
|
|
use rustc_span::Span;
|
2018-06-20 21:34:34 +01:00
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::borrowck_errors;
|
2022-08-26 10:32:59 +08:00
|
|
|
use crate::session_diagnostics::{
|
|
|
|
FnMutError, FnMutReturnTypeErr, GenericDoesNotLiveLongEnough, LifetimeOutliveErr,
|
|
|
|
LifetimeReturnCategoryErr, RequireStaticErr, VarHereDenote,
|
|
|
|
};
|
2019-11-29 19:48:29 -06:00
|
|
|
|
2021-08-28 18:45:37 -05:00
|
|
|
use super::{OutlivesSuggestionBuilder, RegionName};
|
2022-08-30 21:50:22 -04:00
|
|
|
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::{
|
2019-12-28 19:09:42 -06:00
|
|
|
nll::ConstraintDescription,
|
2019-12-20 23:59:31 -06:00
|
|
|
region_infer::{values::RegionElement, TypeTest},
|
2019-12-28 19:09:42 -06:00
|
|
|
universal_regions::DefiningTy,
|
2019-12-20 23:59:31 -06:00
|
|
|
MirBorrowckCtxt,
|
2019-11-29 19:48:29 -06:00
|
|
|
};
|
|
|
|
|
2022-10-19 01:20:24 +00:00
|
|
|
impl ConstraintDescription for ConstraintCategory {
|
2018-10-03 21:33:22 +01:00
|
|
|
fn description(&self) -> &'static str {
|
2018-07-26 15:36:41 +02:00
|
|
|
// Must end with a space. Allows for empty names to be provided.
|
2018-06-26 23:00:24 +01:00
|
|
|
match self {
|
2018-10-03 21:33:22 +01:00
|
|
|
ConstraintCategory::Assignment => "assignment ",
|
2020-05-23 21:40:55 -04:00
|
|
|
ConstraintCategory::Return(_) => "returning this value ",
|
2018-12-03 14:11:53 +01:00
|
|
|
ConstraintCategory::Yield => "yielding this value ",
|
2018-10-13 14:23:23 +01:00
|
|
|
ConstraintCategory::UseAsConst => "using this value as a constant ",
|
|
|
|
ConstraintCategory::UseAsStatic => "using this value as a static ",
|
2018-10-03 21:33:22 +01:00
|
|
|
ConstraintCategory::Cast => "cast ",
|
2022-05-22 17:04:49 +02:00
|
|
|
ConstraintCategory::CallArgument(_) => "argument ",
|
2018-10-03 21:33:22 +01:00
|
|
|
ConstraintCategory::TypeAnnotation => "type annotation ",
|
|
|
|
ConstraintCategory::ClosureBounds => "closure body ",
|
|
|
|
ConstraintCategory::SizedBound => "proving this value is `Sized` ",
|
|
|
|
ConstraintCategory::CopyBound => "copying this value ",
|
|
|
|
ConstraintCategory::OpaqueType => "opaque type ",
|
2020-05-23 21:40:55 -04:00
|
|
|
ConstraintCategory::ClosureUpvar(_) => "closure capture ",
|
2021-09-06 16:59:24 -05:00
|
|
|
ConstraintCategory::Usage => "this usage ",
|
2021-09-27 10:45:34 -05:00
|
|
|
ConstraintCategory::Predicate(_)
|
2021-08-28 18:45:37 -05:00
|
|
|
| ConstraintCategory::Boring
|
2018-09-15 18:30:14 +01:00
|
|
|
| ConstraintCategory::BoringNoLocation
|
2018-10-03 21:33:22 +01:00
|
|
|
| ConstraintCategory::Internal => "",
|
2018-06-26 23:00:24 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 21:34:34 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:40:49 -06:00
|
|
|
/// A collection of errors encountered during region inference. This is needed to efficiently
|
|
|
|
/// report errors after borrow checking.
|
|
|
|
///
|
|
|
|
/// Usually we expect this to either be empty or contain a small number of items, so we can avoid
|
|
|
|
/// allocation most of the time.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) type RegionErrors<'tcx> = Vec<RegionErrorKind<'tcx>>;
|
2019-12-11 16:40:49 -06:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) enum RegionErrorKind<'tcx> {
|
2019-12-28 19:09:42 -06:00
|
|
|
/// A generic bound failure for a type test (`T: 'a`).
|
|
|
|
TypeTestError { type_test: TypeTest<'tcx> },
|
2019-12-11 16:40:49 -06:00
|
|
|
|
|
|
|
/// An unexpected hidden region for an opaque type.
|
|
|
|
UnexpectedHiddenRegion {
|
2020-01-11 14:12:39 +00:00
|
|
|
/// The span for the member constraint.
|
|
|
|
span: Span,
|
2019-12-11 16:40:49 -06:00
|
|
|
/// The hidden type.
|
|
|
|
hidden_ty: Ty<'tcx>,
|
2022-07-26 06:19:58 +00:00
|
|
|
/// The opaque type.
|
|
|
|
key: ty::OpaqueTypeKey<'tcx>,
|
2019-12-11 16:40:49 -06:00
|
|
|
/// The unexpected region.
|
|
|
|
member_region: ty::Region<'tcx>,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Higher-ranked subtyping error.
|
|
|
|
BoundUniversalRegionError {
|
|
|
|
/// The placeholder free region.
|
|
|
|
longer_fr: RegionVid,
|
2019-12-28 19:09:42 -06:00
|
|
|
/// The region element that erroneously must be outlived by `longer_fr`.
|
|
|
|
error_element: RegionElement,
|
2021-03-06 11:22:08 +00:00
|
|
|
/// The placeholder region.
|
|
|
|
placeholder: ty::PlaceholderRegion,
|
2019-12-11 16:40:49 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
/// Any other lifetime error.
|
|
|
|
RegionError {
|
|
|
|
/// The origin of the region.
|
2021-01-28 16:18:25 +09:00
|
|
|
fr_origin: NllRegionVariableOrigin,
|
2019-12-11 16:40:49 -06:00
|
|
|
/// The region that should outlive `shorter_fr`.
|
|
|
|
longer_fr: RegionVid,
|
|
|
|
/// The region that should be shorter, but we can't prove it.
|
|
|
|
shorter_fr: RegionVid,
|
|
|
|
/// Indicates whether this is a reported error. We currently only report the first error
|
|
|
|
/// encountered and leave the rest unreported so as not to overwhelm the user.
|
|
|
|
is_reported: bool,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-12 16:43:36 -05:00
|
|
|
/// Information about the various region constraints involved in a borrow checker error.
|
|
|
|
#[derive(Clone, Debug)]
|
2022-10-19 01:20:24 +00:00
|
|
|
pub struct ErrorConstraintInfo {
|
2019-09-12 16:43:36 -05:00
|
|
|
// fr: outlived_fr
|
2019-11-27 12:22:17 -06:00
|
|
|
pub(super) fr: RegionVid,
|
|
|
|
pub(super) fr_is_local: bool,
|
|
|
|
pub(super) outlived_fr: RegionVid,
|
|
|
|
pub(super) outlived_fr_is_local: bool,
|
2019-09-12 16:43:36 -05:00
|
|
|
|
|
|
|
// Category and span for best blame constraint
|
2022-10-19 01:20:24 +00:00
|
|
|
pub(super) category: ConstraintCategory,
|
2019-11-27 12:22:17 -06:00
|
|
|
pub(super) span: Span,
|
2019-09-12 16:43:36 -05:00
|
|
|
}
|
|
|
|
|
2019-12-20 23:59:31 -06:00
|
|
|
impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
2019-12-15 20:11:59 -06:00
|
|
|
/// Converts a region inference variable into a `ty::Region` that
|
|
|
|
/// we can use for error reporting. If `r` is universally bound,
|
|
|
|
/// then we use the name that we have on record for it. If `r` is
|
|
|
|
/// existentially bound, then we check its inferred value and try
|
|
|
|
/// to find a good name from that. Returns `None` if we can't find
|
|
|
|
/// one (e.g., this is just some random part of the CFG).
|
2019-12-28 19:28:50 -06:00
|
|
|
pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
|
2019-12-28 20:02:20 -06:00
|
|
|
self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
|
2019-12-15 20:11:59 -06:00
|
|
|
}
|
|
|
|
|
2019-12-20 23:59:31 -06:00
|
|
|
/// Returns the `RegionVid` corresponding to the region returned by
|
2019-12-15 20:11:59 -06:00
|
|
|
/// `to_error_region`.
|
2019-12-20 23:59:31 -06:00
|
|
|
pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
|
2019-12-28 20:02:20 -06:00
|
|
|
if self.regioncx.universal_regions().is_universal_region(r) {
|
2019-12-15 20:11:59 -06:00
|
|
|
Some(r)
|
|
|
|
} else {
|
2020-06-27 13:54:56 -04:00
|
|
|
// We just want something nameable, even if it's not
|
|
|
|
// actually an upper bound.
|
|
|
|
let upper_bound = self.regioncx.approx_universal_upper_bound(r);
|
2019-12-28 19:35:18 -06:00
|
|
|
|
2019-12-28 20:02:20 -06:00
|
|
|
if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
|
2019-12-15 20:11:59 -06:00
|
|
|
self.to_error_region_vid(upper_bound)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 19:25:57 -06:00
|
|
|
/// Returns `true` if a closure is inferred to be an `FnMut` closure.
|
2019-12-20 23:59:31 -06:00
|
|
|
fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
|
2022-02-27 11:10:20 -03:00
|
|
|
if let Some(ty::ReFree(free_region)) = self.to_error_region(fr).as_deref()
|
|
|
|
&& let ty::BoundRegionKind::BrEnv = free_region.bound_region
|
|
|
|
&& let DefiningTy::Closure(_, substs) = self.regioncx.universal_regions().defining_ty
|
|
|
|
{
|
|
|
|
return substs.as_closure().kind() == ty::ClosureKind::FnMut;
|
2018-06-20 21:34:34 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 19:25:57 -06:00
|
|
|
false
|
2018-06-20 21:34:34 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 19:28:50 -06:00
|
|
|
/// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
|
2019-12-28 19:28:50 -06:00
|
|
|
// Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
|
|
|
|
// buffered in the `MirBorrowckCtxt`.
|
|
|
|
|
|
|
|
let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
|
|
|
|
|
|
|
|
for nll_error in nll_errors.into_iter() {
|
|
|
|
match nll_error {
|
|
|
|
RegionErrorKind::TypeTestError { type_test } => {
|
|
|
|
// Try to convert the lower-bound region into something named we can print for the user.
|
|
|
|
let lower_bound_region = self.to_error_region(type_test.lower_bound);
|
|
|
|
|
|
|
|
let type_test_span = type_test.locations.span(&self.body);
|
|
|
|
|
|
|
|
if let Some(lower_bound_region) = lower_bound_region {
|
2022-04-24 09:36:23 +02:00
|
|
|
let generic_ty = type_test.generic_kind.to_ty(self.infcx.tcx);
|
|
|
|
let origin = RelateParamBound(type_test_span, generic_ty, None);
|
2022-09-09 15:08:06 -05:00
|
|
|
self.buffer_error(self.infcx.err_ctxt().construct_generic_bound_failure(
|
2022-07-08 10:59:35 +02:00
|
|
|
self.body.source.def_id().expect_local(),
|
2022-02-06 12:15:39 -08:00
|
|
|
type_test_span,
|
2022-04-24 09:36:23 +02:00
|
|
|
Some(origin),
|
2022-02-06 12:15:39 -08:00
|
|
|
type_test.generic_kind,
|
|
|
|
lower_bound_region,
|
|
|
|
));
|
2019-12-28 19:28:50 -06:00
|
|
|
} else {
|
|
|
|
// FIXME. We should handle this case better. It
|
|
|
|
// indicates that we have e.g., some region variable
|
|
|
|
// whose value is like `'a+'b` where `'a` and `'b` are
|
2022-03-30 01:39:38 -04:00
|
|
|
// distinct unrelated universal regions that are not
|
2019-12-28 19:28:50 -06:00
|
|
|
// known to outlive one another. It'd be nice to have
|
|
|
|
// some examples where this arises to decide how best
|
|
|
|
// to report it; we could probably handle it by
|
|
|
|
// iterating over the universal regions and reporting
|
|
|
|
// an error that multiple bounds are required.
|
2022-06-22 21:43:01 -07:00
|
|
|
self.buffer_error(self.infcx.tcx.sess.create_err(
|
|
|
|
GenericDoesNotLiveLongEnough {
|
|
|
|
kind: type_test.generic_kind.to_string(),
|
|
|
|
span: type_test_span,
|
|
|
|
},
|
2022-02-06 12:15:39 -08:00
|
|
|
));
|
2019-12-28 19:28:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 06:19:58 +00:00
|
|
|
RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, key, member_region } => {
|
2020-01-11 14:12:39 +00:00
|
|
|
let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
|
2022-07-26 06:19:58 +00:00
|
|
|
let named_key = self.regioncx.name_regions(self.infcx.tcx, key);
|
2020-01-11 14:12:39 +00:00
|
|
|
let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
|
2022-02-06 12:15:39 -08:00
|
|
|
self.buffer_error(unexpected_hidden_region_diagnostic(
|
2019-12-28 19:28:50 -06:00
|
|
|
self.infcx.tcx,
|
2020-01-11 14:12:39 +00:00
|
|
|
span,
|
|
|
|
named_ty,
|
|
|
|
named_region,
|
2022-07-26 06:19:58 +00:00
|
|
|
named_key,
|
2022-02-06 12:15:39 -08:00
|
|
|
));
|
2019-12-28 19:28:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
RegionErrorKind::BoundUniversalRegionError {
|
|
|
|
longer_fr,
|
2021-03-06 11:22:08 +00:00
|
|
|
placeholder,
|
2019-12-28 19:28:50 -06:00
|
|
|
error_element,
|
|
|
|
} => {
|
2021-07-12 21:11:02 +02:00
|
|
|
let error_vid = self.regioncx.region_from_element(longer_fr, &error_element);
|
2019-12-28 19:28:50 -06:00
|
|
|
|
|
|
|
// Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
|
2021-08-28 18:45:37 -05:00
|
|
|
let (_, cause) = self.regioncx.find_outlives_blame_span(
|
2019-12-28 19:28:50 -06:00
|
|
|
longer_fr,
|
2021-03-06 11:22:08 +00:00
|
|
|
NllRegionVariableOrigin::Placeholder(placeholder),
|
|
|
|
error_vid,
|
2019-12-28 19:28:50 -06:00
|
|
|
);
|
|
|
|
|
2021-03-07 15:08:52 +00:00
|
|
|
let universe = placeholder.universe;
|
|
|
|
let universe_info = self.regioncx.universe_info(universe);
|
|
|
|
|
2021-08-28 18:45:37 -05:00
|
|
|
universe_info.report_error(self, placeholder, error_element, cause);
|
2019-12-28 19:28:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
|
|
|
|
if is_reported {
|
2020-01-01 14:35:08 -06:00
|
|
|
self.report_region_error(
|
2019-12-28 19:28:50 -06:00
|
|
|
longer_fr,
|
|
|
|
fr_origin,
|
|
|
|
shorter_fr,
|
|
|
|
&mut outlives_suggestion,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// We only report the first error, so as not to overwhelm the user. See
|
|
|
|
// `RegRegionErrorKind` docs.
|
|
|
|
//
|
|
|
|
// FIXME: currently we do nothing with these, but perhaps we can do better?
|
|
|
|
// FIXME: try collecting these constraints on the outlives suggestion
|
|
|
|
// builder. Does it make the suggestions any better?
|
|
|
|
debug!(
|
|
|
|
"Unreported region error: can't prove that {:?}: {:?}",
|
|
|
|
longer_fr, shorter_fr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit one outlives suggestions for each MIR def we borrowck
|
2019-12-28 20:36:42 -06:00
|
|
|
outlives_suggestion.add_suggestion(self);
|
2019-12-28 19:28:50 -06:00
|
|
|
}
|
|
|
|
|
2022-05-22 17:04:49 +02:00
|
|
|
fn get_impl_ident_and_self_ty_from_trait(
|
|
|
|
&self,
|
|
|
|
def_id: DefId,
|
|
|
|
trait_objects: &FxHashSet<DefId>,
|
|
|
|
) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
match tcx.hir().get_if_local(def_id) {
|
|
|
|
Some(Node::ImplItem(impl_item)) => {
|
2022-09-20 14:11:23 +09:00
|
|
|
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id()).def_id)
|
|
|
|
{
|
2022-05-22 17:04:49 +02:00
|
|
|
Some(Node::Item(Item {
|
|
|
|
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
|
|
|
|
..
|
|
|
|
})) => Some((impl_item.ident, self_ty)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Node::TraitItem(trait_item)) => {
|
|
|
|
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
|
2022-09-20 14:11:23 +09:00
|
|
|
match tcx.hir().find_by_def_id(trait_did.def_id) {
|
2022-05-22 17:04:49 +02:00
|
|
|
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
|
|
|
|
// The method being called is defined in the `trait`, but the `'static`
|
|
|
|
// obligation comes from the `impl`. Find that `impl` so that we can point
|
|
|
|
// at it in the suggestion.
|
|
|
|
let trait_did = trait_did.to_def_id();
|
|
|
|
match tcx
|
|
|
|
.hir()
|
|
|
|
.trait_impls(trait_did)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|&impl_did| {
|
|
|
|
match tcx.hir().get_if_local(impl_did.to_def_id()) {
|
|
|
|
Some(Node::Item(Item {
|
|
|
|
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
|
|
|
|
..
|
|
|
|
})) if trait_objects.iter().all(|did| {
|
|
|
|
// FIXME: we should check `self_ty` against the receiver
|
|
|
|
// type in the `UnifyReceiver` context, but for now, use
|
|
|
|
// this imperfect proxy. This will fail if there are
|
|
|
|
// multiple `impl`s for the same trait like
|
|
|
|
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
|
|
|
|
// In that case, only the first one will get suggestions.
|
|
|
|
let mut traits = vec![];
|
|
|
|
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
|
|
|
|
hir_v.visit_ty(self_ty);
|
|
|
|
!traits.is_empty()
|
|
|
|
}) =>
|
|
|
|
{
|
|
|
|
Some(self_ty)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
{
|
|
|
|
Some(self_ty) => Some((trait_item.ident, self_ty)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:34:34 +01:00
|
|
|
/// Report an error because the universal region `fr` was required to outlive
|
|
|
|
/// `outlived_fr` but it is not known to do so. For example:
|
|
|
|
///
|
2022-10-02 11:00:43 +09:00
|
|
|
/// ```compile_fail
|
2018-06-20 21:34:34 +01:00
|
|
|
/// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_region_error(
|
2019-12-20 23:59:31 -06:00
|
|
|
&mut self,
|
2018-06-20 21:34:34 +01:00
|
|
|
fr: RegionVid,
|
2021-01-28 16:18:25 +09:00
|
|
|
fr_origin: NllRegionVariableOrigin,
|
2018-06-20 21:34:34 +01:00
|
|
|
outlived_fr: RegionVid,
|
2019-12-20 23:59:31 -06:00
|
|
|
outlives_suggestion: &mut OutlivesSuggestionBuilder,
|
2019-12-20 23:59:31 -06:00
|
|
|
) {
|
2020-01-01 14:35:08 -06:00
|
|
|
debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
|
2018-06-20 21:34:34 +01:00
|
|
|
|
2022-08-30 21:50:22 -04:00
|
|
|
let (blame_constraint, extra_info) =
|
|
|
|
self.regioncx.best_blame_constraint(fr, fr_origin, |r| {
|
2019-12-28 20:02:20 -06:00
|
|
|
self.regioncx.provides_universal_region(r, fr, outlived_fr)
|
2022-08-30 21:50:22 -04:00
|
|
|
});
|
|
|
|
let BlameConstraint { category, cause, variance_info, .. } = blame_constraint;
|
2018-07-20 17:30:31 +01:00
|
|
|
|
2021-08-28 18:45:37 -05:00
|
|
|
debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
|
2022-05-22 17:04:49 +02:00
|
|
|
|
2018-07-23 19:46:42 +03:00
|
|
|
// Check if we can use one of the "nice region errors".
|
2018-09-05 23:26:50 +02:00
|
|
|
if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
|
2022-09-09 15:08:06 -05:00
|
|
|
let infer_err = self.infcx.err_ctxt();
|
|
|
|
let nice = NiceRegionError::new_from_span(&infer_err, cause.span, o, f);
|
2019-02-06 14:11:09 +01:00
|
|
|
if let Some(diag) = nice.try_report_from_nll() {
|
2022-02-06 12:15:39 -08:00
|
|
|
self.buffer_error(diag);
|
2019-12-20 23:59:31 -06:00
|
|
|
return;
|
2018-07-23 19:46:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 13:07:11 +02:00
|
|
|
let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
|
2019-12-28 20:02:20 -06:00
|
|
|
self.regioncx.universal_regions().is_local_free_region(fr),
|
|
|
|
self.regioncx.universal_regions().is_local_free_region(outlived_fr),
|
2018-07-23 13:07:11 +02:00
|
|
|
);
|
|
|
|
|
2018-10-24 08:51:39 -04:00
|
|
|
debug!(
|
2020-01-01 14:35:08 -06:00
|
|
|
"report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
|
2018-10-24 08:51:39 -04:00
|
|
|
fr_is_local, outlived_fr_is_local, category
|
|
|
|
);
|
2019-09-12 16:43:36 -05:00
|
|
|
|
|
|
|
let errci = ErrorConstraintInfo {
|
|
|
|
fr,
|
|
|
|
outlived_fr,
|
|
|
|
fr_is_local,
|
|
|
|
outlived_fr_is_local,
|
|
|
|
category,
|
2021-08-28 18:45:37 -05:00
|
|
|
span: cause.span,
|
2019-09-12 16:43:36 -05:00
|
|
|
};
|
|
|
|
|
2021-05-13 09:30:14 -04:00
|
|
|
let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
|
2020-05-23 21:40:55 -04:00
|
|
|
(ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
|
|
|
|
self.report_fnmut_error(&errci, kind)
|
2018-10-24 08:51:39 -04:00
|
|
|
}
|
|
|
|
(ConstraintCategory::Assignment, true, false)
|
2022-05-22 17:04:49 +02:00
|
|
|
| (ConstraintCategory::CallArgument(_), true, false) => {
|
2019-12-28 20:36:42 -06:00
|
|
|
let mut db = self.report_escaping_data_error(&errci);
|
2018-11-28 15:05:36 -06:00
|
|
|
|
2019-12-28 20:36:42 -06:00
|
|
|
outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
|
2018-11-28 15:05:36 -06:00
|
|
|
outlives_suggestion.collect_constraint(fr, outlived_fr);
|
|
|
|
|
|
|
|
db
|
|
|
|
}
|
|
|
|
_ => {
|
2019-12-28 20:36:42 -06:00
|
|
|
let mut db = self.report_general_error(&errci);
|
2018-11-28 15:05:36 -06:00
|
|
|
|
2019-12-28 20:36:42 -06:00
|
|
|
outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
|
2018-11-28 15:05:36 -06:00
|
|
|
outlives_suggestion.collect_constraint(fr, outlived_fr);
|
|
|
|
|
|
|
|
db
|
|
|
|
}
|
2019-12-20 23:59:31 -06:00
|
|
|
};
|
|
|
|
|
2021-05-13 09:30:14 -04:00
|
|
|
match variance_info {
|
|
|
|
ty::VarianceDiagInfo::None => {}
|
2021-09-27 20:22:44 -05:00
|
|
|
ty::VarianceDiagInfo::Invariant { ty, param_index } => {
|
|
|
|
let (desc, note) = match ty.kind() {
|
|
|
|
ty::RawPtr(ty_mut) => {
|
|
|
|
assert_eq!(ty_mut.mutbl, rustc_hir::Mutability::Mut);
|
|
|
|
(
|
2022-04-02 15:32:39 -07:00
|
|
|
format!("a mutable pointer to `{}`", ty_mut.ty),
|
2021-09-27 20:22:44 -05:00
|
|
|
"mutable pointers are invariant over their type parameter".to_string(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ty::Ref(_, inner_ty, mutbl) => {
|
|
|
|
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
|
|
|
|
(
|
2022-04-09 00:01:40 +09:00
|
|
|
format!("a mutable reference to `{inner_ty}`"),
|
2021-09-27 20:22:44 -05:00
|
|
|
"mutable references are invariant over their type parameter"
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ty::Adt(adt, substs) => {
|
|
|
|
let generic_arg = substs[param_index as usize];
|
|
|
|
let identity_substs =
|
2022-03-05 07:28:41 +11:00
|
|
|
InternalSubsts::identity_for_item(self.infcx.tcx, adt.did());
|
|
|
|
let base_ty = self.infcx.tcx.mk_adt(*adt, identity_substs);
|
2021-09-27 20:22:44 -05:00
|
|
|
let base_generic_arg = identity_substs[param_index as usize];
|
|
|
|
let adt_desc = adt.descr();
|
|
|
|
|
|
|
|
let desc = format!(
|
2022-04-02 15:32:39 -07:00
|
|
|
"the type `{ty}`, which makes the generic argument `{generic_arg}` invariant"
|
2021-09-27 20:22:44 -05:00
|
|
|
);
|
|
|
|
let note = format!(
|
2022-04-02 15:32:39 -07:00
|
|
|
"the {adt_desc} `{base_ty}` is invariant over the parameter `{base_generic_arg}`"
|
2021-09-27 20:22:44 -05:00
|
|
|
);
|
|
|
|
(desc, note)
|
|
|
|
}
|
2022-04-02 15:27:14 -07:00
|
|
|
ty::FnDef(def_id, _) => {
|
|
|
|
let name = self.infcx.tcx.item_name(*def_id);
|
|
|
|
let identity_substs =
|
|
|
|
InternalSubsts::identity_for_item(self.infcx.tcx, *def_id);
|
|
|
|
let desc = format!("a function pointer to `{name}`");
|
|
|
|
let note = format!(
|
|
|
|
"the function `{name}` is invariant over the parameter `{}`",
|
|
|
|
identity_substs[param_index as usize]
|
|
|
|
);
|
|
|
|
(desc, note)
|
|
|
|
}
|
2021-09-27 20:22:44 -05:00
|
|
|
_ => panic!("Unexpected type {:?}", ty),
|
2021-05-13 09:30:14 -04:00
|
|
|
};
|
2021-09-27 20:22:44 -05:00
|
|
|
diag.note(&format!("requirement occurs because of {desc}",));
|
|
|
|
diag.note(¬e);
|
2021-05-13 09:30:14 -04:00
|
|
|
diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 21:50:22 -04:00
|
|
|
for extra in extra_info {
|
|
|
|
match extra {
|
|
|
|
ExtraConstraintInfo::PlaceholderFromPredicate(span) => {
|
|
|
|
diag.span_note(span, format!("due to current limitations in the borrow checker, this implies a `'static` lifetime"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:15:39 -08:00
|
|
|
self.buffer_error(diag);
|
2018-07-20 17:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 01:50:23 +02:00
|
|
|
/// Report a specialized error when `FnMut` closures return a reference to a captured variable.
|
|
|
|
/// This function expects `fr` to be local and `outlived_fr` to not be local.
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// error: captured variable cannot escape `FnMut` closure body
|
|
|
|
/// --> $DIR/issue-53040.rs:15:8
|
|
|
|
/// |
|
|
|
|
/// LL | || &mut v;
|
|
|
|
/// | -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
|
|
|
|
/// | |
|
|
|
|
/// | inferred to be a `FnMut` closure
|
|
|
|
/// |
|
|
|
|
/// = note: `FnMut` closures only have access to their captured variables while they are
|
|
|
|
/// executing...
|
|
|
|
/// = note: ...therefore, returned references to captured variables will escape the closure
|
|
|
|
/// ```
|
2020-05-23 21:40:55 -04:00
|
|
|
fn report_fnmut_error(
|
|
|
|
&self,
|
2022-10-19 01:20:24 +00:00
|
|
|
errci: &ErrorConstraintInfo,
|
2020-05-23 21:40:55 -04:00
|
|
|
kind: ReturnConstraint,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
2019-09-12 16:43:36 -05:00
|
|
|
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
|
|
|
|
|
2020-05-23 21:40:55 -04:00
|
|
|
let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Opaque(def_id, _) = *output_ty.kind() {
|
2020-05-23 21:40:55 -04:00
|
|
|
output_ty = self.infcx.tcx.type_of(def_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("report_fnmut_error: output_ty={:?}", output_ty);
|
|
|
|
|
2022-08-26 10:32:59 +08:00
|
|
|
let err = FnMutError {
|
|
|
|
span: *span,
|
|
|
|
ty_err: match output_ty.kind() {
|
|
|
|
ty::Closure(_, _) => FnMutReturnTypeErr::ReturnClosure { span: *span },
|
|
|
|
ty::Adt(def, _)
|
|
|
|
if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did()) =>
|
|
|
|
{
|
|
|
|
FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
|
|
|
|
}
|
|
|
|
_ => FnMutReturnTypeErr::ReturnRef { span: *span },
|
|
|
|
},
|
2018-10-04 21:48:50 +02:00
|
|
|
};
|
|
|
|
|
2022-08-26 10:32:59 +08:00
|
|
|
let mut diag = self.infcx.tcx.sess.create_err(err);
|
2018-10-04 01:50:23 +02:00
|
|
|
|
2022-02-14 16:04:21 -06:00
|
|
|
if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
|
2020-05-23 21:40:55 -04:00
|
|
|
let def_id = match self.regioncx.universal_regions().defining_ty {
|
|
|
|
DefiningTy::Closure(def_id, _) => def_id,
|
2020-09-15 22:36:43 +02:00
|
|
|
ty => bug!("unexpected DefiningTy {:?}", ty),
|
2020-05-23 21:40:55 -04:00
|
|
|
};
|
|
|
|
|
2022-02-14 16:04:21 -06:00
|
|
|
let captured_place = &self.upvars[upvar_field.index()].place;
|
|
|
|
let defined_hir = match captured_place.place.base {
|
|
|
|
PlaceBase::Local(hirid) => Some(hirid),
|
|
|
|
PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2022-05-26 13:14:24 +02:00
|
|
|
if let Some(def_hir) = defined_hir {
|
2022-02-14 16:04:21 -06:00
|
|
|
let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
|
2022-05-26 13:14:24 +02:00
|
|
|
let upvar_def_span = self.infcx.tcx.hir().span(def_hir);
|
|
|
|
let upvar_span = upvars_map.get(&def_hir).unwrap().span;
|
2022-08-26 10:32:59 +08:00
|
|
|
diag.subdiagnostic(VarHereDenote::Defined { span: upvar_def_span });
|
|
|
|
diag.subdiagnostic(VarHereDenote::Captured { span: upvar_span });
|
2022-02-14 16:04:21 -06:00
|
|
|
}
|
2020-05-23 21:40:55 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 22:50:56 +01:00
|
|
|
if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
|
2022-08-26 10:32:59 +08:00
|
|
|
diag.subdiagnostic(VarHereDenote::FnMutInferred { span: fr_span });
|
2018-10-04 01:50:23 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 09:53:39 -07:00
|
|
|
self.suggest_move_on_borrowing_closure(&mut diag);
|
2018-10-04 01:50:23 +02:00
|
|
|
|
2019-09-12 16:43:36 -05:00
|
|
|
diag
|
2018-10-04 01:50:23 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 14:46:15 +02:00
|
|
|
/// Reports an error specifically for when data is escaping a closure.
|
2018-10-04 01:50:23 +02:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// error: borrowed data escapes outside of function
|
|
|
|
/// --> $DIR/lifetime-bound-will-change-warning.rs:44:5
|
|
|
|
/// |
|
|
|
|
/// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
|
|
|
|
/// | - `x` is a reference that is only valid in the function body
|
|
|
|
/// LL | // but ref_obj will not, so warn.
|
|
|
|
/// LL | ref_obj(x)
|
|
|
|
/// | ^^^^^^^^^^ `x` escapes the function body here
|
|
|
|
/// ```
|
2022-08-30 21:50:22 -04:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2022-01-27 09:44:25 +00:00
|
|
|
fn report_escaping_data_error(
|
|
|
|
&self,
|
2022-10-19 01:20:24 +00:00
|
|
|
errci: &ErrorConstraintInfo,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
2019-09-12 16:43:36 -05:00
|
|
|
let ErrorConstraintInfo { span, category, .. } = errci;
|
|
|
|
|
2019-12-28 20:02:20 -06:00
|
|
|
let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
|
2019-12-20 23:59:31 -06:00
|
|
|
self.infcx.tcx,
|
|
|
|
&self.body,
|
|
|
|
&self.local_names,
|
|
|
|
&self.upvars,
|
2019-12-20 23:59:31 -06:00
|
|
|
errci.fr,
|
|
|
|
);
|
2019-12-28 20:02:20 -06:00
|
|
|
let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
|
2019-12-20 23:59:31 -06:00
|
|
|
self.infcx.tcx,
|
|
|
|
&self.body,
|
|
|
|
&self.local_names,
|
|
|
|
&self.upvars,
|
2018-05-16 18:58:54 +03:00
|
|
|
errci.outlived_fr,
|
|
|
|
);
|
2018-07-21 15:50:06 +01:00
|
|
|
|
2020-01-25 19:09:23 -06:00
|
|
|
let (_, escapes_from) = self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
|
|
|
.article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
|
2018-07-24 16:22:41 +02:00
|
|
|
|
2018-09-15 18:28:52 +01:00
|
|
|
// Revert to the normal error in these cases.
|
|
|
|
// Assignments aren't "escapes" in function items.
|
|
|
|
if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
|
2019-12-30 19:46:30 -06:00
|
|
|
|| (*category == ConstraintCategory::Assignment
|
2020-01-25 19:09:23 -06:00
|
|
|
&& self.regioncx.universal_regions().defining_ty.is_fn_def())
|
|
|
|
|| self.regioncx.universal_regions().defining_ty.is_const()
|
2018-09-15 18:28:52 +01:00
|
|
|
{
|
2019-12-28 20:36:42 -06:00
|
|
|
return self.report_general_error(&ErrorConstraintInfo {
|
|
|
|
fr_is_local: true,
|
|
|
|
outlived_fr_is_local: false,
|
|
|
|
..*errci
|
|
|
|
});
|
2018-07-21 15:50:06 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut diag =
|
2019-12-20 23:59:31 -06:00
|
|
|
borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
|
2018-07-20 17:30:31 +01:00
|
|
|
|
2018-10-04 01:50:23 +02:00
|
|
|
if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
|
|
|
|
diag.span_label(
|
|
|
|
outlived_fr_span,
|
2022-04-09 00:01:40 +09:00
|
|
|
format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",),
|
2018-10-04 01:50:23 +02:00
|
|
|
);
|
2018-07-20 17:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 01:50:23 +02:00
|
|
|
if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
|
|
|
|
diag.span_label(
|
|
|
|
fr_span,
|
|
|
|
format!(
|
2022-04-09 00:01:40 +09:00
|
|
|
"`{fr_name}` is a reference that is only valid in the {escapes_from} body",
|
2018-10-04 01:50:23 +02:00
|
|
|
),
|
|
|
|
);
|
2018-07-20 17:30:31 +01:00
|
|
|
|
2022-04-09 00:01:40 +09:00
|
|
|
diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here"));
|
2018-07-20 17:30:31 +01:00
|
|
|
}
|
|
|
|
|
2021-10-03 15:25:26 -05:00
|
|
|
// Only show an extra note if we can find an 'error region' for both of the region
|
|
|
|
// variables. This avoids showing a noisy note that just mentions 'synthetic' regions
|
|
|
|
// that don't help the user understand the error.
|
2022-05-22 17:04:49 +02:00
|
|
|
match (self.to_error_region(errci.fr), self.to_error_region(errci.outlived_fr)) {
|
|
|
|
(Some(f), Some(o)) => {
|
|
|
|
self.maybe_suggest_constrain_dyn_trait_impl(&mut diag, f, o, category);
|
2021-10-03 15:25:26 -05:00
|
|
|
|
2022-05-22 17:04:49 +02:00
|
|
|
let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
|
|
|
|
fr_region_name.highlight_region_name(&mut diag);
|
|
|
|
let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
|
|
|
|
outlived_fr_region_name.highlight_region_name(&mut diag);
|
|
|
|
|
|
|
|
diag.span_label(
|
|
|
|
*span,
|
|
|
|
format!(
|
|
|
|
"{}requires that `{}` must outlive `{}`",
|
|
|
|
category.description(),
|
|
|
|
fr_region_name,
|
|
|
|
outlived_fr_region_name,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
2021-10-03 15:25:26 -05:00
|
|
|
}
|
2022-05-22 17:04:49 +02:00
|
|
|
|
2019-09-12 16:43:36 -05:00
|
|
|
diag
|
2018-07-20 17:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 01:50:23 +02:00
|
|
|
/// Reports a region inference error for the general case with named/synthesized lifetimes to
|
|
|
|
/// explain what is happening.
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// error: unsatisfied lifetime constraints
|
|
|
|
/// --> $DIR/regions-creating-enums3.rs:17:5
|
|
|
|
/// |
|
|
|
|
/// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
|
|
|
|
/// | -- -- lifetime `'b` defined here
|
|
|
|
/// | |
|
|
|
|
/// | lifetime `'a` defined here
|
|
|
|
/// LL | ast::add(x, y)
|
|
|
|
/// | ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
|
|
|
|
/// | is returning data with lifetime `'b`
|
|
|
|
/// ```
|
2022-01-27 09:44:25 +00:00
|
|
|
fn report_general_error(
|
|
|
|
&self,
|
2022-10-19 01:20:24 +00:00
|
|
|
errci: &ErrorConstraintInfo,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
2019-09-12 16:43:36 -05:00
|
|
|
let ErrorConstraintInfo {
|
|
|
|
fr,
|
|
|
|
fr_is_local,
|
|
|
|
outlived_fr,
|
|
|
|
outlived_fr_is_local,
|
|
|
|
span,
|
|
|
|
category,
|
|
|
|
..
|
|
|
|
} = errci;
|
|
|
|
|
2020-10-04 17:23:02 -07:00
|
|
|
let (_, mir_def_name) =
|
|
|
|
self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
|
2018-07-24 16:22:41 +02:00
|
|
|
|
2022-08-26 10:32:59 +08:00
|
|
|
let err = LifetimeOutliveErr { span: *span };
|
|
|
|
let mut diag = self.infcx.tcx.sess.create_err(err);
|
|
|
|
|
2019-12-28 20:36:42 -06:00
|
|
|
let fr_name = self.give_region_a_name(*fr).unwrap();
|
2019-09-12 16:43:36 -05:00
|
|
|
fr_name.highlight_region_name(&mut diag);
|
2019-12-28 20:36:42 -06:00
|
|
|
let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
|
2019-09-12 16:43:36 -05:00
|
|
|
outlived_fr_name.highlight_region_name(&mut diag);
|
|
|
|
|
2022-08-26 10:32:59 +08:00
|
|
|
let err_category = match (category, outlived_fr_is_local, fr_is_local) {
|
|
|
|
(ConstraintCategory::Return(_), true, _) => LifetimeReturnCategoryErr::WrongReturn {
|
|
|
|
span: *span,
|
|
|
|
mir_def_name,
|
|
|
|
outlived_fr_name,
|
|
|
|
fr_name: &fr_name,
|
|
|
|
},
|
|
|
|
_ => LifetimeReturnCategoryErr::ShortReturn {
|
|
|
|
span: *span,
|
|
|
|
category_desc: category.description(),
|
|
|
|
free_region_name: &fr_name,
|
|
|
|
outlived_fr_name,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
diag.subdiagnostic(err_category);
|
2018-07-03 11:38:09 -04:00
|
|
|
|
2019-12-20 23:59:31 -06:00
|
|
|
self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
|
2022-04-26 00:17:25 +02:00
|
|
|
self.suggest_adding_lifetime_params(&mut diag, *fr, *outlived_fr);
|
2022-07-22 09:53:39 -07:00
|
|
|
self.suggest_move_on_borrowing_closure(&mut diag);
|
2018-09-03 22:50:56 +02:00
|
|
|
|
2019-09-12 16:43:36 -05:00
|
|
|
diag
|
2018-09-03 22:50:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 14:46:15 +02:00
|
|
|
/// Adds a suggestion to errors where an `impl Trait` is returned.
|
2018-10-04 01:50:23 +02:00
|
|
|
///
|
|
|
|
/// ```text
|
2019-02-28 22:43:53 +00:00
|
|
|
/// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
|
2018-10-04 01:50:23 +02:00
|
|
|
/// a constraint
|
|
|
|
/// |
|
|
|
|
/// LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
|
|
|
|
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
/// ```
|
2018-09-03 22:50:56 +02:00
|
|
|
fn add_static_impl_trait_suggestion(
|
|
|
|
&self,
|
2022-01-23 20:41:46 +00:00
|
|
|
diag: &mut Diagnostic,
|
2018-09-05 23:26:50 +02:00
|
|
|
fr: RegionVid,
|
|
|
|
// We need to pass `fr_name` - computing it again will label it twice.
|
2018-09-03 22:50:56 +02:00
|
|
|
fr_name: RegionName,
|
2018-09-05 23:26:50 +02:00
|
|
|
outlived_fr: RegionVid,
|
2018-09-03 22:50:56 +02:00
|
|
|
) {
|
2022-04-24 16:44:09 +02:00
|
|
|
if let (Some(f), Some(outlived_f)) =
|
|
|
|
(self.to_error_region(fr), self.to_error_region(outlived_fr))
|
2018-10-24 08:51:39 -04:00
|
|
|
{
|
2022-04-24 16:44:09 +02:00
|
|
|
if *outlived_f != ty::ReStatic {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fn_returns = self
|
2019-12-20 23:59:31 -06:00
|
|
|
.infcx
|
2018-10-24 08:51:39 -04:00
|
|
|
.tcx
|
|
|
|
.is_suitable_region(f)
|
2022-04-24 16:44:09 +02:00
|
|
|
.map(|r| self.infcx.tcx.return_type_impl_or_dyn_traits(r.def_id))
|
|
|
|
.unwrap_or_default();
|
2018-09-04 18:56:14 +02:00
|
|
|
|
2022-04-24 16:44:09 +02:00
|
|
|
if fn_returns.is_empty() {
|
|
|
|
return;
|
2018-09-02 11:10:51 +02:00
|
|
|
}
|
2022-04-24 16:44:09 +02:00
|
|
|
|
|
|
|
let param = if let Some(param) = find_param_with_region(self.infcx.tcx, f, outlived_f) {
|
|
|
|
param
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2022-07-17 04:09:20 +09:00
|
|
|
let lifetime = if f.has_name() { fr_name.name } else { kw::UnderscoreLifetime };
|
2022-04-24 16:44:09 +02:00
|
|
|
|
|
|
|
let arg = match param.param.pat.simple_ident() {
|
|
|
|
Some(simple_ident) => format!("argument `{}`", simple_ident),
|
|
|
|
None => "the argument".to_string(),
|
|
|
|
};
|
|
|
|
let captures = format!("captures data from {}", arg);
|
|
|
|
|
|
|
|
return nice_region_error::suggest_new_region_bound(
|
|
|
|
self.infcx.tcx,
|
|
|
|
diag,
|
|
|
|
fn_returns,
|
2022-07-17 04:09:20 +09:00
|
|
|
lifetime.to_string(),
|
2022-04-24 16:44:09 +02:00
|
|
|
Some(arg),
|
|
|
|
captures,
|
|
|
|
Some((param.param_ty_span, param.param_ty.to_string())),
|
|
|
|
);
|
2018-09-02 11:10:51 +02:00
|
|
|
}
|
2018-06-20 21:34:34 +01:00
|
|
|
}
|
2022-04-26 00:17:25 +02:00
|
|
|
|
2022-05-22 17:04:49 +02:00
|
|
|
fn maybe_suggest_constrain_dyn_trait_impl(
|
|
|
|
&self,
|
2022-08-10 03:39:41 +00:00
|
|
|
diag: &mut Diagnostic,
|
2022-05-22 17:04:49 +02:00
|
|
|
f: Region<'tcx>,
|
|
|
|
o: Region<'tcx>,
|
2022-10-19 01:20:24 +00:00
|
|
|
category: &ConstraintCategory,
|
2022-05-22 17:04:49 +02:00
|
|
|
) {
|
|
|
|
if !o.is_static() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
|
2022-10-19 01:20:24 +00:00
|
|
|
let instance =
|
|
|
|
if let ConstraintCategory::CallArgument(location) = category
|
|
|
|
&& let Either::Right(term) = self.body.stmt_at(*location)
|
|
|
|
&& let TerminatorKind::Call { func, .. } = &term.kind
|
|
|
|
{
|
|
|
|
let func_ty = func.ty(self.body, tcx);
|
2022-05-25 23:32:27 +02:00
|
|
|
let (fn_did, substs) = match func_ty.kind() {
|
|
|
|
ty::FnDef(fn_did, substs) => (fn_did, substs),
|
|
|
|
_ => return,
|
|
|
|
};
|
2022-05-22 17:04:49 +02:00
|
|
|
debug!(?fn_did, ?substs);
|
|
|
|
|
|
|
|
// Only suggest this on function calls, not closures
|
|
|
|
let ty = tcx.type_of(fn_did);
|
|
|
|
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
|
|
|
|
if let ty::Closure(_, _) = ty.kind() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(Some(instance)) = ty::Instance::resolve(
|
|
|
|
tcx,
|
|
|
|
self.param_env,
|
|
|
|
*fn_did,
|
|
|
|
self.infcx.resolve_vars_if_possible(substs),
|
|
|
|
) {
|
|
|
|
instance
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let param = match find_param_with_region(tcx, f, o) {
|
|
|
|
Some(param) => param,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
debug!(?param);
|
|
|
|
|
|
|
|
let mut visitor = TraitObjectVisitor(FxHashSet::default());
|
|
|
|
visitor.visit_ty(param.param_ty);
|
|
|
|
|
2022-05-25 23:32:27 +02:00
|
|
|
let Some((ident, self_ty)) =
|
|
|
|
self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &visitor.0) else {return};
|
|
|
|
|
|
|
|
self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
|
2022-05-22 17:04:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(self, err), level = "debug")]
|
|
|
|
fn suggest_constrain_dyn_trait_in_impl(
|
|
|
|
&self,
|
|
|
|
err: &mut Diagnostic,
|
|
|
|
found_dids: &FxHashSet<DefId>,
|
|
|
|
ident: Ident,
|
|
|
|
self_ty: &hir::Ty<'_>,
|
|
|
|
) -> bool {
|
|
|
|
debug!("err: {:#?}", err);
|
|
|
|
let mut suggested = false;
|
|
|
|
for found_did in found_dids {
|
|
|
|
let mut traits = vec![];
|
|
|
|
let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
|
|
|
|
hir_v.visit_ty(&self_ty);
|
|
|
|
debug!("trait spans found: {:?}", traits);
|
|
|
|
for span in &traits {
|
|
|
|
let mut multi_span: MultiSpan = vec![*span].into();
|
2022-07-25 22:40:00 +09:00
|
|
|
multi_span
|
|
|
|
.push_span_label(*span, "this has an implicit `'static` lifetime requirement");
|
2022-05-22 17:04:49 +02:00
|
|
|
multi_span.push_span_label(
|
|
|
|
ident.span,
|
2022-07-25 22:40:00 +09:00
|
|
|
"calling this method introduces the `impl`'s 'static` requirement",
|
2022-05-22 17:04:49 +02:00
|
|
|
);
|
2022-08-26 10:32:59 +08:00
|
|
|
err.subdiagnostic(RequireStaticErr::UsedImpl { multi_span });
|
2022-05-22 17:04:49 +02:00
|
|
|
err.span_suggestion_verbose(
|
|
|
|
span.shrink_to_hi(),
|
|
|
|
"consider relaxing the implicit `'static` requirement",
|
2022-06-13 15:48:40 +09:00
|
|
|
" + '_",
|
2022-05-22 17:04:49 +02:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
suggested = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
suggested
|
|
|
|
}
|
|
|
|
|
2022-04-26 00:17:25 +02:00
|
|
|
fn suggest_adding_lifetime_params(
|
|
|
|
&self,
|
|
|
|
diag: &mut Diagnostic,
|
|
|
|
sub: RegionVid,
|
|
|
|
sup: RegionVid,
|
|
|
|
) {
|
|
|
|
let (Some(sub), Some(sup)) = (self.to_error_region(sub), self.to_error_region(sup)) else {
|
|
|
|
return
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some((ty_sub, _)) = self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
|
|
|
.is_suitable_region(sub)
|
|
|
|
.and_then(|anon_reg| find_anon_type(self.infcx.tcx, sub, &anon_reg.boundregion)) else {
|
|
|
|
return
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some((ty_sup, _)) = self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
|
|
|
.is_suitable_region(sup)
|
|
|
|
.and_then(|anon_reg| find_anon_type(self.infcx.tcx, sup, &anon_reg.boundregion)) else {
|
|
|
|
return
|
|
|
|
};
|
|
|
|
|
|
|
|
suggest_adding_lifetime_params(self.infcx.tcx, sub, ty_sup, ty_sub, diag);
|
|
|
|
}
|
2022-07-22 09:53:39 -07:00
|
|
|
|
|
|
|
fn suggest_move_on_borrowing_closure(&self, diag: &mut Diagnostic) {
|
|
|
|
let map = self.infcx.tcx.hir();
|
|
|
|
let body_id = map.body_owned_by(self.mir_def_id());
|
|
|
|
let expr = &map.body(body_id).value;
|
|
|
|
let mut closure_span = None::<rustc_span::Span>;
|
|
|
|
match expr.kind {
|
|
|
|
hir::ExprKind::MethodCall(.., args, _) => {
|
2022-09-01 13:27:31 +09:00
|
|
|
for arg in args {
|
2022-09-01 23:50:51 +09:00
|
|
|
if let hir::ExprKind::Closure(hir::Closure {
|
|
|
|
capture_clause: hir::CaptureBy::Ref,
|
|
|
|
..
|
2022-09-02 22:48:14 +09:00
|
|
|
}) = arg.kind
|
|
|
|
{
|
2022-09-01 13:27:31 +09:00
|
|
|
closure_span = Some(arg.span.shrink_to_lo());
|
2022-07-22 09:53:39 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::ExprKind::Block(blk, _) => {
|
|
|
|
if let Some(ref expr) = blk.expr {
|
|
|
|
// only when the block is a closure
|
2022-09-01 23:50:51 +09:00
|
|
|
if let hir::ExprKind::Closure(hir::Closure {
|
|
|
|
capture_clause: hir::CaptureBy::Ref,
|
|
|
|
..
|
|
|
|
}) = expr.kind
|
|
|
|
{
|
2022-07-22 09:53:39 -07:00
|
|
|
closure_span = Some(expr.span.shrink_to_lo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
if let Some(closure_span) = closure_span {
|
|
|
|
diag.span_suggestion_verbose(
|
|
|
|
closure_span,
|
2022-09-01 23:50:51 +09:00
|
|
|
"consider adding 'move' keyword before the nested closure",
|
2022-07-22 09:53:39 -07:00
|
|
|
"move ",
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 21:34:34 +01:00
|
|
|
}
|