llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.87k stars 11.93k forks source link

gnu extension "?:" with enums not faithful #15998

Open ggreif opened 11 years ago

ggreif commented 11 years ago
Bugzilla Link 15626
Version trunk
OS All
CC @DougGregor

Extended Description

$ clang gnutest.cc gnutest.cc:7:10: error: cannot initialize return object of type 'Howdy' with an rvalue of type 'int' return you ?: Hi; ^~~~~ 1 error generated.

This works nicely with g++ (v3.4 and v4.4). I guess clang models this as

int temp = you; return temp ? temp : Hi;

while g++ does:

Howdy temp = you; return temp ? temp : Hi;

The test program is:

$ cat gnutest.cc

enum Howdy {
  Lo, Hi 
};

Howdy test(Howdy you)
{
  return you ?: Hi;
}
ggreif commented 10 years ago

This is in 3.4 still.

Btw. is there an official translation of the binary operator to the ternary one? How does GCC do it?

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-frontend

Author: Gabor Greif (ggreif)

| | | | --- | --- | | Bugzilla Link | [15626](https://llvm.org/bz15626) | | Version | trunk | | OS | All | | CC | @DougGregor | ## Extended Description $ clang gnutest.cc gnutest.cc:7:10: error: cannot initialize return object of type 'Howdy' with an rvalue of type 'int' return you ?: Hi; ^~~~~~~~~ 1 error generated. This works nicely with g++ (v3.4 and v4.4). I guess clang models this as > int temp = you; > return temp ? temp : Hi; while g++ does: > Howdy temp = you; > return temp ? temp : Hi; The test program is: `$ cat gnutest.cc` ```cpp enum Howdy { Lo, Hi }; Howdy test(Howdy you) { return you ?: Hi; } ```
shafik commented 12 months ago

CC @zygoloid any pointers here, I think we want to remain compatible. I guess we need to fix something in Sema::CXXCheckConditionalOperands