Skip to content

Commit 0328df9

Browse files
amandasystemslcnr
andcommitted
Code review adjustments to naming, comments, docstrings
Co-authored-by: lcnr <rust@lcnr.de>
1 parent 6a325fd commit 0328df9

File tree

5 files changed

+39
-48
lines changed

5 files changed

+39
-48
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
6262
| ConstraintCategory::Boring
6363
| ConstraintCategory::BoringNoLocation
6464
| ConstraintCategory::Internal
65-
| ConstraintCategory::IllegalPlaceholder(..) => "",
65+
| ConstraintCategory::OutlivesUnnameablePlaceholder(..) => "",
6666
}
6767
}
6868
}

compiler/rustc_borrowck/src/handle_placeholders.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ impl RegionTracker {
163163

164164
/// Determine if the tracked universes of the two SCCs are compatible.
165165
pub(crate) fn universe_compatible_with(&self, other: Self) -> bool {
166+
// HACK: We first check whether we can name the highest existential universe
167+
// of `other`. This only exists to avoid errors in case that scc already
168+
// depends on a placeholder it cannot name itself.
166169
self.max_nameable_universe().can_name(other.max_nameable_universe())
167170
|| other.reachable_placeholders.can_be_named_by(self.max_nameable_universe())
168171
}
@@ -419,12 +422,12 @@ fn rewrite_placeholder_outlives<'tcx>(
419422
annotation.representative
420423
);
421424
// We only add one `r: 'static` constraint per SCC, where `r` is the SCC representative.
422-
// That constraint is annotated with some outlives relation `tries: unnameable` where
423-
// `unnameable` is unnameable from `tries` and there is a path in the constraint
424-
// graph between them.
425+
// That constraint is annotated with some outlives relation `lt: unnameable` where
426+
// `unnameable` is unnameable from `lt` and there is a path in the constraint graph
427+
// between them.
425428
//
426-
// We prefer the representative as `tries` in all cases but one: where the problem
427-
// is that the SCC has had its universe lowered to accomodate some other region and
429+
// We prefer the representative, `r`, as `lt` in all cases but one: where the problem
430+
// is that the SCC has had its universe lowered to accommodate some other region and
428431
// no longer can name its representative. In that case, we blame `r: low_u`, where `low_u`
429432
// cannot name `r` so that any explanation always starts with the SCC representative.
430433
let blame_to = if annotation.representative.rvid() == max_u_rvid {
@@ -455,7 +458,7 @@ fn rewrite_placeholder_outlives<'tcx>(
455458
outlives_constraints.push(OutlivesConstraint {
456459
sup: annotation.representative.rvid(),
457460
sub: fr_static,
458-
category: ConstraintCategory::IllegalPlaceholder(
461+
category: ConstraintCategory::OutlivesUnnameablePlaceholder(
459462
annotation.representative.rvid(),
460463
blame_to,
461464
),

compiler/rustc_borrowck/src/region_infer/graphviz.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::ty::UniverseIndex;
1212
use super::*;
1313

1414
fn render_outlives_constraint(constraint: &OutlivesConstraint<'_>) -> String {
15-
if let ConstraintCategory::IllegalPlaceholder(from, to) = constraint.category {
15+
if let ConstraintCategory::OutlivesUnnameablePlaceholder(from, to) = constraint.category {
1616
return format!("b/c {from:?}: {to:?}");
1717
}
1818
match constraint.locations {

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,38 +1730,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17301730
None
17311731
}
17321732

1733-
/// Find a path of outlives constraints from `from` to `to`,
1734-
/// taking placeholder blame constraints into account, e.g.
1735-
/// if there is a relationship where `r1` reaches `r2` and
1736-
/// r2 has a larger universe or if r1 and r2 both come from
1737-
/// placeholder regions.
1738-
///
1739-
/// Returns the path. It panics if there is no such path.
1740-
fn path_to_modulo_placeholders(
1741-
&self,
1742-
from: RegionVid,
1743-
to: RegionVid,
1744-
) -> Vec<OutlivesConstraint<'tcx>> {
1745-
let path = self.constraint_path_between_regions(from, to).unwrap();
1746-
1747-
// If we are looking for a path to 'static, and we are passing
1748-
// through a constraint synthesised from an illegal placeholder
1749-
// relation, redirect the search to the placeholder to blame.
1750-
// FIXME: the performance of this is Not Great, and the logic
1751-
// should be folded into the search itself if possible.
1752-
for constraint in path.iter() {
1753-
let ConstraintCategory::IllegalPlaceholder(cl_fr, culprit) = constraint.category else {
1754-
continue;
1755-
};
1756-
1757-
debug!("{culprit:?} is the reason {from:?}: 'static!");
1758-
return self.constraint_path_to(cl_fr, |r| r == culprit, false).unwrap().0;
1759-
}
1760-
1761-
// No funny business; just return the path!
1762-
path
1763-
}
1764-
17651733
/// Finds some region R such that `fr1: R` and `R` is live at `location`.
17661734
#[instrument(skip(self), level = "trace", ret)]
17671735
pub(crate) fn find_sub_region_live_at(&self, fr1: RegionVid, location: Location) -> RegionVid {
@@ -1823,8 +1791,24 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18231791
to_region: RegionVid,
18241792
) -> (BlameConstraint<'tcx>, Vec<OutlivesConstraint<'tcx>>) {
18251793
assert!(from_region != to_region, "Trying to blame a region for itself!");
1826-
// Find all paths
1827-
let path = self.path_to_modulo_placeholders(from_region, to_region);
1794+
1795+
let path = self.constraint_path_between_regions(from_region, to_region).unwrap();
1796+
1797+
// If we are passing through a constraint added because `'lt: 'unnameable`,
1798+
// where cannot name `'unnameable`, redirect search towards `'unnameable`.
1799+
let path = if let Some((lt, unnameable)) = path.iter().find_map(|c| {
1800+
if let ConstraintCategory::OutlivesUnnameablePlaceholder(lt, unnameable) = c.category {
1801+
Some((lt, unnameable))
1802+
} else {
1803+
None
1804+
}
1805+
}) {
1806+
// This the `false` argument is what prevents circular reasoning here!
1807+
self.constraint_path_to(lt, |r| r == unnameable, false).unwrap().0
1808+
} else {
1809+
path
1810+
};
1811+
18281812
debug!(
18291813
"path={:#?}",
18301814
path.iter()
@@ -1965,7 +1949,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
19651949
// specific, and are not used for relations that would make sense to blame.
19661950
ConstraintCategory::BoringNoLocation => 6,
19671951
// Do not blame internal constraints.
1968-
ConstraintCategory::IllegalPlaceholder(_, _) => 7,
1952+
ConstraintCategory::OutlivesUnnameablePlaceholder(_, _) => 7,
19691953
ConstraintCategory::Internal => 8,
19701954
};
19711955

@@ -2003,7 +1987,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20031987
};
20041988

20051989
assert!(
2006-
!matches!(best_constraint.category, ConstraintCategory::IllegalPlaceholder(_, _)),
1990+
!matches!(
1991+
best_constraint.category,
1992+
ConstraintCategory::OutlivesUnnameablePlaceholder(_, _)
1993+
),
20071994
"Illegal placeholder constraint blamed; should have redirected to other region relation"
20081995
);
20091996

compiler/rustc_middle/src/mir/query.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ pub enum ConstraintCategory<'tcx> {
149149
/// A constraint that doesn't correspond to anything the user sees.
150150
Internal,
151151

152-
/// An internal constraint derived from an illegal placeholder relation
153-
/// to this region. The arguments are a source -> drain of a path
154-
/// that caused the problem, used when reporting errors.
155-
IllegalPlaceholder(
152+
/// An internal constraint added when a region outlives a placeholder
153+
/// it cannot name and therefore has to outlive `'static`. The arguments
154+
/// are a source -> drain of a path that caused the problem and always
155+
/// leads to `'static`.
156+
OutlivesUnnameablePlaceholder(
156157
#[type_foldable(identity)]
157158
#[type_visitable(ignore)]
158159
ty::RegionVid,

0 commit comments

Comments
 (0)