Skip to content

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

Merged
merged 2 commits into from
Sep 29, 2014
Merged

Reject casts to unsized types #17598

merged 2 commits into from
Sep 29, 2014

Conversation

bkoropoff
Copy link
Contributor

This closes issue #17441 and generally gives more helpful diagnostics.

@bkoropoff
Copy link
Contributor Author

cc @nick29581

fn main() {
let _foo = &[1u, 2] as [uint];
//~^ ERROR cast to unsized type: `&[uint, .. 2]` as `[uint]`
//~^^ NOTE did you mean `&[uint]`?
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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

@bkoropoff
Copy link
Contributor Author

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.

@bkoropoff
Copy link
Contributor Author

Changed the note suggesting Box<T> to a more generic suggestion of using a box or reference

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)
Copy link
Member

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

Copy link
Contributor Author

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.

@nrc
Copy link
Member

nrc commented Sep 29, 2014

r=me with that error message change.

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Sep 29, 2014
@bors bors merged commit 69d570f into rust-lang:master Sep 29, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 28, 2024
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
RalfJung pushed a commit to RalfJung/rust that referenced this pull request Aug 1, 2024
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
RalfJung pushed a commit to RalfJung/rust that referenced this pull request Aug 1, 2024
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
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

Successfully merging this pull request may close these issues.

3 participants