Address comments in lowering + parsing.

This commit is contained in:
Mazdak Farrokhzad 2019-07-24 02:00:46 +02:00
parent 397a027aa7
commit becdba80ea
2 changed files with 17 additions and 14 deletions

View file

@ -4185,7 +4185,7 @@ impl<'a> LoweringContext<'a> {
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::disallowed(),
); );
let (pats, ddpos) = self.lower_pat_tuple(&*pats, "tuple struct"); let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
hir::PatKind::TupleStruct(qpath, pats, ddpos) hir::PatKind::TupleStruct(qpath, pats, ddpos)
} }
PatKind::Path(ref qself, ref path) => { PatKind::Path(ref qself, ref path) => {
@ -4224,7 +4224,7 @@ impl<'a> LoweringContext<'a> {
hir::PatKind::Struct(qpath, fs, etc) hir::PatKind::Struct(qpath, fs, etc)
} }
PatKind::Tuple(ref pats) => { PatKind::Tuple(ref pats) => {
let (pats, ddpos) = self.lower_pat_tuple(&*pats, "tuple"); let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple");
hir::PatKind::Tuple(pats, ddpos) hir::PatKind::Tuple(pats, ddpos)
} }
PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)), PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
@ -4245,7 +4245,7 @@ impl<'a> LoweringContext<'a> {
PatKind::Mac(_) => panic!("Shouldn't exist here"), PatKind::Mac(_) => panic!("Shouldn't exist here"),
}; };
self.pat_bound(p, node) self.pat_with_node_id_of(p, node)
} }
fn lower_pat_tuple( fn lower_pat_tuple(
@ -4291,14 +4291,14 @@ impl<'a> LoweringContext<'a> {
match pat.node { match pat.node {
PatKind::Rest => { PatKind::Rest => {
prev_rest_span = Some(pat.span); prev_rest_span = Some(pat.span);
slice = Some(self.pat_bound_wild(pat)); slice = Some(self.pat_wild_with_node_id_of(pat));
break; break;
}, },
PatKind::Ident(ref bm, ident, Some(ref sub)) if sub.is_rest() => { PatKind::Ident(ref bm, ident, Some(ref sub)) if sub.is_rest() => {
prev_rest_span = Some(sub.span); prev_rest_span = Some(sub.span);
let lower_sub = |this: &mut Self| Some(this.pat_bound_wild(sub)); let lower_sub = |this: &mut Self| Some(this.pat_wild_with_node_id_of(sub));
let node = self.lower_pat_ident(pat, bm, ident, lower_sub); let node = self.lower_pat_ident(pat, bm, ident, lower_sub);
slice = Some(self.pat_bound(pat, node)); slice = Some(self.pat_with_node_id_of(pat, node));
break; break;
}, },
_ => {} _ => {}
@ -4314,7 +4314,7 @@ impl<'a> LoweringContext<'a> {
PatKind::Rest => Some(pat.span), PatKind::Rest => Some(pat.span),
PatKind::Ident(.., Some(ref sub)) if sub.is_rest() => { PatKind::Ident(.., Some(ref sub)) if sub.is_rest() => {
// The `HirValidator` is merciless; add a `_` pattern to avoid ICEs. // The `HirValidator` is merciless; add a `_` pattern to avoid ICEs.
after.push(self.pat_bound_wild(pat)); after.push(self.pat_wild_with_node_id_of(pat));
Some(sub.span) Some(sub.span)
}, },
_ => None, _ => None,
@ -4362,12 +4362,12 @@ impl<'a> LoweringContext<'a> {
} }
} }
fn pat_bound_wild(&mut self, p: &Pat) -> P<hir::Pat> { fn pat_wild_with_node_id_of(&mut self, p: &Pat) -> P<hir::Pat> {
self.pat_bound(p, hir::PatKind::Wild) self.pat_with_node_id_of(p, hir::PatKind::Wild)
} }
/// Construct a `Pat` with the `HirId` of `p.id` lowered. /// Construct a `Pat` with the `HirId` of `p.id` lowered.
fn pat_bound(&mut self, p: &Pat, node: hir::PatKind) -> P<hir::Pat> { fn pat_with_node_id_of(&mut self, p: &Pat, node: hir::PatKind) -> P<hir::Pat> {
P(hir::Pat { P(hir::Pat {
hir_id: self.lower_node_id(p.id), hir_id: self.lower_node_id(p.id),
node, node,

View file

@ -3748,8 +3748,9 @@ impl<'a> Parser<'a> {
}) })
} }
/// Parse a parentesized comma separated sequence of patterns until `delim` is reached. /// Parse and throw away a parentesized comma separated
fn parse_recover_pat_list(&mut self) -> PResult<'a, ()> { /// sequence of patterns until `)` is reached.
fn skip_pat_list(&mut self) -> PResult<'a, ()> {
while !self.check(&token::CloseDelim(token::Paren)) { while !self.check(&token::CloseDelim(token::Paren)) {
self.parse_pat(None)?; self.parse_pat(None)?;
if !self.eat(&token::Comma) { if !self.eat(&token::Comma) {
@ -3772,7 +3773,7 @@ impl<'a> Parser<'a> {
// later. // later.
let comma_span = self.token.span; let comma_span = self.token.span;
self.bump(); self.bump();
if let Err(mut err) = self.parse_recover_pat_list() { if let Err(mut err) = self.skip_pat_list() {
// We didn't expect this to work anyway; we just wanted // We didn't expect this to work anyway; we just wanted
// to advance to the end of the comma-sequence so we know // to advance to the end of the comma-sequence so we know
// the span to suggest parenthesizing // the span to suggest parenthesizing
@ -3877,9 +3878,11 @@ impl<'a> Parser<'a> {
pat = PatKind::Ref(subpat, mutbl); pat = PatKind::Ref(subpat, mutbl);
} }
token::OpenDelim(token::Paren) => { token::OpenDelim(token::Paren) => {
// Parse `(pat, pat, pat, ...)` as tuple pattern. // Parse a tuple or parenthesis pattern.
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| p.parse_pat(None))?; let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| p.parse_pat(None))?;
// Here, `(pat,)` is a tuple pattern.
// For backward compatibility, `(..)` is a tuple pattern as well.
pat = if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) { pat = if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) {
PatKind::Paren(fields.into_iter().nth(0).unwrap()) PatKind::Paren(fields.into_iter().nth(0).unwrap())
} else { } else {