Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fail to compile due to -Werror=type-limits when char is unsigned #337

Closed
musicinmybrain opened this issue Apr 17, 2023 · 6 comments · Fixed by #338
Closed

Tests fail to compile due to -Werror=type-limits when char is unsigned #337

musicinmybrain opened this issue Apr 17, 2023 · 6 comments · Fixed by #338

Comments

@musicinmybrain
Copy link
Contributor

On Fedora Linux Rawhide, with GCC 13.0.1, while testing gsl-lite 0.41.0, we’re seeing that test/lest_cpp03.hpp fails to compile with an error from -Werror=type-limits on our non-x86 architectures (aarch64, ppc64le, s390x).

In file included from /builddir/build/BUILD/gsl-lite-0.41.0/test/gsl-lite.t.hpp:104,
                 from /builddir/build/BUILD/gsl-lite-0.41.0/test/emulation.t.cpp:20:
/builddir/build/BUILD/gsl-lite-0.41.0/test/lest_cpp03.hpp: In function 'bool lest::unprintable(char)':
/builddir/build/BUILD/gsl-lite-0.41.0/test/lest_cpp03.hpp:707:46: error: comparison is always true due to limited range of data type [-Werror=type-limits]
  707 | inline bool unprintable( char c ) { return 0 <= c && c < ' '; }
      |                                            ~~^~~~

This seems to be happening because char is equivalent to signed char on i686 and x86_64, but is equivalent to unsigned char on the other platforms. The useless comparison is not a real problem here, but the failure to compile is.

I am not sure why we are seeing this in 0.41.0 but not in 0.40.0. Nothing stands out in the changes as potentially relevant.

There is existing logic to suppress this warning on “GNU” compilers, but only for old compiler versions:

if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8 )
list( APPEND localOptions "-Wno-type-limits" ) # irrelevant warning about `unsigned value < 0` comparison
endif()

Should we just make that unconditional, like -Wno-long-long and -Wuseless-cast?

@mbeutel
Copy link
Collaborator

mbeutel commented Apr 17, 2023

Thank you for the report. The problem might be related to this change to the PCH setup. Previously, we had simply set gsl-lite.t.hpp as the precompiled header; but apparently this didn't interact well with the warning suppressions in that header file; at least I had to address several previously unseen warnings after making these changes.

As a general rule, I would like to avoid unconditional warning suppressions for current compiler versions. "-Wno-long-long", being relevant only for C++98, is an exception; "-Wuseless-cast" unconditionally enables the warning which is fine.

(Edit: pardon me, I had only looked at MakeTestTarget.cmake. We indeed have a lot of unconditional warning suppressions in gsl-lite.t.hpp, "-Wuseless-cast" being among them. As a comment there points out, this is required to pacify the compiler with regard to lest. I aim to eventually replace our outdated and somewhat customized copy of lest with Catch2.)

If the warning appears only once, I'd prefer to work around locally, either by suppressing it:

inline bool unprintable( char c )
{
#if defined (__GNUC__)
# pragma GCC diagnostic suppress "-Wtype-limits"  // comparison is always true due to limited range of data type
#endif // defined (__GNUC__)
    return 0 <= c && c < ' ';
}

or by casting c to signed char:

inline bool unprintable( char c ) { return 0 <= static_cast<signed char>(c) && c < ' '; }

Would you mind verifying if either of these fixes the issue, and possibly issuing a PR?

Edit: you can also add "-Wtype-limits" to the ignored diagnostics in gsl-lite.t.hpp if you like. I'll find it there if I ever get around to replacing lest.

@martinmoene
Copy link
Collaborator

martinmoene commented Apr 17, 2023

An intermediate step could perhaps be to suppress lest's warnings by handling it as a system header. See e.g. this commit of string_view lite.

musicinmybrain added a commit to musicinmybrain/gsl-lite that referenced this issue Apr 18, 2023
On at least GCC 13, on platforms where char is unsigned by default,
checking if a char’s value is nonnegative by comparing to integer zero
produced a warning which was treated as an error: “comparison is always
true due to limited range of data type [-Werror=type-limits]”

Fixes gsl-lite#337.
@musicinmybrain
Copy link
Contributor Author

Thank you for the report. The problem might be related to this change to the PCH setup. Previously, we had simply set gsl-lite.t.hpp as the precompiled header; but apparently this didn't interact well with the warning suppressions in that header file; at least I had to address several previously unseen warnings after making these changes.

Thanks for explaining that; it seems like a plausible root cause.

As a general rule, I would like to avoid unconditional warning suppressions for current compiler versions. […]

If the warning appears only once, I'd prefer to work around locally, either by suppressing it:

[…]

or by casting c to signed char:

[…]

Would you mind verifying if either of these fixes the issue, and possibly issuing a PR?

Thanks for the suggestions. Both of these look reasonable; if you have no particular preference, I think I prefer the cast. I’ve tested it as a patch and opened a PR.

@mbeutel
Copy link
Collaborator

mbeutel commented Apr 18, 2023

Thanks for the contribution. Do you need a patch release (v0.41.1) as soon as possible? If not, I'd let it sit for a few days before issuing a release in case there is more fallout.

@mbeutel
Copy link
Collaborator

mbeutel commented Apr 18, 2023

@martinmoene, thanks for the suggestion, I think that might indeed be better than suppressing all these warnings in all of our test suite.

@musicinmybrain
Copy link
Contributor Author

Thanks for the contribution. Do you need a patch release (v0.41.1) as soon as possible? If not, I'd let it sit for a few days before issuing a release in case there is more fallout.

There is no hurry at all. Thanks for working on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants