f18 icon indicating copy to clipboard operation
f18 copied to clipboard

Scrub the compiler for uses of "enum" rather than "enum class"

Open psteinfeld opened this issue 5 years ago • 3 comments

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.

psteinfeld avatar May 30 '19 23:05 psteinfeld

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?

tskeith avatar Jun 03 '19 16:06 tskeith

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.

klausler avatar Jun 03 '19 16:06 klausler

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.

psteinfeld avatar Jun 03 '19 18:06 psteinfeld