From 3778038615e0d3cfe581fde82fca24b4ef353ad7 Mon Sep 17 00:00:00 2001 From: s0mnathh Date: Thu, 23 Apr 2026 20:33:56 +0800 Subject: [PATCH] Fix function debug info when file content is missing --- .../src/debug_info/function_debug_info.rs | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/crates/cairo-lang-sierra-generator/src/debug_info/function_debug_info.rs b/crates/cairo-lang-sierra-generator/src/debug_info/function_debug_info.rs index fb07b0982ad..602daab7ab0 100644 --- a/crates/cairo-lang-sierra-generator/src/debug_info/function_debug_info.rs +++ b/crates/cairo-lang-sierra-generator/src/debug_info/function_debug_info.rs @@ -10,6 +10,7 @@ use cairo_lang_defs::ids::{ TraitItemId, TraitLongId, TraitTypeLongId, UseLongId, }; use cairo_lang_filesystem::db::FilesGroup; +use cairo_lang_filesystem::ids::SpanInFile; use cairo_lang_lowering::Location; use cairo_lang_lowering::ids::LocationId; use cairo_lang_semantic::Expr; @@ -143,10 +144,7 @@ impl<'db> FunctionDebugInfo<'db> { }; let user_location = location.span_in_file(db).user_location(db); - let var_name = user_location - .span - .take(db.file_content(user_location.file_id).unwrap()) - .to_string(); + let var_name = variable_name_from_user_location(db, user_location)?; let position = user_location.span.position_in_file(db, user_location.file_id)?; let source_location = SourceCodeSpan { @@ -181,6 +179,13 @@ impl<'db> FunctionDebugInfo<'db> { } } +fn variable_name_from_user_location<'db>( + db: &'db dyn Database, + user_location: SpanInFile<'db>, +) -> Option { + Some(user_location.span.take(db.file_content(user_location.file_id)?).to_string()) +} + /// This function gets a node that a sierra variable was mapped to. /// It tries to make an educated guess to find an identifier corresponding to a cairo variable /// which value in the given location is represented by the sierra variable. @@ -522,3 +527,22 @@ fn lookup_variable_in_resolved_items<'db>( None } + +#[cfg(test)] +mod test { + use cairo_lang_filesystem::ids::{FileLongId, SpanInFile}; + use cairo_lang_filesystem::span::{TextOffset, TextSpan}; + use cairo_lang_utils::Intern; + + use super::variable_name_from_user_location; + use crate::test_utils::SierraGenDatabaseForTesting; + + #[test] + fn variable_name_from_user_location_returns_none_when_file_content_is_missing() { + let db = SierraGenDatabaseForTesting::new_empty(); + let file_id = FileLongId::OnDisk("missing.cairo".into()).intern(&db); + let user_location = SpanInFile { file_id, span: TextSpan::cursor(TextOffset::START) }; + + assert_eq!(variable_name_from_user_location(&db, user_location), None); + } +}