fix: Fix macro transcriber emitting incorrect lifetime tokens

This commit is contained in:
Lukas Wirth 2024-02-11 12:10:38 +01:00
parent ddf105b646
commit c990587593
4 changed files with 73 additions and 6 deletions

View File

@ -1090,3 +1090,57 @@ fn main() {
"#]], "#]],
); );
} }
#[test]
fn regression_16529() {
check(
r#"
mod any {
#[macro_export]
macro_rules! nameable {
{
struct $name:ident[$a:lifetime]
} => {
$crate::any::nameable! {
struct $name[$a]
a
}
};
{
struct $name:ident[$a:lifetime]
a
} => {};
}
pub use nameable;
nameable! {
Name['a]
}
}
"#,
expect![[r#"
mod any {
#[macro_export]
macro_rules! nameable {
{
struct $name:ident[$a:lifetime]
} => {
$crate::any::nameable! {
struct $name[$a]
a
}
};
{
struct $name:ident[$a:lifetime]
a
} => {};
}
pub use nameable;
/* error: unexpected token in input */$crate::any::nameable! {
struct $name[$a]a
}
}
"#]],
);
}

View File

@ -101,10 +101,20 @@ impl<S: Span> Bindings<S> {
}))) })))
} }
MetaVarKind::Lifetime => { MetaVarKind::Lifetime => {
Fragment::Tokens(tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident { Fragment::Tokens(tt::TokenTree::Subtree(tt::Subtree {
text: SmolStr::new_static("'missing"), delimiter: tt::Delimiter::invisible_spanned(span),
span, token_trees: Box::new([
}))) tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
char: '\'',
span,
spacing: tt::Spacing::Joint,
})),
tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident {
text: SmolStr::new_static("missing"),
span,
})),
]),
}))
} }
MetaVarKind::Literal => { MetaVarKind::Literal => {
Fragment::Tokens(tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident { Fragment::Tokens(tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident {

View File

@ -700,10 +700,12 @@ impl<S> SynToken<S> {
} }
impl<SpanMap, S: std::fmt::Debug> SrcToken<Converter<SpanMap, S>, S> for SynToken<S> { impl<SpanMap, S: std::fmt::Debug> SrcToken<Converter<SpanMap, S>, S> for SynToken<S> {
fn kind(&self, ctx: &Converter<SpanMap, S>) -> SyntaxKind { fn kind(&self, _ctx: &Converter<SpanMap, S>) -> SyntaxKind {
match self { match self {
SynToken::Ordinary(token) => token.kind(), SynToken::Ordinary(token) => token.kind(),
SynToken::Punct { .. } => SyntaxKind::from_char(self.to_char(ctx).unwrap()).unwrap(), SynToken::Punct { token, offset: i } => {
SyntaxKind::from_char(token.text().chars().nth(*i).unwrap()).unwrap()
}
SynToken::Leaf(_) => { SynToken::Leaf(_) => {
never!(); never!();
SyntaxKind::ERROR SyntaxKind::ERROR

View File

@ -152,6 +152,7 @@ pub struct Punct<S> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Spacing { pub enum Spacing {
Alone, Alone,
/// Whether the following token is joint to the current one.
Joint, Joint,
} }