lifetime issue: Bug or not?

Steven Schveighoffer schveiguy at gmail.com
Sat Nov 23 12:10:07 UTC 2019


On 11/22/19 11:26 PM, Walter Bright wrote:
> On 11/22/2019 10:54 AM, 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)
> 
>    ^^^                       ^^^^^^^^^^
> 
>>   Error: returning where(stmt, "`id` = ?", id) escapes a reference to 
>> local variable stmt
>>
>> But... it's not returning a ref.
> 
> Yes it is. See the ^^^^

Yes, where is returning ref, but the complaint is not focused on that. 
It's saying returning the *result* of the where call is escaping the 
local. It's not. foo doesn't return ref.

If I split it into:

auto result = stmt.where("`id` = ?", id);
return result;

It works. This is what I'm expecting the compiler to do automatically.

I'll note that I was using inferred return types previously, and I 
thought maybe that was the problem. But here I have specified the return 
type exactly.

-Steve


More information about the Digitalmars-d mailing list