Skip to content

Fix NativeModule templates to work with clang - #15917

Merged
acoates-ms merged 2 commits into
microsoft:mainfrom
acoates-ms:clangtm
Apr 2, 2026
Merged

acoates-ms merged 2 commits into
microsoft:mainfrom
acoates-ms:clangtm

Conversation

@acoates-ms

@acoates-ms acoates-ms commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Attemping to use the MS.RN.Cxx nativemodule templates in clang results in a build error:
error: class template specialization of 'IsReactTurboModule' must occur at global scope [clang-diagnostic-error]

Root cause: IsReactTurboModule was a global-scope template, and the macros tried to create an explicit specialization (template <> struct IsReactTurboModule) wherever they were expanded. C++ requires that specializations occur at the same namespace as the primary template — so if the macro was used inside namespace Foo, the specialization was at ::Foo instead of ::, triggering the error.

Fix ([ModuleRegistration.h:20-87]:

Replaced IsReactTurboModule's primary template with an ADL-based detection scheme. A global template constexpr bool ReactIsReactTurboModuleImpl(T*) provides the false default.
Each macro now injects a non-template constexpr bool ReactIsReactTurboModuleImpl(moduleStruct*) into the current scope (which may be any namespace).
When IsReactTurboModuleFoo::MyModule is instantiated, ReactIsReactTurboModuleImpl(static_castFoo::MyModule*(nullptr)) is evaluated. ADL looks in namespace Foo and finds the non-template overload injected by the macro there — non-template functions are preferred over templates in overload resolution, so it always wins over the fallback.

Microsoft Reviewers: Open in CodeFlow
@acoates-ms
acoates-ms requested a review from a team as a code owner April 2, 2026 16:44
@acoates-ms
acoates-ms enabled auto-merge (squash) April 2, 2026 18:59
@acoates-ms
acoates-ms merged commit 33b5099 into microsoft:main Apr 2, 2026
32 checks passed
@acoates-ms
acoates-ms deleted the clangtm branch April 2, 2026 19:31
acoates-ms added a commit to acoates-ms/react-native-windows that referenced this pull request Apr 2, 2026
* Fix NativeModule templates to work with clang

* Change files
acoates-ms added a commit that referenced this pull request Apr 2, 2026
* Implement onClick (#15889)

* Implement onClick

* Change files

* Add onAuxClick event

* Update snapshots and fix lint error

* Fix NativeModule templates to work with clang (#15917)

* Fix NativeModule templates to work with clang

* Change files

* update change files

* lint

* lint

* Remove homeUIA tests

* fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants