about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-07-20 08:56:09 +0200
committerGitHub <noreply@github.com>2025-07-20 08:56:09 +0200
commit23c81a75e1c64d12ec9211522828402be03747eb (patch)
tree80641f596acc7c8c1776c6ba10dc9306b12bc8d2
parent9a927a2f03b4436fbc2371dfd50f9847420be905 (diff)
parent4cebbabd5ce3fdb1444cc7efdb8e07eff52b6652 (diff)
downloadrust-23c81a75e1c64d12ec9211522828402be03747eb.tar.gz
rust-23c81a75e1c64d12ec9211522828402be03747eb.zip
Rollup merge of #144142 - compiler-errors:itib, r=fmease
Add implicit sized bound to trait ascription types

r? ```@fmease``` or reassign

Thanks for catching this :)

Fixes rust-lang/rust#144135
-rw-r--r--compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs8
-rw-r--r--tests/ui/impl-trait/in-bindings/implicit-sized.rs19
-rw-r--r--tests/ui/impl-trait/in-bindings/implicit-sized.stderr11
3 files changed, 38 insertions, 0 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
index 78794cd8eb6..d7687998358 100644
--- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
+++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
@@ -2495,6 +2495,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
                     ty::List::empty(),
                     PredicateFilter::All,
                 );
+                self.add_sizedness_bounds(
+                    &mut bounds,
+                    self_ty,
+                    hir_bounds,
+                    None,
+                    None,
+                    hir_ty.span,
+                );
                 self.register_trait_ascription_bounds(bounds, hir_ty.hir_id, hir_ty.span);
                 self_ty
             }
diff --git a/tests/ui/impl-trait/in-bindings/implicit-sized.rs b/tests/ui/impl-trait/in-bindings/implicit-sized.rs
new file mode 100644
index 00000000000..2f16db94189
--- /dev/null
+++ b/tests/ui/impl-trait/in-bindings/implicit-sized.rs
@@ -0,0 +1,19 @@
+#![feature(impl_trait_in_bindings)]
+
+trait Trait {}
+impl<T: ?Sized> Trait for T {}
+
+fn doesnt_work() {
+    let x: &impl Trait = "hi";
+    //~^ ERROR the size for values of type `str` cannot be known at compilation time
+}
+
+fn works() {
+    let x: &(impl Trait + ?Sized) = "hi";
+    // No implicit sized.
+
+    let x: &impl Trait = &();
+    // Is actually sized.
+}
+
+fn main() {}
diff --git a/tests/ui/impl-trait/in-bindings/implicit-sized.stderr b/tests/ui/impl-trait/in-bindings/implicit-sized.stderr
new file mode 100644
index 00000000000..465a928cf86
--- /dev/null
+++ b/tests/ui/impl-trait/in-bindings/implicit-sized.stderr
@@ -0,0 +1,11 @@
+error[E0277]: the size for values of type `str` cannot be known at compilation time
+  --> $DIR/implicit-sized.rs:7:13
+   |
+LL |     let x: &impl Trait = "hi";
+   |             ^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `str`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0277`.