Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ab45ab8

Browse files
committed
review comments
* take diagnostic logic out of happy-path * sort/dedup once * add more comments
1 parent dd81e98 commit ab45ab8

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
150150
if mention_capture {
151151
spans.push(sup_origin.span());
152152
}
153-
spans.sort();
154-
spans.dedup();
153+
// We sort the spans *ignoring* expansion context. Below, the closure logic is repeated
154+
// because one method expects a closure taking `&Span` and the other `&mut Span`.
155+
spans.sort_by_key(|span| (span.lo(), span.hi()));
156+
spans.dedup_by_key(|span| (span.lo(), span.hi()));
155157

156158
// We try to make the output have fewer overlapping spans if possible.
157159
let (require_msg, require_span) = if sup_origin.span().overlaps(return_sp) {
@@ -165,8 +167,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
165167
}
166168

167169
if spans.iter().any(|sp| sp.overlaps(return_sp) || *sp > return_sp) {
170+
// If any of the "captured here" labels appears on the same line or after
171+
// `require_span`, we put it on a note to ensure the text flows by appearing
172+
// always at the end.
168173
err.span_note(require_span, require_msg);
169174
} else {
175+
// We don't need a note, it's already at the end, it can be shown as a `span_label`.
170176
err.span_label(require_span, require_msg);
171177
}
172178

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
146146
let graph = self.construct_graph();
147147
self.expand_givens(&graph);
148148
self.expansion(&mut var_data);
149-
let captures = self.collect_errors(&mut var_data, errors);
150-
self.collect_var_errors(&var_data, &graph, errors, captures);
149+
self.collect_errors(&mut var_data, errors);
150+
self.collect_var_errors(&var_data, &graph, errors);
151151
var_data
152152
}
153153

@@ -445,16 +445,9 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
445445
&self,
446446
var_data: &mut LexicalRegionResolutions<'tcx>,
447447
errors: &mut Vec<RegionResolutionError<'tcx>>,
448-
) -> Vec<Span> {
449-
let mut captures = vec![];
450-
448+
) {
451449
for (constraint, origin) in &self.data.constraints {
452450
debug!(?constraint, ?origin);
453-
if let (Constraint::VarSubVar(_, _), SubregionOrigin::DataBorrowed(_, sp)) =
454-
(constraint, origin)
455-
{
456-
captures.push(*sp);
457-
}
458451
match *constraint {
459452
Constraint::RegSubVar(..) | Constraint::VarSubVar(..) => {
460453
// Expansion will ensure that these constraints hold. Ignore.
@@ -524,7 +517,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
524517
sub,
525518
));
526519
}
527-
captures
528520
}
529521

530522
/// Go over the variables that were declared to be error variables
@@ -534,7 +526,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
534526
var_data: &LexicalRegionResolutions<'tcx>,
535527
graph: &RegionGraph<'tcx>,
536528
errors: &mut Vec<RegionResolutionError<'tcx>>,
537-
captures: Vec<Span>,
538529
) {
539530
debug!("collect_var_errors, var_data = {:#?}", var_data.values);
540531

@@ -578,12 +569,27 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
578569
// if this rule starts to create problems we'll
579570
// have to revisit this portion of the code and
580571
// think hard about it. =) -- nikomatsakis
572+
573+
// Obtain the spans for all the capture points for
574+
// richer diagnostics in `static_impl_trait`.
575+
let captures: Vec<Span> = self
576+
.data
577+
.constraints
578+
.iter()
579+
.filter_map(|(constraint, origin)| match (constraint, origin) {
580+
(Constraint::VarSubVar(_, _), SubregionOrigin::DataBorrowed(_, sp)) => {
581+
Some(*sp)
582+
}
583+
_ => None,
584+
})
585+
.collect();
586+
581587
self.collect_error_for_expanding_node(
582588
graph,
583589
&mut dup_vec,
584590
node_vid,
585591
errors,
586-
&captures,
592+
captures,
587593
);
588594
}
589595
}
@@ -638,7 +644,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
638644
dup_vec: &mut IndexVec<RegionVid, Option<RegionVid>>,
639645
node_idx: RegionVid,
640646
errors: &mut Vec<RegionResolutionError<'tcx>>,
641-
captures: &[Span],
647+
captures: Vec<Span>,
642648
) {
643649
// Errors in expanding nodes result from a lower-bound that is
644650
// not contained by an upper-bound.
@@ -686,18 +692,14 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
686692
origin, node_idx, lower_bound.region, upper_bound.region
687693
);
688694

689-
let mut capture_spans: Vec<Span> = captures.iter().cloned().collect();
690-
// Below, one span expects `&Span` and the other `&mut Span`, hence the dupes.
691-
capture_spans.sort_by_key(|span| (span.lo(), span.hi()));
692-
capture_spans.dedup_by_key(|span| (span.lo(), span.hi()));
693695
errors.push(RegionResolutionError::SubSupConflict(
694696
node_idx,
695697
origin,
696698
lower_bound.origin.clone(),
697699
lower_bound.region,
698700
upper_bound.origin.clone(),
699701
upper_bound.region,
700-
capture_spans,
702+
captures,
701703
));
702704
return;
703705
}

0 commit comments

Comments
 (0)