From 79ec558d085c21b8afb5454f04d5769e613eedfc Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Sun, 20 Apr 2025 18:48:54 -0700 Subject: [PATCH] ast: remove Default implementation This doesn't make much conceptual sense, and isn't required. Do some other miscellaneous cleanup. --- src/ast.rs | 66 +++++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 45 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index e61ff752a..1854c6c8d 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -2394,17 +2394,6 @@ pub struct Ast { pub extras: Extras, } -#[allow(clippy::derivable_impls)] // false positive -impl Default for Ast { - fn default() -> Ast { - Self { - top: Box::::default(), - any_error: false, - extras: Extras::default(), - } - } -} - impl Ast { /// Construct an ast by parsing `src` as a job list. /// The ast attempts to produce `type` as the result. @@ -3658,21 +3647,6 @@ fn try_parse(&mut self) -> Option { Some(self.allocate_visit()) } - /// Given a node type, allocate it and invoke its default constructor. - /// Return the resulting Node - fn allocate(&self) -> Box { - let result = Box::::default(); - FLOGF!( - ast_construction, - "%*smake %ls %ls", - self.spaces(), - "", - ast_type_to_string(result.typ()), - format!("{result:p}") - ); - result - } - // Given a node type, allocate it, invoking its default constructor, // and then visit it as a field. // Return the resulting Node. @@ -3845,34 +3819,36 @@ fn parse_from_top( out_errors: Option<&mut ParseErrorList>, top_type: Type, ) -> Ast { - assert!( - [Type::job_list, Type::freestanding_argument_list].contains(&top_type), - "Invalid top type" - ); - - let mut ast = Ast::default(); - let mut pops = Populator::new(src, flags, top_type, out_errors); - if top_type == Type::job_list { - let mut list = pops.allocate::(); - pops.populate_list(&mut *list, true /* exhaust_stream */); - ast.top = list; - } else { - let mut list = pops.allocate::(); - pops.populate_list(&mut list.arguments, true /* exhaust_stream */); - ast.top = list; - } + let top: Box = match top_type { + Type::job_list => { + let mut list: Box = Box::default(); + pops.populate_list(&mut *list, true); + list + } + Type::freestanding_argument_list => { + let mut list = Box::::default(); + pops.populate_list(&mut list.arguments, true); + list + } + _ => panic!("Invalid top type"), + }; // Chomp trailing extras, etc. pops.chomp_extras(Type::job_list); - ast.any_error = pops.any_error; - ast.extras = Extras { + let any_error = pops.any_error; + let extras = Extras { comments: pops.tokens.comment_ranges, semis: pops.semis, errors: pops.errors, }; - ast + + Ast { + top, + any_error, + extras, + } } /// Return tokenizer flags corresponding to parse tree flags.