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

Type conversions #401

Merged
merged 2 commits into from
Oct 30, 2014
Merged

Type conversions #401

merged 2 commits into from
Oct 30, 2014

Conversation

nrc
Copy link
Member

@nrc nrc commented Oct 16, 2014

Casting and coercions. Adds custom DST coercions for smart pointers. Otherwise, mostly descriptive, with a few bits of tidying up.

Rendered

Casting and coercions. Adds custom DST coercions for smart pointers. Otherwise, mostly descriptive, with a few bits of tidying up.
@Gankra
Copy link
Contributor

Gankra commented Oct 16, 2014

rendered (draft)


* `&mut T` to `&T`;

* `*mut T` to `*const T`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally! ❤️

@bstrie
Copy link
Contributor

bstrie commented Oct 16, 2014

Is this the opportunity to discuss possibly allowing numeric types to implicitly coerce when there's no loss of precision, e.g. u8 -> u32? I'm not necessarily advocating for it myself, but it has been requested in the past.

@nrc
Copy link
Member Author

nrc commented Oct 16, 2014

@bstrie I think that is a big enough change that it deserves its own RFC

```

The `Unsize` trait is a marker trait and a lang item. It should not be
implemented by users and user implementations will be ignored. The compiler will
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to ban such implementations than ignore them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a reasonable alternative. It would require an ad-hoc check in the type checker though

@Kimundi
Copy link
Member

Kimundi commented Oct 16, 2014

Does this make coercion an special case of casts? Eg, is is true that with the changes every implicit coercion can also be written as an explicit cast?

@nrc nrc self-assigned this Oct 16, 2014
@nodakai
Copy link

nodakai commented Oct 17, 2014

Will generic integer literals be covered by this RFC? (Perhaps they aren't technically coercion...) I have a little frustration about generic literals with minus sign:

fn main() {
    for x in std::iter::range_step(3u32, 0, -1) {
        println!("{}", x);
    }
}

I don't like -1 silently evaluates to some 4 billion of type u32.

@ben0x539
Copy link

@nodakai fwiw that's unrelated to generics, a plain let x: u32 = -1; already silently evaluates to upwards of 4 billion. I don't think it's a coercion, it's just a legal value of unsigned integer literals.

@nodakai
Copy link

nodakai commented Oct 18, 2014

@ben0x539 When you say "generics," you mean the <T> things, right? I'm talking about those literals which rustc call "generic integer":

fn main() {
    0.f();
}
<anon>:2:7: 2:10 error: type `<generic integer #0>` does not implement any method in scope named `f`

@ben0x539
Copy link

@nodakai Yeah, I thought you were talking about the <T> in fn range_step<T: ...>. Disregard that, I must have been confused. :)

@abonander
Copy link

I would like &T to coerce to T where T is a primitive. It would eliminate so many unnecessary explicit derefs.

@aturon
Copy link
Member

aturon commented Oct 30, 2014

Merged. Discussion. Tracking.

@eddyb eddyb mentioned this pull request Mar 16, 2015
@Centril Centril added A-typesystem Type system related proposals & ideas A-expressions Term language related proposals & ideas A-machine Proposals relating to Rust's abstract machine. A-coercions Proposals relating to coercions. A-dst Proposals re. DSTs A-raw-pointers Proposals relating to raw pointers. A-cast Proposals relating to explicit casts. labels Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cast Proposals relating to explicit casts. A-coercions Proposals relating to coercions. A-dst Proposals re. DSTs A-expressions Term language related proposals & ideas A-machine Proposals relating to Rust's abstract machine. A-raw-pointers Proposals relating to raw pointers. A-typesystem Type system related proposals & ideas
Projects
None yet
Development

Successfully merging this pull request may close these issues.