From 242b01549a472d4eeca1404b8f63427e23224253 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 16 May 2022 19:13:39 +0200 Subject: Safe `eval` function --- src/source.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/source.rs') diff --git a/src/source.rs b/src/source.rs index 780e12a8..deeaef4b 100644 --- a/src/source.rs +++ b/src/source.rs @@ -12,7 +12,7 @@ use crate::diag::TypResult; use crate::loading::{FileHash, Loader}; use crate::parse::{is_newline, parse, reparse}; use crate::syntax::ast::Markup; -use crate::syntax::{self, Category, GreenNode, RedNode}; +use crate::syntax::{self, Category, GreenNode, RedNode, Span}; use crate::util::{PathExt, StrExt}; #[cfg(feature = "codespan-reporting")] @@ -23,6 +23,11 @@ use codespan_reporting::files::{self, Files}; pub struct SourceId(u32); impl SourceId { + /// Create a new source id for a file that is not part of a store. + pub const fn detached() -> Self { + Self(u32::MAX) + } + /// Create a source id from the raw underlying value. /// /// This should only be called with values returned by @@ -165,7 +170,12 @@ impl SourceFile { /// Create a source file without a real id and path, usually for testing. pub fn detached(src: impl Into) -> Self { - Self::new(SourceId(0), Path::new(""), src.into()) + Self::new(SourceId::detached(), Path::new(""), src.into()) + } + + /// Set a synthetic span for all nodes in this file. + pub fn synthesize(&mut self, span: Span) { + Arc::make_mut(&mut self.root).synthesize(Arc::new(span)); } /// The root node of the file's untyped green tree. -- cgit v1.2.3