2022-10-13 10:13:02 +01:00
|
|
|
use crate::fluent_generated as fluent;
|
2022-08-30 18:28:50 +03:00
|
|
|
use crate::infer::error_reporting::nice_region_error::find_anon_type;
|
2023-11-10 10:11:24 +08:00
|
|
|
use rustc_errors::{AddToDiagnostic, Diagnostic, IntoDiagnosticArg, SubdiagnosticMessage};
|
2022-08-30 18:28:50 +03:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
|
|
|
use rustc_span::{symbol::kw, Span};
|
|
|
|
|
|
|
|
struct DescriptionCtx<'a> {
|
|
|
|
span: Option<Span>,
|
|
|
|
kind: &'a str,
|
|
|
|
arg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DescriptionCtx<'a> {
|
|
|
|
fn new<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
region: ty::Region<'tcx>,
|
|
|
|
alt_span: Option<Span>,
|
|
|
|
) -> Option<Self> {
|
2023-04-11 17:14:41 +10:00
|
|
|
let (span, kind, arg) = match *region {
|
2023-11-14 13:13:27 +00:00
|
|
|
ty::ReEarlyParam(ref br) => {
|
2023-04-11 17:14:41 +10:00
|
|
|
let scope = region.free_region_binding_scope(tcx).expect_local();
|
|
|
|
let span = if let Some(param) =
|
2022-08-30 18:28:50 +03:00
|
|
|
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
|
|
|
|
{
|
2023-04-11 17:14:41 +10:00
|
|
|
param.span
|
2022-08-30 18:28:50 +03:00
|
|
|
} else {
|
2023-04-11 17:14:41 +10:00
|
|
|
tcx.def_span(scope)
|
2022-08-30 18:28:50 +03:00
|
|
|
};
|
2023-04-11 17:14:41 +10:00
|
|
|
if br.has_name() {
|
|
|
|
(Some(span), "as_defined", br.name.to_string())
|
|
|
|
} else {
|
|
|
|
(Some(span), "as_defined_anon", String::new())
|
|
|
|
}
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
2023-11-14 13:13:27 +00:00
|
|
|
ty::ReLateParam(ref fr) => {
|
2022-08-30 18:28:50 +03:00
|
|
|
if !fr.bound_region.is_named()
|
|
|
|
&& let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
|
|
|
|
{
|
2023-04-11 17:14:41 +10:00
|
|
|
(Some(ty.span), "defined_here", String::new())
|
2022-08-30 18:28:50 +03:00
|
|
|
} else {
|
2023-04-11 17:14:41 +10:00
|
|
|
let scope = region.free_region_binding_scope(tcx).expect_local();
|
2022-08-30 18:28:50 +03:00
|
|
|
match fr.bound_region {
|
|
|
|
ty::BoundRegionKind::BrNamed(_, name) => {
|
2023-04-11 17:14:41 +10:00
|
|
|
let span = if let Some(param) = tcx
|
|
|
|
.hir()
|
|
|
|
.get_generics(scope)
|
|
|
|
.and_then(|generics| generics.get_named(name))
|
2022-08-30 18:28:50 +03:00
|
|
|
{
|
2023-04-11 17:14:41 +10:00
|
|
|
param.span
|
2022-08-30 18:28:50 +03:00
|
|
|
} else {
|
2023-04-11 17:14:41 +10:00
|
|
|
tcx.def_span(scope)
|
2022-08-30 18:28:50 +03:00
|
|
|
};
|
2023-04-11 17:14:41 +10:00
|
|
|
if name == kw::UnderscoreLifetime {
|
|
|
|
(Some(span), "as_defined_anon", String::new())
|
|
|
|
} else {
|
|
|
|
(Some(span), "as_defined", name.to_string())
|
|
|
|
}
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
2023-08-03 15:56:56 +00:00
|
|
|
ty::BrAnon => {
|
|
|
|
let span = Some(tcx.def_span(scope));
|
2023-04-11 17:14:41 +10:00
|
|
|
(span, "defined_here", String::new())
|
|
|
|
}
|
|
|
|
_ => (Some(tcx.def_span(scope)), "defined_here_reg", region.to_string()),
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-11 17:14:41 +10:00
|
|
|
|
|
|
|
ty::ReStatic => (alt_span, "restatic", String::new()),
|
|
|
|
|
|
|
|
ty::RePlaceholder(_) | ty::ReError(_) => return None,
|
|
|
|
|
|
|
|
// FIXME(#13998) RePlaceholder should probably print like
|
2023-11-14 13:13:27 +00:00
|
|
|
// ReLateParam rather than dumping Debug output on the user.
|
2023-04-11 17:14:41 +10:00
|
|
|
//
|
|
|
|
// We shouldn't really be having unification failures with ReVar
|
2023-11-13 14:00:05 +00:00
|
|
|
// and ReBound though.
|
|
|
|
//
|
2023-11-15 17:40:54 +00:00
|
|
|
// FIXME(@lcnr): figure out why we have to handle `ReBound`
|
2023-11-13 14:00:05 +00:00
|
|
|
// here, this feels somewhat off.
|
|
|
|
ty::ReVar(_) | ty::ReBound(..) | ty::ReErased => {
|
2023-07-25 23:04:01 +02:00
|
|
|
(alt_span, "revar", format!("{region:?}"))
|
2023-04-11 17:14:41 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(DescriptionCtx { span, kind, arg })
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum PrefixKind {
|
|
|
|
Empty,
|
2023-01-18 16:34:08 +03:00
|
|
|
RefValidFor,
|
|
|
|
ContentValidFor,
|
2023-01-19 17:35:09 +03:00
|
|
|
TypeObjValidFor,
|
2023-01-18 16:34:08 +03:00
|
|
|
SourcePointerValidFor,
|
|
|
|
TypeSatisfy,
|
|
|
|
TypeOutlive,
|
2023-01-21 19:45:45 +03:00
|
|
|
LfParamInstantiatedWith,
|
|
|
|
LfParamMustOutlive,
|
2023-01-18 16:34:08 +03:00
|
|
|
LfInstantiatedWith,
|
|
|
|
LfMustOutlive,
|
2023-01-21 18:16:53 +03:00
|
|
|
PointerValidFor,
|
|
|
|
DataValidFor,
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SuffixKind {
|
2023-01-18 16:34:08 +03:00
|
|
|
Empty,
|
2022-08-30 18:28:50 +03:00
|
|
|
Continues,
|
2023-01-18 16:34:08 +03:00
|
|
|
ReqByBinding,
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:05:00 +03:00
|
|
|
impl IntoDiagnosticArg for PrefixKind {
|
|
|
|
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
|
|
|
|
let kind = match self {
|
|
|
|
Self::Empty => "empty",
|
2023-01-18 16:34:08 +03:00
|
|
|
Self::RefValidFor => "ref_valid_for",
|
|
|
|
Self::ContentValidFor => "content_valid_for",
|
2023-01-19 17:35:09 +03:00
|
|
|
Self::TypeObjValidFor => "type_obj_valid_for",
|
2023-01-18 16:34:08 +03:00
|
|
|
Self::SourcePointerValidFor => "source_pointer_valid_for",
|
|
|
|
Self::TypeSatisfy => "type_satisfy",
|
|
|
|
Self::TypeOutlive => "type_outlive",
|
2023-01-21 19:45:45 +03:00
|
|
|
Self::LfParamInstantiatedWith => "lf_param_instantiated_with",
|
|
|
|
Self::LfParamMustOutlive => "lf_param_must_outlive",
|
2023-01-18 16:34:08 +03:00
|
|
|
Self::LfInstantiatedWith => "lf_instantiated_with",
|
|
|
|
Self::LfMustOutlive => "lf_must_outlive",
|
2023-01-21 18:16:53 +03:00
|
|
|
Self::PointerValidFor => "pointer_valid_for",
|
|
|
|
Self::DataValidFor => "data_valid_for",
|
2022-09-02 16:05:00 +03:00
|
|
|
}
|
|
|
|
.into();
|
|
|
|
rustc_errors::DiagnosticArgValue::Str(kind)
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:05:00 +03:00
|
|
|
impl IntoDiagnosticArg for SuffixKind {
|
|
|
|
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
|
|
|
|
let kind = match self {
|
2023-01-18 16:34:08 +03:00
|
|
|
Self::Empty => "empty",
|
2022-09-02 16:05:00 +03:00
|
|
|
Self::Continues => "continues",
|
2023-01-18 16:34:08 +03:00
|
|
|
Self::ReqByBinding => "req_by_binding",
|
2022-09-02 16:05:00 +03:00
|
|
|
}
|
|
|
|
.into();
|
|
|
|
rustc_errors::DiagnosticArgValue::Str(kind)
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RegionExplanation<'a> {
|
|
|
|
desc: DescriptionCtx<'a>,
|
|
|
|
prefix: PrefixKind,
|
|
|
|
suffix: SuffixKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RegionExplanation<'_> {
|
|
|
|
pub fn new<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
region: ty::Region<'tcx>,
|
|
|
|
alt_span: Option<Span>,
|
|
|
|
prefix: PrefixKind,
|
|
|
|
suffix: SuffixKind,
|
|
|
|
) -> Option<Self> {
|
|
|
|
Some(Self { desc: DescriptionCtx::new(tcx, region, alt_span)?, prefix, suffix })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 00:01:44 -04:00
|
|
|
impl AddToDiagnostic for RegionExplanation<'_> {
|
2023-01-18 16:34:08 +03:00
|
|
|
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
|
2022-10-03 14:09:05 +01:00
|
|
|
where
|
|
|
|
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
|
|
|
|
{
|
2023-12-24 09:08:41 +11:00
|
|
|
diag.arg("pref_kind", self.prefix);
|
|
|
|
diag.arg("suff_kind", self.suffix);
|
|
|
|
diag.arg("desc_kind", self.desc.kind);
|
|
|
|
diag.arg("desc_arg", self.desc.arg);
|
2023-04-11 17:29:51 +10:00
|
|
|
|
2023-01-18 16:34:08 +03:00
|
|
|
let msg = f(diag, fluent::infer_region_explanation.into());
|
2023-04-11 17:29:51 +10:00
|
|
|
if let Some(span) = self.desc.span {
|
2023-01-18 16:34:08 +03:00
|
|
|
diag.span_note(span, msg);
|
|
|
|
} else {
|
|
|
|
diag.note(msg);
|
|
|
|
}
|
2022-08-30 18:28:50 +03:00
|
|
|
}
|
|
|
|
}
|