Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upLossy type conversion via 'as' #114
Comments
Manishearth
added
T-middle
good first issue
L-lint
labels
Aug 11, 2015
This comment has been minimized.
This comment has been minimized.
tbu-
commented
Aug 11, 2015
|
How should one perform these lossy conversions? |
This comment has been minimized.
This comment has been minimized.
|
We don't perform the conversions, we lint for them. So if we find an expression which cases a uX to a uY where X>Y, we lint. Et cetera. |
This comment has been minimized.
This comment has been minimized.
tbu-
commented
Aug 12, 2015
|
@Manishearth I was just wondering what one should do to avert this lint? #[allow] it? |
This comment has been minimized.
This comment has been minimized.
|
@tbu- Since all of them are This is not about being opinionated, this is about being helpful when explicitly asked for. |
This comment has been minimized.
This comment has been minimized.
tbu-
commented
Aug 12, 2015
|
I understand this, but suppose you have it enabled, like |
This comment has been minimized.
This comment has been minimized.
|
If you know you need a lossy conversion, why would you The only point of I work in statistical data analysis, and having a lint to show me the places where I must be wary of overflow would be quite useful to me. If we wanted to get extra fancy, we could add some guards to find |
This comment has been minimized.
This comment has been minimized.
|
It's a lint, so it's supposed to be overridden with |
This comment has been minimized.
This comment has been minimized.
|
I'm currently (painfully) getting to know the compiler internals and working on this. |
This comment has been minimized.
This comment has been minimized.
|
Both Manish and I will be glad to help. Your starting point will be matching on ExprCast. |
This comment has been minimized.
This comment has been minimized.
|
Thank you very much. :) I did do that (all by myself! (woohoo!)), so at least I'm going somewhere. In the meantime, I forked the repo and pushed what I have. I would really appreciate it if you took a look and gave me some feedback.
edit2 : Oh. I |
This comment has been minimized.
This comment has been minimized.
|
Try using Otherwise you seem to be going down the right path! I'm not sure what @llogiq intended for the third precision-loss lint, though. (But it looks like you'll need to match on the actual type variants) |
This comment has been minimized.
This comment has been minimized.
|
It appears I'll have to explain my reasoning a bit more: Let X and Y be one of { 8, 16, 32, 64 }. Then we get:
The |
This comment has been minimized.
This comment has been minimized.
|
ohh, that's what you're trying to catch |
This comment has been minimized.
This comment has been minimized.
|
As I said, I work in statistical data processing, and while we often use floating point for performance reasons, it's often interesting to see where we lose precision. Rust with its explicit conversions is well suited for the task, but local type inference means it's still possible to lose track. Therefore this lints. Btw. this would be a good time to start the wiki... |
This comment has been minimized.
This comment has been minimized.
|
Heh. I need time, I've got a million quizzes and assignments piling on top of me now :p Feel free to start it though, any format will do. |
Robzz
referenced this issue
Aug 19, 2015
Merged
Implementation of lossy cast lints (issue #114) #208
Manishearth
added
the
C-assigned
label
Aug 20, 2015
llogiq
added a commit
that referenced
this issue
Aug 21, 2015
llogiq
closed this
Aug 21, 2015
This comment has been minimized.
This comment has been minimized.
vitiral
commented
Aug 31, 2017
•
|
@llogiq what is the recommened function to use for explicit lossy conversions. For example:
|
llogiq commentedJul 14, 2015
E.g.
x as u8wherexisu32. Forisize/usizewe could even have a variant that warns of lossy conversions when compiled on 32-bit/16-bit systems. Suggested on internals.I'd suggest we make this Allow by default. We may also want to split this into 3 "lossy conversion" lints:
u32 as u8)