Skip to content

Commit

Permalink
Improve diagnostics from the mir proc-macro on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
chorman0773 committed Oct 8, 2024
1 parent a27b1f0 commit 06142a5
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 115 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions rust/rust_mir_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
xlang_frontend = {path="../../xlang/xlang_frontend"}
xlang_frontend = { path = "../../xlang/xlang_frontend" }
proc-macro2 = { version = "1.0.87", features = ["span-locations"] }


[lib]
proc-macro = true
proc-macro = true
15 changes: 8 additions & 7 deletions rust/rust_mir_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use parse::Error;
use parse::{Error, ExpectedTokenType};
use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use xlang_frontend::iter::{PeekMoreIterator, Peekmore};

Expand Down Expand Up @@ -52,12 +52,12 @@ fn write_crate_path<'a, I: IntoIterator<Item = &'a str>>(

fn write_error(e: Error) -> TokenStream {
let mut ts = TokenStream::new();
write_global_path(&mut ts, e.span, ["core", "compile_error"]);
write_global_path(&mut ts, e.span.unwrap(), ["core", "compile_error"]);
let mut bang = Punct::new('!', Spacing::Alone);
bang.set_span(e.span);
bang.set_span(e.span.unwrap());
let mut inner = TokenStream::new();
let mut st = Literal::string(&e.text);
st.set_span(e.span);
let mut st = Literal::string(&e.to_string());
st.set_span(e.span.unwrap());
inner.extend([TokenTree::Literal(st)]);

ts.extend([
Expand Down Expand Up @@ -87,8 +87,9 @@ fn eval_mir<I: Iterator<Item = TokenTree>>(
Ok(ts) => {
if let Some(tok) = tree.peek_next() {
write_error(Error {
text: format!("Unexpected garbage after input `{}`", tok),
span: tok.span(),
expected: vec![parse::ExpectedTokenType::Eof],
got: tok.to_string(),
span: tok.span().into(),
})
} else {
ts
Expand Down
Loading

0 comments on commit 06142a5

Please sign in to comment.