Skip to content

Commit

Permalink
Merge pull request #423 from dtolnay/trackcaller
Browse files Browse the repository at this point in the history
Track caller for Ident validation panics
  • Loading branch information
dtolnay committed Nov 26, 2023
2 parents f15383f + 690ab11 commit 4482662
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ pub(crate) struct Ident {
}

impl Ident {
#[track_caller]
pub fn new_checked(string: &str, span: Span) -> Self {
validate_ident(string);
Ident::new_unchecked(string, span)
Expand All @@ -768,6 +769,7 @@ impl Ident {
}
}

#[track_caller]
pub fn new_raw_checked(string: &str, span: Span) -> Self {
validate_ident_raw(string);
Ident::new_raw_unchecked(string, span)
Expand Down Expand Up @@ -798,6 +800,7 @@ pub(crate) fn is_ident_continue(c: char) -> bool {
unicode_ident::is_xid_continue(c)
}

#[track_caller]
fn validate_ident(string: &str) {
if string.is_empty() {
panic!("Ident is not allowed to be empty; use Option<Ident>");
Expand Down Expand Up @@ -826,6 +829,7 @@ fn validate_ident(string: &str) {
}
}

#[track_caller]
fn validate_ident_raw(string: &str) {
validate_ident(string);

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ impl Ident {
/// style="padding-right:0;">syn::parse_str</code></a><code
/// style="padding-left:0;">::&lt;Ident&gt;</code>
/// rather than `Ident::new`.
#[track_caller]
pub fn new(string: &str, span: Span) -> Self {
Ident::_new(imp::Ident::new_checked(string, span.inner))
}
Expand All @@ -959,6 +960,7 @@ impl Ident {
/// (including keywords, e.g. `fn`). Keywords which are usable in path
/// segments (e.g. `self`, `super`) are not supported, and will cause a
/// panic.
#[track_caller]
pub fn new_raw(string: &str, span: Span) -> Self {
Ident::_new(imp::Ident::new_raw_checked(string, span.inner))
}
Expand Down
2 changes: 2 additions & 0 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ pub(crate) enum Ident {
}

impl Ident {
#[track_caller]
pub fn new_checked(string: &str, span: Span) -> Self {
match span {
Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
Expand All @@ -650,6 +651,7 @@ impl Ident {
Ident::Fallback(fallback::Ident::new_unchecked(string, span))
}

#[track_caller]
pub fn new_raw_checked(string: &str, span: Span) -> Self {
match span {
Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new_raw(string, s)),
Expand Down

0 comments on commit 4482662

Please sign in to comment.