Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ?Sized bound to SourceCode to allow casting &str` into it. #414

Open
Diegovsky opened this issue Dec 7, 2024 · 6 comments
Open

Add ?Sized bound to SourceCode to allow casting &str` into it. #414

Diegovsky opened this issue Dec 7, 2024 · 6 comments

Comments

@Diegovsky
Copy link

Diegovsky commented Dec 7, 2024

I'm trying to cast a borrowed &str to a dyn SourceCode but I'm receiving the error:
error[E0277]: the size for values of type str cannot be known at compilation time
and
help: the trait `Sized` is not implemented for `str`
note: required for the cast from `&str` to `&dyn SourceCode`

Which leads me to Source Code's definition, where it is said to be implemented by str and &str (as expected), but it is not marked as ?Sized which I suspect is the reason why this cast can't be made.

@zkat
Copy link
Owner

zkat commented Dec 7, 2024

I think this is happening because you’re trying to turn &str into &dyn SourceCode… that is, str (which is unsized, and does NOT implement SourceCode), into dyn SourceCode

try casting &&str to &dyn SourceCode

@Diegovsky
Copy link
Author

Diegovsky commented Dec 18, 2024

Well, it is implemented for str.

I couldn't do it with &&str because I store an owned String and I'm trying to provide a SourceCode for Diagnostic::source_code.

@JustusFluegel
Copy link

actually afaik that is currently a restriction of the rust compiler, you can't actually use CoerceUnsized for !Sized objects. I don't know why the error message is so confusing, that could certainly use some improvement. See the section here, it mentiones T: Sized as a requirement for the unsizing coercion.

https://doc.rust-lang.org/stable/std/marker/trait.Unsize.html

@Diegovsky
Copy link
Author

Well, in any case, it seems like this is a rust bug. That trait is clearly implemented for str, which is !Sized.

It is a bit of a paradox that you can implement a trait for an unsized type but can't actually use it.

@JustusFluegel
Copy link

JustusFluegel commented Feb 14, 2025

well you can use it, you just can't perform the unsizing coercion on !Sized types. It's actually pretty common, for example in rand the recommended way to write a function that takes a rng is fn foo<R: Rng + ?Sized>(rng: &mut R) (which is great since then you can, for example, also pass a &mut dyn RngCore to it since RngCore is implemented for dyn RngCore which itsself is unsized), still behind a reference but since the methods in the trait take references that works just fine. There was some good reason why CoerceUnsized doesn't work (i.e converting from &T to &dyn Trait where T: !Sized + Trait) for unsized types, but I would have to look up why again ( I think it was something with having to store the layout (size, alignment, etc.) in the vtable for the drop impl and that beeing difficult cuz of layouts and supertraits and stuff, not quite sure ) - definetely something that should be fixable in the compiler but I think just a bunch of work no one has gotten around to yet.

@Diegovsky
Copy link
Author

Well, that certainly went over my head :)

In any case, I gathered from your explanation that making SourceCode: ?Sized could allow for CoerceUnsized to work, is that correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants