diff --git a/api/sixtyfps-rs/sixtyfps-macros/Cargo.toml b/api/sixtyfps-rs/sixtyfps-macros/Cargo.toml index b6d10527c..3c754de4b 100644 --- a/api/sixtyfps-rs/sixtyfps-macros/Cargo.toml +++ b/api/sixtyfps-rs/sixtyfps-macros/Cargo.toml @@ -16,6 +16,6 @@ path = "lib.rs" [dependencies] quote = "1.0" proc-macro2 = "1.0.17" -sixtyfps-compilerlib = { version = "=0.0.2", path = "../../../sixtyfps_compiler", features = ["proc_macro_span", "rust"] } +sixtyfps-compilerlib = { version = "=0.0.2", path = "../../../sixtyfps_compiler", features = ["proc_macro_span", "rust", "display-diagnostics"] } spin_on = "0.1" diff --git a/api/sixtyfps-rs/sixtyfps-macros/lib.rs b/api/sixtyfps-rs/sixtyfps-macros/lib.rs index 23f51f884..1a3c7d438 100644 --- a/api/sixtyfps-rs/sixtyfps-macros/lib.rs +++ b/api/sixtyfps-rs/sixtyfps-macros/lib.rs @@ -336,8 +336,7 @@ pub fn sixtyfps(stream: TokenStream) -> TokenStream { spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config)); //println!("{:#?}", tree); if diag.has_error() { - diag.map_offsets_to_span(&tokens); - return diag.into_token_stream().into(); + return report_diagnostics(diag, &tokens); } let mut result = generator::rust::generate(&root_component, &mut diag); @@ -353,10 +352,27 @@ pub fn sixtyfps(stream: TokenStream) -> TokenStream { x.extend(quote! {const _ : Option<&'static str> = ::core::option_env!("SIXTYFPS_STYLE");}); }); - result - .unwrap_or_else(|| { - diag.map_offsets_to_span(&tokens); - diag.into_token_stream() - }) - .into() + let diags = report_diagnostics(diag, &tokens); + result.map_or(diags, |r| r.into()) +} + +fn report_diagnostics( + diag: diagnostics::BuildDiagnostics, + span_map: &[parser::Token], +) -> TokenStream { + let mut result = TokenStream::new(); + let mut needs_error = diag.has_error(); + for mut file_diag in diag.into_iter() { + if file_diag.source.is_none() { + file_diag.map_offsets_to_span(span_map); + needs_error &= !file_diag.has_error(); + result.extend(TokenStream::from(file_diag.into_token_stream())) + } else { + file_diag.print(); + } + } + if needs_error { + result.extend(TokenStream::from(quote!(compile_error! { "Error occured" }))) + } + result } diff --git a/sixtyfps_compiler/diagnostics.rs b/sixtyfps_compiler/diagnostics.rs index 7b9a188e3..e60647871 100644 --- a/sixtyfps_compiler/diagnostics.rs +++ b/sixtyfps_compiler/diagnostics.rs @@ -473,13 +473,6 @@ impl BuildDiagnostics { } } -#[cfg(feature = "proc_macro_span")] -impl quote::ToTokens for BuildDiagnostics { - fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { - self.iter().for_each(|diag| diag.to_tokens(tokens)) - } -} - impl Extend for BuildDiagnostics { fn extend>(&mut self, iter: T) { for diag in iter {