Re: ref is unsafe

2013-01-10 Thread Tommi
On Thursday, 10 January 2013 at 16:42:09 UTC, Tommi wrote: ...which won't compile because T(4) is an rvalue, and according to D, rvalues can't be passed as ref (nor const ref). I don't know which one is flawed, my analogy, or the logic of how D is designed. My analogy is a bit broken in the s

Re: ref is unsafe

2013-01-10 Thread Tommi
...Although, I should add that my analogy between methods and free functions seems to break when the object is an rvalue. Like in: struct T { int v; this(int a) { v = a; } int get() { return v; } } int v = T(4).get(); Given my analogy, the method

Re: ref is unsafe

2013-01-10 Thread Tommi
On Thursday, 3 January 2013 at 21:56:22 UTC, David Nadlinger wrote: I must admit that I haven't read the rest of the thread yet, but I think the obvious and correct solution is to disallow passing locals (including non-ref parameters, which are effectively locals in D) as non-scope ref argument

Re: ref is unsafe

2013-01-09 Thread comco
On Sunday, 30 December 2012 at 22:02:16 UTC, Jonathan M Davis wrote: But that's very different from any attribute that we currently have. It would be like having a throw attribute instead of a nothrow attribute. I suppose that it is a possible solution though. I could also see an argument that

Re: ref is unsafe

2013-01-09 Thread Tommi
On Wednesday, 9 January 2013 at 04:33:21 UTC, Zach the Mystic wrote: I felt confident enough about my proposal to submit it as enhancement request: http://d.puremagic.com/issues/show_bug.cgi?id=9283 By the way, what do you propose is the correct placement of this "new" out keyword: #1: out

Re: ref is unsafe

2013-01-09 Thread Tommi
On Wednesday, 9 January 2013 at 04:33:21 UTC, Zach the Mystic wrote: I felt confident enough about my proposal to submit it as enhancement request: http://d.puremagic.com/issues/show_bug.cgi?id=9283 I like it. One issue though, like you also indicated by putting question marks on it: ref T

Re: ref is unsafe

2013-01-06 Thread comco
On Friday, 4 January 2013 at 20:20:08 UTC, Jonathan M Davis wrote: On Friday, January 04, 2013 17:26:59 Zach the Mystic wrote: > Honestly though, I'm inclined to argue that functions which > return by ref and > have a ref parameter of that same type just be considered > @system. Structs mess th

Re: ref is unsafe

2013-01-05 Thread Zach the Mystic
I've here formalized how I think the constraints on a non-scope ref taking and ref returning function should work. This represents a whole addition to the type system. The attribute "@outref" from my previous post has been shortened to keyword "out" (must come before parentheses). This is all I

Re: ref is unsafe

2013-01-04 Thread Zach the Mystic
On Friday, 4 January 2013 at 20:20:08 UTC, Jonathan M Davis wrote: ... But it doesn't change the statement that a function which takes a parameter by ref and returns by ref can't be considered @safe without additional constraints of some kind. It just shows why you don't have an easy way out to

Re: ref is unsafe

2013-01-04 Thread Jonathan M Davis
On Friday, January 04, 2013 17:26:59 Zach the Mystic wrote: > > Honestly though, I'm inclined to argue that functions which > > return by ref and > > have a ref parameter of that same type just be considered > > @system. > > Structs mess that up as well: > struct S { int i; } > ref int d(ref S s)

Re: ref is unsafe

2013-01-04 Thread Zach the Mystic
On Sunday, 30 December 2012 at 22:02:16 UTC, Jonathan M Davis wrote: The closest that we could get to what you suggest would be to add a new attribute similar to nothrow but which guarantees that the function does not return a ref to a parameter. So, you'd have to mark your functions that way (

Re: ref is unsafe

