Skip to content

Commit

Permalink
Unsafe expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
arcnmx committed Oct 29, 2015
1 parent ffe3f8b commit 7c74e10
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions text/0000-unsafe-expressions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
- Feature Name: `unsafe_expr`
- Start Date: 2015-10-29
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary

Proposal to extend the syntax of the `unsafe` keyword to be followed by an expression
rather than a block as it is currently defined.


# Motivation

`unsafe` is often used for single method calls, and requires one to wrap the expression
in curly braces. It looks ugly, makes for strange-looking inline blocks, causes code
to drift rightward in block indenting, and in general just doesn't feel very ergonomic.


# Detailed design

Let's take a look at a trivial example that makes use of unsafe:

```rust
match senders.len() {
1 => if unsafe { senders.get_unchecked(0) }.send(value).is_err() {
// ...
},
_ => {},
}
```

The inline braces are awkward and feel strange from a formatting perspective.
Now, suppose `unsafe` accepted an expression rather than a block.

```rust
match senders.len() {
1 => if unsafe senders.get_unchecked(0).send(value).is_err() {
// ...
},
_ => {},
}
```

We've removed the strange punctuation, and all works as usual. This change
would be backwards-compatible, as it will allow all existing Rust code to compile.


# Drawbacks

Less friction to the ergonomics of using `unsafe` is not necessarily a good thing,
we certainly don't want to encourage its use. I believe this change would still
be beneficial in general, however.

This encourages `unsafe` to be more coarse-grained, rather than wrapping the
single function call in its own block. However, I've found this style to not
be common in the greater rust ecosystem, which more often means the example
is written as such:

```rust
match senders.len() {
1 => if unsafe { senders.get_unchecked(0).send(value).is_err() } {
// ...
},
_ => {},
}
```

or worse, like so:

```rust
unsafe {
match senders.len() {
1 => if senders.get_unchecked(0).send(value).is_err() {
// ...
},
_ => {},
}
}
```

Note that this issue exists in other contexts as well, such as `unsafe` functions
themselves.


# Unresolved questions

- Is there any reason this proposal will not work from a grammar or syntax perspective?
- Are there any strange interactions with the language here that haven't been addressed?
- Does this conflict with or complicate parsing due to the `unsafe fn x()` declaration syntax?

0 comments on commit 7c74e10

Please sign in to comment.