From c9a76bd63494c13fa36e545cf8cc56006db4da37 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 26 May 2024 14:49:51 -0700 Subject: [PATCH] Make OperationContext not hold a Parser via Rc Exploit Rust's lifetimes. This will lead to simplifications. --- src/builtins/commandline.rs | 2 +- src/operation_context.rs | 10 +++++----- src/parser.rs | 4 ++-- src/tests/complete.rs | 6 +++--- src/tests/expand.rs | 7 +++---- src/tests/parser.rs | 6 +++--- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/builtins/commandline.rs b/src/builtins/commandline.rs index eadb811a2..a98665465 100644 --- a/src/builtins/commandline.rs +++ b/src/builtins/commandline.rs @@ -143,7 +143,7 @@ fn write_part( &mut args, ExpandFlags::SKIP_CMDSUBST, &OperationContext::foreground( - parser.shared(), + parser, Box::new(no_cancel), COMMANDLINE_TOKENS_MAX_EXPANSION, ), diff --git a/src/operation_context.rs b/src/operation_context.rs index 1f4b859c9..7569c2161 100644 --- a/src/operation_context.rs +++ b/src/operation_context.rs @@ -1,7 +1,7 @@ use crate::common::CancelChecker; use crate::env::EnvDyn; use crate::env::{EnvStack, Environment}; -use crate::parser::{Parser, ParserRef}; +use crate::parser::Parser; use crate::proc::JobGroupRef; use crate::reader::read_generation_count; @@ -21,11 +21,11 @@ pub fn no_cancel() -> bool { enum Vars<'a> { // The parser, if this is a foreground operation. If this is a background operation, this may be // nullptr. - Parser(ParserRef), + Parser(&'a Parser), // A set of variables. 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, @@ -70,7 +70,7 @@ pub fn globals() -> OperationContext<'static> { /// Construct from a full set of properties. pub fn foreground( - parser: ParserRef, + parser: &'a Parser, cancel_checker: CancelChecker, expansion_limit: usize, ) -> OperationContext<'a> { @@ -83,7 +83,7 @@ pub fn foreground( } pub fn test_only_foreground( - parser: ParserRef, + parser: &'a Parser, vars: &'a dyn Environment, cancel_checker: CancelChecker, ) -> OperationContext<'a> { diff --git a/src/parser.rs b/src/parser.rs index 7dccb02ef..a11ae5d76 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1134,9 +1134,9 @@ pub fn shared(&self) -> ParserRef { } /// Return the operation context for this parser. - pub fn context(&self) -> OperationContext<'static> { + pub fn context(&self) -> OperationContext<'_> { OperationContext::foreground( - self.shared(), + self, Box::new(|| signal_check_cancel() != 0), EXPANSION_LIMIT_DEFAULT, ) diff --git a/src/tests/complete.rs b/src/tests/complete.rs index 785a48d71..360a1eaae 100644 --- a/src/tests/complete.rs +++ b/src/tests/complete.rs @@ -43,8 +43,8 @@ fn test_complete() { }, }; - let parser = Parser::principal_parser().shared(); - let ctx = OperationContext::test_only_foreground(parser.clone(), &vars, Box::new(no_cancel)); + let parser = Parser::principal_parser(); + let ctx = OperationContext::test_only_foreground(parser, &vars, Box::new(no_cancel)); let do_complete = |cmd: &wstr, flags: CompletionRequestOptions| complete(cmd, flags, &ctx).0; @@ -489,7 +489,7 @@ macro_rules! perform_one_completion_cd_test { L!($command), CompletionRequestOptions::default(), &OperationContext::foreground( - Parser::principal_parser().shared(), + Parser::principal_parser(), Box::new(no_cancel), EXPANSION_LIMIT_DEFAULT, ), diff --git a/src/tests/expand.rs b/src/tests/expand.rs index 533091621..ee351579a 100644 --- a/src/tests/expand.rs +++ b/src/tests/expand.rs @@ -27,7 +27,7 @@ fn expand_test_impl( let mut errors = ParseErrorList::new(); let pwd = PwdEnvironment::default(); let ctx = OperationContext::test_only_foreground( - Parser::principal_parser().shared(), + Parser::principal_parser(), &pwd, Box::new(no_cancel), ); @@ -358,14 +358,13 @@ fn test_expand_overflow() { let vals: Vec = (1..=64).map(|i| i.to_wstring()).collect(); let expansion = WString::from_str(&str::repeat("$bigvar", 64)); - let parser = Parser::principal_parser().shared(); + let parser = Parser::principal_parser(); parser.vars().push(true); let set = parser.vars().set(L!("bigvar"), EnvMode::LOCAL, vals); assert_eq!(set, EnvStackSetResult::Ok); let mut errors = ParseErrorList::new(); - let ctx = - OperationContext::foreground(parser.clone(), Box::new(no_cancel), EXPANSION_LIMIT_DEFAULT); + let ctx = OperationContext::foreground(parser, Box::new(no_cancel), EXPANSION_LIMIT_DEFAULT); // We accept only 1024 completions. let mut output = CompletionReceiver::new(1024); diff --git a/src/tests/parser.rs b/src/tests/parser.rs index 2fce062e2..a3729049b 100644 --- a/src/tests/parser.rs +++ b/src/tests/parser.rs @@ -614,7 +614,7 @@ fn test_eval_recursion_detection() { let _cleanup = test_init(); // 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. - let parser = Parser::principal_parser().shared(); + let parser = Parser::principal_parser(); parser.eval( L!("function recursive ; recursive ; end ; recursive; "), &IoChain::new(), @@ -665,7 +665,7 @@ macro_rules! validate { #[serial] fn test_eval_empty_function_name() { let _cleanup = test_init(); - let parser = Parser::principal_parser().shared(); + let parser = Parser::principal_parser(); parser.eval( L!("function '' ; echo fail; exit 42 ; end ; ''"), &IoChain::new(), @@ -676,7 +676,7 @@ fn test_eval_empty_function_name() { #[serial] fn test_expand_argument_list() { let _cleanup = test_init(); - let parser = Parser::principal_parser().shared(); + let parser = Parser::principal_parser(); let comps: Vec = Parser::expand_argument_list( L!("alpha 'beta gamma' delta"), ExpandFlags::default(),