Skip to content

Implement Default for raw pointers #139535

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 1 commit into from
Apr 19, 2025
Merged

Conversation

ChrisDenton
Copy link
Member

@ChrisDenton ChrisDenton commented Apr 8, 2025

ACP: rust-lang/libs-team#571

This is instantly stable so we will need an FCP here.

Closes rust-lang/rfcs#2464

@rustbot
Copy link
Collaborator

rustbot commented Apr 8, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 8, 2025
@tgross35
Copy link
Contributor

tgross35 commented Apr 8, 2025

Implementation looks good to me, cc @rust-lang/libs-api for FCP consideration.

@rustbot label +I-libs-api-nominated

@rustbot rustbot added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Apr 8, 2025
@tgross35 tgross35 added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. I-libs-api-nominated Nominated for discussion during a libs-api team meeting. labels Apr 8, 2025
@Amanieu Amanieu added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 8, 2025
@Amanieu
Copy link
Member

Amanieu commented Apr 8, 2025

@rfcbot merge

@rfcbot
Copy link
Collaborator

rfcbot commented Apr 8, 2025

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Apr 8, 2025
@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Apr 8, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Apr 8, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@scottmcm
Copy link
Member

scottmcm commented Apr 8, 2025

Something I only thought of now, despite commenting on the ACP:

What are the trade-offs compared to using = ptr::null() for this, with rust-lang/rfcs#3681 ?

My "look it's a C struct" examples would arguably be fine defaulting it in the struct definition instead, since they know it's definitely a pointer.

How often is this wanted on generics vs concrete things?


Trying to find this PR again I accidentally found this from @joshtriplett:

Null isn’t necessarily a reasonable default value for a pointer. Some users of pointers may expect a potential null; other users may expect a valid pointer and explode on a null.

@ChrisDenton
Copy link
Member Author

Hm, even with default field values I still think it good to have sensible defaults even if you may want to override them.

As for the risk, I'm not personally persuaded that this adds any atm but could be wrong. std::ptr::null is a safe function already and mem::zeroed is fairly inconspicuous amongst other unsafe code so if code would blow up on nulls then you need to be enforcing that it doesn't happen somehow. I don't think this PR materially changes that.

I also don't think defaulting to null is even mildly surprising. Even if you ignore all the other languages, Rust's primitive's tend to default to the zero bit pattern even if that doesn't make sense as a default. E.g. the default bool is false. the default char is \0, etc. In any case we do already have AtomicPtr defaulting to null so there is a strong precedence.

@bors
Copy link
Collaborator

bors commented Apr 11, 2025

☔ The latest upstream changes (presumably #137412) made this pull request unmergeable. Please resolve the merge conflicts.

@LegionMammal978
Copy link
Contributor

How often is this wanted on generics vs concrete things?

At least in my case, I was implementing native methods for jni-sys. When a JNI native method throws a Java exception, it typically calls a JNI function to register the exception, then returns the logical zero value for its return type. This can either be a zeroed integer, an f64 equal to 0.0, or a null jobject. I was writing a generic wrapper that turned Rust panics into Java exceptions on the boundary, but I found that T: Default didn't work for a jobject return type, since it's a typedef for an opaque *mut _jobject. I ultimately ended up writing a separate trait for zero values that included jobject pointers.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Apr 18, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Apr 18, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Apr 18, 2025
@ChrisDenton
Copy link
Member Author

ChrisDenton commented Apr 18, 2025

Implementation looks good to me

The final comment period, with a disposition to merge, as per the review above, is now complete.

@bors r=tgross35

@bors
Copy link
Collaborator

bors commented Apr 18, 2025

📌 Commit 830bd8b has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Apr 18, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 19, 2025
Implement `Default` for raw pointers

ACP: rust-lang/libs-team#571

This is instantly stable so we will need an FCP here.

Closes rust-lang/rfcs#2464
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 19, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang#137454 (not lint break with label and unsafe block)
 - rust-lang#139297 (Deduplicate & clean up Nix shell)
 - rust-lang#139535 (Implement `Default` for raw pointers)
 - rust-lang#139753 (Make `#[naked]` an unsafe attribute)
 - rust-lang#139922 (fix incorrect type in cstr `to_string_lossy()` docs)
 - rust-lang#139978 (Add citool command for generating a test dashboard)
 - rust-lang#140007 (Disable has_thread_local on i686-win7-windows-msvc)
 - rust-lang#140016 (std: Use fstatat() on illumos)
 - rust-lang#140025 (Re-remove `AdtFlags::IS_ANONYMOUS`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 19, 2025
…enton

Rollup of 8 pull requests

Successful merges:

 - rust-lang#137454 (not lint break with label and unsafe block)
 - rust-lang#139297 (Deduplicate & clean up Nix shell)
 - rust-lang#139535 (Implement `Default` for raw pointers)
 - rust-lang#139919 (Make rustdoc JSON Span column 1-based, just like line numbers)
 - rust-lang#139922 (fix incorrect type in cstr `to_string_lossy()` docs)
 - rust-lang#140007 (Disable has_thread_local on i686-win7-windows-msvc)
 - rust-lang#140016 (std: Use fstatat() on illumos)
 - rust-lang#140025 (Re-remove `AdtFlags::IS_ANONYMOUS`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit dff14f0 into rust-lang:master Apr 19, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 19, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 19, 2025
Rollup merge of rust-lang#139535 - ChrisDenton:default-ptr, r=tgross35

Implement `Default` for raw pointers

ACP: rust-lang/libs-team#571

This is instantly stable so we will need an FCP here.

Closes rust-lang/rfcs#2464
@ChrisDenton ChrisDenton deleted the default-ptr branch April 19, 2025 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default is not implemented for raw pointers (*const and *mut)
8 participants