Add ExprType to HIR and make everything compile
+ Apply parser changes manually + Add feature gate
This commit is contained in:
@@ -233,6 +233,9 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status
|
||||
|
||||
// Allows `#[deprecated]` attribute
|
||||
("deprecated", "1.6.0", Some(29935), Active),
|
||||
|
||||
// allow using type ascription in expressions
|
||||
("type_ascription", "1.6.0", Some(23416), Active),
|
||||
];
|
||||
// (changing above list without updating src/doc/reference.md makes @cmr sad)
|
||||
|
||||
@@ -960,6 +963,10 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
|
||||
"box expression syntax is experimental; \
|
||||
you can call `Box::new` instead.");
|
||||
}
|
||||
ast::ExprType(..) => {
|
||||
self.gate_feature("type_ascription", e.span,
|
||||
"type ascription is experimental");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
visit::walk_expr(self, e);
|
||||
|
||||
@@ -2789,6 +2789,10 @@ impl<'a> Parser<'a> {
|
||||
lhs = self.mk_expr(lhs.span.lo, rhs.span.hi,
|
||||
ExprCast(lhs, rhs), None);
|
||||
continue
|
||||
} else if op == AssocOp::Colon {
|
||||
let rhs = try!(self.parse_ty());
|
||||
lhs = self.mk_expr(lhs.span.lo, rhs.span.hi, ExprType(lhs, rhs));
|
||||
continue
|
||||
} else if op == AssocOp::DotDot {
|
||||
// If we didn’t have to handle `x..`, it would be pretty easy to generalise
|
||||
// it to the Fixity::None code.
|
||||
@@ -2857,7 +2861,9 @@ impl<'a> Parser<'a> {
|
||||
let aopexpr = self.mk_assign_op(codemap::respan(cur_op_span, aop), lhs, rhs);
|
||||
self.mk_expr(lhs_span.lo, rhs_span.hi, aopexpr, None)
|
||||
}
|
||||
AssocOp::As | AssocOp::DotDot => self.bug("As or DotDot branch reached")
|
||||
AssocOp::As | AssocOp::Colon | AssocOp::DotDot => {
|
||||
self.bug("As, Colon or DotDot branch reached")
|
||||
}
|
||||
};
|
||||
|
||||
if op.fixity() == Fixity::None { break }
|
||||
|
||||
@@ -60,7 +60,9 @@ pub enum AssocOp {
|
||||
/// `as`
|
||||
As,
|
||||
/// `..` range
|
||||
DotDot
|
||||
DotDot,
|
||||
/// `:`
|
||||
Colon,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
@@ -100,6 +102,7 @@ impl AssocOp {
|
||||
Token::AndAnd => Some(LAnd),
|
||||
Token::OrOr => Some(LOr),
|
||||
Token::DotDot => Some(DotDot),
|
||||
Token::Colon => Some(Colon),
|
||||
_ if t.is_keyword(keywords::As) => Some(As),
|
||||
_ => None
|
||||
}
|
||||
@@ -134,7 +137,7 @@ impl AssocOp {
|
||||
pub fn precedence(&self) -> usize {
|
||||
use self::AssocOp::*;
|
||||
match *self {
|
||||
As => 14,
|
||||
As | Colon => 14,
|
||||
Multiply | Divide | Modulus => 13,
|
||||
Add | Subtract => 12,
|
||||
ShiftLeft | ShiftRight => 11,
|
||||
@@ -158,7 +161,7 @@ impl AssocOp {
|
||||
Inplace | Assign | AssignOp(_) => Fixity::Right,
|
||||
As | Multiply | Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd |
|
||||
BitXor | BitOr | Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual |
|
||||
LAnd | LOr => Fixity::Left,
|
||||
LAnd | LOr | Colon => Fixity::Left,
|
||||
DotDot => Fixity::None
|
||||
}
|
||||
}
|
||||
@@ -168,7 +171,7 @@ impl AssocOp {
|
||||
match *self {
|
||||
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => true,
|
||||
Inplace | Assign | AssignOp(_) | As | Multiply | Divide | Modulus | Add | Subtract |
|
||||
ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd | LOr | DotDot => false
|
||||
ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd | LOr | DotDot | Colon => false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,7 +181,7 @@ impl AssocOp {
|
||||
Assign | AssignOp(_) | Inplace => true,
|
||||
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual | As | Multiply | Divide |
|
||||
Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd |
|
||||
LOr | DotDot => false
|
||||
LOr | DotDot | Colon => false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,8 +206,7 @@ impl AssocOp {
|
||||
BitOr => Some(ast::BiBitOr),
|
||||
LAnd => Some(ast::BiAnd),
|
||||
LOr => Some(ast::BiOr),
|
||||
Inplace | Assign | AssignOp(_) | As | DotDot => None
|
||||
Inplace | Assign | AssignOp(_) | As | DotDot | Colon => None
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -693,14 +693,10 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
|
||||
visitor.visit_expr(subexpression)
|
||||
}
|
||||
ExprLit(_) => {}
|
||||
ExprCast(ref subexpression, ref typ) => {
|
||||
ExprCast(ref subexpression, ref typ) | ExprType(ref subexpression, ref typ) => {
|
||||
visitor.visit_expr(subexpression);
|
||||
visitor.visit_ty(typ)
|
||||
}
|
||||
ExprType(ref subexpression, ref typ) => {
|
||||
visitor.visit_expr(&**subexpression);
|
||||
visitor.visit_ty(&**typ)
|
||||
}
|
||||
ExprIf(ref head_expression, ref if_block, ref optional_else) => {
|
||||
visitor.visit_expr(head_expression);
|
||||
visitor.visit_block(if_block);
|
||||
|
||||
Reference in New Issue
Block a user