Skip to content

Commit 1eb716e

Browse files
authored
Fix macro error handling (#811)
This is a follow-up for #807.
1 parent 675fbd3 commit 1eb716e

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

macro/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ fn main() -> Result<(), Box<dyn Error>> {
1212
llvm_config("--includedir", &version_variable)?
1313
);
1414

15+
// For testing
16+
println!("cargo:rustc-env=ENV_VAR_FOR_MLIR_DIALECT_DIR=foo/bar/baz");
17+
1518
Ok(())
1619
}
1720

macro/src/dialect.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,14 @@ pub fn generate_dialect(input: DialectInput) -> Result<TokenStream, Error> {
3131

3232
parser = parser.add_include_directory(LLVM_INCLUDE_DIRECTORY);
3333

34-
let get_path = |path: &str| {
35-
let path = if matches!(
36-
Path::new(path).components().next(),
37-
Some(Component::CurDir | Component::ParentDir)
38-
) {
39-
path.into()
40-
} else {
41-
Path::new(LLVM_INCLUDE_DIRECTORY).join(path)
42-
};
43-
path.display().to_string()
44-
};
45-
4634
for path in input.directories() {
47-
let path = get_path(path);
48-
parser = parser.add_include_directory(&path);
35+
parser = parser.add_include_directory(&resolve_include_directory(path));
4936
}
5037

5138
for (env_var, span) in input.directory_env_vars() {
52-
let path = match env::var(env_var) {
53-
Ok(path) => path,
54-
Err(err) => {
55-
return Err(syn::Error::new(span.clone(), err.to_string()).into())
56-
}
57-
};
58-
let path = get_path(&path);
59-
parser = parser.add_include_directory(&path);
39+
parser = parser.add_include_directory(&resolve_include_directory(
40+
&env::var(env_var).map_err(|error| syn::Error::new(*span, error.to_string()))?,
41+
));
6042
}
6143

6244
if input.files().count() > 0 {
@@ -112,6 +94,19 @@ fn generate_dialect_module(
11294
})
11395
}
11496

97+
fn resolve_include_directory(path: &str) -> String {
98+
if matches!(
99+
Path::new(path).components().next(),
100+
Some(Component::CurDir | Component::ParentDir)
101+
) {
102+
path.into()
103+
} else {
104+
Path::new(LLVM_INCLUDE_DIRECTORY).join(path)
105+
}
106+
.display()
107+
.to_string()
108+
}
109+
115110
fn create_syn_error(error: impl Display) -> syn::Error {
116111
syn::Error::new(Span::call_site(), format!("{error}"))
117112
}

macro/src/dialect/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
mod ods;
22

33
pub use self::ods::OdsError;
4+
use proc_macro::TokenStream;
5+
use quote::quote;
46
use std::{
57
error,
68
fmt::{self, Display, Formatter},
79
io,
810
string::FromUtf8Error,
911
};
10-
use quote::quote;
11-
use proc_macro::TokenStream;
1212
use tblgen::{
1313
SourceInfo,
1414
error::{SourceError, TableGenError},

macro/src/dialect/input.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod input_field;
22

33
use self::input_field::InputField;
4-
use std::ops::Deref;
54
use proc_macro2::Span;
5+
use std::ops::Deref;
66
use syn::{Token, parse::Parse, punctuated::Punctuated};
77

88
pub struct DialectInput {
@@ -26,9 +26,9 @@ impl DialectInput {
2626
}
2727

2828
pub fn directory_env_vars(&self) -> impl Iterator<Item = (&str, &Span)> {
29-
self.directory_env_vars.iter().map(|(value, span)|{
30-
(value.deref(), span)
31-
})
29+
self.directory_env_vars
30+
.iter()
31+
.map(|(value, span)| (value.deref(), span))
3232
}
3333
}
3434

@@ -49,7 +49,10 @@ impl Parse for DialectInput {
4949
directories = field.into_iter().map(|literal| literal.value()).collect()
5050
}
5151
InputField::DirectoryEnvVars(field) => {
52-
directory_env_vars = field.into_iter().map(|literal| (literal.value(), literal.span())).collect()
52+
directory_env_vars = field
53+
.into_iter()
54+
.map(|literal| (literal.value(), literal.span()))
55+
.collect()
5356
}
5457
}
5558
}

macro/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ use syn::parse_macro_input;
2828
#[proc_macro]
2929
pub fn dialect(input: TokenStream) -> TokenStream {
3030
let input = parse_macro_input!(input as DialectInput);
31-
dialect::generate_dialect(input).unwrap_or_else(|error| {
32-
error.to_compile_error().into()
33-
})
31+
dialect::generate_dialect(input).unwrap_or_else(|error| error.to_compile_error())
3432
}
3533

3634
#[proc_macro]

0 commit comments

Comments
 (0)