mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Merge #6813
6813: negative sign matching in mbe matching for literal r=edwin0cheng a=edwin0cheng Fix #6292 r @jonas-schievink Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
commit
4998807039
2 changed files with 39 additions and 6 deletions
|
@ -356,6 +356,18 @@ impl<'a> TtIter<'a> {
|
|||
ExpandResult { value: _, err: Some(_) } => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn eat_char(&mut self, c: char) -> Option<tt::TokenTree> {
|
||||
let mut fork = self.clone();
|
||||
match fork.expect_char(c) {
|
||||
Ok(_) => {
|
||||
let tt = self.next().cloned();
|
||||
*self = fork;
|
||||
tt
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn match_repeat(
|
||||
|
@ -447,10 +459,22 @@ fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult<Option<Fragmen
|
|||
.expect_lifetime()
|
||||
.map(|tt| Some(tt))
|
||||
.map_err(|()| err!("expected lifetime")),
|
||||
"literal" => input
|
||||
.expect_literal()
|
||||
.map(|literal| Some(tt::Leaf::from(literal.clone()).into()))
|
||||
.map_err(|()| err!()),
|
||||
"literal" => {
|
||||
let neg = input.eat_char('-');
|
||||
input
|
||||
.expect_literal()
|
||||
.map(|literal| {
|
||||
let lit = tt::Leaf::from(literal.clone());
|
||||
match neg {
|
||||
None => Some(lit.into()),
|
||||
Some(neg) => Some(tt::TokenTree::Subtree(tt::Subtree {
|
||||
delimiter: None,
|
||||
token_trees: vec![neg, lit.into()],
|
||||
})),
|
||||
}
|
||||
})
|
||||
.map_err(|()| err!())
|
||||
}
|
||||
// `vis` is optional
|
||||
"vis" => match input.eat_vis() {
|
||||
Some(vis) => Ok(Some(vis)),
|
||||
|
|
|
@ -1008,11 +1008,20 @@ fn test_literal() {
|
|||
parse_macro(
|
||||
r#"
|
||||
macro_rules! foo {
|
||||
($ type:ty $ lit:literal) => { const VALUE: $ type = $ lit;};
|
||||
($ type:ty , $ lit:literal) => { const VALUE: $ type = $ lit;};
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.assert_expand_items(r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#);
|
||||
.assert_expand_items(r#"foo!(u8,0);"#, r#"const VALUE : u8 = 0 ;"#);
|
||||
|
||||
parse_macro(
|
||||
r#"
|
||||
macro_rules! foo {
|
||||
($ type:ty , $ lit:literal) => { const VALUE: $ type = $ lit;};
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.assert_expand_items(r#"foo!(i32,-1);"#, r#"const VALUE : i32 = - 1 ;"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue