-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Reject casts to unsized types #17598
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
Conversation
cc @nick29581 |
fn main() { | ||
let _foo = &[1u, 2] as [uint]; | ||
//~^ ERROR cast to unsized type: `&[uint, .. 2]` as `[uint]` | ||
//~^^ NOTE did you mean `&[uint]`? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this cast doesn't work at the moment, so the note is a bit wrong. Should probably suggest using an implicit coercion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But please add a check for &obj types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll suggest using an implicit coercion if the cast type is not a trait
505fd2c
to
5f46fdb
Compare
Force-pushed some changes. It will now suggest using an implicit coerceion where the explicit cast doesn't presently work, and will only suggest boxing an expression when attempting to cast to a trait, since this is generally the only time boxing is appropriate. |
This prevents ICEs or less helpful diagnostics if typeck proceeds further. Closes issue rust-lang#17441
5f46fdb
to
69d570f
Compare
Changed the note suggesting |
if !ty::type_is_sized(fcx.tcx(), t_1) { | ||
let tstr = fcx.infcx().ty_to_string(t_1); | ||
fcx.type_error_message(span, |actual| { | ||
format!("cast to unsized type: `{}` as `{}`", actual, tstr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the error message should only show the unsized type, not the actual type too. Otherwise its a bit confusing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I patterned it after messages in the existing code, e.g. "cast to nil:
{}as
{}"
. I can still change it.
r=me with that error message change. |
feat: add preliminary support for `+ use<..>` `precise_capturing` syntax ## Summary This PR adds basic support for the following syntax. ```rs fn captures<'a: 'a, 'b: 'b, T>() -> impl Sized + use<'b, T> {} // ~~~~~~~~~~~~~~~~~~~~~~~ // This opaque type does not capture `'a`. fn outlives<'o, T: 'o>(_: T) {} fn caller<'o, 'a, 'b: 'o, T: 'o>() { // ~~ // ^ Note that we don't need `'a: 'o`. outlives::<'o>(captures::<'a, 'b, T>()); } ``` Related to rust-lang#17598
feat: add preliminary support for `+ use<..>` `precise_capturing` syntax ## Summary This PR adds basic support for the following syntax. ```rs fn captures<'a: 'a, 'b: 'b, T>() -> impl Sized + use<'b, T> {} // ~~~~~~~~~~~~~~~~~~~~~~~ // This opaque type does not capture `'a`. fn outlives<'o, T: 'o>(_: T) {} fn caller<'o, 'a, 'b: 'o, T: 'o>() { // ~~ // ^ Note that we don't need `'a: 'o`. outlives::<'o>(captures::<'a, 'b, T>()); } ``` Related to rust-lang#17598
feat: add preliminary support for `+ use<..>` `precise_capturing` syntax ## Summary This PR adds basic support for the following syntax. ```rs fn captures<'a: 'a, 'b: 'b, T>() -> impl Sized + use<'b, T> {} // ~~~~~~~~~~~~~~~~~~~~~~~ // This opaque type does not capture `'a`. fn outlives<'o, T: 'o>(_: T) {} fn caller<'o, 'a, 'b: 'o, T: 'o>() { // ~~ // ^ Note that we don't need `'a: 'o`. outlives::<'o>(captures::<'a, 'b, T>()); } ``` Related to rust-lang#17598
This closes issue #17441 and generally gives more helpful diagnostics.