Skip to content

Commit

Permalink
Rollup merge of rust-lang#47247 - estebank:suggest-cast, r=petrochenkov
Browse files Browse the repository at this point in the history
Suggest casting on numeric type error

Re rust-lang#47168.
  • Loading branch information
GuillaumeGomez committed Jan 21, 2018
2 parents 97520cc + 71c0873 commit 6bb1b0d
Show file tree
Hide file tree
Showing 10 changed files with 1,717 additions and 114 deletions.
64 changes: 63 additions & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ use util::nodemap::{NodeMap, FxHashSet};
use syntax_pos::{Span, DUMMY_SP};
use syntax::codemap::{self, Spanned};
use syntax::abi::Abi;
use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
use syntax::ast::{self, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
use syntax::ext::hygiene::SyntaxContext;
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::tokenstream::TokenStream;
use syntax::util::ThinVec;
use syntax::util::parser::ExprPrecedence;
use ty::AdtKind;

use rustc_data_structures::indexed_vec;
Expand Down Expand Up @@ -958,6 +959,31 @@ impl BinOp_ {
}
}

impl Into<ast::BinOpKind> for BinOp_ {
fn into(self) -> ast::BinOpKind {
match self {
BiAdd => ast::BinOpKind::Add,
BiSub => ast::BinOpKind::Sub,
BiMul => ast::BinOpKind::Mul,
BiDiv => ast::BinOpKind::Div,
BiRem => ast::BinOpKind::Rem,
BiAnd => ast::BinOpKind::And,
BiOr => ast::BinOpKind::Or,
BiBitXor => ast::BinOpKind::BitXor,
BiBitAnd => ast::BinOpKind::BitAnd,
BiBitOr => ast::BinOpKind::BitOr,
BiShl => ast::BinOpKind::Shl,
BiShr => ast::BinOpKind::Shr,
BiEq => ast::BinOpKind::Eq,
BiLt => ast::BinOpKind::Lt,
BiLe => ast::BinOpKind::Le,
BiNe => ast::BinOpKind::Ne,
BiGe => ast::BinOpKind::Ge,
BiGt => ast::BinOpKind::Gt,
}
}
}

pub type BinOp = Spanned<BinOp_>;

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
Expand Down Expand Up @@ -1166,6 +1192,42 @@ pub struct Expr {
pub hir_id: HirId,
}

impl Expr {
pub fn precedence(&self) -> ExprPrecedence {
match self.node {
ExprBox(_) => ExprPrecedence::Box,
ExprArray(_) => ExprPrecedence::Array,
ExprCall(..) => ExprPrecedence::Call,
ExprMethodCall(..) => ExprPrecedence::MethodCall,
ExprTup(_) => ExprPrecedence::Tup,
ExprBinary(op, ..) => ExprPrecedence::Binary(op.node.into()),
ExprUnary(..) => ExprPrecedence::Unary,
ExprLit(_) => ExprPrecedence::Lit,
ExprType(..) | ExprCast(..) => ExprPrecedence::Cast,
ExprIf(..) => ExprPrecedence::If,
ExprWhile(..) => ExprPrecedence::While,
ExprLoop(..) => ExprPrecedence::Loop,
ExprMatch(..) => ExprPrecedence::Match,
ExprClosure(..) => ExprPrecedence::Closure,
ExprBlock(..) => ExprPrecedence::Block,
ExprAssign(..) => ExprPrecedence::Assign,
ExprAssignOp(..) => ExprPrecedence::AssignOp,
ExprField(..) => ExprPrecedence::Field,
ExprTupField(..) => ExprPrecedence::TupField,
ExprIndex(..) => ExprPrecedence::Index,
ExprPath(..) => ExprPrecedence::Path,
ExprAddrOf(..) => ExprPrecedence::AddrOf,
ExprBreak(..) => ExprPrecedence::Break,
ExprAgain(..) => ExprPrecedence::Continue,
ExprRet(..) => ExprPrecedence::Ret,
ExprInlineAsm(..) => ExprPrecedence::InlineAsm,
ExprStruct(..) => ExprPrecedence::Struct,
ExprRepeat(..) => ExprPrecedence::Repeat,
ExprYield(..) => ExprPrecedence::Yield,
}
}
}

impl fmt::Debug for Expr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "expr({}: {})", self.id,
Expand Down
51 changes: 1 addition & 50 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ impl<'a> State<'a> {
}

pub fn print_expr_maybe_paren(&mut self, expr: &hir::Expr, prec: i8) -> io::Result<()> {
let needs_par = expr_precedence(expr) < prec;
let needs_par = expr.precedence().order() < prec;
if needs_par {
self.popen()?;
}
Expand Down Expand Up @@ -2318,55 +2318,6 @@ fn stmt_ends_with_semi(stmt: &hir::Stmt_) -> bool {
}
}


fn expr_precedence(expr: &hir::Expr) -> i8 {
use syntax::util::parser::*;

match expr.node {
hir::ExprClosure(..) => PREC_CLOSURE,

hir::ExprBreak(..) |
hir::ExprAgain(..) |
hir::ExprRet(..) |
hir::ExprYield(..) => PREC_JUMP,

// Binop-like expr kinds, handled by `AssocOp`.
hir::ExprBinary(op, _, _) => bin_op_to_assoc_op(op.node).precedence() as i8,

hir::ExprCast(..) => AssocOp::As.precedence() as i8,
hir::ExprType(..) => AssocOp::Colon.precedence() as i8,

hir::ExprAssign(..) |
hir::ExprAssignOp(..) => AssocOp::Assign.precedence() as i8,

// Unary, prefix
hir::ExprBox(..) |
hir::ExprAddrOf(..) |
hir::ExprUnary(..) => PREC_PREFIX,

// Unary, postfix
hir::ExprCall(..) |
hir::ExprMethodCall(..) |
hir::ExprField(..) |
hir::ExprTupField(..) |
hir::ExprIndex(..) |
hir::ExprInlineAsm(..) => PREC_POSTFIX,

// Never need parens
hir::ExprArray(..) |
hir::ExprRepeat(..) |
hir::ExprTup(..) |
hir::ExprLit(..) |
hir::ExprPath(..) |
hir::ExprIf(..) |
hir::ExprWhile(..) |
hir::ExprLoop(..) |
hir::ExprMatch(..) |
hir::ExprBlock(..) |
hir::ExprStruct(..) => PREC_PAREN,
}
}

fn bin_op_to_assoc_op(op: hir::BinOp_) -> AssocOp {
use hir::BinOp_::*;
match op {
Expand Down
Loading

0 comments on commit 6bb1b0d

Please sign in to comment.