Skip to content

Commit

Permalink
Merge pull request #485 from dtolnay/fallbackident
Browse files Browse the repository at this point in the history
Make parser's fallback Ident symmetric with Group and Literal
  • Loading branch information
dtolnay authored Nov 21, 2024
2 parents 56c3e31 + 75d0818 commit 1ce5f04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,13 @@ impl Ident {
}
}

fn _new_fallback(inner: fallback::Ident) -> Self {
Ident {
inner: imp::Ident::from(inner),
_marker: MARKER,
}
}

/// Creates a new `Ident` with the given `string` as well as the specified
/// `span`.
///
Expand Down
16 changes: 6 additions & 10 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::fallback::{
self, is_ident_continue, is_ident_start, Group, LexError, Literal, Span, TokenStream,
self, is_ident_continue, is_ident_start, Group, Ident, LexError, Literal, Span, TokenStream,
TokenStreamBuilder,
};
use crate::{Delimiter, Punct, Spacing, TokenTree};
Expand Down Expand Up @@ -300,10 +300,8 @@ fn ident_any(input: Cursor) -> PResult<crate::Ident> {
let (rest, sym) = ident_not_raw(rest)?;

if !raw {
let ident = crate::Ident::_new(crate::imp::Ident::new_unchecked(
sym,
fallback::Span::call_site(),
));
let ident =
crate::Ident::_new_fallback(Ident::new_unchecked(sym, fallback::Span::call_site()));
return Ok((rest, ident));
}

Expand All @@ -312,10 +310,8 @@ fn ident_any(input: Cursor) -> PResult<crate::Ident> {
_ => {}
}

let ident = crate::Ident::_new(crate::imp::Ident::new_raw_unchecked(
sym,
fallback::Span::call_site(),
));
let ident =
crate::Ident::_new_fallback(Ident::new_raw_unchecked(sym, fallback::Span::call_site()));
Ok((rest, ident))
}

Expand Down Expand Up @@ -941,7 +937,7 @@ fn doc_comment<'a>(input: Cursor<'a>, trees: &mut TokenStreamBuilder) -> PResult
trees.push_token_from_parser(TokenTree::Punct(bang));
}

let doc_ident = crate::Ident::_new(crate::imp::Ident::new_unchecked("doc", fallback_span));
let doc_ident = crate::Ident::_new_fallback(Ident::new_unchecked("doc", fallback_span));
let mut equal = Punct::new('=', Spacing::Alone);
equal.set_span(span);
let mut literal = crate::Literal::_new_fallback(Literal::string(comment));
Expand Down
14 changes: 6 additions & 8 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,6 @@ impl Ident {
}
}

pub(crate) fn new_unchecked(string: &str, span: fallback::Span) -> Self {
Ident::Fallback(fallback::Ident::new_unchecked(string, span))
}

#[track_caller]
pub(crate) fn new_raw_checked(string: &str, span: Span) -> Self {
match span {
Expand All @@ -705,10 +701,6 @@ impl Ident {
}
}

pub(crate) fn new_raw_unchecked(string: &str, span: fallback::Span) -> Self {
Ident::Fallback(fallback::Ident::new_raw_unchecked(string, span))
}

pub(crate) fn span(&self) -> Span {
match self {
Ident::Compiler(t) => Span::Compiler(t.span()),
Expand All @@ -733,6 +725,12 @@ impl Ident {
}
}

impl From<fallback::Ident> for Ident {
fn from(inner: fallback::Ident) -> Self {
Ident::Fallback(inner)
}
}

impl PartialEq for Ident {
fn eq(&self, other: &Ident) -> bool {
match (self, other) {
Expand Down

0 comments on commit 1ce5f04

Please sign in to comment.