From 54c93c9660296fa2d919e6e71c6f3af3617b25e6 Mon Sep 17 00:00:00 2001 From: VolodymyrBg Date: Sat, 19 Apr 2025 22:47:11 +0300 Subject: [PATCH] Fix: Remove panic risk in AST pragma processing This commit fixes a potential panic in the AST parsing logic when processing pragma directives. Instead of directly accessing the `start` and `end` fields of the Meta struct, which could potentially be uninitialized, the code now safely uses the `location` field through cloning. This change ensures more robust error handling when processing malformed pragma directives in the source code. --- program_structure/src/abstract_syntax_tree/ast.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/program_structure/src/abstract_syntax_tree/ast.rs b/program_structure/src/abstract_syntax_tree/ast.rs index bc8b7ce0e..341a6bf7b 100644 --- a/program_structure/src/abstract_syntax_tree/ast.rs +++ b/program_structure/src/abstract_syntax_tree/ast.rs @@ -105,15 +105,15 @@ impl AST { let mut reports = Vec::new(); for p in pragmas { match p { - // TODO: don't panic + // Handle version pragma without panicking Pragma::Version(location, file_id, ver) => match compiler_version { Some(_) => reports.push(produce_report( - ReportCode::MultiplePragma,location.start..location.end, file_id)), + ReportCode::MultiplePragma, location.location.clone(), file_id)), None => compiler_version = Some(ver), }, Pragma::CustomGates(location, file_id ) => match custom_gates { Some(_) => reports.push(produce_report( - ReportCode::MultiplePragma, location.start..location.end, file_id)), + ReportCode::MultiplePragma, location.location.clone(), file_id)), None => custom_gates = Some(true), }, Pragma::Unrecognized => {}, //This error is previously handled, and the @@ -655,4 +655,4 @@ pub fn tuple_general_error(meta : Meta, msg : String) -> Report { "This is the tuple whose use is not allowed".to_string(), ); report -} \ No newline at end of file +}