1
Fork 0
rust/compiler/rustc_infer/src/errors/note_and_explain.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

180 lines
6.1 KiB
Rust
Raw Normal View History

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;
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> {
let (span, kind, arg) = match *region {
ty::ReEarlyParam(ref br) => {
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))
{
param.span
2022-08-30 18:28:50 +03:00
} else {
tcx.def_span(scope)
2022-08-30 18:28:50 +03: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
}
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)
{
(Some(ty.span), "defined_here", String::new())
2022-08-30 18:28:50 +03:00
} else {
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) => {
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
{
param.span
2022-08-30 18:28:50 +03:00
} else {
tcx.def_span(scope)
2022-08-30 18:28:50 +03: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));
(span, "defined_here", String::new())
}
_ => (Some(tcx.def_span(scope)), "defined_here_reg", region.to_string()),
2022-08-30 18:28:50 +03:00
}
}
}
ty::ReStatic => (alt_span, "restatic", String::new()),
ty::RePlaceholder(_) | ty::ReError(_) => return None,
// FIXME(#13998) RePlaceholder should probably print like
// ReLateParam rather than dumping Debug output on the user.
//
// We shouldn't really be having unification failures with ReVar
// and ReBound though.
//
// FIXME(@lcnr): figure out why we have to handle `ReBound`
// here, this feels somewhat off.
ty::ReVar(_) | ty::ReBound(..) | ty::ReErased => {
(alt_span, "revar", format!("{region:?}"))
}
};
Some(DescriptionCtx { span, kind, arg })
2022-08-30 18:28:50 +03:00
}
}
pub enum PrefixKind {
Empty,
RefValidFor,
ContentValidFor,
2023-01-19 17:35:09 +03:00
TypeObjValidFor,
SourcePointerValidFor,
TypeSatisfy,
TypeOutlive,
2023-01-21 19:45:45 +03:00
LfParamInstantiatedWith,
LfParamMustOutlive,
LfInstantiatedWith,
LfMustOutlive,
2023-01-21 18:16:53 +03:00
PointerValidFor,
DataValidFor,
2022-08-30 18:28:50 +03:00
}
pub enum SuffixKind {
Empty,
2022-08-30 18:28:50 +03:00
Continues,
ReqByBinding,
2022-08-30 18:28:50 +03:00
}
impl IntoDiagnosticArg for PrefixKind {
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
let kind = match self {
Self::Empty => "empty",
Self::RefValidFor => "ref_valid_for",
Self::ContentValidFor => "content_valid_for",
2023-01-19 17:35:09 +03:00
Self::TypeObjValidFor => "type_obj_valid_for",
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",
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",
}
.into();
rustc_errors::DiagnosticArgValue::Str(kind)
2022-08-30 18:28:50 +03:00
}
}
impl IntoDiagnosticArg for SuffixKind {
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
let kind = match self {
Self::Empty => "empty",
Self::Continues => "continues",
Self::ReqByBinding => "req_by_binding",
}
.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 })
}
}
impl AddToDiagnostic for RegionExplanation<'_> {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
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);
let msg = f(diag, fluent::infer_region_explanation.into());
if let Some(span) = self.desc.span {
diag.span_note(span, msg);
} else {
diag.note(msg);
}
2022-08-30 18:28:50 +03:00
}
}