Skip to content

Cleanup product code uses of feature-test macros #3053

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

Merged

Conversation

CaseyCarter
Copy link
Contributor

@CaseyCarter CaseyCarter commented Aug 24, 2022

We should try to minimize the number of feature-test macros used to guard conditionally-compiled product code, so that readers aren't required to memorize the content of SD-6 in order to comprehend the STL. Consequently, feature-test macros should only be used in product code when their values are not implied by language mode or a conjunction of language mode and some other feature-test macro. We should instead test _HAS_CXXYY && defined(__other_feature_macro) directly. Feature-test macros whose values vary according to compiler flags other than language modes, or for features that aren't yet implemented by all compilers should be tested directly (although latter will eventually be replace by langage mode tests when the minimum required versions of all supported compilers implement the feature).

Some consequences of these rules:

  • _HAS_CXX20 && defined(__cpp_lib_concepts) is redundant since defined(__cpp_lib_concepts) implies _HAS_CXX20.

  • _HAS_CXX20 implies __cpp_constexpr_dynamic_alloc now that all supported C++20 front ends implement that feature

  • defined(__cpp_lib_concepts) implies defined(__cpp_lib_format)

  • _HAS_CXX23 && defined(__cpp_lib_concepts) implies defined(__cpp_expected) and defined(__cpp_lib_containers_ranges)

Note that we don't annotate plain defined(__cpp_lib_concepts) with // TRANSITION, GH-395 in product because GH-395 clearly indicates that all such tests should be replaced and we don't want to churn just to add the annotation. I could be convinced to apply such annotations and remove the special case if people really want to review another few hundred mechanical changes, but for now I have instead stripped such transition comments from the few places they did appear.

We should try to minimize the number of feature-test macros used to guard conditionally-compiled product code, so that readers aren't required to memorize the content of SD-6 in order to comprehend the STL. Consequently, feature-test macros should only be used in product code when their values are not implied by language mode or a conjunction of language mode and some other feature-test macro. We should instead test `_HAS_CXXYY && defined(__other_feature_macro)` directly. Feature-test macros whose values vary according to compiler flags other than language modes, or for features that aren't yet implemented by all compilers should be tested directly (although latter will eventually be replace by langage mode tests when the minimum required versions of all supported compilers implement the feature).

Some consequences of these rules:

* `_HAS_CXX20 && defined(__cpp_lib_concepts)` is redundant since `defined(__cpp_lib_concepts)` implies `_HAS_CXX20`.

* `_HAS_CXX20` implies `__cpp_constexpr_dynamic_alloc` now that all supported C++20 front ends implement that feature

* `defined(__cpp_lib_concepts)` implies `defined(__cpp_lib_format)`

* `_HAS_CXX23 && defined(__cpp_lib_concepts)` implies `defined(__cpp_expected)` and `defined(__cpp_lib_containers_ranges)`

[Note that we don't annotate plain `defined(__cpp_lib_concepts)` with `// TRANSITION, microsoftGH-395` in product because microsoftGH-395 clearly indicates that all such tests should be replaced and we don't want to churn just to add the annotation.]
@CaseyCarter CaseyCarter added the enhancement Something can be improved label Aug 24, 2022
@CaseyCarter CaseyCarter requested a review from a team as a code owner August 24, 2022 19:44
Copy link
Contributor

@strega-nil-ms strega-nil-ms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Casey!

* remove redundant nested `_HAS_CXX23`s
* remove `// TRANSITION, microsoftGH-395` from `#ifn?def __cpp_lib_concepts` for consistency
* don't be lazy and use `// TRANSITION, microsoftGH-395` as an `#endif` comment instead of the actual test condition
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 05e5e68 into microsoft:main Aug 27, 2022
@StephanTLavavej
Copy link
Member

Thanks for increasing codebase consistency! 🧹 ✨ 💯

@CaseyCarter CaseyCarter deleted the and_now_our_feature_presentation branch August 29, 2022 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants