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

Raw pointer coercion #361

Closed
wants to merge 3 commits into from
Closed

Raw pointer coercion #361

wants to merge 3 commits into from

Conversation

mahkoh
Copy link
Contributor

@mahkoh mahkoh commented Oct 7, 2014

*mut T and &mut T should coerce to *const T.

Rendered

@Gankra
Copy link
Contributor

Gankra commented Oct 7, 2014

Yes

Thank you

This is a constant source of pain with the ptr API, where the read and copy_memory functions take *const T's. It's 100% safe to do, and shouldn't have any surprising consequences.

@petrochenkov
Copy link
Contributor

+1 (#16926)

@aturon
Copy link
Member

aturon commented Oct 7, 2014

Definitely. I doubt that the current behavior is intended.

@aturon
Copy link
Member

aturon commented Oct 7, 2014

cc @nikomatsakis @nick29581

@Manishearth
Copy link
Member

+1

@nrc
Copy link
Member

nrc commented Oct 8, 2014

I was hesitant at first to allow these (philosophically we are dereferencing the raw pointer, which is unsafe, and then referencing with a different raw point), but since we don't actually do a dereference I think they are fine. I have a big RFCs on coercions almost ready which includes this change too.

@ghost
Copy link

ghost commented Oct 9, 2014

Counterpoint: remove *const T completely.

Pointers have no semantics, they just point to things. There is no difference between *const T and *mut c_void because they're one transmute away in a block where we need to do it back and forth on every single line. *const T is just a weird crippled *mut T that tries to play with the type system on a level where we've already explicitly thrown away the type system. It tries to force rules on us after we've explicitly told the compiler to let us break all the rules.

Pure Rust does not need it, references work better. C bindings don't need it either, just running bindgen won't give you the full story anyway. A C binding needs the full documentation encoded in a safe interface, not just individual unsafe signatures. *const T has no good reason to exist.

@pnkfelix
Copy link
Member

pnkfelix commented Oct 9, 2014

assigning to @nick29581 to shepherd

@ben0x539
Copy link

Should we then remove ptr::null() and rename ptr::null_mut() to ptr::null()?

@mzabaluev
Copy link
Contributor

I'm not sure I agree with @Jurily about the need for const raw pointers. Even in C, constness is used as a hint for mutability and ownership transfer, allowing to catch many errors.
That said, I agree that *const T should automatically coerce to *mut T.
On a somewhat related note, was the *const syntax intended or is it just a workaround for the parser?

@ghost
Copy link

ghost commented Oct 26, 2014

Rust can express the *const vs *mut difference without the need for language constructs: struct ConstPtr<T>(pub *mut T). This is enough because a *const does not mean the data pointed to is immutable, only that it shouldn't be written through this particular pointer. Just look at how Arena or the slice sorting methods use pointers.

We already need Option for function pointers, and if we really want to write well-typed C bindings, we'd also want things like CArrayPtr, COwnedPtr, CRefCount and strong typedefs for c_int and friends (like when a newbie writes int instead of c_int and it happens to work on their machine, and you still have to read all the headers to figure out whether that's an offset or a bit field).

*const only makes sense for C because that's all they have. Reducing boilerplate for tuple structs would do a whole lot more for Rust than a single arbitrary builtin.

@mahkoh
Copy link
Contributor Author

mahkoh commented Oct 26, 2014

I do think it is worth talking about these things, but this particular RFC is just about some very simple and safe additions to the coercion rules.

@mahkoh
Copy link
Contributor Author

mahkoh commented Oct 26, 2014

@nick29581: Do you think this RFC needs more details or discussion?

@nrc
Copy link
Member

nrc commented Oct 27, 2014

@mahkoh is there anything here that should be added to #401? I think there is an advantage in having this RFC as a separate one if #401 is going to get bogged down in discussion, but that doesn't seem to be the case. Otherwise it seems better to have all the coercion specification in one place.

@mahkoh
Copy link
Contributor Author

mahkoh commented Oct 27, 2014

Feel free to add the content of this RFC to #401 if it's not already a superset. There was little debate here regarding the content of the RFC and I believe this RFC could be implemented quickly. There are enormous ergonomics benefits to be had for all those who work with lots of FFI code. Therefore I would appreciate it if this RFC could be merged and implemented quickly, even if it is later superseded by your RFC.

@nrc
Copy link
Member

nrc commented Oct 27, 2014

I believe #401 is a strict superset of this one. I'll push this one if it looks like #401 is going to take some time.

@nrc
Copy link
Member

nrc commented Nov 17, 2014

#401 has been accepted and I believe includes everything in this one. Closing for now, but if there is anything I've missed and I can re-open

@nrc nrc closed this Nov 17, 2014
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.

9 participants