On Borrow Checking
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Sun May 18 00:32:35 UTC 2025
On 11/05/2025 11:40 AM, Walter Bright wrote:
> No multiple output support, which severely limits its capabilities.
>
> It does not severely limit it. Even the Rust manual says it's a rare
> case. It's a minor inconvenience that can be refactored away.
So I'm watching a video on Swift memory safety that Serg posted, and as
it turns out Clang has a solution to this!
https://fosdem.org/2025/schedule/event/fosdem-2025-6176-incremental-memory-safety-in-an-established-software-stack-lessons-learned-from-swift/
For escaping through another parameter:
https://clang.llvm.org/docs/AttributeReference.html#lifetime-capture-by
For escaping out the return value:
https://clang.llvm.org/docs/AttributeReference.html#id8
The clang way:
```c
void addToSet(std::string_view a [[clang::lifetime_capture_by(s)]],
std::set<std::string_view>& s) {
s.insert(a);
}
struct string {
// The returned pointer should not outlive ``*this``.
const char *data() const [[clang::lifetimebound]];
};
```
My way (ignoring the lack of inferration):
```d
void addToSet(@escape(s=) string a, ref bool[string] s) {
set[a] = true;
}
struct string {
const char* data() const return&;
}
```
More information about the Digitalmars-d
mailing list