From 628d66bbb4c47884c2d2069ec39604984bedd522 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Wed, 11 Dec 2024 13:20:50 -0800 Subject: [PATCH] try_emit --- Cargo.lock | 1 + hydroflow_datalog/src/lib.rs | 14 +++++++++---- hydroflow_datalog_core/src/lib.rs | 1 + hydroflow_macro/Cargo.toml | 1 + hydroflow_macro/build.rs | 9 +++++++++ hydroflow_macro/src/lib.rs | 33 ++++++++++++------------------- 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e762579e135..d97a41f0818 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1577,6 +1577,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", + "rustc_version 0.4.1", "syn 2.0.75", ] diff --git a/hydroflow_datalog/src/lib.rs b/hydroflow_datalog/src/lib.rs index f1bd816360f..919115bd986 100644 --- a/hydroflow_datalog/src/lib.rs +++ b/hydroflow_datalog/src/lib.rs @@ -1,3 +1,4 @@ +use hydroflow_datalog_core::diagnostic::Diagnostic; use hydroflow_datalog_core::{gen_hydroflow_graph, hydroflow_graph_to_program}; use proc_macro2::Span; use quote::{quote, ToTokens}; @@ -31,10 +32,15 @@ pub fn datalog(item: proc_macro::TokenStream) -> proc_macro::TokenStream { program.to_token_stream().into() } Err(diagnostics) => { - for diagnostic in diagnostics { - diagnostic.emit(); - } - proc_macro::TokenStream::from(quote!(hydroflow::scheduled::graph::Hydroflow::new())) + let diagnostic_tokens = Diagnostic::try_emit_all(diagnostics.iter()) + .err() + .unwrap_or_default(); + proc_macro::TokenStream::from(quote! { + { + #diagnostic_tokens + hydroflow::scheduled::graph::Hydroflow::new() + } + }) } } } diff --git a/hydroflow_datalog_core/src/lib.rs b/hydroflow_datalog_core/src/lib.rs index 27203bdd6a3..64835e74ab6 100644 --- a/hydroflow_datalog_core/src/lib.rs +++ b/hydroflow_datalog_core/src/lib.rs @@ -1,6 +1,7 @@ use std::collections::{HashMap, HashSet}; use std::ops::Deref; +pub use hydroflow_lang::diagnostic; use hydroflow_lang::diagnostic::{Diagnostic, Level}; use hydroflow_lang::graph::{ eliminate_extra_unions_tees, partition_graph, FlatGraphBuilder, HydroflowGraph, diff --git a/hydroflow_macro/Cargo.toml b/hydroflow_macro/Cargo.toml index 16c9450b7fd..4581adb9960 100644 --- a/hydroflow_macro/Cargo.toml +++ b/hydroflow_macro/Cargo.toml @@ -27,3 +27,4 @@ syn = { version = "2.0.46", features = [ "parsing", "extra-traits" ] } hydroflow_lang = { path = "../hydroflow_lang", version = "^0.10.0" } itertools = "0.10.0" quote = "1.0.35" +rustc_version = "0.4.0" diff --git a/hydroflow_macro/build.rs b/hydroflow_macro/build.rs index adf5e9459b2..3fa4cb3c6f8 100644 --- a/hydroflow_macro/build.rs +++ b/hydroflow_macro/build.rs @@ -10,6 +10,7 @@ use hydroflow_lang::graph::ops::{PortListSpec, OPERATORS}; use hydroflow_lang::graph::PortIndexValue; use itertools::Itertools; use quote::ToTokens; +use rustc_version::{version_meta, Channel}; const FILENAME: &str = "surface_ops_gen.md"; @@ -207,6 +208,14 @@ fn update_book() -> Result<()> { } fn main() { + println!("cargo::rustc-check-cfg=cfg(nightly)"); + if matches!( + version_meta().map(|meta| meta.channel), + Ok(Channel::Nightly) + ) { + println!("cargo:rustc-cfg=nightly"); + } + if Err(VarError::NotPresent) != std::env::var("CARGO_CFG_HYDROFLOW_GENERATE_DOCS") { if let Err(err) = update_book() { eprintln!("hydroflow_macro/build.rs error: {:?}", err); diff --git a/hydroflow_macro/src/lib.rs b/hydroflow_macro/src/lib.rs index 96ff839cd7d..f03ae0dc87e 100644 --- a/hydroflow_macro/src/lib.rs +++ b/hydroflow_macro/src/lib.rs @@ -1,5 +1,5 @@ #![cfg_attr( - feature = "diagnostics", + nightly, feature(proc_macro_diagnostic, proc_macro_span, proc_macro_def_site) )] @@ -72,25 +72,16 @@ fn hydroflow_syntax_internal( .iter() .filter(|diag: &&Diagnostic| Some(diag.level) <= min_diagnostic_level); - #[cfg(feature = "diagnostics")] - { - diagnostics.for_each(Diagnostic::emit); - tokens.into() - } - - #[cfg(not(feature = "diagnostics"))] - { - let diagnostics = diagnostics.map(Diagnostic::to_tokens); - quote! { - { - #( - #diagnostics - )* - #tokens - } + let diagnostic_tokens = Diagnostic::try_emit_all(diagnostics) + .err() + .unwrap_or_default(); + quote! { + { + #diagnostic_tokens + #tokens } - .into() } + .into() } /// Parse Hydroflow "surface syntax" without emitting code. @@ -123,8 +114,10 @@ pub fn hydroflow_parser(input: proc_macro::TokenStream) -> proc_macro::TokenStre } } - diagnostics.iter().for_each(Diagnostic::emit); - quote! {}.into() + Diagnostic::try_emit_all(diagnostics.iter()) + .err() + .unwrap_or_default() + .into() } #[doc(hidden)]