circle icon indicating copy to clipboard operation
circle copied to clipboard

Invalid values when using structured binding on struct with bit-fields

Open cmarcelo opened this issue 2 years ago • 0 comments

With circle build 195, the following C++ 20 program output different results for f and g calls. clang++ 14 and g++ 11.3 both work as expected.

$ for c in circle g++ clang++; do $c -std=c++20 -o test-$c test.cpp; done
$ ./test-circle 
; 1 2 3
; 105 105 105
$ ./test-g++
; 1 2 3
; 1 2 3
$ ./test-clang++
; 1 2 3
; 1 2 3

The test.cpp source below:

#include <iostream>

struct result {
	unsigned a;
	unsigned b;
	unsigned c;
};

result f(int v) {
	if (v > 1)
		return {0b11, 0b10, 0b1};
	else
		return {0b1, 0b10, 0b11};
}

struct result_bitfield {
	unsigned a : 2;
	unsigned b : 3;
	unsigned c : 3;
};

result_bitfield g(int v) {
	if (v > 1)
		return {0b11, 0b10, 0b1};
	else
		return {0b1, 0b10, 0b11};
}

int main(int argc, char *argv[]) {
	const auto [x, y, z] = f(argc);
	std::cout << "; " << x << " " << y << " " << z << "\n";

	const auto [xx, yy, zz] = g(argc);
	std::cout << "; " << xx << " " << yy << " " << zz << "\n";

	return 0;
}

cmarcelo avatar May 22 '23 04:05 cmarcelo