|
186 | 186 |
|
187 | 187 | pub use self::StaticFields::*; |
188 | 188 | pub use self::SubstructureFields::*; |
189 | | -use self::StructType::*; |
190 | 189 |
|
191 | 190 | use std::cell::RefCell; |
192 | 191 | use std::collections::HashSet; |
@@ -1409,11 +1408,6 @@ impl<'a> MethodDef<'a> { |
1409 | 1408 | } |
1410 | 1409 | } |
1411 | 1410 |
|
1412 | | -#[derive(PartialEq)] // dogfooding! |
1413 | | -enum StructType { |
1414 | | - Unknown, Record, Tuple |
1415 | | -} |
1416 | | - |
1417 | 1411 | // general helper methods. |
1418 | 1412 | impl<'a> TraitDef<'a> { |
1419 | 1413 | fn set_expn_info(&self, |
@@ -1441,9 +1435,9 @@ impl<'a> TraitDef<'a> { |
1441 | 1435 | let mut just_spans = Vec::new(); |
1442 | 1436 | for field in struct_def.fields(){ |
1443 | 1437 | let sp = self.set_expn_info(cx, field.span); |
1444 | | - match field.node.kind { |
1445 | | - ast::NamedField(ident, _) => named_idents.push((ident, sp)), |
1446 | | - ast::UnnamedField(..) => just_spans.push(sp), |
| 1438 | + match field.node.ident { |
| 1439 | + Some(ident) => named_idents.push((ident, sp)), |
| 1440 | + _ => just_spans.push(sp), |
1447 | 1441 | } |
1448 | 1442 | } |
1449 | 1443 |
|
@@ -1479,61 +1473,34 @@ impl<'a> TraitDef<'a> { |
1479 | 1473 | -> (P<ast::Pat>, Vec<(Span, Option<Ident>, |
1480 | 1474 | P<Expr>, |
1481 | 1475 | &'a [ast::Attribute])>) { |
1482 | | - if struct_def.fields().is_empty() { |
1483 | | - if struct_def.is_struct() { |
1484 | | - return (cx.pat_struct(self.span, struct_path, vec![]), vec![]); |
1485 | | - } else { |
1486 | | - return (cx.pat_enum(self.span, struct_path, vec![]), vec![]); |
1487 | | - } |
1488 | | - } |
1489 | | - |
1490 | 1476 | let mut paths = Vec::new(); |
1491 | | - let mut ident_expr = Vec::new(); |
1492 | | - let mut struct_type = Unknown; |
1493 | | - |
| 1477 | + let mut ident_exprs = Vec::new(); |
1494 | 1478 | for (i, struct_field) in struct_def.fields().iter().enumerate() { |
1495 | 1479 | let sp = self.set_expn_info(cx, struct_field.span); |
1496 | | - let opt_id = match struct_field.node.kind { |
1497 | | - ast::NamedField(ident, _) if (struct_type == Unknown || |
1498 | | - struct_type == Record) => { |
1499 | | - struct_type = Record; |
1500 | | - Some(ident) |
1501 | | - } |
1502 | | - ast::UnnamedField(..) if (struct_type == Unknown || |
1503 | | - struct_type == Tuple) => { |
1504 | | - struct_type = Tuple; |
1505 | | - None |
1506 | | - } |
1507 | | - _ => { |
1508 | | - cx.span_bug(sp, "a struct with named and unnamed fields in `derive`"); |
1509 | | - } |
1510 | | - }; |
1511 | 1480 | let ident = cx.ident_of(&format!("{}_{}", prefix, i)); |
1512 | 1481 | paths.push(codemap::Spanned{span: sp, node: ident}); |
1513 | 1482 | let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident))); |
1514 | 1483 | let val = cx.expr(sp, ast::ExprKind::Paren(val)); |
1515 | | - ident_expr.push((sp, opt_id, val, &struct_field.node.attrs[..])); |
| 1484 | + ident_exprs.push((sp, struct_field.node.ident, val, &struct_field.node.attrs[..])); |
1516 | 1485 | } |
1517 | 1486 |
|
1518 | 1487 | let subpats = self.create_subpatterns(cx, paths, mutbl); |
1519 | | - |
1520 | | - // struct_type is definitely not Unknown, since struct_def.fields |
1521 | | - // must be nonempty to reach here |
1522 | 1488 | let pattern = if struct_def.is_struct() { |
1523 | | - let field_pats = subpats.into_iter().zip(&ident_expr) |
1524 | | - .map(|(pat, &(_, id, _, _))| { |
1525 | | - // id is guaranteed to be Some |
| 1489 | + let field_pats = subpats.into_iter().zip(&ident_exprs).map(|(pat, &(sp, ident, _, _))| { |
| 1490 | + if ident.is_none() { |
| 1491 | + cx.span_bug(sp, "a braced struct with unnamed fields in `derive`"); |
| 1492 | + } |
1526 | 1493 | codemap::Spanned { |
1527 | 1494 | span: pat.span, |
1528 | | - node: ast::FieldPat { ident: id.unwrap(), pat: pat, is_shorthand: false }, |
| 1495 | + node: ast::FieldPat { ident: ident.unwrap(), pat: pat, is_shorthand: false }, |
1529 | 1496 | } |
1530 | 1497 | }).collect(); |
1531 | 1498 | cx.pat_struct(self.span, struct_path, field_pats) |
1532 | 1499 | } else { |
1533 | 1500 | cx.pat_enum(self.span, struct_path, subpats) |
1534 | 1501 | }; |
1535 | 1502 |
|
1536 | | - (pattern, ident_expr) |
| 1503 | + (pattern, ident_exprs) |
1537 | 1504 | } |
1538 | 1505 |
|
1539 | 1506 | fn create_enum_variant_pattern(&self, |
|
0 commit comments