From 86b126628c0672425d8a6328a19b6bd201ba0780 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sat, 31 Jan 2026 02:37:50 +0000 Subject: [PATCH] fix: update SourceLineCache to use NonNull for last_source pointer Closes #12401 --- src/parse_tree.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/parse_tree.rs b/src/parse_tree.rs index 23bad58cc..af5308514 100644 --- a/src/parse_tree.rs +++ b/src/parse_tree.rs @@ -2,6 +2,7 @@ use std::ops::Deref; use std::pin::Pin; +use std::ptr::NonNull; use std::sync::Arc; use crate::ast::{self, Ast, JobList, Node}; @@ -114,19 +115,23 @@ unsafe impl Sync for ParsedSource {} /// Caches the last source's offset and newline count. #[derive(Default)] pub struct SourceLineCache { - last_source: *const ParsedSource, // Pointer to last source used - offset: usize, // Exclusive offset of the number of newlines counted - count: usize, // Count of newlines up to offset + /// Pointer to last source used + last_source: Option>, + /// Exclusive offset of the number of newlines counted + offset: usize, + /// Count of newlines up to offset + count: usize, } impl ParsedSource { /// Compute the 1-based line number for a given offset, using and updating the cache. pub fn lineno_for_offset(&self, offset: usize, cache: &mut SourceLineCache) -> u32 { - let self_ptr = std::ptr::from_ref(self); + // TODO(MSRV>=1.89): feature(non_null_from_ref) + let self_ptr = unsafe { NonNull::new_unchecked(std::ptr::from_ref(self).cast_mut()) }; // If source changed, reset cache. - if cache.last_source != self_ptr { - cache.last_source = self_ptr; + if cache.last_source != Some(self_ptr) { + cache.last_source = Some(self_ptr); cache.offset = 0; cache.count = 0; }