C++20 module support v1 (import winrt;) #1556
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1556"
general
weight: 0.35
YexuanXiao
C++20 Module Support (
import winrt;)This PR adds C++20 named module support to C++/WinRT, allowing consumers to
write
import winrt;instead of using#includedirectives and precompiled headers. In multi-project solutions, a shared module builder project compiles the platform projection once; consumer projects reference the pre-built module for significantly faster builds.Quick overview
Two new MSBuild properties control module support:
CppWinRTModuleBuild— Generates the platform SDK projection and compileswinrt.ixx. Set on a dedicated static library project (the "module builder"), or on a standalone project for single-project scenarios.CppWinRTModuleConsume— Consumes a pre-built module from aProjectReferenceto a builder. The NuGet targets automatically resolve the IFC, OBJ, and include paths.Design choices
Macro-driven, not flag-driven. Module-aware behavior in generated component files (
.g.h,.g.cpp,module.g.cpp) is controlled by#ifdef WINRT_MODULEguards emitted by the code generator. The NuGet targets defineWINRT_MODULEas a preprocessor definition — no special cppwinrt.exe command-line flag is needed. This means the same generated files work in both module and header mode without regeneration.Boundary-based module guard. Each namespace header contains a "module guard" — a preprocessor block that checks whether the header's own namespace is in the module (via
WINRT_MODULE_NS_<self>). If it IS in the module, the TU is doing a traditional#include(not via import), so the header falls through to#include "base.h"and all cross-dep guards are bypassed. If it is NOT in the module (e.g., a component header), the TU must have already doneimport winrt;, so the header usesbase_macros.honly and cross-dep guards skip module namespaces. This means the same headers work in both module and traditional header mode without regeneration.Per-namespace include guards with compound conditions. After
import winrt;, consumers textually#includereference/component projection headers. Platform namespace deps (already in the module) must be skipped to avoid MSVC redeclaration errors, but component cross-namespace deps must NOT be skipped. Cross-namespace#includedeps use compound guards —#if !defined(WINRT_MODULE_NS_<dep>) || defined(WINRT_MODULE_NS_<self>)— so deps in the module are skipped only when the including header is NOT itself in the module.extern "C++"wrapping for include-then-import compatibility. The winrt.ixx module purview wraps all#includedirectives inextern "C++" { ... }, andnamespace stdblocks useWINRT_IMPL_EXTERN_CXX(which expands toextern "C++"in the ixx, empty in header mode). This gives declarations external C++ linkage so MSVC can merge textually-included declarations with module-exported declarations. This is critical for real-world codebases where 3rd-party headers (e.g., WIL's cppwinrt.h)#includewinrt headers before user code doesimport winrt;.Scoped
import std;.import std;is NOT placed insidebase.hbecause platform headers (<intrin.h>) transitively include STL headers, making a subsequentimport std;unsafe. Instead,import std;is emitted at the TU level — inwinrt.ixx(module purview),module.g.cpp, and.g.hfiles — controlled by theWINRT_IMPORT_STDmacro.Builder/consumer split.
CppWinRTModuleBuildandCppWinRTModuleConsumeare separate properties because in multi-project solutions, only one project should compile the expensivewinrt.ixx. TheCppWinRTGetModuleOutputs/CppWinRTResolveModuleReferencestargets handle cross-project IFC/OBJ resolution via MSBuild'sProjectReferenceinfrastructure.import std;is orthogonal.import winrt;works with C++20.import std;is optional and independently controlled by the existingBuildStlModulesproperty. On v143 (VS 2022),import std;requires/std:c++latest; on v145 (VS 2026),/std:c++20suffices.Exported
winrt::implnamespace. Thewinrt::implnamespace is exported from the module alongsidewinrt. This is necessary because component projection headers specializeimpltemplates likecategory<>,abi<>,guid_v<>, andname_v<>for their types.What's in this PR
Code generator (
cppwinrt/):.g.hfiles use#ifdef WINRT_MODULEtoimport winrt;(and optionallyimport std;) before including component namespace headers.g.cppandmodule.g.cppuse#ifdef WINRT_MODULE/#ifndef WINRT_MODULEfor import vs include pathswinrt.ixxwraps the module purview inextern "C++" { ... }, definesWINRT_IMPL_EXTERN_CXXasextern "C++", and conditionally doesimport std;winrt_module_namespaces.hgenerated alongsidewinrt.ixxwith per-namespace macroswrite_module_guard()— boundary-based: checksWINRT_MODULE_NS_<self>to decide betweenbase_macros.h(import path) andbase.h(traditional path)write_root_include_guarded()— compound guard:!defined(NS_dep) || defined(NS_self)for cross-namespace depsWINRT_IMPL_EXTERN_CXXapplied to allnamespace stdblocks for module linkage compatibilityimport std;removed frombase.h— only emitted in ixx/module.g.cpp/.g.hNuGet targets (
nuget/):CppWinRTBuildModuletarget — addswinrt.ixxto compilation, definesWINRT_MODULEandWINRT_IMPORT_STDCppWinRTGetModuleOutputstarget — exports IFC/OBJ/GeneratedFilesDir for consumersCppWinRTResolveModuleReferencestarget — resolves module fromProjectReferenceitemsCppWinRTModuleBuild/CppWinRTModuleConsumeproperties inCppWinrtRules.Project.xmlNuGet test projects (
test/nuget/):TestModuleBuilder— static lib, builds the moduleTestModuleConsumerApp— console app consuming the module + a component reference (multi-namespace, cross-namespace struct fields, platform type returns)TestModuleComponent— DLL with IDL, two namespaces, cross-namespace value type field, platformUrireturn typeTestModuleSingleProject— single project that builds and consumes its own moduleinclude_test.cpp— regression tests for include-with-WINRT_MODULERepo test projects (
test/):test_cpp20_module/test_cpp20_module_winrt— C++20 module consumer/builder (noimport std;)test_cpp23_module/test_cpp23_module_winrt— C++23 module consumer/builder (withimport std;)test_component_module— component DLL with modulesinclude_before_import_test.cpp— verifies#includebeforeimport winrt;works (extern "C++" coexistence)test_cpp20_moduleadded to CI test matrix (all architectures, MSVC only)Documentation (
docs/):modules.md— user-facing guide (Quick Start, properties, macros, architecture)modules-internals.md— maintainer guide (codegen pipeline, macro flow, extern "C++", boundary-based guards)nuget/readme.md— C++20 Modules section.github/instructions/modules.instructions.md— AI assistant instructionsKey macros
| Macro | Scope | Purpose |
|-------|-------|---------|
|
WINRT_MODULE| Project-wide | Controls.g.h/.g.cpp/module.g.cppbehavior; activates boundary-based module guard in namespace headers ||
WINRT_BUILD_MODULE| winrt.ixx only | Set in ixx global module fragment; usesbase_macros.honly ||
WINRT_MODULE_NS_*| winrt_module_namespaces.h | Per-namespace macros. Module guard and cross-dep guards use compound conditions ||
WINRT_IMPL_EXTERN_CXX| winrt.ixx purview |extern "C++"in module, empty in header mode. Applied tonamespace stdblocks ||
WINRT_IMPORT_STD| Project-wide (optional) | Enablesimport std;in winrt.ixx,.g.h, andmodule.g.cpp. Does NOT affect base.h |Current limitations
CppWinRTSDKReferencesproperty on the builder project.import std;requires/std:c++lateston v143 toolset.import std;is NOT used insidebase.h— platform headers transitively include STL headers first. Instead,import std;is at the TU level.#include "base.h"behavior.import winrt;works thanks toextern "C++"wrapping.Future directions
CppWinRTModuleInputitem group.Documentation
See [docs/modules.md](docs/modules.md) for the full user guide and [docs/modules-internals.md](docs/modules-internals.md) for code generation internals.
Acknowledgements
Credit to @sylveon and @YexuanXiao for the trailblazing they've done in their forks, as well as their early feedback while this was in draft. Also @zadjii-msft and @Scottj1s for their earlier attempts and showing the potential build improvements.