[llvm-bugs] [Bug 50038] New: Miscompilation of for-loop with a declaration condition initialized with ternary operator
via llvm-bugs
llvm-bugs at lists.llvm.org
Tue Apr 20 06:13:32 PDT 2021
https://bugs.llvm.org/show_bug.cgi?id=50038
Bug ID: 50038
Summary: Miscompilation of for-loop with a declaration
condition initialized with ternary operator
Product: clang
Version: trunk
Hardware: PC
OS: Windows NT
Status: NEW
Severity: enhancement
Priority: P
Component: -New Bugs
Assignee: unassignedclangbugs at nondot.org
Reporter: egor.suvorov at gmail.com
CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
neeilans at live.com, richard-llvm at metafoo.co.uk
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:
https://godbolt.org/z/jMb37G7nq
If I replace `i < 2 ? 1 : 0` with `2 - i`, the output is `21` as expected.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210420/0ec31f11/attachment.html>
More information about the llvm-bugs
mailing list