Boost C++ Libraries: Ticket #8665: Unreachable code warnings in variant.hpp - msvc https://svn.boost.org/trac10/ticket/8665 <p> This code produces C4702 warnings when compiled with warning level 4 and optimization enabled (/W4 and /O2). Tested with both VC10 and VC11. </p> <pre class="wiki">#include "boost/variant/variant.hpp" int main() { boost::variant&lt; int, double&gt; a, b; if ( a &lt; b ) { return 1; } return 0; } </pre><p> d:\development\include\boost\variant\variant.hpp(353) : warning C4702: unreachable code d:\development\include\boost\variant\variant.hpp(353) : warning C4702: unreachable code d:\development\include\boost\variant\variant.hpp(1017) : warning C4702: unreachable code d:\development\include\boost\variant\variant.hpp(1017) : warning C4702: unreachable code </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8665 Trac 1.4.3 Antony Polukhin Thu, 06 Jun 2013 14:46:33 GMT cc set https://svn.boost.org/trac10/ticket/8665#comment:1 https://svn.boost.org/trac10/ticket/8665#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">antoshkka@…</span> added </li> </ul> <p> Tested on boost 1_54_0_beta1 and this looks more like a visual studio bug. In debug mode we do <strong>reach</strong> that code. But in release mode Visual studio inlines all the function calls and complains on function that <strong>is actually called</strong>. </p> <p> Unfortunately I fail to reproduce this issue without <code>boost::variant</code>. Can you? </p> Ticket hvemha@… Fri, 07 Jun 2013 08:38:08 GMT <link>https://svn.boost.org/trac10/ticket/8665#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8665#comment:2</guid> <description> <p> Not sure about the warning on line 1017. Only got this when building from the command line: cl /W4 /O2 Test.cpp </p> <p> A default C++ project in Visual Studio and only changing the warning level in release build produces 4 C4702 warnings about line 353 for code. </p> <p> It is clearly a bug to reach this line. I think it is the return and not the function all producing the warning. Probably an issue with the compiler (inline and <code>__declspec(noreturn)</code>?). </p> <p> Also not sure what you mean by without boost::variant? operator&lt; and operator== instantiates know_get, and this in turn results in the warnings on line 353. </p> <p> Is it possible to just disable the C4702 warning in the never to be called operator()(U&amp;)? It should be OK in this case. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 07 Jun 2013 11:48:48 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8665#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8665#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8665#comment:2" title="Comment 2">hvemha@…</a>: </p> <blockquote class="citation"> <p> Not sure about the warning on line 1017. Only got this when building from the command line: cl /W4 /O2 Test.cpp </p> </blockquote> <p> This is the only warning I can reproduce on my visual studio. My previous message was about it.<br /><br /> </p> <blockquote class="citation"> <p> A default C++ project in Visual Studio and only changing the warning level in release build produces 4 C4702 warnings about line 353 for code. </p> </blockquote> <p> Can not reproduce it on visual studio 11. May be it disappears after some updates of visual studio? My version is <code>11.0.60315.01 Update 2</code>. What's yours?<br /><br /> </p> <blockquote class="citation"> <p> Also not sure what you mean by without boost::variant? operator&lt; and operator== instantiates know_get, and this in turn results in the warnings on line 353. </p> </blockquote> <p> Usually all compiler developers require simplified version of code to reproduce compiler issue. Such code shall not include additional third party headers (<code>variant.hpp</code>) and shall contain minimal amount of code.<br /><br /> </p> <blockquote class="citation"> <p> Is it possible to just disable the C4702 warning in the never to be called operator()(U&amp;)? It should be OK in this case. </p> </blockquote> <p> It is OK if this warning can not be fixed by code modifications. I'm just trying to figure out how to reproduce it, does MS know about that issue, is that really an issue, how can it be reproduced outside Boost. </p> </description> <category>Ticket</category> </item> <item> <author>hvemha@…</author> <pubDate>Fri, 06 Sep 2013 15:03:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8665#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8665#comment:4</guid> <description> <p> The warning on line 1017 is supressed by defining NDEBUG (adding /D NDEBUG to the command line). I’m not too concerned about this as NDEBUG should be defined when creating optimized builds. </p> <p> I used the same version (now on update 3). The warning is also produced by earlier versions of the VC. </p> <p> This simple code will produce the warning: </p> <pre class="wiki">__declspec(noreturn) void no_return() {} int call_no_return() { no_return(); } int main(int argc, char**) { if( argc &gt; 0 ) { return call_no_return(); // warning C4702 } return 7; } </pre><p> I guess the warning is related to assigning the return value of <code>call_no_return</code> to a variable in main scope (in an intermediate compiler language?), and not the function call. </p> <p> Looking at the machine code it looks like the optimizer will assume the function never returns when declared with <code>__declspec(noreturn)</code>. The optimizer change the above code to <code>"return 7;"</code>, but if a side effect is introduces into <code>no_return</code>, the behaviour is undefined (typically a crash when returning from <code>no_return</code> as the compiler has not included the required instructions). In non-optimized builds the compiler ignores the <code>noreturn</code> declaration and generates instructions as if the function will/can return. </p> <p> In my opinion the compiler is correct to issue the warning (the programmer has told the compiler that the function will not return). </p> <p> Note: the fact that the compiler changes my example code to <code>"return 7;"</code> is also OK as for <code>argc &gt; 0</code> the behaviour is undefined... </p> </description> <category>Ticket</category> </item> <item> <author>hvemha@…</author> <pubDate>Fri, 06 Sep 2013 15:04:39 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/8665 https://svn.boost.org/trac10/ticket/8665 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">variant.hpp.patch</span> </li> </ul> <p> Suggested patch </p> Ticket Antony Polukhin Wed, 25 Sep 2013 07:13:58 GMT <link>https://svn.boost.org/trac10/ticket/8665#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8665#comment:5</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/85876" title="Fix unreachable code warning, thanks 'hvemha' for providing it (refs #8665)">[85876]</a>) Fix unreachable code warning, thanks 'hvemha' for providing it (refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8665" title="#8665: Bugs: Unreachable code warnings in variant.hpp - msvc (closed: fixed)">#8665</a>) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Wed, 25 Sep 2013 08:04:37 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8665#comment:6 https://svn.boost.org/trac10/ticket/8665#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/85882" title="Merge from trunk: * Add some merge infos * Fix unreachable code ...">[85882]</a>) Merge from trunk: </p> <ul><li>Add some merge infos </li><li>Fix unreachable code warning (fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8665" title="#8665: Bugs: Unreachable code warnings in variant.hpp - msvc (closed: fixed)">#8665</a>) </li></ul> Ticket