Make OperationContext not hold a Parser via Rc

Exploit Rust's lifetimes. This will lead to simplifications.
This commit is contained in:
ridiculousfish 2024-05-26 14:49:51 -07:00 committed by Peter Ammon
parent d36f94d96c
commit c9a76bd634
No known key found for this signature in database
6 changed files with 17 additions and 18 deletions

View File

@ -143,7 +143,7 @@ fn write_part(
&mut args, &mut args,
ExpandFlags::SKIP_CMDSUBST, ExpandFlags::SKIP_CMDSUBST,
&OperationContext::foreground( &OperationContext::foreground(
parser.shared(), parser,
Box::new(no_cancel), Box::new(no_cancel),
COMMANDLINE_TOKENS_MAX_EXPANSION, COMMANDLINE_TOKENS_MAX_EXPANSION,
), ),

View File

@ -1,7 +1,7 @@
use crate::common::CancelChecker; use crate::common::CancelChecker;
use crate::env::EnvDyn; use crate::env::EnvDyn;
use crate::env::{EnvStack, Environment}; use crate::env::{EnvStack, Environment};
use crate::parser::{Parser, ParserRef}; use crate::parser::Parser;
use crate::proc::JobGroupRef; use crate::proc::JobGroupRef;
use crate::reader::read_generation_count; use crate::reader::read_generation_count;
@ -21,11 +21,11 @@ pub const EXPANSION_LIMIT_BACKGROUND: usize = 512;
enum Vars<'a> { enum Vars<'a> {
// The parser, if this is a foreground operation. If this is a background operation, this may be // The parser, if this is a foreground operation. If this is a background operation, this may be
// nullptr. // nullptr.
Parser(ParserRef), Parser(&'a Parser),
// A set of variables. // A set of variables.
Vars(&'a dyn Environment), Vars(&'a dyn Environment),
TestOnly(ParserRef, &'a dyn Environment), TestOnly(&'a Parser, &'a dyn Environment),
} }
/// A operation_context_t is a simple property bag which wraps up data needed for highlighting, /// A operation_context_t is a simple property bag which wraps up data needed for highlighting,
@ -70,7 +70,7 @@ impl<'a> OperationContext<'a> {
/// Construct from a full set of properties. /// Construct from a full set of properties.
pub fn foreground( pub fn foreground(
parser: ParserRef, parser: &'a Parser,
cancel_checker: CancelChecker, cancel_checker: CancelChecker,
expansion_limit: usize, expansion_limit: usize,
) -> OperationContext<'a> { ) -> OperationContext<'a> {
@ -83,7 +83,7 @@ impl<'a> OperationContext<'a> {
} }
pub fn test_only_foreground( pub fn test_only_foreground(
parser: ParserRef, parser: &'a Parser,
vars: &'a dyn Environment, vars: &'a dyn Environment,
cancel_checker: CancelChecker, cancel_checker: CancelChecker,
) -> OperationContext<'a> { ) -> OperationContext<'a> {

View File

@ -1134,9 +1134,9 @@ impl Parser {
} }
/// Return the operation context for this parser. /// Return the operation context for this parser.
pub fn context(&self) -> OperationContext<'static> { pub fn context(&self) -> OperationContext<'_> {
OperationContext::foreground( OperationContext::foreground(
self.shared(), self,
Box::new(|| signal_check_cancel() != 0), Box::new(|| signal_check_cancel() != 0),
EXPANSION_LIMIT_DEFAULT, EXPANSION_LIMIT_DEFAULT,
) )

View File

@ -43,8 +43,8 @@ fn test_complete() {
}, },
}; };
let parser = Parser::principal_parser().shared(); let parser = Parser::principal_parser();
let ctx = OperationContext::test_only_foreground(parser.clone(), &vars, Box::new(no_cancel)); let ctx = OperationContext::test_only_foreground(parser, &vars, Box::new(no_cancel));
let do_complete = |cmd: &wstr, flags: CompletionRequestOptions| complete(cmd, flags, &ctx).0; let do_complete = |cmd: &wstr, flags: CompletionRequestOptions| complete(cmd, flags, &ctx).0;
@ -489,7 +489,7 @@ fn test_autosuggest_suggest_special() {
L!($command), L!($command),
CompletionRequestOptions::default(), CompletionRequestOptions::default(),
&OperationContext::foreground( &OperationContext::foreground(
Parser::principal_parser().shared(), Parser::principal_parser(),
Box::new(no_cancel), Box::new(no_cancel),
EXPANSION_LIMIT_DEFAULT, EXPANSION_LIMIT_DEFAULT,
), ),

View File

@ -27,7 +27,7 @@ fn expand_test_impl(
let mut errors = ParseErrorList::new(); let mut errors = ParseErrorList::new();
let pwd = PwdEnvironment::default(); let pwd = PwdEnvironment::default();
let ctx = OperationContext::test_only_foreground( let ctx = OperationContext::test_only_foreground(
Parser::principal_parser().shared(), Parser::principal_parser(),
&pwd, &pwd,
Box::new(no_cancel), Box::new(no_cancel),
); );
@ -358,14 +358,13 @@ fn test_expand_overflow() {
let vals: Vec<WString> = (1..=64).map(|i| i.to_wstring()).collect(); let vals: Vec<WString> = (1..=64).map(|i| i.to_wstring()).collect();
let expansion = WString::from_str(&str::repeat("$bigvar", 64)); let expansion = WString::from_str(&str::repeat("$bigvar", 64));
let parser = Parser::principal_parser().shared(); let parser = Parser::principal_parser();
parser.vars().push(true); parser.vars().push(true);
let set = parser.vars().set(L!("bigvar"), EnvMode::LOCAL, vals); let set = parser.vars().set(L!("bigvar"), EnvMode::LOCAL, vals);
assert_eq!(set, EnvStackSetResult::Ok); assert_eq!(set, EnvStackSetResult::Ok);
let mut errors = ParseErrorList::new(); let mut errors = ParseErrorList::new();
let ctx = let ctx = OperationContext::foreground(parser, Box::new(no_cancel), EXPANSION_LIMIT_DEFAULT);
OperationContext::foreground(parser.clone(), Box::new(no_cancel), EXPANSION_LIMIT_DEFAULT);
// We accept only 1024 completions. // We accept only 1024 completions.
let mut output = CompletionReceiver::new(1024); let mut output = CompletionReceiver::new(1024);

View File

@ -614,7 +614,7 @@ fn test_eval_recursion_detection() {
let _cleanup = test_init(); let _cleanup = test_init();
// Ensure that we don't crash on infinite self recursion and mutual recursion. These must use // Ensure that we don't crash on infinite self recursion and mutual recursion. These must use
// the principal parser because we cannot yet execute jobs on other parsers. // the principal parser because we cannot yet execute jobs on other parsers.
let parser = Parser::principal_parser().shared(); let parser = Parser::principal_parser();
parser.eval( parser.eval(
L!("function recursive ; recursive ; end ; recursive; "), L!("function recursive ; recursive ; end ; recursive; "),
&IoChain::new(), &IoChain::new(),
@ -665,7 +665,7 @@ fn test_eval_illegal_exit_code() {
#[serial] #[serial]
fn test_eval_empty_function_name() { fn test_eval_empty_function_name() {
let _cleanup = test_init(); let _cleanup = test_init();
let parser = Parser::principal_parser().shared(); let parser = Parser::principal_parser();
parser.eval( parser.eval(
L!("function '' ; echo fail; exit 42 ; end ; ''"), L!("function '' ; echo fail; exit 42 ; end ; ''"),
&IoChain::new(), &IoChain::new(),
@ -676,7 +676,7 @@ fn test_eval_empty_function_name() {
#[serial] #[serial]
fn test_expand_argument_list() { fn test_expand_argument_list() {
let _cleanup = test_init(); let _cleanup = test_init();
let parser = Parser::principal_parser().shared(); let parser = Parser::principal_parser();
let comps: Vec<WString> = Parser::expand_argument_list( let comps: Vec<WString> = Parser::expand_argument_list(
L!("alpha 'beta gamma' delta"), L!("alpha 'beta gamma' delta"),
ExpandFlags::default(), ExpandFlags::default(),