Skip to content

Commit 91c53c9

Browse files
Consider polarity in sizedness fast path
1 parent d3c0ef0 commit 91c53c9

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,17 @@ pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
368368
// Proving `Sized`/`MetaSized`, very often on "obviously sized" types like
369369
// `&T`, accounts for about 60% percentage of the predicates we have to prove. No need to
370370
// canonicalize and all that for such cases.
371-
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_ref)) =
371+
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
372372
predicate.kind().skip_binder()
373+
&& trait_pred.polarity == ty::PredicatePolarity::Positive
373374
{
374-
let sizedness = match tcx.as_lang_item(trait_ref.def_id()) {
375+
let sizedness = match tcx.as_lang_item(trait_pred.def_id()) {
375376
Some(LangItem::Sized) => SizedTraitKind::Sized,
376377
Some(LangItem::MetaSized) => SizedTraitKind::MetaSized,
377378
_ => return false,
378379
};
379380

380-
if trait_ref.self_ty().has_trivial_sizedness(tcx, sizedness) {
381+
if trait_pred.self_ty().has_trivial_sizedness(tcx, sizedness) {
381382
debug!("fast path -- trivial sizedness");
382383
return true;
383384
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(negative_bounds)]
2+
3+
fn foo<T: !Sized>() {}
4+
5+
fn main() {
6+
foo::<()>();
7+
//~^ ERROR the trait bound `(): !Sized` is not satisfied
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: the trait bound `(): !Sized` is not satisfied
2+
--> $DIR/negative-sized.rs:6:11
3+
|
4+
LL | foo::<()>();
5+
| ^^ the trait bound `(): !Sized` is not satisfied
6+
|
7+
note: required by a bound in `foo`
8+
--> $DIR/negative-sized.rs:3:11
9+
|
10+
LL | fn foo<T: !Sized>() {}
11+
| ^^^^^^ required by this bound in `foo`
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)