Skip to content

Commit 634ebd5

Browse files
committed
Adjust lowering of Tuple/TupleStruct patterns.
- Make sure we ban duplicate '..'. - Avoid ICEs on PatKind::Rest that doesn't generate HIR nodes.
1 parent 4dcc713 commit 634ebd5

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

src/librustc/hir/lowering.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use std::mem;
5858
use smallvec::SmallVec;
5959
use syntax::attr;
6060
use syntax::ast;
61+
use syntax::ptr::P as AstP;
6162
use syntax::ast::*;
6263
use syntax::errors;
6364
use syntax::ext::hygiene::ExpnId;
@@ -468,7 +469,7 @@ impl<'a> LoweringContext<'a> {
468469
fn visit_pat(&mut self, p: &'tcx Pat) {
469470
match p.node {
470471
// Doesn't generate a HIR node
471-
PatKind::Paren(..) => {},
472+
PatKind::Paren(..) | PatKind::Rest => {},
472473
_ => {
473474
if let Some(owner) = self.hir_id_owner {
474475
self.lctx.lower_node_id_with_owner(p.id, owner);
@@ -4199,19 +4200,16 @@ impl<'a> LoweringContext<'a> {
41994200
}
42004201
}
42014202
PatKind::Lit(ref e) => hir::PatKind::Lit(P(self.lower_expr(e))),
4202-
PatKind::TupleStruct(ref path, ref pats, ddpos) => {
4203+
PatKind::TupleStruct(ref path, ref pats) => {
42034204
let qpath = self.lower_qpath(
42044205
p.id,
42054206
&None,
42064207
path,
42074208
ParamMode::Optional,
42084209
ImplTraitContext::disallowed(),
42094210
);
4210-
hir::PatKind::TupleStruct(
4211-
qpath,
4212-
pats.iter().map(|x| self.lower_pat(x)).collect(),
4213-
ddpos,
4214-
)
4211+
let (pats, ddpos) = self.lower_pat_tuple(&*pats, "tuple struct");
4212+
hir::PatKind::TupleStruct(qpath, pats, ddpos)
42154213
}
42164214
PatKind::Path(ref qself, ref path) => {
42174215
let qpath = self.lower_qpath(
@@ -4248,8 +4246,9 @@ impl<'a> LoweringContext<'a> {
42484246
.collect();
42494247
hir::PatKind::Struct(qpath, fs, etc)
42504248
}
4251-
PatKind::Tuple(ref elts, ddpos) => {
4252-
hir::PatKind::Tuple(elts.iter().map(|x| self.lower_pat(x)).collect(), ddpos)
4249+
PatKind::Tuple(ref pats) => {
4250+
let (pats, ddpos) = self.lower_pat_tuple(&*pats, "tuple");
4251+
hir::PatKind::Tuple(pats, ddpos)
42534252
}
42544253
PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
42554254
PatKind::Ref(ref inner, mutbl) => {
@@ -4280,6 +4279,46 @@ impl<'a> LoweringContext<'a> {
42804279
})
42814280
}
42824281

4282+
fn lower_pat_tuple(
4283+
&mut self,
4284+
pats: &[AstP<Pat>],
4285+
ctx: &str,
4286+
) -> (HirVec<P<hir::Pat>>, Option<usize>) {
4287+
let mut elems = Vec::with_capacity(pats.len());
4288+
let mut rest = None;
4289+
4290+
let mut iter = pats.iter().enumerate();
4291+
while let Some((idx, pat)) = iter.next() {
4292+
// Interpret the first `..` pattern as a subtuple pattern.
4293+
if pat.is_rest() {
4294+
rest = Some((idx, pat.span));
4295+
break;
4296+
}
4297+
// It was not a subslice pattern so lower it normally.
4298+
elems.push(self.lower_pat(pat));
4299+
}
4300+
4301+
while let Some((_, pat)) = iter.next() {
4302+
// There was a previous subtuple pattern; make sure we don't allow more.
4303+
if pat.is_rest() {
4304+
self.ban_extra_rest_pat(pat.span, rest.unwrap().1, ctx);
4305+
} else {
4306+
elems.push(self.lower_pat(pat));
4307+
}
4308+
}
4309+
4310+
(elems.into(), rest.map(|(ddpos, _)| ddpos))
4311+
}
4312+
4313+
/// Emit a friendly error for extra `..` patterns in a tuple/tuple struct/slice pattern.
4314+
fn ban_extra_rest_pat(&self, sp: Span, prev_sp: Span, ctx: &str) {
4315+
self.diagnostic()
4316+
.struct_span_err(sp, &format!("`..` can only be used once per {} pattern", ctx))
4317+
.span_label(sp, &format!("can only be used once per {} pattern", ctx))
4318+
.span_label(prev_sp, "previously used here")
4319+
.emit();
4320+
}
4321+
42834322
/// Used to ban the `..` pattern in places it shouldn't be semantically.
42844323
fn ban_illegal_rest_pat(&self, sp: Span) -> hir::PatKind {
42854324
self.diagnostic()

0 commit comments

Comments
 (0)