2013-01-04 Thread Tommi
On Friday, 4 January 2013 at 14:15:01 UTC, Tommi wrote: 'Tainting function call': A call to a 'tainter function' where at least one of the arguments passed by reference is ref to a local variable I forgot to point out that the return value of a 'tainting function call' is considered to

Re: ref is unsafe

2013-01-04 Thread Tommi
On Friday, 4 January 2013 at 06:30:55 UTC, Sönke Ludwig wrote: In other words, references returned by a function call that took any references to locals would be tainted as possibly local (in the function local data flow) and thus are not allowed to escape the scope. References derived from non-

Re: ref is unsafe

2013-01-04 Thread deadalnix
On Friday, 4 January 2013 at 06:30:55 UTC, Sönke Ludwig wrote: In other words, references returned by a function call that took any references to locals would be tainted as possibly local (in the function local data flow) and thus are not allowed to escape the scope. References derived from non-

Re: ref is unsafe

2013-01-03 Thread Sönke Ludwig
Am 03.01.2013 00:48, schrieb Jason House: > On Sunday, 30 December 2012 at 08:38:27 UTC, Jonathan M Davis wrote: >> After some recent discussions relating to auto ref and const ref, I have come >> to the conlusion that as it stands, ref is not @safe. It's @system. And I >> think that we need to tak

Re: ref is unsafe

2013-01-03 Thread Rob T
On Friday, 4 January 2013 at 00:46:33 UTC, Araq wrote: On Wednesday, 2 January 2013 at 23:33:16 UTC, Thiez wrote: On Wednesday, 2 January 2013 at 22:53:04 UTC, Jonathan M Davis wrote: Then we're going to have to disagree, and I believe that Walter and Andrei are completely with me on this one.

Re: ref is unsafe

2013-01-03 Thread Araq
On Wednesday, 2 January 2013 at 23:33:16 UTC, Thiez wrote: On Wednesday, 2 January 2013 at 22:53:04 UTC, Jonathan M Davis wrote: Then we're going to have to disagree, and I believe that Walter and Andrei are completely with me on this one. If all of the constructs that you use are @safe, then i

Re: ref is unsafe

2013-01-03 Thread Rob T
On Thursday, 3 January 2013 at 23:06:03 UTC, David Nadlinger wrote: The problem with that idea, is that a ref return with no arguments may call another ref return that returns something that escapes the scope it was created in. If the source code is not available, then there's no way for the co

Re: ref is unsafe

2013-01-03 Thread Rob T
OK, I understand what you mean by "scope" and how that can be used to prevent leaking a local ref out. Don't forget to consider this kind of scenario, which has no ref arguments to consider struct X { int _i; ref int f() { return _i; } } ref int F() { X x; return x.f

Re: ref is unsafe

2013-01-03 Thread Jonathan M Davis
On Thursday, January 03, 2013 23:50:37 Rob T wrote: > My understanding was that in some cases that source code is not > available to the compiler, which I would think means that > preventing scope escaping cannot be 100% guaranteed, correct? The source code is always available when compiling the f

Re: ref is unsafe

2013-01-03 Thread deadalnix
On Thursday, 3 January 2013 at 22:50:38 UTC, Rob T wrote: The problem with that idea, is that a ref return with no arguments may call another ref return that returns something that escapes the scope it was created in. If the source code is not available, then there's no way for the compiler to

Re: ref is unsafe

2013-01-03 Thread David Nadlinger
On Thursday, 3 January 2013 at 22:50:38 UTC, Rob T wrote: On Thursday, 3 January 2013 at 21:56:22 UTC, David Nadlinger wrote: I must admit that I haven't read the rest of the thread yet, but I think the obvious and correct solution is to disallow passing locals (including non-ref parameters, wh

Re: ref is unsafe

2013-01-03 Thread deadalnix
On Thursday, 3 January 2013 at 21:56:22 UTC, David Nadlinger wrote: I must admit that I haven't read the rest of the thread yet, but I think the obvious and correct solution is to disallow passing locals (including non-ref parameters, which are effectively locals in D) as non-scope ref argument

Re: ref is unsafe

2013-01-03 Thread Rob T
On Thursday, 3 January 2013 at 21:56:22 UTC, David Nadlinger wrote: I must admit that I haven't read the rest of the thread yet, but I think the obvious and correct solution is to disallow passing locals (including non-ref parameters, which are effectively locals in D) as non-scope ref argument

Re: ref is unsafe

2013-01-03 Thread David Nadlinger
On Sunday, 30 December 2012 at 08:38:27 UTC, Jonathan M Davis wrote: After some recent discussions relating to auto ref and const ref, I have come to the conlusion that as it stands, ref is not @safe. It's @system. And I think that we need to take a serious look at it to see what we can do to m

Re: ref is unsafe

2013-01-03 Thread Timon Gehr
On 01/03/2013 01:52 PM, Jason House wrote: On Thursday, 3 January 2013 at 05:56:27 UTC, Timon Gehr wrote: On 01/03/2013 12:48 AM, Jason House wrote: ... ref int bar() { int i = 7; return foo(i); } If @safe, this code will not compile. Error: foo may return a local stack variable Sinc

Re: ref is unsafe

2013-01-03 Thread Zach the Mystic
On Sunday, 30 December 2012 at 08:38:27 UTC, Jonathan M Davis wrote: And maybe another solution which I can't think of at the moment would be better. But my point is that we currently have a _major_ hole in SafeD thanks to the combination of ref parameters and ref return types, and we need to f

Re: ref is unsafe

2013-01-03 Thread Jason House
On Thursday, 3 January 2013 at 05:56:27 UTC, Timon Gehr wrote: On 01/03/2013 12:48 AM, Jason House wrote: ... ref int bar() { int i = 7; return foo(i); } If @safe, this code will not compile. Error: foo may return a local stack variable Since "i" is a local variable, "foo(i)" might re

Re: ref is unsafe

2013-01-03 Thread Artur Skawina
On 01/03/13 00:06, H. S. Teoh wrote: > All extern(C) functions must be @system by default. It makes no sense to > allow a @safe extern(C) function, since there is no way for the compiler > to verify anything at all. The best you can do is @trusted. extern(C) does not imply extern. And for extern

Re: ref is unsafe

2013-01-02 Thread Rob T
On Thursday, 3 January 2013 at 00:13:41 UTC, H. S. Teoh wrote: On Thu, Jan 03, 2013 at 12:53:56AM +0100, David Nadlinger wrote: On Wednesday, 2 January 2013 at 23:08:14 UTC, H. S. Teoh wrote: >All extern(C) functions must be @system by default. It makes >no sense >to allow a @safe extern(C) fun

Re: ref is unsafe

2013-01-02 Thread Timon Gehr
On 01/03/2013 12:48 AM, Jason House wrote: ... ref int bar() { int i = 7; return foo(i); } If @safe, this code will not compile. Error: foo may return a local stack variable Since "i" is a local variable, "foo(i)" might return it. ref int baz(int i) { return foo(i); } This

Re: ref is unsafe

2013-01-02 Thread H. S. Teoh
On Thu, Jan 03, 2013 at 12:53:56AM +0100, David Nadlinger wrote: > On Wednesday, 2 January 2013 at 23:08:14 UTC, H. S. Teoh wrote: > >All extern(C) functions must be @system by default. It makes no sense > >to allow a @safe extern(C) function, since there is no way for the > >compiler to verify any

Re: ref is unsafe

2013-01-02 Thread David Nadlinger
On Wednesday, 2 January 2013 at 23:08:14 UTC, H. S. Teoh wrote: All extern(C) functions must be @system by default. It makes no sense to allow a @safe extern(C) function, since there is no way for the compiler to verify anything at all. The best you can do is @trusted. @trusted shouldn't be a

Re: ref is unsafe

