lifetime issue: Bug or not?

Steven Schveighoffer schveiguy at gmail.com
Fri Nov 22 18:54:49 UTC 2019


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


More information about the Digitalmars-d mailing list