I use "reference" term here like in C++ world, not like in C# (for example). I use non-C++ syntax on purpose -- this is general question, not about this particular implementation.
Starting something like C++ afresh I would like to make rules and validation in order to prevent a case when reference outlives its source. For example this looks like a valid usage:
def foo(x ref int) ref int return x; end
But this is wrong:
def bar() ref int x int = 5; return x; end
x is put on stack and when reference to it is returned this stack is already gone.
I didn't so far find the analysis algorithm description so that is why I am asking -- what to allow (for example defining parameters as references), and how to check when the usage is abused creating dangling references?