<div dir="ltr">Thanks! I've committed the _2 patch as r<span style="color:rgb(0,0,0)">200098. Please go ahead and close the bug.</span></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jan 25, 2014 at 6:48 AM, Rahul Jain <span dir="ltr"><<a href="mailto:1989.rahuljain@gmail.com" target="_blank">1989.rahuljain@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Good catch Richard! Thanks.</div><div><br></div><div>Have attached a couple of workarounds for the same. Both make sure that the correct statements are executed</div>
<div>for respective cases.</div><div>

<br></div><div>Thanks,</div><div>Rahul</div><div><div><br></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jan 25, 2014 at 4:29 AM, Richard Smith <span dir="ltr"><<a href="mailto:metafoo@gmail.com" target="_blank">metafoo@gmail.com</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div>On Fri Jan 24 2014 at 3:07:55 AM, Rahul Jain <<a href="mailto:rahul1.jain@samsung.com" target="_blank">rahul1.jain@samsung.com</a>> wrote:</div>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">






<div>
<p> </p>
<p>Hi Richard,</p>
<p> </p>
<p>Thanks for your inputs.</p>
<p>As per review comments, I am now checking if the element contained in the InitList is an ICE.</p>
<p>Also added a test in constant-expression.cpp. </p>
<p>Please comment for further improvements.</p>
<p> </p>
<p>Patch:</p>
<p> </p>
<p>Index: test/SemaCXX/constant-expression.cpp<br>===================================================================<br>--- test/SemaCXX/constant-expression.cpp (revision 199888)<br>+++ test/SemaCXX/constant-expression.cpp (working copy)<br>



@@ -124,3 +124,12 @@<br>   struct Y { bool b; X x; }; // expected-error {{field has incomplete type 'test3::X'}}<br>   int f() { return Y().b; }<br> }<br>+<br>+// PR18283<br>+namespace test4 {<br>+  template <int> struct A {};<br>



+  int const i = { 42 };<br>+  // i can be used as non-type template-parameter as "const int x = { 42 };" is<br>+  // equivalent to "const int x = 42;" as per C++03 8.5/p13.<br>+  typedef A<i> Ai; // ok<br>



+}<br>Index: lib/AST/ExprConstant.cpp<br>===================================================================<br>--- lib/AST/ExprConstant.cpp (revision 199888)<br>+++ lib/AST/ExprConstant.cpp (working copy)<br>@@ -8331,7 +8331,14 @@<br>



   case Expr::MaterializeTemporaryExprClass:<br>   case Expr::PseudoObjectExprClass:<br>   case Expr::AtomicExprClass:<br>-  case Expr::InitListExprClass:<br>+  case Expr::InitListExprClass: {<br></p></div></blockquote><div>



<br></div></div></div><div>The fallthrough here isn't correct. What follows should only apply to InitListExprClass, not to the other kinds of expression that land here.</div><div><div><div><br>

</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><p>+    // C++03 8.5/13 says that If T is a scalar type, then a declaration of the<br>+    // form "T x = { a };" is equivalent to "T x = a;". T is a scalar as it is  <br>+    // known to be of integral or enumeration type.<br>



+    if (E->isRValue())<br>+      if (cast<InitListExpr>(E)->getNumInits() == 1)<br>+        return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx);<br>+  }<br>   case Expr::LambdaExprClass:<br>     return ICEDiag(IK_NotICE, E->getLocStart());</p>



</div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><p>Thanks,</p></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div>
<p>Rahul</p>
<p> </p>
<p>------- <b>Original Message</b> -------</p>
<p><b>Sender</b> : Richard Smith<<a href="mailto:metafoo@gmail.com" target="_blank">metafoo@gmail.com</a>></p>
<p><b>Date</b> : Jan 24, 2014 04:00 (GMT+05:30)</p>
<p><b>Title</b> : [PATCH] Fix for 18283 - Const int initialized with curly braces is not a compile time constant</p>
<p> </p>
<div>On Thu Jan 23 2014 at 2:24:25 AM, Rahul Jain <<a href="mailto:rahul1.jain@samsung.com" target="_blank">rahul1.jain@samsung.com</a>> wrote:</div>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:#ccc 1px solid">
<div>
<p> </p>
<p>Thanks Marshal and Richard for the valuable inputs.</p>
<p> </p>
<p>Hi Richard,</p>
<p> </p>
<p>I have implemented a patch as per your suggestion, adding a check to</p>
<p>handle the case of InitListExpr, when it is used as an RValue.</p>
<p> </p>
<p>Please if you could review it and suggest improvements.</p></div></blockquote>
<div><br></div>
<div>The patch needs a corresponding regression test to be added to some file in tests/. test/SemaCXX/constant-expression.cpp seems like a good place.</div>
<div><br></div>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:#ccc 1px solid">
<div>
<p>Index: tools/clang/lib/AST/ExprConstant.cpp<br>===================================================================<br>--- tools/clang/lib/AST/ExprConstant.cpp (revision 199874)<br>+++ tools/clang/lib/AST/ExprConstant.cpp (working copy)<br>



@@ -8331,7 +8331,14 @@<br>   case Expr::MaterializeTemporaryExprClass:<br>   case Expr::PseudoObjectExprClass:<br>   case Expr::AtomicExprClass:<br>-  case Expr::InitListExprClass:<br>+  case Expr::InitListExprClass: {<br>



+    // Fix for PR 18283, if InitListExpr is used as an RValue containing<br>+    // only one scalar value, it is an ICE.<br></p></div></blockquote>
<div><br></div>
<div>Don't reference a PR number here; the code should be able to justify its existence without reference to bugs. Instead, you can say that C++03 [dcl.init]p13 says that "T x = { a };" is equivalent to "T x = a;" when T is a scalar type (and maybe point out that T must be a scalar type because we know it's an integral or enumeration type). You should reference the PR number in the test case, instead.</div>




<div><br></div>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:#ccc 1px solid">
<div>
<p>+    if (E->isRValue())<br>+      if (cast<InitListExpr>(E)->getNumInits() == 1)<br>+        return NoDiag();             <br></p></div></blockquote>
<div><br></div>
<div>This isn't correct -- you can't assume that any braced initialization is a constant expression. Instead, call CheckICE on the subexpression.</div>
<div><br></div>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:#ccc 1px solid">
<div>
<p>+  }<br>+<br>   case Expr::LambdaExprClass:<br>     return ICEDiag(IK_NotICE, E->getLocStart());</p>
<p><br> </p>
<p>Thanks,</p>
<p>Rahul</p>
<p> </p>
<p>------- <b>Original Message</b> -------</p>
<p><b>Sender</b> : Richard Smith<<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></p>
<p><b>Date</b> : Jan 23, 2014 01:12 (GMT+05:30)</p>
<p><b>Title</b> : Re: [cfe-dev] <u></u>C++ 03 - Need clarification regarding non-type template argument</p>
<p> </p>
<div dir="ltr">
<div>
<div>On Wed, Jan 22, 2014 at 11:22 AM, Marshall Clow <span dir="ltr"><<a href="mailto:mclow.lists@gmail.com" target="_blank">mclow.lists@gmail.com</a>></span> wrote:<br>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:rgb(204,204,204) 1px solid">
<div style="WORD-WRAP:break-word"><br>
<div>
<div>
<div>On Jan 21, 2014, at 5:59 AM, Rahul Jain <<a href="mailto:rahul1.jain@samsung.com" target="_blank">rahul1.jain@samsung.com</a>> wrote:</div><br>
<blockquote type="cite">
<div style="WHITE-SPACE:normal;TEXT-TRANSFORM:none;WORD-SPACING:0px;FONT:9pt/1.4 Arial,arial;MARGIN:10px;LETTER-SPACING:normal;TEXT-INDENT:0px">
<div style="MARGIN-BOTTOM:5px;FONT-SIZE:9pt;FONT-FAMILY:Arial,arial;MARGIN-TOP:5px"><br> </div>
<p style="MARGIN-BOTTOM:5px;FONT-SIZE:9pt;FONT-FAMILY:Arial,arial;MARGIN-TOP:5px">Hi all,</p>
<div style="MARGIN-BOTTOM:5px;FONT-SIZE:9pt;FONT-FAMILY:Arial,arial;MARGIN-TOP:5px"><br> </div>
<div>Note - this is with respect to C++ 03.</div>
<div></div>
<div><em></em> </div>
<div><em>Test code:</em></div>
<div><em></em> </div>
<div><em>template <int> struct A {};<br>int const i = {42};<br>typedef A<i> Ai;</em></div></div></blockquote>
<div><br></div></div>Short, snarky answer - you can’t bracket-initialize a variable in c++03</div></div></blockquote>
<div><br></div>
<div>You can in a few cases: specifically, if the type is an aggregate or a scalar type. In the latter case,</div>
<div><br></div>
<div>   T x = { a };</div>
<div><br></div>
<div>is equivalent to</div>
<div><br></div>
<div>  T x = a;</div>
<div><br></div>
<div>(See C++03 [dcl.init](8.5)/13.)</div>
<div> </div>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:rgb(204,204,204) 1px solid">
<div style="WORD-WRAP:break-word">
<div>
<div><span style="WHITE-SPACE:pre-wrap"></span>int const i = {42};</div>
<div><br></div></div>
<div>
<div>
<blockquote type="cite">
<div style="WHITE-SPACE:normal;TEXT-TRANSFORM:none;WORD-SPACING:0px;FONT:9pt/1.4 Arial,arial;MARGIN:10px;LETTER-SPACING:normal;TEXT-INDENT:0px">
<div>For non-type template argument, standard (C++ 03) says that:</div>
<div></div>
<div><em>14.3.2 / 1</em></div>
<p style="MARGIN-BOTTOM:5px;FONT-SIZE:9pt;FONT-FAMILY:Arial,arial;MARGIN-TOP:5px"><em>A template-argument for a non-type, non-template template-parameter shall be one of:</em></p>
<ul><em></em>
<li style="MARGIN-BOTTOM:5px;FONT-SIZE:9pt;FONT-FAMILY:Arial,arial;MARGIN-TOP:5px"><em>an<span> </span><strong>integral constant-expression</strong><span> </span>of integral or enumeration type; or ......</em></li></ul>
<div>For the test code above, the statement '<em>typedef A<i> Ai</em>', should the compiler</div>
<div>check the rhs (rvalue) of variable 'i' to determine if it is a constant or not?</div></div></blockquote></div></div></div></blockquote>
<div><br></div>
<div>Yes. For 'i' to be an integral constant-expression, it can only mention "const variables [...] of integral or enumeration types initialized with constant expressions" (C++03 [expr.const](5.19)/1). So the compiler is required to check whether 'i' is indeed initialized by a constant expression.</div>




<div><br></div>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:rgb(204,204,204) 1px solid">
<div style="WORD-WRAP:break-word">
<div>
<div>
<blockquote type="cite">
<div style="WHITE-SPACE:normal;TEXT-TRANSFORM:none;WORD-SPACING:0px;FONT:9pt/1.4 Arial,arial;MARGIN:10px;LETTER-SPACING:normal;TEXT-INDENT:0px">
<div>Shouldnt it just check the type of i (which is declared as const)?</div>
<div>Whether 'i' evaluates to constant (at compile time) or not should be the responsibility of compiler</div>
<div>while processing statement '<em>int const i = {42}</em>' and not '<em>typedef A<i> Ai' ? Isnt it?</em></div>
<div> </div>
<div>Another analogy:<br>For arrays, the size of the array needs to be a compile time constant. So, the following code :</div>
<div></div>
<div><em></em> </div>
<div><em>int const x = 42;</em></div>
<div><em>int z[x];</em></div>
<div></div>
<div> </div>
<div>For the statement<span> </span><em>'int z[x]'</em>, will the compiler evaluate the rhs of<span> </span><em>'x'</em><span> </span>to check if it evaluates to constant at runtime</div>
<div>or will it just see that '<em>x</em>' is of 'integer const' type and hence there is no problem with the array declaration?</div>
<div>Whether '<em>x</em>' evaluates to constant should be the responsibility of compiler while processing statement '<em>int const x=42</em>'</div>
<div>and not while processing '<em>int z[x]</em>' ?</div>
<div></div>
<div> </div>
<div>Should this analogy be applicable to non-type template argument?</div></div></blockquote></div></div></div></blockquote>
<div><br></div>
<div>According to the C++ standard, the two cases are exactly the same. However, to support code relying on GCC extensions, we allow any constant-foldable expression as an array bound, whereas we require a constant-expression as a template argument. Use -pedantic-errors to enable Clang's strictly-conforming mode, where we (incorrectly) reject both cases.</div>




<div><br></div>
<div>The bug is that CheckICE in lib/AST/ExprConstant.cpp (or maybe VarDecl::checkInitIsICE) doesn't handle the case of an InitListExpr being used to initialize a scalar.</div>
<div><br></div>
<blockquote style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:rgb(204,204,204) 1px solid">
<div style="WORD-WRAP:break-word">
<div>
<div>
<blockquote type="cite">
<div style="WHITE-SPACE:normal;TEXT-TRANSFORM:none;WORD-SPACING:0px;FONT:9pt/1.4 Arial,arial;MARGIN:10px;LETTER-SPACING:normal;TEXT-INDENT:0px">
<div></div>
<p style="MARGIN-BOTTOM:5px;FONT-SIZE:9pt;FONT-FAMILY:Arial,arial;MARGIN-TOP:5px">Please throw some light on this, am I missing any particular case?</p></div></blockquote><br></div></div>
<div>My understanding (which may be flawed) is that the compiler needs to see both:</div>
<div>* That the compiler needs to be able to determine that the value it needs is const</div>
<div>* That the compiler can see what the value actually is, so that it can allocate space for the array, or instantiate the template.</div>
<div><br></div>
<div><br></div>
<div>So, this would not work:</div>
<div><br></div><span style="WHITE-SPACE:pre-wrap"></span>extern const int i; 
<div><span style="WHITE-SPACE:pre-wrap"></span>template <int> struct A {};<br><span style="WHITE-SPACE:pre-wrap"></span>typedef A<i> Ai;</div></div></blockquote>
<div><br></div>
<div>Right. </div></div></div></div>
<p> </p>
<p> </p>
<p> </p>
<table>
<tbody>
<tr>
<td>
<p><img border="0" src="cid:Z5JE7EUABGFC@namo.co.kr" width="520"></p></td></tr></tbody></table></div><img border="0" src="http://ext.samsung.net/mailcheck/SeenTimeChecker?do=138117183f8501de87a76644c0563226b38ca38a19637fd57fd152fcb9c0a7b71949e5361b5b07078e8c935117e1541af8ae1b241f6e633f08cece8541bc14eacf878f9a26ce15a0" width="0" height="0"></blockquote>




<p> </p>
<p> </p>
<p> </p>
<table>
<tbody>
<tr>
<td>
<p><img border="0" src="cid:LK7CT9SZN3WZ@namo.co.kr" width="520"></p></td></tr></tbody></table></div><img src="http://ext.samsung.net/mailcheck/SeenTimeChecker?do=138117183f8501de249d92c6b5635fb1975cffd5c35225147fd152fcb9c0a7b71949e5361b5b07078e8c935117e1541af8ae1b241f6e633f08cece8541bc14eacf878f9a26ce15a0" border="0" width="0" height="0"></blockquote>



</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>