From c13038b9680c5fa664fedc64e61ff812b780f0f0 Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Sat, 11 Apr 2026 02:29:39 +0200 Subject: [PATCH] refactor: move move char consts to widestring This time, move char constants from `src/expand.rs` to `fish_widestring`, which resolves a dependency cycle between `src/expand.rs` and `src/common.rs`. Part of #12625 --- crates/widestring/src/lib.rs | 27 +++++++++++++++++++++++++++ src/builtins/fish_indent.rs | 3 +-- src/common.rs | 11 +++++------ src/expand.rs | 32 +++----------------------------- src/highlight/file_tester.rs | 11 ++++++----- src/parse_util.rs | 11 +++++------ src/path.rs | 4 ++-- 7 files changed, 49 insertions(+), 50 deletions(-) diff --git a/crates/widestring/src/lib.rs b/crates/widestring/src/lib.rs index 7c147a414..68cb7ee0a 100644 --- a/crates/widestring/src/lib.rs +++ b/crates/widestring/src/lib.rs @@ -76,6 +76,33 @@ pub mod prelude { #[allow(dead_code)] pub const ANY_SENTINEL: char = char_offset(WILDCARD_RESERVED_BASE, 3); +/// Character representing a home directory. +pub const HOME_DIRECTORY: char = char_offset(EXPAND_RESERVED_BASE, 0); +/// Character representing process expansion for %self. +pub const PROCESS_EXPAND_SELF: char = char_offset(EXPAND_RESERVED_BASE, 1); +/// Character representing variable expansion. +pub const VARIABLE_EXPAND: char = char_offset(EXPAND_RESERVED_BASE, 2); +/// Character representing variable expansion into a single element. +pub const VARIABLE_EXPAND_SINGLE: char = char_offset(EXPAND_RESERVED_BASE, 3); +/// Character representing the start of a bracket expansion. +pub const BRACE_BEGIN: char = char_offset(EXPAND_RESERVED_BASE, 4); +/// Character representing the end of a bracket expansion. +pub const BRACE_END: char = char_offset(EXPAND_RESERVED_BASE, 5); +/// Character representing separation between two bracket elements. +pub const BRACE_SEP: char = char_offset(EXPAND_RESERVED_BASE, 6); +/// Character that takes the place of any whitespace within non-quoted text in braces +pub const BRACE_SPACE: char = char_offset(EXPAND_RESERVED_BASE, 7); +/// Separate subtokens in a token with this character. +pub const INTERNAL_SEPARATOR: char = char_offset(EXPAND_RESERVED_BASE, 8); +/// Character representing an empty variable expansion. Only used transitively while expanding +/// variables. +pub const VARIABLE_EXPAND_EMPTY: char = char_offset(EXPAND_RESERVED_BASE, 9); + +const _: () = assert!( + EXPAND_RESERVED_END as u32 > VARIABLE_EXPAND_EMPTY as u32, + "Characters used in expansions must stay within private use area" +); + /// Return true if the character is in a range reserved for fish's private use. /// /// NOTE: This is used when tokenizing the input. It is also used when reading input, before diff --git a/src/builtins/fish_indent.rs b/src/builtins/fish_indent.rs index 96ff9f873..1168b5308 100644 --- a/src/builtins/fish_indent.rs +++ b/src/builtins/fish_indent.rs @@ -7,7 +7,6 @@ common::{PROGRAM_NAME, get_program_name, unescape_string}, env::{EnvStack, env_init, environment::Environment as _}, err_fmt, err_str, - expand::INTERNAL_SEPARATOR, global_safety::RelaxedAtomicBool, highlight::{HighlightRole, HighlightSpec, colorize, highlight_shell}, locale::set_libc_locales, @@ -26,7 +25,7 @@ use fish_common::{ReadExt as _, UnescapeFlags, UnescapeStringStyle}; use fish_wcstringutil::count_preceding_backslashes; use fish_wgetopt::{ArgType, WGetopter, WOption, wopt}; -use fish_widestring::{bytes2wcstring, osstr2wcstring, wcs2bytes}; +use fish_widestring::{INTERNAL_SEPARATOR, bytes2wcstring, osstr2wcstring, wcs2bytes}; use std::{ ffi::OsStr, fmt::Write as _, diff --git a/src/common.rs b/src/common.rs index 5299182c5..3a2db9039 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,10 +1,7 @@ //! Prototypes for various functions, mostly string utilities, that are used by most parts of fish. -use crate::expand::{ - BRACE_BEGIN, BRACE_END, BRACE_SEP, BRACE_SPACE, HOME_DIRECTORY, INTERNAL_SEPARATOR, - PROCESS_EXPAND_SELF, PROCESS_EXPAND_SELF_STR, VARIABLE_EXPAND, VARIABLE_EXPAND_SINGLE, -}; use crate::{ + expand::PROCESS_EXPAND_SELF_STR, global_safety::{AtomicRef, RelaxedAtomicBool}, prelude::*, terminal::Outputter, @@ -14,8 +11,10 @@ use fish_fallback::fish_wcwidth; use fish_feature_flags::{FeatureFlag, feature_test}; use fish_widestring::{ - ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE, ASCII_MAX, BYTE_MAX, UCS2_MAX, bytes2wcstring, - decode_byte_from_char, fish_reserved_codepoint, subslice_position, wcs2bytes, + ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE, ASCII_MAX, BRACE_BEGIN, BRACE_END, BRACE_SEP, + BRACE_SPACE, BYTE_MAX, HOME_DIRECTORY, INTERNAL_SEPARATOR, PROCESS_EXPAND_SELF, UCS2_MAX, + VARIABLE_EXPAND, VARIABLE_EXPAND_SINGLE, bytes2wcstring, decode_byte_from_char, + fish_reserved_codepoint, subslice_position, wcs2bytes, }; use nix::sys::termios::Termios; use std::{ diff --git a/src/expand.rs b/src/expand.rs index 9c1caf455..4ba48744e 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -31,8 +31,9 @@ use fish_util::wcsfilecmp_glob; use fish_wcstringutil::{join_strings, trim}; use fish_widestring::{ - ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE, EXPAND_RESERVED_BASE, EXPAND_RESERVED_END, - char_offset, osstr2wcstring, + ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE, BRACE_BEGIN, BRACE_END, BRACE_SEP, BRACE_SPACE, + HOME_DIRECTORY, INTERNAL_SEPARATOR, PROCESS_EXPAND_SELF, VARIABLE_EXPAND, + VARIABLE_EXPAND_EMPTY, VARIABLE_EXPAND_SINGLE, osstr2wcstring, }; use nix::unistd::{User, getpid}; @@ -82,33 +83,6 @@ pub struct ExpandFlags : u16 { } } -/// Character representing a home directory. -pub const HOME_DIRECTORY: char = char_offset(EXPAND_RESERVED_BASE, 0); -/// Character representing process expansion for %self. -pub const PROCESS_EXPAND_SELF: char = char_offset(EXPAND_RESERVED_BASE, 1); -/// Character representing variable expansion. -pub const VARIABLE_EXPAND: char = char_offset(EXPAND_RESERVED_BASE, 2); -/// Character representing variable expansion into a single element. -pub const VARIABLE_EXPAND_SINGLE: char = char_offset(EXPAND_RESERVED_BASE, 3); -/// Character representing the start of a bracket expansion. -pub const BRACE_BEGIN: char = char_offset(EXPAND_RESERVED_BASE, 4); -/// Character representing the end of a bracket expansion. -pub const BRACE_END: char = char_offset(EXPAND_RESERVED_BASE, 5); -/// Character representing separation between two bracket elements. -pub const BRACE_SEP: char = char_offset(EXPAND_RESERVED_BASE, 6); -/// Character that takes the place of any whitespace within non-quoted text in braces -pub const BRACE_SPACE: char = char_offset(EXPAND_RESERVED_BASE, 7); -/// Separate subtokens in a token with this character. -pub const INTERNAL_SEPARATOR: char = char_offset(EXPAND_RESERVED_BASE, 8); -/// Character representing an empty variable expansion. Only used transitively while expanding -/// variables. -pub const VARIABLE_EXPAND_EMPTY: char = char_offset(EXPAND_RESERVED_BASE, 9); - -const _: () = assert!( - EXPAND_RESERVED_END as u32 > VARIABLE_EXPAND_EMPTY as u32, - "Characters used in expansions must stay within private use area" -); - impl ExpandResult { pub fn new(result: ExpandResultCode) -> Self { Self { result, status: 0 } diff --git a/src/highlight/file_tester.rs b/src/highlight/file_tester.rs index b5fa79677..d1ac9418e 100644 --- a/src/highlight/file_tester.rs +++ b/src/highlight/file_tester.rs @@ -4,10 +4,7 @@ // and provide them optimistically. use crate::common::unescape_string; use crate::{ - expand::{ - BRACE_BEGIN, BRACE_END, BRACE_SEP, ExpandFlags, HOME_DIRECTORY, INTERNAL_SEPARATOR, - PROCESS_EXPAND_SELF, VARIABLE_EXPAND, VARIABLE_EXPAND_SINGLE, expand_one, expand_tilde, - }, + expand::{ExpandFlags, expand_one, expand_tilde}, operation_context::OperationContext, path::path_apply_working_directory, redirection::RedirectionMode, @@ -18,7 +15,11 @@ use fish_wcstringutil::{ string_prefixes_string, string_prefixes_string_case_insensitive, string_suffixes_string, }; -use fish_widestring::{ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE, L, WExt as _, WString, wstr}; +use fish_widestring::{ + ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE, BRACE_BEGIN, BRACE_END, BRACE_SEP, HOME_DIRECTORY, + INTERNAL_SEPARATOR, L, PROCESS_EXPAND_SELF, VARIABLE_EXPAND, VARIABLE_EXPAND_SINGLE, WExt as _, + WString, wstr, +}; use libc::PATH_MAX; use nix::unistd::AccessFlags; use std::collections::{HashMap, HashSet}; diff --git a/src/parse_util.rs b/src/parse_util.rs index 27efa87b7..ca0f5be0f 100644 --- a/src/parse_util.rs +++ b/src/parse_util.rs @@ -6,11 +6,7 @@ }, builtins::shared::builtin_exists, common::{unescape_string, valid_var_name, valid_var_name_char}, - expand::{ - BRACE_BEGIN, BRACE_END, BRACE_SEP, ExpandFlags, ExpandResultCode, INTERNAL_SEPARATOR, - VARIABLE_EXPAND, VARIABLE_EXPAND_EMPTY, VARIABLE_EXPAND_SINGLE, expand_one, - expand_to_command_and_args, - }, + expand::{ExpandFlags, ExpandResultCode, expand_one, expand_to_command_and_args}, operation_context::OperationContext, parse_constants::{ ERROR_BAD_VAR_CHAR1, ERROR_BRACKETED_VARIABLE_QUOTED1, ERROR_BRACKETED_VARIABLE1, @@ -29,7 +25,10 @@ use fish_common::{UnescapeFlags, UnescapeStringStyle, help_section}; use fish_feature_flags::{FeatureFlag, feature_test}; use fish_wcstringutil::{count_newlines, truncate}; -use fish_widestring::{ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE}; +use fish_widestring::{ + ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE, BRACE_BEGIN, BRACE_END, BRACE_SEP, + INTERNAL_SEPARATOR, VARIABLE_EXPAND, VARIABLE_EXPAND_EMPTY, VARIABLE_EXPAND_SINGLE, +}; use std::ops::Range; use std::{iter, ops}; diff --git a/src/path.rs b/src/path.rs index 6894da4ab..616822fed 100644 --- a/src/path.rs +++ b/src/path.rs @@ -3,13 +3,13 @@ //! path-related issues. use crate::env::{EnvMode, EnvSetMode, EnvStack, Environment, FALLBACK_PATH}; -use crate::expand::{HOME_DIRECTORY, expand_tilde}; +use crate::expand::expand_tilde; use crate::flog::{flog, flogf}; use crate::prelude::*; use crate::wutil::{normalize_path, path_normalize_for_cd, waccess, wdirname, wstat}; use cfg_if::cfg_if; use errno::{Errno, errno, set_errno}; -use fish_widestring::{wcs2osstring, wcs2zstring}; +use fish_widestring::{HOME_DIRECTORY, wcs2osstring, wcs2zstring}; use libc::{EACCES, ENOENT, ENOTDIR, X_OK}; use nix::unistd::AccessFlags; use std::ffi::OsStr;