Skip to content

Commit 7614ea4

Browse files
authored
Rollup merge of #110210 - nnethercote:DescriptionCtx-cleanups, r=davidtwco
`DescriptionCtx` cleanups Best reviewed one commit at a time. r? `@davidtwco`
2 parents 4976926 + 76d0c6f commit 7614ea4

File tree

3 files changed

+79
-119
lines changed

3 files changed

+79
-119
lines changed

compiler/rustc_infer/messages.ftl

-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ infer_region_explanation = {$pref_kind ->
163163
[as_defined] the lifetime `{$desc_arg}` as defined here
164164
[as_defined_anon] the anonymous lifetime as defined here
165165
[defined_here] the anonymous lifetime defined here
166-
[anon_num_here] the anonymous lifetime #{$desc_num_arg} defined here
167166
[defined_here_reg] the lifetime `{$desc_arg}` as defined here
168167
}{$suff_kind ->
169168
*[should_not_happen] [{$suff_kind}]

compiler/rustc_infer/src/errors/note_and_explain.rs

+47-76
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use rustc_errors::{self, AddToDiagnostic, Diagnostic, IntoDiagnosticArg, Subdiag
44
use rustc_middle::ty::{self, TyCtxt};
55
use rustc_span::{symbol::kw, Span};
66

7-
#[derive(Default)]
87
struct DescriptionCtx<'a> {
98
span: Option<Span>,
109
kind: &'a str,
1110
arg: String,
12-
num_arg: u32,
1311
}
1412

1513
impl<'a> DescriptionCtx<'a> {
@@ -18,102 +16,74 @@ impl<'a> DescriptionCtx<'a> {
1816
region: ty::Region<'tcx>,
1917
alt_span: Option<Span>,
2018
) -> Option<Self> {
21-
let mut me = DescriptionCtx::default();
22-
me.span = alt_span;
23-
match *region {
24-
ty::ReEarlyBound(_) | ty::ReFree(_) => {
25-
return Self::from_early_bound_and_free_regions(tcx, region);
26-
}
27-
ty::ReStatic => {
28-
me.kind = "restatic";
29-
}
30-
31-
ty::RePlaceholder(_) => return None,
32-
33-
ty::ReError(_) => return None,
34-
35-
// FIXME(#13998) RePlaceholder should probably print like
36-
// ReFree rather than dumping Debug output on the user.
37-
//
38-
// We shouldn't really be having unification failures with ReVar
39-
// and ReLateBound though.
40-
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
41-
me.kind = "revar";
42-
me.arg = format!("{:?}", region);
43-
}
44-
};
45-
Some(me)
46-
}
47-
48-
fn from_early_bound_and_free_regions<'tcx>(
49-
tcx: TyCtxt<'tcx>,
50-
region: ty::Region<'tcx>,
51-
) -> Option<Self> {
52-
let mut me = DescriptionCtx::default();
53-
let scope = region.free_region_binding_scope(tcx).expect_local();
54-
match *region {
19+
let (span, kind, arg) = match *region {
5520
ty::ReEarlyBound(ref br) => {
56-
let mut sp = tcx.def_span(scope);
57-
if let Some(param) =
21+
let scope = region.free_region_binding_scope(tcx).expect_local();
22+
let span = if let Some(param) =
5823
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
5924
{
60-
sp = param.span;
61-
}
62-
if br.has_name() {
63-
me.kind = "as_defined";
64-
me.arg = br.name.to_string();
25+
param.span
6526
} else {
66-
me.kind = "as_defined_anon";
27+
tcx.def_span(scope)
6728
};
68-
me.span = Some(sp)
29+
if br.has_name() {
30+
(Some(span), "as_defined", br.name.to_string())
31+
} else {
32+
(Some(span), "as_defined_anon", String::new())
33+
}
6934
}
7035
ty::ReFree(ref fr) => {
7136
if !fr.bound_region.is_named()
7237
&& let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
7338
{
74-
me.kind = "defined_here";
75-
me.span = Some(ty.span);
39+
(Some(ty.span), "defined_here", String::new())
7640
} else {
41+
let scope = region.free_region_binding_scope(tcx).expect_local();
7742
match fr.bound_region {
7843
ty::BoundRegionKind::BrNamed(_, name) => {
79-
let mut sp = tcx.def_span(scope);
80-
if let Some(param) =
81-
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
44+
let span = if let Some(param) = tcx
45+
.hir()
46+
.get_generics(scope)
47+
.and_then(|generics| generics.get_named(name))
8248
{
83-
sp = param.span;
84-
}
85-
if name == kw::UnderscoreLifetime {
86-
me.kind = "as_defined_anon";
49+
param.span
8750
} else {
88-
me.kind = "as_defined";
89-
me.arg = name.to_string();
51+
tcx.def_span(scope)
9052
};
91-
me.span = Some(sp);
53+
if name == kw::UnderscoreLifetime {
54+
(Some(span), "as_defined_anon", String::new())
55+
} else {
56+
(Some(span), "as_defined", name.to_string())
57+
}
9258
}
9359
ty::BrAnon(span) => {
94-
me.kind = "defined_here";
95-
me.span = match span {
60+
let span = match span {
9661
Some(_) => span,
9762
None => Some(tcx.def_span(scope)),
98-
}
99-
},
63+
};
64+
(span, "defined_here", String::new())
65+
}
10066
_ => {
101-
me.kind = "defined_here_reg";
102-
me.arg = region.to_string();
103-
me.span = Some(tcx.def_span(scope));
104-
},
67+
(Some(tcx.def_span(scope)), "defined_here_reg", region.to_string())
68+
}
10569
}
10670
}
10771
}
108-
_ => bug!(),
109-
}
110-
Some(me)
111-
}
11272

