Skip to content

Commit 1e96d7a

Browse files
Consider param-env for fast path
1 parent 0864097 commit 1e96d7a

14 files changed

+82
-72
lines changed

compiler/rustc_trait_selection/src/solve/delegate.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use rustc_infer::traits::solve::Goal;
1212
use rustc_middle::traits::query::NoSolution;
1313
use rustc_middle::traits::solve::Certainty;
1414
use rustc_middle::ty::{
15-
self, SizedTraitKind, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeVisitableExt as _, TypingMode,
15+
self, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeVisitableExt as _, TypingMode,
1616
};
1717
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
1818

19-
use crate::traits::{EvaluateConstErr, ObligationCause, specialization_graph};
19+
use crate::traits::{EvaluateConstErr, ObligationCause, sizedness_fast_path, specialization_graph};
2020

2121
#[repr(transparent)]
2222
pub struct SolverDelegate<'tcx>(InferCtxt<'tcx>);
@@ -76,19 +76,11 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
7676

7777
if trait_pred.polarity() == ty::PredicatePolarity::Positive {
7878
match self.0.tcx.as_lang_item(trait_pred.def_id()) {
79-
Some(LangItem::Sized)
80-
if self
81-
.resolve_vars_if_possible(trait_pred.self_ty().skip_binder())
82-
.has_trivial_sizedness(self.0.tcx, SizedTraitKind::Sized) =>
83-
{
84-
return Some(Certainty::Yes);
85-
}
86-
Some(LangItem::MetaSized)
87-
if self
88-
.resolve_vars_if_possible(trait_pred.self_ty().skip_binder())
89-
.has_trivial_sizedness(self.0.tcx, SizedTraitKind::MetaSized) =>
90-
{
91-
return Some(Certainty::Yes);
79+
Some(LangItem::Sized) | Some(LangItem::MetaSized) => {
80+
let predicate = self.resolve_vars_if_possible(goal.predicate);
81+
if sizedness_fast_path(self.tcx, predicate, goal.param_env) {
82+
return Some(Certainty::Yes);
83+
}
9284
}
9385
Some(LangItem::Copy | LangItem::Clone) => {
9486
let self_ty =

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
363363

364364
let infcx = self.selcx.infcx;
365365

366-
if sizedness_fast_path(infcx.tcx, obligation.predicate) {
366+
if sizedness_fast_path(infcx.tcx, obligation.predicate, obligation.param_env) {
367367
return ProcessResult::Changed(thin_vec![]);
368368
}
369369

compiler/rustc_trait_selection/src/traits/query/type_op/prove_predicate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ProvePredicate<'tcx> {
1515
tcx: TyCtxt<'tcx>,
1616
key: &ParamEnvAnd<'tcx, Self>,
1717
) -> Option<Self::QueryResponse> {
18-
if sizedness_fast_path(tcx, key.value.predicate) {
18+
if sizedness_fast_path(tcx, key.value.predicate, key.param_env) {
1919
return Some(());
2020
}
2121

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
597597
None => self.check_recursion_limit(&obligation, &obligation)?,
598598
}
599599

600-
if sizedness_fast_path(self.tcx(), obligation.predicate) {
600+
if sizedness_fast_path(self.tcx(), obligation.predicate, obligation.param_env) {
601601
return Ok(EvaluatedToOk);
602602
}
603603

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
365365
}
366366
}
367367

368-
pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>) -> bool {
368+
pub fn sizedness_fast_path<'tcx>(
369+
tcx: TyCtxt<'tcx>,
370+
predicate: ty::Predicate<'tcx>,
371+
param_env: ty::ParamEnv<'tcx>,
372+
) -> bool {
369373
// Proving `Sized`/`MetaSized`, very often on "obviously sized" types like
370374
// `&T`, accounts for about 60% percentage of the predicates we have to prove. No need to
371375
// canonicalize and all that for such cases.
@@ -390,6 +394,20 @@ pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
390394
debug!("fast path -- trivial sizedness");
391395
return true;
392396
}
397+
398+
if matches!(trait_pred.self_ty().kind(), ty::Param(_) | ty::Placeholder(_)) {
399+
for clause in param_env.caller_bounds() {
400+
if let ty::ClauseKind::Trait(clause_pred) = clause.kind().skip_binder()
401+
&& clause_pred.polarity == ty::PredicatePolarity::Positive
402+
&& clause_pred.self_ty() == trait_pred.self_ty()
403+
&& (clause_pred.def_id() == trait_pred.def_id()
404+
|| (sizedness == SizedTraitKind::MetaSized
405+
&& tcx.is_lang_item(clause_pred.def_id(), LangItem::Sized)))
406+
{
407+
return true;
408+
}
409+
}
410+
}
393411
}
394412

395413
false

compiler/rustc_traits/src/evaluate_obligation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn evaluate_obligation<'tcx>(
2424
debug!("evaluate_obligation: goal={:#?}", goal);
2525
let ParamEnvAnd { param_env, value: predicate } = goal;
2626

27-
if sizedness_fast_path(tcx, predicate) {
27+
if sizedness_fast_path(tcx, predicate, param_env) {
2828
return Ok(EvaluationResult::EvaluatedToOk);
2929
}
3030

tests/ui/sized-hierarchy/incomplete-inference-issue-143992.next.stderr renamed to tests/ui/sized-hierarchy/incomplete-inference-issue-143992.current_sized_hierarchy.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0308]: mismatched types
2-
--> $DIR/incomplete-inference-issue-143992.rs:27:28
2+
--> $DIR/incomplete-inference-issue-143992.rs:30:28
33
|
44
LL | let _x = T::Assoc::new(());
55
| ------------- ^^ expected `[u32; 1]`, found `()`
66
| |
77
| arguments to this function are incorrect
88
|
99
note: associated function defined here
10-
--> $DIR/incomplete-inference-issue-143992.rs:18:8
10+
--> $DIR/incomplete-inference-issue-143992.rs:21:8
1111
|
1212
LL | fn new(r: R) -> R {
1313
| ^^^ ----
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/incomplete-inference-issue-143992.rs:30:28
3+
|
4+
LL | let _x = T::Assoc::new(());
5+
| ------------- ^^ expected `[u32; 1]`, found `()`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
note: associated function defined here
10+
--> $DIR/incomplete-inference-issue-143992.rs:21:8
11+
|
12+
LL | fn new(r: R) -> R {
13+
| ^^^ ----
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0308`.

tests/ui/sized-hierarchy/incomplete-inference-issue-143992.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//@ compile-flags: --crate-type=lib
2-
//@ revisions: current next
2+
//@ revisions: current next current_sized_hierarchy next_sized_hierarchy
33
//@ ignore-compare-mode-next-solver (explicit revisions)
44
//@[current] check-pass
5+
//@[next] check-pass
56
//@[next] compile-flags: -Znext-solver
6-
//@[next] check-fail
7+
//@[next_sized_hierarchy] compile-flags: -Znext-solver
8+
9+
#![cfg_attr(any(current_sized_hierarchy, next_sized_hierarchy), feature(sized_hierarchy))]
710

811
// Test that we avoid incomplete inference when normalizing. Without this,
912
// `Trait`'s implicit `MetaSized` supertrait requires proving `T::Assoc<_>: MetaSized`
@@ -25,5 +28,6 @@ where
2528
T::Assoc<[u32; 1]>: Clone,
2629
{
2730
let _x = T::Assoc::new(());
28-
//[next]~^ ERROR mismatched types
31+
//[next_sized_hierarchy]~^ ERROR mismatched types
32+
//[current_sized_hierarchy]~^^ ERROR mismatched types
2933
}

tests/ui/traits/next-solver/cycles/normalizes-to-is-not-productive.stderr

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ LL | impl<T: Bound, U> Trait<U> for T {
1212
| ----- ^^^^^^^^ ^
1313
| |
1414
| unsatisfied trait bound introduced here
15-
note: required by a bound in `Bound`
16-
--> $DIR/normalizes-to-is-not-productive.rs:8:1
17-
|
18-
LL | / trait Bound {
19-
LL | | fn method();
20-
LL | | }
21-
| |_^ required by this bound in `Bound`
2215

2316
error[E0277]: the trait bound `Foo: Bound` is not satisfied
2417
--> $DIR/normalizes-to-is-not-productive.rs:47:19

0 commit comments

Comments
 (0)