r/cpp • • Oct 24 '24

Why Safety Profiles Failed

https://www.circle-lang.org/draft-profiles.html
179 Upvotes

347 comments sorted by

View all comments

Show parent comment

4

u/[deleted] Oct 26 '24

The assertion is that with just the following function signature (no function body)

DLLEXPORT void func(std::vector<int>& vec, int& x)
  • profiles cannot know whether this is safe to use. Inside the body, compiler doesn't know whether the arguments alias. At call site, compiler doesn't know whether this function accepts aliased inputs or not.
  • With safe-cpp, this is valid code. Because it uses the same aliasing model as rust (only one exclusive mutable borrow active at a time).

I just checked with godbolt at https://godbolt.org/z/1TEhP3nfj

#feature on safety
#include <https://raw.githubusercontent.com/cppalliance/safe-cpp/master/libsafecxx/single-header/std2.h?token=$(date%20+%s)>

extern void func(std2::vector<int>^ vec, int^ x);

int main() {
std2::vector<int> vec {};
//int^ x = mut vec[0];
func(^vec, mut vec[0]);
return 0;
}

error message:

safety: during safety checking of int main()
borrow checking: example.cpp:11:20
    func(^vec, mut vec[0]); 
                    ^
mutable borrow of vec between its mutable borrow and its use
loan created at example.cpp:11:10
    func(^vec, mut vec[0]); 
        ^

0

u/kronicum Oct 26 '24

With safe-cpp, this is valid code. Because it uses the same aliasing model as rust (only one exclusive mutable borrow active at a time).

How is it safe when you have two mutable (C++) references where one can indirectly alias another?

Your example uses a different kind of reference. You're answering a different question.