about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-07-02 00:32:54 +0000
committerMichael Goulet <michael@errs.io>2025-07-02 00:37:40 +0000
commit91c53c97a990e9d1ab87b2d932362a53236fba49 (patch)
treea13a6929fcd784790af9b4308f5b9f944dc2c0dc
parentd3c0ef0c9fb000d09529e2ff71ed2e69e224a8d6 (diff)
downloadrust-91c53c97a990e9d1ab87b2d932362a53236fba49.tar.gz
rust-91c53c97a990e9d1ab87b2d932362a53236fba49.zip
Consider polarity in sizedness fast path
-rw-r--r--compiler/rustc_trait_selection/src/traits/util.rs7
-rw-r--r--tests/ui/traits/negative-bounds/negative-sized.rs8
-rw-r--r--tests/ui/traits/negative-bounds/negative-sized.stderr15
3 files changed, 27 insertions, 3 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs
index a05bae53566..141454bfe37 100644
--- a/compiler/rustc_trait_selection/src/traits/util.rs
+++ b/compiler/rustc_trait_selection/src/traits/util.rs
@@ -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;
         }
diff --git a/tests/ui/traits/negative-bounds/negative-sized.rs b/tests/ui/traits/negative-bounds/negative-sized.rs
new file mode 100644
index 00000000000..18369c78427
--- /dev/null
+++ b/tests/ui/traits/negative-bounds/negative-sized.rs
@@ -0,0 +1,8 @@
+#![feature(negative_bounds)]
+
+fn foo<T: !Sized>() {}
+
+fn main() {
+    foo::<()>();
+    //~^ ERROR the trait bound `(): !Sized` is not satisfied
+}
diff --git a/tests/ui/traits/negative-bounds/negative-sized.stderr b/tests/ui/traits/negative-bounds/negative-sized.stderr
new file mode 100644
index 00000000000..143933803b8
--- /dev/null
+++ b/tests/ui/traits/negative-bounds/negative-sized.stderr
@@ -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`.