lifetime issue: Bug or not?

Meta jared771 at gmail.com
Sat Nov 23 03:46:18 UTC 2019


On Friday, 22 November 2019 at 18:54:49 UTC, Steven Schveighoffer 
wrote:
> I have code in my proprietary project that looks like this:
>
> struct X
> {
>    ...
>    SQLStatement foo()
>    {
>       auto stmt = SQLStatement("`foo`"); // table
>       return stmt.where("`id` = ?", id);
>    }
> }
>
> where is a function that adds a where clause with a given value 
> to the SQL statement. It takes an SQLStatement by ref, and 
> returns that statement by ref.
>
> Signature looks like this:
>
> ref SQLStatement where(T)(return ref SQLStatement stmt, string 
> clause, T val)
>
> In this case, I'm using the convenience of returning the 
> statement to return that from the function.
>
> However, the compiler is complaining:
>
>  Error: returning where(stmt, "`id` = ?", id) escapes a 
> reference to local variable stmt
>
> But... it's not returning a ref. So why the complaint?
>
> I have to separate into 2 lines.
>
> I tried to make a simple example to show the problem, but looks 
> like it doesn't fail in that case.
>
> Any ideas?
>
> -Steve

Are you passing the -dip1000 switch to the compiler? I reproduced 
this with a toy version of your code, but the compile error goes 
away when I pass the -dip1000 switch to the compiler.


More information about the Digitalmars-d mailing list