diff --git a/toolchain/lex/token_info.h b/toolchain/lex/token_info.h index 363013c82094..8453a7d8f872 100644 --- a/toolchain/lex/token_info.h +++ b/toolchain/lex/token_info.h @@ -162,14 +162,19 @@ class TokenInfo { // // Only used by the lexer which enforces only the correct kinds are used. // - // When the payload is not being set, we leave it uninitialized. At least in - // some cases, this will allow MSan to correctly detect erroneous attempts - // to access the payload, as it works to track uninitialized memory - // bit-for-bit specifically to handle complex cases like bitfields. + // Under MSan, leave `token_payload_` uninitialized so bit-level tracking can + // detect erroneous attempts to access a missing payload. In non-MSan builds, + // zero-initialize it so constructing a payloadless `TokenInfo` compiles to a + // plain 32-bit store rather than a read-modify-write preserving uninitialized + // upper bits (which SROA otherwise promotes to loop-carried registers). TokenInfo(TokenKind kind, bool has_leading_space, int32_t byte_offset) : kind_(kind), has_leading_space_(has_leading_space), - byte_offset_(byte_offset) {} +#if !__has_feature(memory_sanitizer) + token_payload_(0), +#endif + byte_offset_(byte_offset) { + } // Constructor for a TokenKind that carries a payload. //