Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MingweiSamuel committed Dec 11, 2024
1 parent 646bb8f commit 5b9b563
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 41 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion hydroflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ workspace = true
[features]
default = [ "macros", "nightly", "debugging" ]

nightly = [ "hydroflow_macro", "hydroflow_macro/diagnostics" ]
nightly = []
macros = [ "hydroflow_macro", "hydroflow_datalog" ]
hydroflow_macro = [ "dep:hydroflow_macro" ]
hydroflow_datalog = [ "dep:hydroflow_datalog" ]
Expand Down
3 changes: 0 additions & 3 deletions hydroflow_datalog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ workspace = true
proc-macro = true
path = "src/lib.rs"

[features]
diagnostics = [ "hydroflow_datalog_core/diagnostics" ]

[dependencies]
quote = "1.0.35"
syn = { version = "2.0.46", features = [ "parsing", "extra-traits" ] }
Expand Down
4 changes: 0 additions & 4 deletions hydroflow_datalog_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ workspace = true
[lib]
path = "src/lib.rs"

[features]
default = []
diagnostics = [ "hydroflow_lang/diagnostics" ]

[dependencies]
quote = "1.0.35"
slotmap = "1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion hydroflow_lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ workspace = true

[features]
default = []
diagnostics = []
debugging = [ "dep:data-encoding", "dep:webbrowser", "clap-derive" ]
clap-derive = [ "dep:clap" ]

Expand All @@ -34,3 +33,4 @@ webbrowser = { version = "1.0.0", optional = true }

[build-dependencies]
syn = { version = "2.0.46", features = [ "extra-traits", "full", "parsing" ] }
rustc_version = "0.4.0"
10 changes: 10 additions & 0 deletions hydroflow_lang/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fs::File;
use std::io::{BufWriter, Error, ErrorKind, Result, Write};
use std::path::PathBuf;

use rustc_version::{version_meta, Channel};
use syn::{
parse_quote, AttrStyle, Expr, ExprLit, Ident, Item, Lit, Member, Meta, MetaNameValue, Path,
};
Expand All @@ -12,6 +13,15 @@ const OPS_PATH: &str = "src/graph/ops";

