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

Stabilize unsafe extern blocks (RFC 3484) #127921

Merged
merged 1 commit into from
Aug 3, 2024

Conversation

spastorino
Copy link
Member

@spastorino spastorino commented Jul 18, 2024

Stabilization report

Summary

This is a tracking issue for the RFC 3484: Unsafe Extern Blocks

We are stabilizing #![feature(unsafe_extern_blocks)], as described in Unsafe Extern Blocks RFC 3484. This feature makes explicit that declaring an extern block is unsafe. Starting in Rust 2024, all extern blocks must be marked as unsafe. In all editions, items within unsafe extern blocks may be marked as safe to use.

RFC: rust-lang/rfcs#3484
Tracking issue: #123743

What is stabilized

Summary of stabilization

We now need extern blocks to be marked as unsafe and items inside can also have safety modifiers (unsafe or safe), by default items with no modifiers are unsafe to offer easy migration without surprising results.

unsafe extern {
    // sqrt (from libm) may be called with any `f64`
    pub safe fn sqrt(x: f64) -> f64;

    // strlen (from libc) requires a valid pointer,
    // so we mark it as being an unsafe fn
    pub unsafe fn strlen(p: *const c_char) -> usize;

    // this function doesn't say safe or unsafe, so it defaults to unsafe
    pub fn free(p: *mut core::ffi::c_void);

    pub safe static IMPORTANT_BYTES: [u8; 256];

    pub safe static LINES: SyncUnsafeCell<i32>;
}

Tests

The relevant tests are in tests/ui/rust-2024/unsafe-extern-blocks.

History

Unresolved questions

I am not aware of any unresolved questions.

@rustbot
Copy link
Collaborator

rustbot commented Jul 18, 2024

r? @nnethercote

rustbot has assigned @nnethercote.
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jul 18, 2024
@spastorino
Copy link
Member Author

r? @traviscross

I wasn't sure if I should leave this as a draft but would leave all this up to t-lang.

@rustbot rustbot assigned traviscross and unassigned nnethercote Jul 18, 2024
@traviscross traviscross added T-lang Relevant to the language team, which will review and decide on the PR/issue. I-lang-nominated Nominated for discussion during a lang team meeting. labels Jul 18, 2024
@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino force-pushed the stabilize-unsafe-extern-blocks branch from 8e21e78 to 9a8888c Compare July 18, 2024 15:38
@oli-obk
Copy link
Contributor

oli-obk commented Jul 18, 2024

default items with no modifiers are unsafe to keep backwards compatibility.

We don't need back compat when unsafe is added to the extern block, why do we have this rule?

@spastorino
Copy link
Member Author

default items with no modifiers are unsafe to keep backwards compatibility.

We don't need back compat when unsafe is added to the extern block, why do we have this rule?

Well ... is not really about backwards compatibility and when I wrote this, I kind of knew that I wasn't properly explaining myself. What I meant was ... if we were to flip default safety to safe, it would not only be surprising but would may also led to end with something that was unsafe being safe when you migrate.
Imagine ...

extern "C" {
    fn foo();
    // foo is unsafe
}

When I migrate my code I do ...

unsafe extern "C" {
    fn foo();
    // now foo is safe
}

if the semantics of not providing safety is now being safe this would be a disaster.

This is what I meant in that text, but written really poorly :). If you want to suggest an edit, I'd be happy to apply it.

@oli-obk

This comment was marked as resolved.

@workingjubilee
Copy link
Member

It is best to avoid the phrase "backwards compatibility", as it is clear that it means different things to different people. For some, it means to keep code that currently exists compiling. For others, they primarily mean preserving functional equivalence if the code is recompiled, but not necessarily allowing it to still compile (e.g. rejecting unsound code). For others, it means offering easy migration to a new paradigm that does not have surprising results.

@workingjubilee

This comment was marked as resolved.

@workingjubilee
Copy link
Member

Is it possible to write k#safe in previous editions? Did we ever commit to that prefix?

@workingjubilee

This comment was marked as resolved.

@workingjubilee
Copy link
Member

@spastorino Would you please update your stabilization report to explain the finer-grained implementation choices that have been made? e.g. why safe is now parsed in previous editions, and whether safe is a restricted keyword in later editions? The RFC focused on Edition 2024, mostly.

...and is unsafe static now accepted outside extern "C"?

@workingjubilee
Copy link
Member

workingjubilee commented Jul 18, 2024