2013-01-02 Thread Jason House
On Sunday, 30 December 2012 at 08:38:27 UTC, Jonathan M Davis wrote: After some recent discussions relating to auto ref and const ref, I have come to the conlusion that as it stands, ref is not @safe. It's @system. And I think that we need to take a serious look at it to see what we can do to m

Re: ref is unsafe

2013-01-02 Thread Andrei Alexandrescu
On 1/2/13 5:21 PM, Maxim Fomin wrote: On Wednesday, 2 January 2013 at 19:37:51 UTC, Jonathan M Davis wrote: On Wednesday, January 02, 2013 13:45:32 Maxim Fomin wrote: I think it should not be fixed, but probably compiler may issue warning at some circumstances when it can realize this situation

Re: ref is unsafe

2013-01-02 Thread H. S. Teoh
On Wed, Jan 02, 2013 at 06:30:38PM -0500, Jonathan M Davis wrote: > On Wednesday, January 02, 2013 15:06:24 H. S. Teoh wrote: > > All extern(C) functions must be @system by default. It makes no sense to > > allow a @safe extern(C) function, since there is no way for the compiler > > to verify anyth

Re: ref is unsafe

2013-01-02 Thread Thiez
On Wednesday, 2 January 2013 at 22:53:04 UTC, Jonathan M Davis wrote: Then we're going to have to disagree, and I believe that Walter and Andrei are completely with me on this one. If all of the constructs that you use are @safe, then it should be _guaranteed_ that your program is memory-safe.

Re: ref is unsafe

2013-01-02 Thread Jonathan M Davis
On Wednesday, January 02, 2013 15:06:24 H. S. Teoh wrote: > All extern(C) functions must be @system by default. It makes no sense to > allow a @safe extern(C) function, since there is no way for the compiler > to verify anything at all. The best you can do is @trusted. Agreed. And @trusted is seri

Re: ref is unsafe

2013-01-02 Thread H. S. Teoh
On Wed, Jan 02, 2013 at 05:52:54PM -0500, Jonathan M Davis wrote: > On Wednesday, January 02, 2013 23:21:55 Maxim Fomin wrote: > > Again, I argue that D is a system language and there are many > > possibilities to break @safity. Although fixing holes does make > > sense in general, it does not make

Re: ref is unsafe

2013-01-02 Thread Jonathan M Davis
On Wednesday, January 02, 2013 23:21:55 Maxim Fomin wrote: > Again, I argue that D is a system language and there are many > possibilities to break @safity. Although fixing holes does make > sense in general, it does not make sense fixing obvious issues so > that plenty of code becomes uncompilable

Re: ref is unsafe

2013-01-02 Thread Maxim Fomin
On Wednesday, 2 January 2013 at 19:37:51 UTC, Jonathan M Davis wrote: On Wednesday, January 02, 2013 13:45:32 Maxim Fomin wrote: I think it should not be fixed, but probably compiler may issue warning at some circumstances when it can realize this situation. It's a hole in @safe. It must be f

Re: ref is unsafe

2013-01-02 Thread Jonathan M Davis
On Wednesday, January 02, 2013 13:45:32 Maxim Fomin wrote: > I think it should not be fixed, but probably compiler may issue > warning at some circumstances when it can realize this situation. It's a hole in @safe. It must be fixed. That's not even vaguely up for discussion. The question is _how_

Re: ref is unsafe

2013-01-02 Thread Maxim Fomin
On Sunday, 30 December 2012 at 08:38:27 UTC, Jonathan M Davis wrote: After some recent discussions relating to auto ref and const ref, I have come to the conlusion that as it stands, ref is not @safe. It's @system. This is not a surprise, I remember Andrei was talking about it 1.5 year ago.

Re: ref is unsafe

2013-01-01 Thread Jonathan M Davis
On Monday, December 31, 2012 22:25:52 Mehrdad wrote: > I don't understand why there is a discussion on trying to > special-case ref parameters. There's nothing special about ref > parameters... what's special is ref _returns_. > > Therefore all we need to do is disallow ref returns in @safe code.

