Skip to content

Commit

Permalink
compiler: Remove version info from SourceFile again
Browse files Browse the repository at this point in the history
... and fix the fallout of that change.
  • Loading branch information
hunger committed Aug 21, 2024
1 parent 1c447db commit 45c2480
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 81 deletions.
18 changes: 4 additions & 14 deletions internal/compiler/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ pub trait Spanned {
}
}

pub type SourceFileVersion = Option<i32>;

#[derive(Default)]
pub struct SourceFileInner {
path: PathBuf,
Expand All @@ -73,21 +71,17 @@ pub struct SourceFileInner {

/// The offset of each linebreak
line_offsets: once_cell::unsync::OnceCell<Vec<usize>>,

/// The version of the source file. `None` means "as seen on disk"
version: SourceFileVersion,
}

impl std::fmt::Debug for SourceFileInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = if let Some(v) = self.version { format!("@{v}") } else { String::new() };
write!(f, "{:?}{v}", self.path)
write!(f, "{:?}", self.path)
}
}

impl SourceFileInner {
pub fn new(path: PathBuf, source: String, version: SourceFileVersion) -> Self {
Self { path, source: Some(source), line_offsets: Default::default(), version }
pub fn new(path: PathBuf, source: String) -> Self {
Self { path, source: Some(source), line_offsets: Default::default() }
}

pub fn path(&self) -> &Path {
Expand Down Expand Up @@ -154,10 +148,6 @@ impl SourceFileInner {
pub fn source(&self) -> Option<&str> {
self.source.as_deref()
}

pub fn version(&self) -> SourceFileVersion {
self.version
}
}

pub type SourceFile = Rc<SourceFileInner>;
Expand Down Expand Up @@ -590,7 +580,7 @@ component MainWindow inherits Window {
"#.to_string();
let sf = SourceFileInner::new(PathBuf::from("foo.slint"), content.clone(), None);
let sf = SourceFileInner::new(PathBuf::from("foo.slint"), content.clone());

let mut line = 1;
let mut column = 1;
Expand Down
12 changes: 4 additions & 8 deletions internal/compiler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,15 @@ pub async fn compile_syntax_node(
/// a set of `BuildDiagnostics` and a `TypeLoader` with all compilation passes applied.
pub async fn load_root_file(
path: &Path,
version: diagnostics::SourceFileVersion,
source_path: &Path,
source_code: String,
mut diagnostics: diagnostics::BuildDiagnostics,
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
) -> (std::path::PathBuf, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);

let (path, _) = loader
.load_root_file(path, version, source_path, source_code, false, &mut diagnostics)
.await;
let (path, _) =
loader.load_root_file(path, source_path, source_code, false, &mut diagnostics).await;

(path, diagnostics, loader)
}
Expand All @@ -335,7 +333,6 @@ pub async fn load_root_file(
/// applied and another `TypeLoader` with a minimal set of passes applied to it.
pub async fn load_root_file_with_raw_type_loader(
path: &Path,
version: diagnostics::SourceFileVersion,
source_path: &Path,
source_code: String,
mut diagnostics: diagnostics::BuildDiagnostics,
Expand All @@ -348,9 +345,8 @@ pub async fn load_root_file_with_raw_type_loader(
) {
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);

let (path, raw_type_loader) = loader
.load_root_file(path, version, source_path, source_code, true, &mut diagnostics)
.await;
let (path, raw_type_loader) =
loader.load_root_file(path, source_path, source_code, true, &mut diagnostics).await;

(path, diagnostics, loader, raw_type_loader)
}
2 changes: 1 addition & 1 deletion internal/compiler/load_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::typeregister::TypeRegister;
/// At this point, it really should already contain the basic Types (string, int, ...)
pub(crate) fn load_builtins(register: &mut TypeRegister) {
let mut diag = crate::diagnostics::BuildDiagnostics::default();
let node = crate::parser::parse(include_str!("builtins.slint").into(), None, None, &mut diag);
let node = crate::parser::parse(include_str!("builtins.slint").into(), None, &mut diag);
if !diag.is_empty() {
let vec = diag.to_string_vec();
#[cfg(feature = "display-diagnostics")]
Expand Down
6 changes: 2 additions & 4 deletions internal/compiler/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This module has different sub modules with the actual parser functions
*/

use crate::diagnostics::{BuildDiagnostics, SourceFile, SourceFileVersion, Spanned};
use crate::diagnostics::{BuildDiagnostics, SourceFile, Spanned};
pub use smol_str::SmolStr;
use std::fmt::Display;

Expand Down Expand Up @@ -976,14 +976,12 @@ pub fn normalize_identifier(ident: &str) -> String {
pub fn parse(
source: String,
path: Option<&std::path::Path>,
version: SourceFileVersion,
build_diagnostics: &mut BuildDiagnostics,
) -> SyntaxNode {
let mut p = DefaultParser::new(&source, build_diagnostics);
p.source_file = std::rc::Rc::new(crate::diagnostics::SourceFileInner::new(
path.map(crate::pathutils::clean_path).unwrap_or_default(),
source,
version,
));
document::parse_document(&mut p);
SyntaxNode {
Expand All @@ -1000,7 +998,7 @@ pub fn parse_file<P: AsRef<std::path::Path>>(
let source = crate::diagnostics::load_from_path(&path)
.map_err(|d| build_diagnostics.push_internal_error(d))
.ok()?;
Some(parse(source, Some(path.as_ref()), None, build_diagnostics))
Some(parse(source, Some(path.as_ref()), build_diagnostics))
}

pub fn parse_tokens(
Expand Down
1 change: 0 additions & 1 deletion internal/compiler/passes/const_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ export component Foo {
"#
.into(),
Some(std::path::Path::new("HELLO")),
None,
&mut test_diags,
);
let (doc, diag, _) =
Expand Down
1 change: 0 additions & 1 deletion internal/compiler/passes/default_geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ fn test_no_property_for_100pc() {
"#
.into(),
Some(std::path::Path::new("HELLO")),
None,
&mut test_diags,
);
let (doc, diag, _) =
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/tests/syntax_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn process_file_source(
) -> std::io::Result<bool> {
let mut parse_diagnostics = i_slint_compiler::diagnostics::BuildDiagnostics::default();
let syntax_node =
i_slint_compiler::parser::parse(source.clone(), Some(path), None, &mut parse_diagnostics);
i_slint_compiler::parser::parse(source.clone(), Some(path), &mut parse_diagnostics);

let has_parse_error = parse_diagnostics.has_errors();
let mut compiler_config = i_slint_compiler::CompilerConfiguration::new(
Expand Down
18 changes: 2 additions & 16 deletions internal/compiler/typeloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::rc::{Rc, Weak};

use crate::diagnostics::{BuildDiagnostics, SourceFileVersion, Spanned};
use crate::diagnostics::{BuildDiagnostics, Spanned};
use crate::object_tree::{self, Document, ExportedName, Exports};
use crate::parser::{syntax_nodes, NodeOrToken, SyntaxKind, SyntaxToken};
use crate::typeregister::TypeRegister;
Expand Down Expand Up @@ -1128,7 +1128,6 @@ impl TypeLoader {
Self::load_file_impl(
state,
&path_canon,
None,
&path_canon,
source,
builtin.is_some(),
Expand Down Expand Up @@ -1182,7 +1181,6 @@ impl TypeLoader {
pub async fn load_file(
&mut self,
path: &Path,
version: SourceFileVersion,
source_path: &Path,
source_code: String,
is_builtin: bool,
Expand All @@ -1192,7 +1190,6 @@ impl TypeLoader {
Self::load_file_impl(
&state,
path,
version,
source_path,
source_code,
is_builtin,
Expand All @@ -1207,7 +1204,6 @@ impl TypeLoader {
pub async fn load_root_file(
&mut self,
path: &Path,
version: SourceFileVersion,
source_path: &Path,
source_code: String,
keep_raw: bool,
Expand All @@ -1218,7 +1214,6 @@ impl TypeLoader {
let (path, mut doc) = Self::load_file_no_pass(
&state,
&path,
version,
source_path,
source_code,
false,
Expand All @@ -1240,7 +1235,6 @@ impl TypeLoader {
async fn load_file_impl<'a>(
state: &'a RefCell<BorrowedTypeLoader<'a>>,
path: &Path,
version: SourceFileVersion,
source_path: &Path,
source_code: String,
is_builtin: bool,
Expand All @@ -1249,7 +1243,6 @@ impl TypeLoader {
let (path, doc) = Self::load_file_no_pass(
state,
path,
version,
source_path,
source_code,
is_builtin,
Expand All @@ -1268,15 +1261,13 @@ impl TypeLoader {
async fn load_file_no_pass<'a>(
state: &'a RefCell<BorrowedTypeLoader<'a>>,
path: &Path,
version: SourceFileVersion,
source_path: &Path,
source_code: String,
is_builtin: bool,
import_stack: &HashSet<PathBuf>,
) -> (PathBuf, Document) {
let dependency_doc: syntax_nodes::Document =
crate::parser::parse(source_code, Some(source_path), version, state.borrow_mut().diag)
.into();
crate::parser::parse(source_code, Some(source_path), state.borrow_mut().diag).into();

let dependency_registry =
Rc::new(RefCell::new(TypeRegister::new(&state.borrow().tl.global_type_registry)));
Expand Down Expand Up @@ -1652,7 +1643,6 @@ X := XX {}
"#
.into(),
Some(std::path::Path::new("HELLO")),
None,
&mut test_diags,
);

Expand Down Expand Up @@ -1686,7 +1676,6 @@ component Foo { XX {} }
"#
.into(),
Some(std::path::Path::new("HELLO")),
None,
&mut test_diags,
);

Expand Down Expand Up @@ -1825,7 +1814,6 @@ import { LibraryHelperType } from "@libdir/library_helper_type.slint";
"#
.into(),
Some(std::path::Path::new("HELLO")),
None,
&mut test_diags,
);

Expand Down Expand Up @@ -1870,7 +1858,6 @@ import { E } from "@unknown/lib.slint";
"#
.into(),
Some(std::path::Path::new("HELLO")),
None,
&mut test_diags,
);

Expand Down Expand Up @@ -1916,7 +1903,6 @@ fn test_snapshotting() {
let mut diag = BuildDiagnostics::default();
spin_on::spin_on(type_loader.load_file(
&path,
None,
&path,
"export component Foobar inherits Rectangle { }".to_string(),
false,
Expand Down
9 changes: 4 additions & 5 deletions internal/interpreter/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,7 @@ impl ComponentCompiler {
}
};

let r =
crate::dynamic_item_tree::load(source, path.into(), None, self.config.clone()).await;
let r = crate::dynamic_item_tree::load(source, path.into(), self.config.clone()).await;
self.diagnostics = r.diagnostics.into_iter().collect();
r.components.into_values().next()
}
Expand All @@ -643,7 +642,7 @@ impl ComponentCompiler {
source_code: String,
path: PathBuf,
) -> Option<ComponentDefinition> {
let r = crate::dynamic_item_tree::load(source_code, path, None, self.config.clone()).await;
let r = crate::dynamic_item_tree::load(source_code, path, self.config.clone()).await;
self.diagnostics = r.diagnostics.into_iter().collect();
r.components.into_values().next()
}
Expand Down Expand Up @@ -778,7 +777,7 @@ impl Compiler {
}
};

crate::dynamic_item_tree::load(source, path.into(), None, self.config.clone()).await
crate::dynamic_item_tree::load(source, path.into(), self.config.clone()).await
}

/// Compile some .slint code
Expand All @@ -794,7 +793,7 @@ impl Compiler {
/// If that is not used, then it is fine to use a very simple executor, such as the one
/// provided by the `spin_on` crate
pub async fn build_from_source(&self, source_code: String, path: PathBuf) -> CompilationResult {
crate::dynamic_item_tree::load(source_code, path, None, self.config.clone()).await
crate::dynamic_item_tree::load(source_code, path, self.config.clone()).await
}
}

Expand Down
6 changes: 1 addition & 5 deletions internal/interpreter/dynamic_item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::global_component::CompiledGlobalCollection;
use crate::{dynamic_type, eval};
use core::ptr::NonNull;
use dynamic_type::{Instance, InstanceBox};
use i_slint_compiler::diagnostics::SourceFileVersion;
use i_slint_compiler::expression_tree::{Expression, NamedReference};
use i_slint_compiler::langtype::Type;
use i_slint_compiler::object_tree::ElementRc;
Expand Down Expand Up @@ -813,7 +812,6 @@ fn rtti_for<T: 'static + Default + rtti::BuiltinItem + vtable::HasStaticVTable<I
pub async fn load(
source: String,
path: std::path::PathBuf,
version: SourceFileVersion,
mut compiler_config: CompilerConfiguration,
) -> CompilationResult {
// If the native style should be Qt, resolve it here as we know that we have it
Expand All @@ -833,7 +831,6 @@ pub async fn load(
let (path, mut diag, loader, raw_type_loader) =
i_slint_compiler::load_root_file_with_raw_type_loader(
&path,
version,
&path,
source,
diag,
Expand All @@ -842,8 +839,7 @@ pub async fn load(
.await;
#[cfg(not(feature = "highlight"))]
let (path, mut diag, loader) =
i_slint_compiler::load_root_file(&path, version, &path, source, diag, compiler_config)
.await;
i_slint_compiler::load_root_file(&path, &path, source, diag, compiler_config).await;
if diag.has_errors() {
return CompilationResult {
components: HashMap::new(),
Expand Down
2 changes: 1 addition & 1 deletion tests/driver/cpp/cppdriver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box<dyn Error>>
let cpp_namespace = test_driver_lib::extract_cpp_namespace(&source);

let mut diag = BuildDiagnostics::default();
let syntax_node = parser::parse(source.clone(), Some(&testcase.absolute_path), None, &mut diag);
let syntax_node = parser::parse(source.clone(), Some(&testcase.absolute_path), &mut diag);
let output_format = generator::OutputFormat::Cpp(generator::cpp::Config {
namespace: cpp_namespace,
..Default::default()
Expand Down
3 changes: 1 addition & 2 deletions tests/driver/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ fn generate_source(
.collect::<std::collections::HashMap<_, _>>();

let mut diag = BuildDiagnostics::default();
let syntax_node =
parser::parse(source.to_owned(), Some(&testcase.absolute_path), None, &mut diag);
let syntax_node = parser::parse(source.to_owned(), Some(&testcase.absolute_path), &mut diag);
let mut compiler_config = CompilerConfiguration::new(generator::OutputFormat::Rust);
compiler_config.enable_experimental = true;
compiler_config.include_paths = include_paths;
Expand Down
3 changes: 1 addition & 2 deletions tests/screenshots/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ fn generate_source(
.collect::<Vec<_>>();

let mut diag = BuildDiagnostics::default();
let syntax_node =
parser::parse(source.to_owned(), Some(&testcase.absolute_path), None, &mut diag);
let syntax_node = parser::parse(source.to_owned(), Some(&testcase.absolute_path), &mut diag);
let mut compiler_config = CompilerConfiguration::new(generator::OutputFormat::Rust);
compiler_config.include_paths = include_paths;
compiler_config.embed_resources = EmbedResourcesKind::EmbedTextures;
Expand Down
Loading

0 comments on commit 45c2480

Please sign in to comment.