Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/discouraged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub trait Speculative {
impl<'a> Speculative for ParseBuffer<'a> {
fn advance_to(&self, fork: &Self) {
if !crate::buffer::same_scope(self.cursor(), fork.cursor()) {
panic!("fork was not derived from the advancing parse stream");
crate::panic_with_location!("fork was not derived from the advancing parse stream");
}

let (self_unexp, self_sp) = inner_unexpected(self);
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,25 @@ pub fn parse_file(mut content: &str) -> Result<File> {
file.shebang = shebang;
Ok(file)
}

/// `panic!` macro that includes location information [file:line:column]
/// when inside of a proc-macro, because otherwise you don't get this information without
/// -Zmacro-backtrace, and enabling it is an extra step which makes debugging proc macros harder
macro_rules! panic_with_location {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you called this panic! instead (shorter), it would not compile on the msrv (1.68) because it would be ambiguous with a macro of the same name in the prelude. Later stable versions of Rust do not have this problem (e.g. 1.88)

($message:literal $($tt:tt)*) => {{
#[cfg(feature = "proc-macro")]
let is_available = proc_macro::is_available();
#[cfg(not(feature = "proc-macro"))]
let is_available = false;
if is_available {
let location = core::panic::Location::caller();
let file = location.file();
let line = location.line();
let column = location.column();
::core::panic!(concat!("[{}:{}:{}] ", $message), file, line, column $($tt)*)
} else {
::core::panic!($message $($tt)*)
}
}};
}
pub(crate) use panic_with_location;
6 changes: 3 additions & 3 deletions src/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ impl Lifetime {
/// ```
pub fn new(symbol: &str, span: Span) -> Self {
if !symbol.starts_with('\'') {
panic!(
crate::panic_with_location!(
"lifetime name must start with apostrophe as in \"'a\", got {:?}",
symbol
);
}

if symbol == "'" {
panic!("lifetime name must not be empty");
crate::panic_with_location!("lifetime name must not be empty");
}

if !crate::ident::xid_ok(&symbol[1..]) {
panic!("{:?} is not a valid lifetime name", symbol);
crate::panic_with_location!("{:?} is not a valid lifetime name", symbol);
}

Lifetime {
Expand Down
8 changes: 4 additions & 4 deletions src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl LitInt {
pub fn new(repr: &str, span: Span) -> Self {
let (digits, suffix) = match value::parse_lit_int(repr) {
Some(parse) => parse,
None => panic!("not an integer literal: `{}`", repr),
None => crate::panic_with_location!("not an integer literal: `{}`", repr),
};

let mut token: Literal = repr.parse().unwrap();
Expand Down Expand Up @@ -504,7 +504,7 @@ impl From<Literal> for LitInt {
}),
}
} else {
panic!("not an integer literal: `{}`", repr);
crate::panic_with_location!("not an integer literal: `{}`", repr);
}
}
}
Expand All @@ -520,7 +520,7 @@ impl LitFloat {
pub fn new(repr: &str, span: Span) -> Self {
let (digits, suffix) = match value::parse_lit_float(repr) {
Some(parse) => parse,
None => panic!("not a float literal: `{}`", repr),
None => crate::panic_with_location!("not a float literal: `{}`", repr),
};

let mut token: Literal = repr.parse().unwrap();
Expand Down Expand Up @@ -578,7 +578,7 @@ impl From<Literal> for LitFloat {
}),
}
} else {
panic!("not a float literal: `{}`", repr);
crate::panic_with_location!("not a float literal: `{}`", repr);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parse_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn parse<T: ParseQuote>(token_stream: TokenStream) -> T {
let parser = T::parse;
match parser.parse2(token_stream) {
Ok(t) => t,
Err(err) => panic!("{}", err),
Err(err) => crate::panic_with_location!("{}", err),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ where
let mut nomore = false;
for pair in i {
if nomore {
panic!("punctuated extended with items after a Pair::End");
crate::panic_with_location!("punctuated extended with items after a Pair::End");
}
match pair {
Pair::Punctuated(a, b) => punctuated.inner.push((a, b)),
Expand Down
2 changes: 1 addition & 1 deletion src/verbatim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn between<'a>(begin: ParseStream<'a>, end: ParseStream<'a>) -> Token
cursor = inside;
continue;
} else {
panic!("verbatim end must not be inside a delimited group");
crate::panic_with_location!("verbatim end must not be inside a delimited group");
}
}

Expand Down