Skip to content

Semver: Note that it is not a breaking change to make an unsafe function safe #11200

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/doc/src/reference/semver.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ considered incompatible.
* [Possibly-breaking: introducing a new function type parameter](#fn-generic-new)
* [Minor: generalizing a function to use generics (supporting original type)](#fn-generalize-compatible)
* [Major: generalizing a function to use generics with type mismatch](#fn-generalize-mismatch)
* [Possibly-breaking: making an `unsafe` function safe](#fn-unsafe-safe)
* Attributes
* [Major: switching from `no_std` support to requiring `std`](#attr-no-std-to-std)
* Tooling and environment compatibility
Expand Down Expand Up @@ -1078,6 +1079,71 @@ fn main() {
}
```

<a id="fn-unsafe-safe"></a>
### Possibly-breaking: making an `unsafe` function safe

A previously `unsafe` function can be made safe without breaking code. Note
however that it will likely cause the [`unused_unsafe`][unused_unsafe] lint
to trigger as in the example below, which will cause local crates that have
specified `#![deny(warnings)]` to stop compiling.

Going the other way (making a safe function `unsafe`) is a breaking change.

```rust,ignore
// MINOR CHANGE

///////////////////////////////////////////////////////////
// Before
pub unsafe fn foo() {}

///////////////////////////////////////////////////////////
// After
pub fn foo() {}

///////////////////////////////////////////////////////////
// Example use of the library that will trigger a lint.
use updated_crate::foo;

unsafe fn bar(f: unsafe fn()) {
f()
}

fn main() {
unsafe { foo() }; // The `unused_unsafe` lint will trigger here
unsafe { bar(foo) };
}
```

Making a previously `unsafe` associated function or method on structs / enums
safe is also a minor change, while the same is not true for associated
function on traits:

```rust,ignore
// MAJOR CHANGE

///////////////////////////////////////////////////////////
// Before
pub trait Foo {
unsafe fn foo();
}

///////////////////////////////////////////////////////////
// After
pub trait Foo {
fn foo();
}

///////////////////////////////////////////////////////////
// Example usage that will break.
use updated_crate::Foo;

struct Bar;

impl Foo for Bar {
unsafe fn foo() {} // Error: method `foo` has an incompatible type for trait
}
```

<a id="attr-no-std-to-std"></a>
### Major: switching from `no_std` support to requiring `std`

Expand Down Expand Up @@ -1347,3 +1413,4 @@ document what your commitments are.
[SemVer]: https://semver.org/
[struct literal]: ../../reference/expressions/struct-expr.html
[wildcard patterns]: ../../reference/patterns.html#wildcard-pattern
[unused_unsafe]: ../../rustc/lints/listing/warn-by-default.html#unused-unsafe