Skip to content
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

DescriptionCtx cleanups #110210

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion compiler/rustc_infer/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ infer_region_explanation = {$pref_kind ->
[as_defined] the lifetime `{$desc_arg}` as defined here
[as_defined_anon] the anonymous lifetime as defined here
[defined_here] the anonymous lifetime defined here
[anon_num_here] the anonymous lifetime #{$desc_num_arg} defined here
[defined_here_reg] the lifetime `{$desc_arg}` as defined here
}{$suff_kind ->
*[should_not_happen] [{$suff_kind}]
Expand Down
123 changes: 47 additions & 76 deletions compiler/rustc_infer/src/errors/note_and_explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use rustc_errors::{self, AddToDiagnostic, Diagnostic, IntoDiagnosticArg, Subdiag
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::{symbol::kw, Span};

#[derive(Default)]
struct DescriptionCtx<'a> {
span: Option<Span>,
kind: &'a str,
arg: String,
num_arg: u32,
}

impl<'a> DescriptionCtx<'a> {
Expand All @@ -18,102 +16,74 @@ impl<'a> DescriptionCtx<'a> {
region: ty::Region<'tcx>,
alt_span: Option<Span>,
) -> Option<Self> {
let mut me = DescriptionCtx::default();
me.span = alt_span;
match *region {
ty::ReEarlyBound(_) | ty::ReFree(_) => {
return Self::from_early_bound_and_free_regions(tcx, region);
}
ty::ReStatic => {
me.kind = "restatic";
}

ty::RePlaceholder(_) => return None,

ty::ReError(_) => return None,

// FIXME(#13998) RePlaceholder should probably print like
// ReFree rather than dumping Debug output on the user.
//
// We shouldn't really be having unification failures with ReVar
// and ReLateBound though.
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
me.kind = "revar";
me.arg = format!("{:?}", region);
}
};
Some(me)
}

fn from_early_bound_and_free_regions<'tcx>(
tcx: TyCtxt<'tcx>,
region: ty::Region<'tcx>,
) -> Option<Self> {
let mut me = DescriptionCtx::default();
let scope = region.free_region_binding_scope(tcx).expect_local();
match *region {
let (span, kind, arg) = match *region {
ty::ReEarlyBound(ref br) => {
let mut sp = tcx.def_span(scope);
if let Some(param) =
let scope = region.free_region_binding_scope(tcx).expect_local();
let span = if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
sp = param.span;
}
if br.has_name() {
me.kind = "as_defined";
me.arg = br.name.to_string();
param.span
} else {
me.kind = "as_defined_anon";
tcx.def_span(scope)
};
me.span = Some(sp)
if br.has_name() {
(Some(span), "as_defined", br.name.to_string())
} else {
(Some(span), "as_defined_anon", String::new())
}
}
ty::ReFree(ref fr) => {
if !fr.bound_region.is_named()
&& let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
{
me.kind = "defined_here";
me.span = Some(ty.span);
(Some(ty.span), "defined_here", String::new())
} else {
let scope = region.free_region_binding_scope(tcx).expect_local();
match fr.bound_region {
ty::BoundRegionKind::BrNamed(_, name) => {
let mut sp = tcx.def_span(scope);
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
let span = if let Some(param) = tcx
.hir()
.get_generics(scope)
.and_then(|generics| generics.get_named(name))
{
sp = param.span;
}
if name == kw::UnderscoreLifetime {
me.kind = "as_defined_anon";
param.span
} else {
me.kind = "as_defined";
me.arg = name.to_string();
tcx.def_span(scope)
};
me.span = Some(sp);
if name == kw::UnderscoreLifetime {
(Some(span), "as_defined_anon", String::new())
} else {
(Some(span), "as_defined", name.to_string())
}
}
ty::BrAnon(span) => {
me.kind = "defined_here";
me.span = match span {
let span = match span {
Some(_) => span,
None => Some(tcx.def_span(scope)),
}
},
};
(span, "defined_here", String::new())
}
_ => {
me.kind = "defined_here_reg";
me.arg = region.to_string();
me.span = Some(tcx.def_span(scope));
},
(Some(tcx.def_span(scope)), "defined_here_reg", region.to_string())
}
}
}
}
_ => bug!(),
}
Some(me)
}

fn add_to(self, diag: &mut rustc_errors::Diagnostic) {
diag.set_arg("desc_kind", self.kind);
diag.set_arg("desc_arg", self.arg);
diag.set_arg("desc_num_arg", self.num_arg);
ty::ReStatic => (alt_span, "restatic", String::new()),

ty::RePlaceholder(_) | ty::ReError(_) => return None,

// FIXME(#13998) RePlaceholder should probably print like
// ReFree rather than dumping Debug output on the user.
//
// We shouldn't really be having unification failures with ReVar
// and ReLateBound though.
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
(alt_span, "revar", format!("{:?}", region))
}
};
Some(DescriptionCtx { span, kind, arg })
}
}

Expand Down Expand Up @@ -198,10 +168,11 @@ impl AddToDiagnostic for RegionExplanation<'_> {
{
diag.set_arg("pref_kind", self.prefix);
diag.set_arg("suff_kind", self.suffix);
let desc_span = self.desc.span;
self.desc.add_to(diag);
diag.set_arg("desc_kind", self.desc.kind);
diag.set_arg("desc_arg", self.desc.arg);

let msg = f(diag, fluent::infer_region_explanation.into());
if let Some(span) = desc_span {
if let Some(span) = self.desc.span {
diag.span_note(span, msg);
} else {
diag.note(msg);
Expand Down
74 changes: 32 additions & 42 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,84 +184,74 @@ fn msg_span_from_named_region<'tcx>(
region: ty::Region<'tcx>,
alt_span: Option<Span>,
) -> (String, Option<Span>) {
match *region {
ty::ReEarlyBound(_) | ty::ReFree(_) => {
let (msg, span) = msg_span_from_early_bound_and_free_regions(tcx, region);
(msg, Some(span))
}
ty::ReStatic => ("the static lifetime".to_owned(), alt_span),
ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrNamed(def_id, name), .. },
..
}) => (format!("the lifetime `{name}` as defined here"), Some(tcx.def_span(def_id))),
ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(Some(span)), .. },
..
}) => (format!("the anonymous lifetime defined here"), Some(span)),
ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(None), .. },
..
}) => (format!("an anonymous lifetime"), None),
_ => bug!("{:?}", region),
}
}

fn msg_span_from_early_bound_and_free_regions<'tcx>(
tcx: TyCtxt<'tcx>,
region: ty::Region<'tcx>,
) -> (String, Span) {
let scope = region.free_region_binding_scope(tcx).expect_local();
match *region {
ty::ReEarlyBound(ref br) => {
let mut sp = tcx.def_span(scope);
if let Some(param) =
let scope = region.free_region_binding_scope(tcx).expect_local();
let span = if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
sp = param.span;
}
param.span
} else {
tcx.def_span(scope)
};
let text = if br.has_name() {
format!("the lifetime `{}` as defined here", br.name)
} else {
"the anonymous lifetime as defined here".to_string()
};
(text, sp)
(text, Some(span))
}
ty::ReFree(ref fr) => {
if !fr.bound_region.is_named()
&& let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
{
("the anonymous lifetime defined here".to_string(), ty.span)
("the anonymous lifetime defined here".to_string(), Some(ty.span))
} else {
let scope = region.free_region_binding_scope(tcx).expect_local();
match fr.bound_region {
ty::BoundRegionKind::BrNamed(_, name) => {
let mut sp = tcx.def_span(scope);
if let Some(param) =
let span = if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
{
sp = param.span;
}
param.span
} else {
tcx.def_span(scope)
};
let text = if name == kw::UnderscoreLifetime {
"the anonymous lifetime as defined here".to_string()
} else {
format!("the lifetime `{}` as defined here", name)
};
(text, sp)
(text, Some(span))
}
ty::BrAnon(span) => (
"the anonymous lifetime as defined here".to_string(),
match span {
Some(match span {
Some(span) => span,
None => tcx.def_span(scope)
}
})
),
_ => (
format!("the lifetime `{}` as defined here", region),
tcx.def_span(scope),
Some(tcx.def_span(scope)),
),
}
}
}
_ => bug!(),
ty::ReStatic => ("the static lifetime".to_owned(), alt_span),
ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrNamed(def_id, name), .. },
..
}) => (format!("the lifetime `{name}` as defined here"), Some(tcx.def_span(def_id))),
ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(Some(span)), .. },
..
}) => (format!("the anonymous lifetime defined here"), Some(span)),
ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(None), .. },
..
}) => (format!("an anonymous lifetime"), None),
_ => bug!("{:?}", region),
}
}

Expand Down