<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Miscompilation of for-loop with a declaration condition initialized with ternary operator"
href="https://bugs.llvm.org/show_bug.cgi?id=50038">50038</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Miscompilation of for-loop with a declaration condition initialized with ternary operator
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>egor.suvorov@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Consider the following code:
#include <iostream>
int main()
{
for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
std::cout << x;
}
}
Its expected output is 11: there are two iterations with i=0 (x=1) and i=1
(x=1), and then the final iterations with i=2 (x=0) stops the loop.
However, clang miscompiles it to produce an infinite series of 000000...
It also gives following warnings:
<source>:5:20: warning: variable 'x' is uninitialized when used here
[-Wuninitialized]
std::cout << x;
^
<source>:4:19: note: variable 'x' is declared here
for (int i = 0; int x = i < 2 ? 1 : 0; i++) {
The behavior traces back as far as clang 8.0.0, see Godbolt:
<a href="https://godbolt.org/z/jMb37G7nq">https://godbolt.org/z/jMb37G7nq</a>
If I replace `i < 2 ? 1 : 0` with `2 - i`, the output is `21` as expected.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>