mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Fix and test edge cases of _
as ident
This commit is contained in:
parent
0a0e22235b
commit
0a7f28620a
3 changed files with 13 additions and 3 deletions
|
@ -710,7 +710,6 @@ fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult<Option<Fragmen
|
||||||
let tt_result = match kind {
|
let tt_result = match kind {
|
||||||
"ident" => input
|
"ident" => input
|
||||||
.expect_ident()
|
.expect_ident()
|
||||||
.and_then(|ident| if ident.text == "_" { Err(()) } else { Ok(ident) })
|
|
||||||
.map(|ident| Some(tt::Leaf::from(ident.clone()).into()))
|
.map(|ident| Some(tt::Leaf::from(ident.clone()).into()))
|
||||||
.map_err(|()| err!("expected ident")),
|
.map_err(|()| err!("expected ident")),
|
||||||
"tt" => input.expect_tt().map(Some).map_err(|()| err!()),
|
"tt" => input.expect_tt().map(Some).map_err(|()| err!()),
|
||||||
|
@ -763,7 +762,7 @@ impl<'a> TtIter<'a> {
|
||||||
fn expect_separator(&mut self, separator: &Separator, idx: usize) -> bool {
|
fn expect_separator(&mut self, separator: &Separator, idx: usize) -> bool {
|
||||||
let mut fork = self.clone();
|
let mut fork = self.clone();
|
||||||
let ok = match separator {
|
let ok = match separator {
|
||||||
Separator::Ident(lhs) if idx == 0 => match fork.expect_ident() {
|
Separator::Ident(lhs) if idx == 0 => match fork.expect_ident_or_underscore() {
|
||||||
Ok(rhs) => rhs.text == lhs.text,
|
Ok(rhs) => rhs.text == lhs.text,
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
|
@ -853,7 +852,7 @@ impl<'a> TtIter<'a> {
|
||||||
if punct.char != '\'' {
|
if punct.char != '\'' {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
let ident = self.expect_ident()?;
|
let ident = self.expect_ident_or_underscore()?;
|
||||||
|
|
||||||
Ok(tt::Subtree {
|
Ok(tt::Subtree {
|
||||||
delimiter: None,
|
delimiter: None,
|
||||||
|
|
|
@ -12,6 +12,9 @@ fn test_valid_arms() {
|
||||||
}
|
}
|
||||||
|
|
||||||
check("($i:ident) => ()");
|
check("($i:ident) => ()");
|
||||||
|
check("($(x),*) => ()");
|
||||||
|
check("($(x)_*) => ()");
|
||||||
|
check("($(x)i*) => ()");
|
||||||
check("($($i:ident)*) => ($_)");
|
check("($($i:ident)*) => ($_)");
|
||||||
check("($($true:ident)*) => ($true)");
|
check("($($true:ident)*) => ($true)");
|
||||||
check("($($false:ident)*) => ($false)");
|
check("($($false:ident)*) => ($false)");
|
||||||
|
@ -32,6 +35,7 @@ fn test_invalid_arms() {
|
||||||
|
|
||||||
check("($i) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
|
check("($i) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
|
||||||
check("($i:) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
|
check("($i:) => ($i)", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
|
||||||
|
check("($i:_) => ()", ParseError::UnexpectedToken("bad fragment specifier 1".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> {
|
fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError> {
|
||||||
|
|
|
@ -49,6 +49,13 @@ impl<'a> TtIter<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expect_ident(&mut self) -> Result<&'a tt::Ident, ()> {
|
pub(crate) fn expect_ident(&mut self) -> Result<&'a tt::Ident, ()> {
|
||||||
|
match self.expect_leaf()? {
|
||||||
|
tt::Leaf::Ident(it) if it.text != "_" => Ok(it),
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn expect_ident_or_underscore(&mut self) -> Result<&'a tt::Ident, ()> {
|
||||||
match self.expect_leaf()? {
|
match self.expect_leaf()? {
|
||||||
tt::Leaf::Ident(it) => Ok(it),
|
tt::Leaf::Ident(it) => Ok(it),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue