From b4f7af066e732a1df9aba308159f4c3d3b06feb2 Mon Sep 17 00:00:00 2001 From: John Tur Date: Wed, 5 Nov 2025 03:06:14 -0500 Subject: [PATCH] Work around codegen bug with GetKeyboardState (#41970) Works around an issue, which can be reproduced in the following program: ```rs use windows::Win32::UI::Input::KeyboardAndMouse::{GetKeyboardState, VK_CONTROL}; fn main() { let mut keyboard_state = [0u8; 256]; unsafe { GetKeyboardState(&mut keyboard_state).unwrap(); } let ctrl_down = (keyboard_state[VK_CONTROL.0 as usize] & 0x80) != 0; println!("Is Ctrl down: {ctrl_down}"); } ``` In debug mode, this program prints the correct answer. In release mode, it always prints false. The optimizer appears to think that `keyboard_state` isn't mutated and remains zeroed, and folds the `modifier_down` comparisons to `false`. Release Notes: - N/A --- crates/gpui/src/interactive.rs | 6 ++---- crates/gpui/src/platform/windows/events.rs | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/gpui/src/interactive.rs b/crates/gpui/src/interactive.rs index 4aa0715144090d19be313e8c321a67a7e8682d8a..79a586983f1867847f1aa6b9954b835b6d26cd80 100644 --- a/crates/gpui/src/interactive.rs +++ b/crates/gpui/src/interactive.rs @@ -26,10 +26,8 @@ pub struct KeyDownEvent { /// Whether the key is currently held down. pub is_held: bool, - /// Whether the modifiers are excessive for producing this character. - /// When false, the modifiers are essential for character input (e.g., AltGr), - /// and character input should be prioritized over keybindings. - /// When true, the modifiers are for keybindings (e.g., Ctrl+A). + /// Whether to prefer character input over keybindings for this keystroke. + /// In some cases, like AltGr on Windows, modifiers are significant for character input. pub prefer_character_input: bool, } diff --git a/crates/gpui/src/platform/windows/events.rs b/crates/gpui/src/platform/windows/events.rs index ff88bea3fc6235522ce6f4e62e08be744e804ed2..a8e5a4300fc61d74f7a8e44fd869d492a9a658e8 100644 --- a/crates/gpui/src/platform/windows/events.rs +++ b/crates/gpui/src/platform/windows/events.rs @@ -1387,6 +1387,8 @@ fn should_prefer_character_input(vkey: VIRTUAL_KEY, scan_code: u16) -> bool { return false; } + // Workaround for some bug that makes the compiler think keyboard_state is still zeroed out + let keyboard_state = std::hint::black_box(keyboard_state); let ctrl_down = (keyboard_state[VK_CONTROL.0 as usize] & 0x80) != 0; let alt_down = (keyboard_state[VK_MENU.0 as usize] & 0x80) != 0; let win_down = (keyboard_state[VK_LWIN.0 as usize] & 0x80) != 0