...yes, without a feature gate, on a recent nightly? And beta?

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a5e84d4d6df2b38d60fe18a44e1a2087

https://rust.godbolt.org/z/qdcreaqrh

...Could the stabilization report also explain why we decided to accept unsafe static outside extern "C"? Or reject it, if the implementation decides to change on that, that's fine too.

@compiler-errors
Copy link
Member

compiler-errors commented Jul 18, 2024

#127943 fixes that last part. It was accidental afaict.

@traviscross traviscross added F-unsafe_extern_blocks `#![feature(unsafe_extern_blocks)]` and removed T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 19, 2024
@spastorino
Copy link
Member Author

@workingjubilee, first I've updated the stabilization report to avoid mentioning backwards compatibility and explained in terms of avoiding surprising effects.

why safe is now parsed in previous editions, and whether safe is a restricted keyword in later editions? The RFC focused on Edition 2024, mostly.

I did a short summary because I didn't want to repeat most of what is said in the RFC. Related to this very specific thing, from the RFC:

In all editions, items within unsafe extern blocks may be marked as safe to use.

@rfcbot
Copy link

rfcbot commented Aug 3, 2024

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.

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Aug 3, 2024

📌 Commit 8366c7f has been approved by compiler-errors

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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 3, 2024
tgross35 added a commit to tgross35/rust that referenced this pull request Aug 3, 2024
…-blocks, r=compiler-errors

Stabilize unsafe extern blocks (RFC 3484)

# Stabilization report

## Summary

This is a tracking issue for the RFC 3484: Unsafe Extern Blocks

We are stabilizing `#![feature(unsafe_extern_blocks)]`, as described in [Unsafe Extern Blocks RFC 3484](rust-lang/rfcs#3484). This feature makes explicit that declaring an extern block is unsafe. Starting in Rust 2024, all extern blocks must be marked as unsafe. In all editions, items within unsafe extern blocks may be marked as safe to use.

RFC: rust-lang/rfcs#3484
Tracking issue: rust-lang#123743

## What is stabilized

### Summary of stabilization

We now need extern blocks to be marked as unsafe and items inside can also have safety modifiers (unsafe or safe), by default items with no modifiers are unsafe to offer easy migration without surprising results.

```rust
unsafe extern {
    // sqrt (from libm) may be called with any `f64`
    pub safe fn sqrt(x: f64) -> f64;

    // strlen (from libc) requires a valid pointer,
    // so we mark it as being an unsafe fn
    pub unsafe fn strlen(p: *const c_char) -> usize;

    // this function doesn't say safe or unsafe, so it defaults to unsafe
    pub fn free(p: *mut core::ffi::c_void);

    pub safe static IMPORTANT_BYTES: [u8; 256];

    pub safe static LINES: SyncUnsafeCell<i32>;
}
```

## Tests

The relevant tests are in `tests/ui/rust-2024/unsafe-extern-blocks`.

## History

- rust-lang#124482
- rust-lang#124455
- rust-lang#125077
- rust-lang#125522
- rust-lang#126738
- rust-lang#126749
- rust-lang#126755
- rust-lang#126757
- rust-lang#126758
- rust-lang#126756
- rust-lang#126973
- rust-lang#127535
- rust-lang/rustfmt#6204

## Unresolved questions

I am not aware of any unresolved questions.
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 3, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#127095 (Migrate `reproducible-build-2` and `stable-symbol-names` `run-make` tests to rmake)
 - rust-lang#127921 (Stabilize unsafe extern blocks (RFC 3484))
 - rust-lang#128466 (Update the stdarch submodule)
 - rust-lang#128530 (Implement `UncheckedIterator` directly for `RepeatN`)
 - rust-lang#128581 (Assert that all attributes are actually checked via `CheckAttrVisitor` and aren't accidentally usable on completely unrelated HIR nodes)
 - rust-lang#128603 (Update run-make/used to use `any_symbol_contains`)

Failed merges:

 - rust-lang#128410 (Migrate `remap-path-prefix-dwarf` `run-make` test to rmake)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 3, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#127921 (Stabilize unsafe extern blocks (RFC 3484))
 - rust-lang#128283 (bootstrap: fix bug preventing the use of custom targets)
 - rust-lang#128530 (Implement `UncheckedIterator` directly for `RepeatN`)
 - rust-lang#128551 (chore: refactor backtrace style in panic)
 - rust-lang#128573 (Simplify `body` usage in rustdoc)
 - rust-lang#128581 (Assert that all attributes are actually checked via `CheckAttrVisitor` and aren't accidentally usable on completely unrelated HIR nodes)
 - rust-lang#128603 (Update run-make/used to use `any_symbol_contains`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7d9ed2a into rust-lang:master Aug 3, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 3, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 3, 2024
Rollup merge of rust-lang#127921 - spastorino:stabilize-unsafe-extern-blocks, r=compiler-errors

Stabilize unsafe extern blocks (RFC 3484)

# Stabilization report

## Summary

This is a tracking issue for the RFC 3484: Unsafe Extern Blocks

We are stabilizing `#![feature(unsafe_extern_blocks)]`, as described in [Unsafe Extern Blocks RFC 3484](rust-lang/rfcs#3484). This feature makes explicit that declaring an extern block is unsafe. Starting in Rust 2024, all extern blocks must be marked as unsafe. In all editions, items within unsafe extern blocks may be marked as safe to use.

RFC: rust-lang/rfcs#3484
Tracking issue: rust-lang#123743

## What is stabilized

### Summary of stabilization

We now need extern blocks to be marked as unsafe and items inside can also have safety modifiers (unsafe or safe), by default items with no modifiers are unsafe to offer easy migration without surprising results.

```rust
unsafe extern {
    // sqrt (from libm) may be called with any `f64`
    pub safe fn sqrt(x: f64) -> f64;

    // strlen (from libc) requires a valid pointer,
    // so we mark it as being an unsafe fn
    pub unsafe fn strlen(p: *const c_char) -> usize;

    // this function doesn't say safe or unsafe, so it defaults to unsafe
    pub fn free(p: *mut core::ffi::c_void);

    pub safe static IMPORTANT_BYTES: [u8; 256];

    pub safe static LINES: SyncUnsafeCell<i32>;
}
```

## Tests

The relevant tests are in `tests/ui/rust-2024/unsafe-extern-blocks`.

## History

- rust-lang#124482
- rust-lang#124455
- rust-lang#125077
- rust-lang#125522
- rust-lang#126738
- rust-lang#126749
- rust-lang#126755
- rust-lang#126757
- rust-lang#126758
- rust-lang#126756
- rust-lang#126973
- rust-lang#127535
- rust-lang/rustfmt#6204

## Unresolved questions

I am not aware of any unresolved questions.
@Kobzol
Copy link
Contributor

Kobzol commented Aug 5, 2024

@rust-timer build 6841bd3

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (6841bd3): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.9% [0.4%, 1.3%] 10
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results (primary -1.5%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-1.5%, -1.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.5% [-1.5%, -1.5%] 1

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 758.56s -> 757.756s (-0.11%)
Artifact size: 336.83 MiB -> 336.85 MiB (0.00%)

@rustbot rustbot added the perf-regression Performance regression. label Aug 5, 2024
@compiler-errors
Copy link
Member

Probably has to do with the ungated edition check for extern block spans. I don't think this can be fixed 🤔

@Kobzol
Copy link
Contributor

Kobzol commented Aug 5, 2024

Understood, thanks. I'll mark the rollup as triaged then.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Aug 5, 2024
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Aug 15, 2024
@ehuss ehuss added the relnotes Marks issues that should be documented in the release notes of the next release. label Aug 20, 2024
@Mark-Simulacrum Mark-Simulacrum added relnotes Marks issues that should be documented in the release notes of the next release. and removed relnotes Marks issues that should be documented in the release notes of the next release. labels Aug 25, 2024
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Aug 27, 2024
…-style-guide, r=compiler-errors

Add unsafe to extern blocks in style guide

This goes after this is merged:

- rust-lang#127921

r? `@traviscross`

Tracking:

- rust-lang#123743
tgross35 added a commit to tgross35/rust that referenced this pull request Aug 27, 2024
…-style-guide, r=compiler-errors

Add unsafe to extern blocks in style guide

This goes after this is merged:

- rust-lang#127921

r? ``@traviscross``

Tracking:

- rust-lang#123743
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 27, 2024
Rollup merge of rust-lang#127922 - spastorino:unsafe-extern-blocks-in-style-guide, r=compiler-errors

Add unsafe to extern blocks in style guide

This goes after this is merged:

- rust-lang#127921

r? ``@traviscross``

Tracking:

- rust-lang#123743
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. F-unsafe_extern_blocks `#![feature(unsafe_extern_blocks)]` finished-final-comment-period The final comment period is finished for this PR / Issue. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet