f18
f18 copied to clipboard
Scrub the compiler for uses of "enum" rather than "enum class"
I was trying to determine the type of a variable declared in a Fortran program. I had the declaration of type of the Fortran variable. Its declaration in the f18 code looked like this – DeclTypeSpec *symtype;
Now I wanted to find out if the type of the Fortran type was a REAL. So I wrote this code – Fortran::common::TypeCategory category = symType->category(); If (category == Fortran::common::TypeCategory::Real) { std::cout << “Found a REAL\n”; }
This code compiled without a warning. But I was surprised when I ran it on a test program that declared a LOGICAL variable, and it printed out “Found a REAL”.
My mistake was that the function “category()” returns an enum of type DeclTypeSpec::Category rather than a Fortran::common::TypeCategory. Apparently, it’s OK to assign the former to the latter. The enum value in the second position of Category is Logical, and the value in the second position of TypeCategory is Real, resulting in my surprise.
Oddly, I get a syntax error if I try to compare a value of type Category to a value of type TypeCategory, but the assignment doesn’t even produce a warning.
This means each reference to an enumerator gets longer. E.g. we would change:
if (type->category() == DeclTypeSpec::ClassStar) {
to:
if (type->category() == DeclTypeSpec::Category::ClassStar) {
To me that it slightly less readable.
I assume the motivation for this is type safety. Can you elaborate on when it makes a difference?
Pete had a case in the past few weeks where common::TypeCategory and DeclTypeSpec::Category were interacting without c++ compiler complaint; I don't recall the particulars.
Pete had a case in the past few weeks where common::TypeCategory and DeclTypeSpec::Category were interacting without c++ compiler complaint; I don't recall the particulars.
I've updated the issue description with a more complete description of the original problem that I ran into.