From d1c5dc4d12f5f32e66bc73ac662aa2f1d17f2e72 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Wed, 22 Jan 2025 13:13:15 +0000 Subject: [PATCH] fix(semantic): fix const assertions in `UnresolvedReferencesStack` (#8653) The `_SIZE_CHECK` assertion was ignored, because `const`s are only evaluated if they're referenced in a function which is called. Fix that by referencing the const in `UnresolvedReferencesStack::new`. Also add more assertions to cover all the invariants. --- crates/oxc_semantic/src/unresolved_stack.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/oxc_semantic/src/unresolved_stack.rs b/crates/oxc_semantic/src/unresolved_stack.rs index 50dc8247c..34df37475 100644 --- a/crates/oxc_semantic/src/unresolved_stack.rs +++ b/crates/oxc_semantic/src/unresolved_stack.rs @@ -34,11 +34,17 @@ impl<'a> UnresolvedReferencesStack<'a> { /// SAFETY: Must be >= 2 to ensure soundness of `current_and_parent_mut`. const INITIAL_SIZE: usize = 16; - /// Assert invariant - #[allow(clippy::assertions_on_constants)] - const _SIZE_CHECK: () = assert!(Self::INITIAL_SIZE > Self::INITIAL_DEPTH); + /// Assert invariants + const ASSERT_INVARIANTS: () = { + assert!(Self::INITIAL_DEPTH >= 1); + assert!(Self::INITIAL_SIZE >= 2); + assert!(Self::INITIAL_SIZE > Self::INITIAL_DEPTH); + }; pub(crate) fn new() -> Self { + // Invoke `ASSERT_INVARIANTS` assertions. Without this line, the assertions are ignored. + const { Self::ASSERT_INVARIANTS }; + let mut stack = vec![]; stack.resize_with(Self::INITIAL_SIZE, TempUnresolvedReferences::default); Self { stack, current_scope_depth: Self::INITIAL_DEPTH }