Skip to content

Commit 77924de

Browse files
committed
refactor highlighting to take any RegionKind, making it more general
1 parent 6cbbee1 commit 77924de

File tree

2 files changed

+160
-187
lines changed

2 files changed

+160
-187
lines changed

src/librustc/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 76 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
2424
cause,
2525
values: ValuePairs::TraitRefs(ExpectedFound { expected, found }),
2626
}),
27-
ty::RePlaceholder(sub_placeholder),
27+
sub_placeholder @ ty::RePlaceholder(_),
2828
_,
29-
ty::RePlaceholder(sup_placeholder),
29+
sup_placeholder @ ty::RePlaceholder(_),
3030
)) => if expected.def_id == found.def_id {
3131
return Some(self.try_report_placeholders_trait(
32-
Some(*vid),
32+
Some(self.tcx.mk_region(ty::ReVar(*vid))),
3333
cause,
34-
Some(*sub_placeholder),
35-
Some(*sup_placeholder),
34+
Some(sub_placeholder),
35+
Some(sup_placeholder),
3636
expected.def_id,
3737
expected.substs,
3838
found.substs,
@@ -48,14 +48,14 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
4848
cause,
4949
values: ValuePairs::TraitRefs(ExpectedFound { expected, found }),
5050
}),
51-
ty::RePlaceholder(sub_placeholder),
51+
sub_placeholder @ ty::RePlaceholder(_),
5252
_,
5353
_,
5454
)) => if expected.def_id == found.def_id {
5555
return Some(self.try_report_placeholders_trait(
56-
Some(*vid),
56+
Some(self.tcx.mk_region(ty::ReVar(*vid))),
5757
cause,
58-
Some(*sub_placeholder),
58+
Some(sub_placeholder),
5959
None,
6060
expected.def_id,
6161
expected.substs,
@@ -74,10 +74,10 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
7474
}),
7575
_,
7676
_,
77-
ty::RePlaceholder(sup_placeholder),
77+
sup_placeholder @ ty::RePlaceholder(_),
7878
)) => if expected.def_id == found.def_id {
7979
return Some(self.try_report_placeholders_trait(
80-
Some(*vid),
80+
Some(self.tcx.mk_region(ty::ReVar(*vid))),
8181
cause,
8282
None,
8383
Some(*sup_placeholder),
@@ -106,10 +106,10 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
106106
// = note: However, the type `T` only implements `...` for some specific lifetime `'2`.
107107
fn try_report_placeholders_trait(
108108
&self,
109-
vid: Option<ty::RegionVid>,
109+
vid: Option<ty::Region<'tcx>>,
110110
cause: &ObligationCause<'tcx>,
111-
sub_placeholder: Option<ty::PlaceholderRegion>,
112-
sup_placeholder: Option<ty::PlaceholderRegion>,
111+
sub_placeholder: Option<ty::Region<'tcx>>,
112+
sup_placeholder: Option<ty::Region<'tcx>>,
113113
trait_def_id: DefId,
114114
expected_substs: &'tcx Substs<'tcx>,
115115
actual_substs: &'tcx Substs<'tcx>,
@@ -152,120 +152,75 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
152152
let mut has_sup = None;
153153
let mut has_vid = None;
154154

155-
self.tcx
156-
.for_each_free_region(&expected_trait_ref, |r| match r {
157-
ty::RePlaceholder(p) => {
158-
if Some(*p) == sub_placeholder {
159-
if has_sub.is_none() {
160-
has_sub = Some(counter);
161-
counter += 1;
162-
}
163-
} else if Some(*p) == sup_placeholder {
164-
if has_sup.is_none() {
165-
has_sup = Some(counter);
166-
counter += 1;
167-
}
168-
}
169-
}
170-
_ => {}
171-
});
155+
self.tcx.for_each_free_region(&expected_trait_ref, |r| {
156+
if Some(r) == sub_placeholder && has_sub.is_none() {
157+
has_sub = Some(counter);
158+
counter += 1;
159+
} else if Some(r) == sup_placeholder && has_sup.is_none() {
160+
has_sup = Some(counter);
161+
counter += 1;
162+
}
163+
});
172164

173-
self.tcx
174-
.for_each_free_region(&actual_trait_ref, |r| match r {
175-
ty::ReVar(v) if Some(*v) == vid => {
176-
if has_vid.is_none() {
177-
has_vid = Some(counter);
178-
counter += 1;
165+
self.tcx.for_each_free_region(&actual_trait_ref, |r| {
166+
if Some(r) == vid && has_vid.is_none() {
167+
has_vid = Some(counter);
168+
counter += 1;
169+
}
170+
});
171+
172+
RegionHighlightMode::maybe_highlighting_region(sub_placeholder, has_sub, || {
173+
RegionHighlightMode::maybe_highlighting_region(sup_placeholder, has_sup, || {
174+
match (has_sub, has_sup) {
175+
(Some(n1), Some(n2)) => {
176+
err.note(&format!(
177+
"`{}` must implement `{}` \
178+
for any two lifetimes `'{}` and `'{}`",
179+
expected_trait_ref.self_ty(),
180+
expected_trait_ref,
181+
std::cmp::min(n1, n2),
182+
std::cmp::max(n1, n2),
183+
));
184+
}
185+
(Some(n), _) | (_, Some(n)) => {
186+
err.note(&format!(
187+
"`{}` must implement `{}` \
188+
for any lifetime `'{}`",
189+
expected_trait_ref.self_ty(),
190+
expected_trait_ref,
191+
n,
192+
));
193+
}
194+
(None, None) => {
195+
err.note(&format!(
196+
"`{}` must implement `{}`",
197+
expected_trait_ref.self_ty(),
198+
expected_trait_ref,
199+
));
179200
}
180201
}
181-
_ => {}
182-
});
183-
184-
maybe_highlight(
185-
sub_placeholder,
186-
has_sub,
187-
RegionHighlightMode::highlighting_placeholder,
188-
|| {
189-
maybe_highlight(
190-
sup_placeholder,
191-
has_sup,
192-
RegionHighlightMode::highlighting_placeholder,
193-
|| match (has_sub, has_sup) {
194-
(Some(n1), Some(n2)) => {
195-
err.note(&format!(
196-
"`{}` must implement `{}` \
197-
for any two lifetimes `'{}` and `'{}`",
198-
expected_trait_ref.self_ty(),
199-
expected_trait_ref,
200-
std::cmp::min(n1, n2),
201-
std::cmp::max(n1, n2),
202-
));
203-
}
204-
(Some(n), _) | (_, Some(n)) => {
205-
err.note(&format!(
206-
"`{}` must implement `{}` \
207-
for any lifetime `'{}`",
208-
expected_trait_ref.self_ty(),
209-
expected_trait_ref,
210-
n,
211-
));
212-
}
213-
(None, None) => {
214-
err.note(&format!(
215-
"`{}` must implement `{}`",
216-
expected_trait_ref.self_ty(),
217-
expected_trait_ref,
218-
));
219-
}
220-
},
221-
)
222-
},
223-
);
202+
})
203+
});
224204

225-
maybe_highlight(
226-
vid,
227-
has_vid,
228-
RegionHighlightMode::highlighting_region_vid,
229-
|| match has_vid {
230-
Some(n) => {
231-
err.note(&format!(
232-
"but `{}` only implements `{}` for some lifetime `'{}`",
233-
actual_trait_ref.self_ty(),
234-
actual_trait_ref,
235-
n
236-
));
237-
}
238-
None => {
239-
err.note(&format!(
240-
"but `{}` only implements `{}`",
241-
actual_trait_ref.self_ty(),
242-
actual_trait_ref,
243-
));
244-
}
245-
},
246-
);
205+
RegionHighlightMode::maybe_highlighting_region(vid, has_vid, || match has_vid {
206+
Some(n) => {
207+
err.note(&format!(
208+
"but `{}` only implements `{}` for some lifetime `'{}`",
209+
actual_trait_ref.self_ty(),
210+
actual_trait_ref,
211+
n
212+
));
213+
}
214+
None => {
215+
err.note(&format!(
216+
"but `{}` only implements `{}`",
217+
actual_trait_ref.self_ty(),
218+
actual_trait_ref,
219+
));
220+
}
221+
});
247222

248223
err.emit();
249224
ErrorReported
250225
}
251226
}
252-
253-
/// If both `thing` and `counter` are `Some`, invoke
254-
/// `highlighting_func` with their contents (and the `op`). Else just
255-
/// invoke `op`.
256-
fn maybe_highlight<T, F, R>(
257-
thing: Option<T>,
258-
counter: Option<usize>,
259-
highlighting_func: impl FnOnce(T, usize, F) -> R,
260-
op: F,
261-
) -> R
262-
where
263-
F: FnOnce() -> R,
264-
{
265-
if let Some(thing) = thing {
266-
if let Some(n) = counter {
267-
return highlighting_func(thing, n, op);
268-
}
269-
}
270-
op()
271-
}

0 commit comments

Comments
 (0)