Skip to content

Commit

Permalink
Disable projection sub-obligation optimization in intercrate mode
Browse files Browse the repository at this point in the history
The meaning of an 'evaluation' in intercrate mode is more complicated
than with an normal `SelectionContext`. To avoid potential issues,
we now always preserve all projection sub-obligations when in
intercrate mode. This avoids needing to answer whether or not
`EvaluatedToOk` always means the same thing in intercrate mode
as it does normally.
  • Loading branch information
Aaron1011 committed Sep 15, 2021
1 parent c3c0f80 commit da0816c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
42 changes: 24 additions & 18 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,24 +944,30 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>(
Normalized { value: projected_ty, obligations: projected_obligations }
};

let mut canonical =
SelectionContext::with_query_mode(selcx.infcx(), TraitQueryMode::Canonical);
result.obligations.drain_filter(|projected_obligation| {
// If any global obligations always apply, considering regions, then we don't
// need to include them. The `is_global` check rules out inference variables,
// so there's no need for the caller of `opt_normalize_projection_type`
// to evaluate them.
// Note that we do *not* discard obligations that evaluate to
// `EvaluatedtoOkModuloRegions`. Evaluating these obligations
// inside of a query (e.g. `evaluate_obligation`) can change
// the result to `EvaluatedToOkModuloRegions`, while an
// `EvaluatedToOk` obligation will never change the result.
// See #85360 for more details
projected_obligation.is_global(canonical.tcx())
&& canonical
.evaluate_root_obligation(projected_obligation)
.map_or(false, |res| res.must_apply_considering_regions())
});
// In intercrate mode, things are more complicated, and the 'evaluation result'
// may not be the same as the evaluation result in a normal `SelectionContext`.
// To avoid having to deal with this, always keep all of the sub-obligations
// in intercrate mode.
if !selcx.is_intercrate() {
let mut canonical =
SelectionContext::with_query_mode(selcx.infcx(), TraitQueryMode::Canonical);
result.obligations.drain_filter(|projected_obligation| {
// If any global obligations always apply, considering regions, then we don't
// need to include them. The `is_global` check rules out inference variables,
// so there's no need for the caller of `opt_normalize_projection_type`
// to evaluate them.
// Note that we do *not* discard obligations that evaluate to
// `EvaluatedtoOkModuloRegions`. Evaluating these obligations
// inside of a query (e.g. `evaluate_obligation`) can change
// the result to `EvaluatedToOkModuloRegions`, while an
// `EvaluatedToOk` obligation will never change the result.
// See #85360 for more details
projected_obligation.is_global(canonical.tcx())
&& canonical
.evaluate_root_obligation(projected_obligation)
.map_or(false, |res| res.must_apply_considering_regions())
});
}

infcx.inner.borrow_mut().projection_cache().insert_ty(cache_key, result.clone());
obligations.extend(result.obligations);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
self.infcx.tcx
}

pub fn is_intercrate(&self) -> bool {
self.intercrate
}

/// Returns `true` if the trait predicate is considerd `const` to this selection context.
pub fn is_trait_predicate_const(&self, pred: ty::TraitPredicate<'_>) -> bool {
match pred.constness {
Expand Down

0 comments on commit da0816c

Please sign in to comment.