Consider polarity in sizedness fast path

This commit is contained in:
Michael Goulet
2025-07-02 00:32:54 +00:00
parent d3c0ef0c9f
commit 91c53c97a9
3 changed files with 27 additions and 3 deletions

View File

@@ -368,16 +368,17 @@ pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
// Proving `Sized`/`MetaSized`, very often on "obviously sized" types like
// `&T`, accounts for about 60% percentage of the predicates we have to prove. No need to
// canonicalize and all that for such cases.
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_ref)) =
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
predicate.kind().skip_binder()
&& trait_pred.polarity == ty::PredicatePolarity::Positive
{
let sizedness = match tcx.as_lang_item(trait_ref.def_id()) {
let sizedness = match tcx.as_lang_item(trait_pred.def_id()) {
Some(LangItem::Sized) => SizedTraitKind::Sized,
Some(LangItem::MetaSized) => SizedTraitKind::MetaSized,
_ => return false,
};
if trait_ref.self_ty().has_trivial_sizedness(tcx, sizedness) {
if trait_pred.self_ty().has_trivial_sizedness(tcx, sizedness) {
debug!("fast path -- trivial sizedness");
return true;
}

View File

@@ -0,0 +1,8 @@
#![feature(negative_bounds)]
fn foo<T: !Sized>() {}
fn main() {
foo::<()>();
//~^ ERROR the trait bound `(): !Sized` is not satisfied
}

View File

@@ -0,0 +1,15 @@
error[E0277]: the trait bound `(): !Sized` is not satisfied
--> $DIR/negative-sized.rs:6:11
|
LL | foo::<()>();
| ^^ the trait bound `(): !Sized` is not satisfied
|
note: required by a bound in `foo`
--> $DIR/negative-sized.rs:3:11
|
LL | fn foo<T: !Sized>() {}
| ^^^^^^ required by this bound in `foo`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.