-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Replace C types with Rust types in std, closes #7313 #10943
Conversation
Thanks for working on this cleanup. There's no specific reason for all the runtime functions returning weird combinations of pointer types, it's just the legacy of refactoring in various ways. Ideally, they all agree on the types and there are no casts. CString should probably keep using c_char. I think it's debatable whether the various logging and other runtime functions that deal in c strings should use *c_char or *u8 or some other typedef. This is all internal details so it doesn't matter too much. Ultimately we're probably going to want to be able to compile out |
So should various functions pass C strings around as u8? *c_str functions are used in a lot of places. When working on this issue, another idea came to my mind. There probably are a couple of unnecessary type casts in the rust source code and while working on this issue, I accidentally introduced another. Would a lint for unnecessary type casts be useful in generel? |
@fhahn Public API's that deal with C strings should definitely use |
A lint for unnecessary type casts sounds useful, but I guess we won't know until we try. |
Bors failed to test this PR, because it could not be merged. I've rebased pull request to the current master. |
@fhahn an "unneeded cast" lint sounds great. |
There was a missing type cast on Windows, which failed the build bot. It should be fixed now, but unfortunately I won't have a Windows (or OS X) installation handy over the next couple of days, to test the patch on other platforms than Linux. |
@brson no worries, I didn't have much time to work on this patch the last couple of days. Before this patch gets merged, I'd like to clear up a few points:
|
@@ -833,7 +834,7 @@ pub struct MemoryMap { | |||
/// Pointer to the memory created or modified by this map. | |||
data: *mut u8, | |||
/// Number of bytes this map applies to | |||
len: size_t, | |||
len: u64, |
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.
That should probably be uint
, since 32-bit wants u32.
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've changed the type to uint and added a cast to size_t at the cast.
I've updated the commit. It compiles on 64- and 32-bit linux. |
I've updated the commit again, it should compile on win32 now. |
@fhahn needs a rebase. |
@sfackler I'll look into it tomorrow. I managed to compile this patch on windows (win7 32 bit) without errors today, but 2 run-pass tests fail at the moment. Following code compiles on Windows, but fails when building libstd in stage 1:
I've reverted this change and I'm using the old code (with two c_int variables): https://github.com/mozilla/rust/blob/master/src/libstd/os.rs#L740 |
Needs a snapshot? |
if (ref_count & FROZEN_BIT) != 0 { | ||
fail_borrowed(a, file, line); | ||
} | ||
} |
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 this file came back from the dead!
With the extra file removed, we can give this another go-through with bors. |
I've removed the file again and squashed everything in one commit. I think another try with bors would be good. |
…richton I've started working on a patch for #7313 . So far I tried to replace C types in `src/libstd/unstable/*` and related files. So far, I have two questions. Is there a convention for passing pointers around in `std` as Rust types? Sometimes pointers are passed around as `*c_char` (which seems to be an `*i8`), `*c_void` or `*u8`, which leads to a lot of casts. E.g: [`exchange_malloc`](https://github.com/fhahn/rust/compare/issue-7313-replace-c-types?expand=1#diff-39f44b8c3f4496abab854b3425ac1617R60) used to return a `*c_char` but the function in turn only calls `malloc_raw` which returns a `*c_void`. Is there a specific reason for this? The second question is about `CString` and related functions like `with_c_str`. At the moment these functions use `*c_char*`. Should I replace it with `*u8` or keep it, because it's an wrapper around classical C strings?
[`map_identity`]: recognize tuple identity function Fixes rust-lang#7189 This lint now recognizes `.map(|(a, b)| (a, b))` as a useless `map` call. changelog: [`map_identity`]: recognize tuple identity function
I've started working on a patch for #7313 . So far I tried to replace C types in
src/libstd/unstable/*
and related files.So far, I have two questions. Is there a convention for passing pointers around in
std
as Rust types? Sometimes pointers are passed around as*c_char
(which seems to be an*i8
),*c_void
or*u8
, which leads to a lot of casts. E.g:exchange_malloc
used to return a*c_char
but the function in turn only callsmalloc_raw
which returns a*c_void
.Is there a specific reason for this?
The second question is about
CString
and related functions likewith_c_str
. At the moment these functions use*c_char*
. Should I replace it with*u8
or keep it, because it's an wrapper around classical C strings?