Motivation
The typical purpose of an unsafe
block is to call a single unsafe
function. There is no clean notation for this.
Wrapping the call in an unsafe
block has the downside that it also makes the evaluation of parameters unsafe
. So this is not a minimal unsafe
block:
// SAFETY: foo is safe because ...
let x = unsafe { foo(expr1(), expr2()) };
What you typically really want is only for the single foo
call to be unsafe
, not expr1
and expr2
.
Proposal
The unsafe
keyword in front of a function call indicates the single function call is unsafe
. Parameters are still evaluated safely.
// Only the `foo` call is unsafe.
let x = unsafe foo(expr1(), expr2());
It works with method calls and call chains:
let x = a
.abc()
.unsafe def()
.xyz()
.unsafe ghi();
This syntax is slightly ambiguous:
let x = unsafe foo()(); // which call is unsafe?
This can be resolved by precedence rules, i.e. only the first call is unsafe
.
Alternative postfix notation
To avoid parsing ambiguity postfix notation could be used:
// foo is unsafe
let x = foo.unsafe(expr1(), expr2());
This also works with method calls and call chains:
let x = a
.abc()
.def.unsafe()
.xyz()
.ghi.unsafe();
Edit: Made the prefix syntax the primary proposal.