Add implicit sized bound to trait ascription types

This commit is contained in:
Michael Goulet 2025-07-18 16:47:39 +00:00
parent 82310651b9
commit 4cebbabd5c
3 changed files with 38 additions and 0 deletions

View file

@ -2489,6 +2489,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
}

View file

@ -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() {}

View file

@ -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`.