Re: ref is unsafe

2012-12-31 Thread Mehrdad
On Monday, 31 December 2012 at 23:39:35 UTC, Rob T wrote: ref returns where the return result cannot be proven to be safe Halting problem?

Re: ref is unsafe

2012-12-31 Thread Rob T
On Monday, 31 December 2012 at 21:25:53 UTC, Mehrdad wrote: I don't understand why there is a discussion on trying to special-case ref parameters. There's nothing special about ref parameters... what's special is ref _returns_. Therefore all we need to do is disallow ref returns in @safe cod

Re: ref is unsafe

2012-12-31 Thread Mehrdad
I don't understand why there is a discussion on trying to special-case ref parameters. There's nothing special about ref parameters... what's special is ref _returns_. Therefore all we need to do is disallow ref returns in @safe code.

Re: ref is unsafe

2012-12-31 Thread Nick Treleaven
On 31/12/2012 14:44, Nick Treleaven wrote: On 30/12/2012 22:01, Jonathan M Davis wrote: The closest that we could get to what you suggest would be to add a new attribute similar to nothrow but which guarantees that the function does not return a ref to a parameter. So, you'd have to mark your fu

Re: ref is unsafe

2012-12-31 Thread Nick Treleaven
On 30/12/2012 22:01, Jonathan M Davis wrote: On Sunday, December 30, 2012 17:32:40 Nick Treleaven wrote: I think the compiler needs to be able to mark foo as a function that returns its input reference. Then, any arguments to foo that are locals should cause an error at the call site (e.g. in ba

Re: ref is unsafe

2012-12-30 Thread Zach the Mystic
On Monday, 31 December 2012 at 02:47:46 UTC, Jonathan M Davis wrote: 3. Create a new attribute which has to be used when a function returns a ref to a parameter and use that to make it illegal to pass a ref to a local variable to such functions. If this is the way to go, maybe "@saferef" co

Re: ref is unsafe

2012-12-30 Thread Michel Fortin
On 2012-12-30 22:29:33 +, Jonathan M Davis said: Good point. Member variables of parameters also cause problems. So, it very quickly devolves to any function which accepts a user-defined type by ref and returns anything by ref would have to be @system, which is far from pleasant. Note tha

Re: ref is unsafe

2012-12-30 Thread Jonathan M Davis
On Monday, December 31, 2012 03:34:18 Carl Sturtivant wrote: > The implementation of delegates has solved an analogous problem. Delegates use closures. The stack of the calling function is copied onto the heap so that it will continue to be valid for the delegate after the function returns. We d

Re: ref is unsafe

2012-12-30 Thread Carl Sturtivant
/* The implementation of delegates has solved an analogous problem. e.g. */ import std.stdio; auto getfun( int x) { int y = x*x; int ysquared() { return y*y; } return &ysquared; } void main() { auto f1 = getfun(2); auto f2 = getf

Re: ref is unsafe

2012-12-30 Thread Jonathan M Davis
On Monday, December 31, 2012 02:37:56 Rob T wrote: > This may be far fetched, but consider this, > > If a function returns a ref that is the same as what was passed > in by ref, then the passed and return addresses would match, > which means that it still may be possible for the compiler to > dete

Re: ref is unsafe

2012-12-30 Thread Rob T
On Sunday, 30 December 2012 at 22:30:24 UTC, Jonathan M Davis wrote: On Sunday, December 30, 2012 23:18:43 jerro wrote: > Honestly though, I'm inclined to argue that functions which > return by ref and > have a ref parameter of that same type just be considered > @system. What about this: stru

Re: ref is unsafe

2012-12-30 Thread Jonathan M Davis
On Sunday, December 30, 2012 23:18:43 jerro wrote: > > Honestly though, I'm inclined to argue that functions which > > return by ref and > > have a ref parameter of that same type just be considered > > @system. > > What about this: > > struct Foo > { > int a; > } > > ref int bar(ref Foo fo

Re: ref is unsafe

2012-12-30 Thread jerro
Honestly though, I'm inclined to argue that functions which return by ref and have a ref parameter of that same type just be considered @system. What about this: struct Foo { int a; } ref int bar(ref Foo foo) { return foo.a; } the parameter type and the return type here are different

Re: ref is unsafe

2012-12-30 Thread Jonathan M Davis
On Sunday, December 30, 2012 17:32:40 Nick Treleaven wrote: > I think the compiler needs to be able to mark foo as a function that > returns its input reference. Then, any arguments to foo that are locals > should cause an error at the call site (e.g. in baz). So legal calls to > foo can always be

Re: ref is unsafe

2012-12-30 Thread jerro
Here the compiler already knows that foo returns its input reference. So it checks whether foo is being passed a local - no; but it also has to check if foo is passed any ref parameters of quux, which it is. The compiler now has to mark quux as a function that returns its input reference. Wor

Re: ref is unsafe

2012-12-30 Thread Rob T
On Sunday, 30 December 2012 at 17:32:41 UTC, Nick Treleaven wrote: On 30/12/2012 09:17, Jonathan M Davis wrote: The problem is the wrapper function. You'd also have to disallow functions from returning ref parameters by ref. Otherwise, ref int foo(ref int i) { return i; } ref int baz(int

Re: ref is unsafe

2012-12-30 Thread Nick Treleaven
On 30/12/2012 09:17, Jonathan M Davis wrote: The problem is the wrapper function. You'd also have to disallow functions from returning ref parameters by ref. Otherwise, ref int foo(ref int i) { return i; } ref int baz(int i) { return foo(i); } continues to cause problems. And making

Re: ref is unsafe

2012-12-30 Thread Dmitry Olshansky
12/30/2012 12:37 PM, Jonathan M Davis пишет: After some recent discussions relating to auto ref and const ref, I have come to the conlusion that as it stands, ref is not @safe. It's @system. And I think that we need to take a serious look at it to see what we can do to make it @safe. The problem

Re: ref is unsafe

2012-12-30 Thread Jonathan M Davis
On Sunday, December 30, 2012 11:04:35 monarch_dodra wrote: > Wouldn't it be enough to disallow functions that both take and > return by ref? There would still be some limitations, but at > least: > > // > @property ref T front(T)(T[] a); > // > Would still be @safe. > > It seams the only

Re: ref is unsafe

2012-12-30 Thread monarch_dodra
On Sunday, 30 December 2012 at 09:18:30 UTC, Jonathan M Davis wrote: On Sunday, December 30, 2012 10:04:01 Daniel Kozak wrote: IMHO, try to return ref to local variable should be error, and such a code shouldn't be compilable You can disallow that in the easy case of ref int boo(int i) {

Re: ref is unsafe

2012-12-30 Thread Jonathan M Davis
On Sunday, December 30, 2012 10:04:01 Daniel Kozak wrote: > IMHO, try to return ref to local variable should be error, and > such a code shouldn't be compilable You can disallow that in the easy case of ref int boo(int i) { return i; } and in fact, that's already illegal. The problem is the

Re: ref is unsafe

2012-12-30 Thread Daniel Kozak
On Sunday, 30 December 2012 at 08:38:27 UTC, Jonathan M Davis wrote: After some recent discussions relating to auto ref and const ref, I have come to the conlusion that as it stands, ref is not @safe. It's @system. And I think that we need to take a serious look at it to see what we can do to m

ref is unsafe

2012-12-30 Thread Jonathan M Davis
After some recent discussions relating to auto ref and const ref, I have come to the conlusion that as it stands, ref is not @safe. It's @system. And I think that we need to take a serious look at it to see what we can do to make it @safe. The problem is combining code that takes ref parameters