113-
fn add_to(self, diag: &mut rustc_errors::Diagnostic) {
114-
diag.set_arg("desc_kind", self.kind);
115-
diag.set_arg("desc_arg", self.arg);
116-
diag.set_arg("desc_num_arg", self.num_arg);
73+
ty::ReStatic => (alt_span, "restatic", String::new()),
74+
75+
ty::RePlaceholder(_) | ty::ReError(_) => return None,
76+
77+
// FIXME(#13998) RePlaceholder should probably print like
78+
// ReFree rather than dumping Debug output on the user.
79+
//
80+
// We shouldn't really be having unification failures with ReVar
81+
// and ReLateBound though.
82+
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
83+
(alt_span, "revar", format!("{:?}", region))
84+
}
85+
};
86+
Some(DescriptionCtx { span, kind, arg })
11787
}
11888
}
11989

@@ -198,10 +168,11 @@ impl AddToDiagnostic for RegionExplanation<'_> {
198168
{
199169
diag.set_arg("pref_kind", self.prefix);
200170
diag.set_arg("suff_kind", self.suffix);
201-
let desc_span = self.desc.span;
202-
self.desc.add_to(diag);
171+
diag.set_arg("desc_kind", self.desc.kind);
172+
diag.set_arg("desc_arg", self.desc.arg);
173+
203174
let msg = f(diag, fluent::infer_region_explanation.into());
204-
if let Some(span) = desc_span {
175+
if let Some(span) = self.desc.span {
205176
diag.span_note(span, msg);
206177
} else {
207178
diag.note(msg);

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+32-42
Original file line numberDiff line numberDiff line change
@@ -184,84 +184,74 @@ fn msg_span_from_named_region<'tcx>(
184184
region: ty::Region<'tcx>,
185185
alt_span: Option<Span>,
186186
) -> (String, Option<Span>) {
187-
match *region {
188-
ty::ReEarlyBound(_) | ty::ReFree(_) => {
189-
let (msg, span) = msg_span_from_early_bound_and_free_regions(tcx, region);
190-
(msg, Some(span))
191-
}
192-
ty::ReStatic => ("the static lifetime".to_owned(), alt_span),
193-
ty::RePlaceholder(ty::PlaceholderRegion {
194-
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrNamed(def_id, name), .. },
195-
..
196-
}) => (format!("the lifetime `{name}` as defined here"), Some(tcx.def_span(def_id))),
197-
ty::RePlaceholder(ty::PlaceholderRegion {
198-
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(Some(span)), .. },
199-
..
200-
}) => (format!("the anonymous lifetime defined here"), Some(span)),
201-
ty::RePlaceholder(ty::PlaceholderRegion {
202-
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(None), .. },
203-
..
204-
}) => (format!("an anonymous lifetime"), None),
205-
_ => bug!("{:?}", region),
206-
}
207-
}
208-
209-
fn msg_span_from_early_bound_and_free_regions<'tcx>(
210-
tcx: TyCtxt<'tcx>,
211-
region: ty::Region<'tcx>,
212-
) -> (String, Span) {
213-
let scope = region.free_region_binding_scope(tcx).expect_local();
214187
match *region {
215188
ty::ReEarlyBound(ref br) => {
216-
let mut sp = tcx.def_span(scope);
217-
if let Some(param) =
189+
let scope = region.free_region_binding_scope(tcx).expect_local();
190+
let span = if let Some(param) =
218191
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
219192
{
220-
sp = param.span;
221-
}
193+
param.span
194+
} else {
195+
tcx.def_span(scope)
196+
};
222197
let text = if br.has_name() {
223198
format!("the lifetime `{}` as defined here", br.name)
224199
} else {
225200
"the anonymous lifetime as defined here".to_string()
226201
};
227-
(text, sp)
202+
(text, Some(span))
228203
}
229204
ty::ReFree(ref fr) => {
230205
if !fr.bound_region.is_named()
231206
&& let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
232207
{
233-
("the anonymous lifetime defined here".to_string(), ty.span)
208+
("the anonymous lifetime defined here".to_string(), Some(ty.span))
234209
} else {
210+
let scope = region.free_region_binding_scope(tcx).expect_local();
235211
match fr.bound_region {
236212
ty::BoundRegionKind::BrNamed(_, name) => {
237-
let mut sp = tcx.def_span(scope);
238-
if let Some(param) =
213+
let span = if let Some(param) =
239214
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
240215
{
241-
sp = param.span;
242-
}
216+
param.span
217+
} else {
218+
tcx.def_span(scope)
219+
};
243220
let text = if name == kw::UnderscoreLifetime {
244221
"the anonymous lifetime as defined here".to_string()
245222
} else {
246223
format!("the lifetime `{}` as defined here", name)
247224
};
248-
(text, sp)
225+
(text, Some(span))
249226
}
250227
ty::BrAnon(span) => (
251228
"the anonymous lifetime as defined here".to_string(),
252-
match span {
229+
Some(match span {
253230
Some(span) => span,
254231
None => tcx.def_span(scope)
255-
}
232+
})
256233
),
257234
_ => (
258235
format!("the lifetime `{}` as defined here", region),
259-
tcx.def_span(scope),
236+
Some(tcx.def_span(scope)),
260237
),
261238
}
262239
}
263240
}
264-
_ => bug!(),
241+
ty::ReStatic => ("the static lifetime".to_owned(), alt_span),
242+
ty::RePlaceholder(ty::PlaceholderRegion {
243+
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrNamed(def_id, name), .. },
244+
..
245+
}) => (format!("the lifetime `{name}` as defined here"), Some(tcx.def_span(def_id))),
246+
ty::RePlaceholder(ty::PlaceholderRegion {
247+
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(Some(span)), .. },
248+
..
249+
}) => (format!("the anonymous lifetime defined here"), Some(span)),
250+
ty::RePlaceholder(ty::PlaceholderRegion {
251+
bound: ty::BoundRegion { kind: ty::BoundRegionKind::BrAnon(None), .. },
252+
..
253+
}) => (format!("an anonymous lifetime"), None),
254+
_ => bug!("{:?}", region),
265255
}
266256
}
267257

0 commit comments

Comments
 (0)