Skip to content

Commit

Permalink
try_emit
Browse files Browse the repository at this point in the history
  • Loading branch information
MingweiSamuel committed Dec 11, 2024
1 parent 5b9b563 commit 628d66b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions hydroflow_datalog/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()
}
})
}
}
}
1 change: 1 addition & 0 deletions hydroflow_datalog_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions hydroflow_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions hydroflow_macro/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand Down
33 changes: 13 additions & 20 deletions hydroflow_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg_attr(
feature = "diagnostics",
nightly,
feature(proc_macro_diagnostic, proc_macro_span, proc_macro_def_site)
)]

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 628d66b

Please sign in to comment.