fn main() {
println!("cargo::rerun-if-changed={}", OPS_PATH);

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) != var("CARGO_CFG_HYDROFLOW_GENERATE_DOCS") {
if let Err(err) = generate_op_docs() {
eprintln!("hydroflow_lang/build.rs error: {:?}", err);
Expand Down
60 changes: 42 additions & 18 deletions hydroflow_lang/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl Level {
/// Diagnostic. A warning or error (or lower [`Level`]) with a message and span. Shown by IDEs
/// usually as a squiggly red or yellow underline.
///
/// Must call [`Diagnostic::emit`] or manually emit the output of [`Diagnostic::to_tokens`] for the
/// diagnostic to show up.
/// Diagnostics must be emitted via [`Diagnostic::try_emit`], [`Diagnostic::to_tokens`], or
/// [`Diagnostic::try_emit_all`] for diagnostics to show up.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Diagnostic<S = Span> {
/// Span (source code location).
Expand All @@ -68,23 +68,47 @@ impl Diagnostic {
}
}

/// Emit the diagnostic. Only works from the `proc_macro` context. Does not work outside of
/// that e.g. in normal runtime execution or in tests.
pub fn emit(&self) {
#[cfg(feature = "diagnostics")]
/// Emit if possible, otherwise return `Err` containing a [`TokenStream`] of a
/// `compile_error!(...)` call.
pub fn try_emit(&self) -> Result<(), TokenStream> {
#[cfg(nightly)]
{
let pm_diag = match self.level {
Level::Error => self.span.unwrap().error(&*self.message),
Level::Warning => self.span.unwrap().warning(&*self.message),
Level::Note => self.span.unwrap().note(&*self.message),
Level::Help => self.span.unwrap().help(&*self.message),
};
pm_diag.emit();
if let Ok(()) = std::panic::catch_unwind(|| {
let pm_diag = match self.level {
Level::Error => self.span.unwrap().error(&*self.message),
Level::Warning => self.span.unwrap().warning(&*self.message),
Level::Note => self.span.unwrap().note(&*self.message),
Level::Help => self.span.unwrap().help(&*self.message),
};
pm_diag.emit()
}) {
return Ok(());
}
}
Err(self.to_tokens())
}

/// Emits all if possible, otherwise returns `Err` containing a [`TokenStream`] of
/// `compile_error!(...)` calls.
pub fn try_emit_all<'a>(
diagnostics: impl IntoIterator<Item = &'a Self>,
) -> Result<(), TokenStream> {
if let Some(tokens) = diagnostics
.into_iter()
.filter_map(|diag| diag.try_emit().err())
.reduce(|mut tokens, next| {
tokens.extend(next);
tokens
})
{
Err(tokens)
} else {
Ok(())
}
}

/// Used to emulate [`Diagnostic::emit`] by turning this diagnostic into a properly spanned [`TokenStream`]
/// that emits an error with this diagnostic's message.
/// Used to emulate `proc_macro::Diagnostic::emit` by turning this diagnostic into a properly spanned [`TokenStream`]
/// that emits an error via `compile_error!(...)` with this diagnostic's message.
pub fn to_tokens(&self) -> TokenStream {
let msg_lit: Literal = Literal::string(&self.message);
let unique_ident = {
Expand All @@ -98,7 +122,7 @@ impl Diagnostic {
if Level::Error == self.level {
quote_spanned! {self.span=>
{
::std::compile_error!(#msg_lit);
::core::compile_error!(#msg_lit);
}
}
} else {
Expand Down Expand Up @@ -169,7 +193,7 @@ pub struct SerdeSpan {
}
impl From<Span> for SerdeSpan {
fn from(span: Span) -> Self {
#[cfg(feature = "diagnostics")]
#[cfg(nightly)]
let path = span
.unwrap()
.source_file()
Expand All @@ -178,7 +202,7 @@ impl From<Span> for SerdeSpan {
.to_string()
.into();

#[cfg(not(feature = "diagnostics"))]
#[cfg(not(nightly))]
let path = "unknown".into();

Self {
Expand Down
6 changes: 3 additions & 3 deletions hydroflow_lang/src/graph/hydroflow_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,10 @@ impl HydroflowGraph {
subgraph_op_iter_code.push(write_iterator);

if include_type_guards {
#[cfg(not(feature = "diagnostics"))]
#[cfg(not(nightly))]
let source_info = Option::<String>::None;

#[cfg(feature = "diagnostics")]
#[cfg(nightly)]
let source_info = std::panic::catch_unwind(|| op_span.unwrap())
.map(|op_span| {
format!(
Expand All @@ -1029,7 +1029,7 @@ impl HydroflowGraph {
.ok();

#[cfg_attr(
not(feature = "diagnostics"),
not(nightly),
expect(
clippy::unnecessary_literal_unwrap,
reason = "conditional compilation"
Expand Down
5 changes: 1 addition & 4 deletions hydroflow_lang/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Hydroflow surface syntax
#![warn(missing_docs)]
#![cfg_attr(
feature = "diagnostics",
feature(proc_macro_diagnostic, proc_macro_span)
)]
#![cfg_attr(nightly, feature(proc_macro_diagnostic, proc_macro_span))]
pub mod diagnostic;
pub mod graph;
pub mod parse;
Expand Down
4 changes: 2 additions & 2 deletions hydroflow_lang/src/pretty_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pub struct PrettySpan(pub proc_macro2::Span);
impl std::fmt::Display for PrettySpan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
#[cfg(feature = "diagnostics")]
#[cfg(nightly)]
{
if let Ok(span) = std::panic::catch_unwind(|| self.0.unwrap()) {
write!(
Expand All @@ -21,7 +21,7 @@ impl std::fmt::Display for PrettySpan {

write!(
f,
"nopath:{}:{}",
"unknown:{}:{}",
self.0.start().line,
self.0.start().column
)
Expand Down
3 changes: 0 additions & 3 deletions hydroflow_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ workspace = true
[lib]
proc-macro = true

[features]
diagnostics = [ "hydroflow_lang/diagnostics" ]

[dependencies]
# Note: If we ever compile this proc macro crate to WASM (e.g., if we are
# building on a WASM host), we may need to turn diagnostics off for WASM if
Expand Down
1 change: 0 additions & 1 deletion hydroflow_plus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ path = "src/lib.rs"

[features]
default = ["deploy_runtime"]
diagnostics = [ "hydroflow_lang/diagnostics" ]
stageleft_devel = []
deploy_runtime = [ "hydroflow/deploy_integration" ]
deploy = [ "deploy_runtime", "dep:hydro_deploy", "dep:trybuild-internals-api", "dep:toml", "dep:prettyplease" ]
Expand Down

0 comments on commit 5b9b563

Please sign in to comment.