D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11287 - NRVO should remove dtor call completely
Summary: NRVO should remove dtor call completely
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-10-17 02:02 UTC by Kenji Hara
Modified: 2024-12-13 18:13 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2013-10-17 02:02:16 UTC
This is similar to bug 11286, but slightly different.

Test case:

struct A
{
    ~this() {}
}

A getA() pure
{
    A a;        // line 7
    return a;   // NRVO
}

void main()
{
    A a = getA();
}

Output:

test.d(7): Error: pure function 'test.getA' cannot call impure function 'test.A.~this'

In the function getA(), the variable 'a' will be moved out to the caller by NRVO, and its dtor won't be called. So compiler should accept the case properly.
Comment 1 monarchdodra 2013-10-17 05:41:32 UTC
Question:

Doesn't this mean that the compiler *must* implement NRVO? I thought NRVO was an optimization *opportunity*?

If
//----
A getA() pure
{
    A a;        // line 7
    return a;   // NRVO
}
//----

Is legal, then it means that only compilers hat implement NRVO will handle this.

Sorry if it's a stupid question.
Comment 2 Kenji Hara 2013-10-17 06:48:36 UTC
(In reply to comment #1)
> Question:
> 
> Doesn't this mean that the compiler *must* implement NRVO? I thought NRVO was
> an optimization *opportunity*?
> 
> If
> //----
> A getA() pure
> {
>     A a;        // line 7
>     return a;   // NRVO
> }
> //----
> 
> Is legal, then it means that only compilers hat implement NRVO will handle
> this.
> 
> Sorry if it's a stupid question.

Unfortunately current D language spec does not mention about NRVO, so It's still one of the optimizations. But, at least, current dmd always apply NRVO for the specific test case.

And, in D we can check the semantic analysis result by using is(typeof()) and __traits(compiles). I believe that "if these compile-time checker primitives return true, it should mean that the specific D code will generate correct runtime code, and vice versa". For example, since "cannot access frame pointer" error had not been able to detect in compile-time, but now it can.

Back to the case, dmd could generate correct binary for the code by using NRVO. So dmd should not cause "cannot call impure function" error.
Comment 3 Max Samukha 2013-10-17 08:12:42 UTC
(In reply to comment #2)

> Unfortunately current D language spec does not mention about NRVO, so It's
> still one of the optimizations. But, at least, current dmd always apply NRVO
> for the specific test case.

How are the standard library's constructs such as "scoped" supposed to work if NRVO is optional?
Comment 4 Kenji Hara 2013-10-17 09:08:07 UTC
(In reply to comment #3)
> (In reply to comment #2)
> 
> > Unfortunately current D language spec does not mention about NRVO, so It's
> > still one of the optimizations. But, at least, current dmd always apply NRVO
> > for the specific test case.
> 
> How are the standard library's constructs such as "scoped" supposed to work if
> NRVO is optional?

Yes. std.typecons.scoped requires NRVO in order to implement its own semantic. That's why NRVO should be mentioned properly in language spec.
Comment 5 Denis Shelomovskii 2013-11-05 08:21:57 UTC
(In reply to comment #4)
> Yes. std.typecons.scoped requires NRVO in order to implement its own semantic.
> That's why NRVO should be mentioned properly in language spec.

This is Issue 10372.
Comment 6 dlangBugzillaToGithub 2024-12-13 18:13:06 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18699

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB