Skip to content

Commit 6bb1b0d

Browse files
Rollup merge of #47247 - estebank:suggest-cast, r=petrochenkov
Suggest casting on numeric type error Re #47168.
2 parents 97520cc + 71c0873 commit 6bb1b0d

File tree

10 files changed

+1717
-114
lines changed

10 files changed

+1717
-114
lines changed

src/librustc/hir/mod.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ use util::nodemap::{NodeMap, FxHashSet};
3434
use syntax_pos::{Span, DUMMY_SP};
3535
use syntax::codemap::{self, Spanned};
3636
use syntax::abi::Abi;
37-
use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
37+
use syntax::ast::{self, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
3838
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
3939
use syntax::ext::hygiene::SyntaxContext;
4040
use syntax::ptr::P;
4141
use syntax::symbol::{Symbol, keywords};
4242
use syntax::tokenstream::TokenStream;
4343
use syntax::util::ThinVec;
44+
use syntax::util::parser::ExprPrecedence;
4445
use ty::AdtKind;
4546

4647
use rustc_data_structures::indexed_vec;
@@ -958,6 +959,31 @@ impl BinOp_ {
958959
}
959960
}
960961

962+
impl Into<ast::BinOpKind> for BinOp_ {
963+
fn into(self) -> ast::BinOpKind {
964+
match self {
965+
BiAdd => ast::BinOpKind::Add,
966+
BiSub => ast::BinOpKind::Sub,
967+
BiMul => ast::BinOpKind::Mul,
968+
BiDiv => ast::BinOpKind::Div,
969+
BiRem => ast::BinOpKind::Rem,
970+
BiAnd => ast::BinOpKind::And,
971+
BiOr => ast::BinOpKind::Or,
972+
BiBitXor => ast::BinOpKind::BitXor,
973+
BiBitAnd => ast::BinOpKind::BitAnd,
974+
BiBitOr => ast::BinOpKind::BitOr,
975+
BiShl => ast::BinOpKind::Shl,
976+
BiShr => ast::BinOpKind::Shr,
977+
BiEq => ast::BinOpKind::Eq,
978+
BiLt => ast::BinOpKind::Lt,
979+
BiLe => ast::BinOpKind::Le,
980+
BiNe => ast::BinOpKind::Ne,
981+
BiGe => ast::BinOpKind::Ge,
982+
BiGt => ast::BinOpKind::Gt,
983+
}
984+
}
985+
}
986+
961987
pub type BinOp = Spanned<BinOp_>;
962988

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

1195+
impl Expr {
1196+
pub fn precedence(&self) -> ExprPrecedence {
1197+
match self.node {
1198+
ExprBox(_) => ExprPrecedence::Box,
1199+
ExprArray(_) => ExprPrecedence::Array,
1200+
ExprCall(..) => ExprPrecedence::Call,
1201+
ExprMethodCall(..) => ExprPrecedence::MethodCall,
1202+
ExprTup(_) => ExprPrecedence::Tup,
1203+
ExprBinary(op, ..) => ExprPrecedence::Binary(op.node.into()),
1204+
ExprUnary(..) => ExprPrecedence::Unary,
1205+
ExprLit(_) => ExprPrecedence::Lit,
1206+
ExprType(..) | ExprCast(..) => ExprPrecedence::Cast,
1207+
ExprIf(..) => ExprPrecedence::If,
1208+
ExprWhile(..) => ExprPrecedence::While,
1209+
ExprLoop(..) => ExprPrecedence::Loop,
1210+
ExprMatch(..) => ExprPrecedence::Match,
1211+
ExprClosure(..) => ExprPrecedence::Closure,
1212+
ExprBlock(..) => ExprPrecedence::Block,
1213+
ExprAssign(..) => ExprPrecedence::Assign,
1214+
ExprAssignOp(..) => ExprPrecedence::AssignOp,
1215+
ExprField(..) => ExprPrecedence::Field,
1216+
ExprTupField(..) => ExprPrecedence::TupField,
1217+
ExprIndex(..) => ExprPrecedence::Index,
1218+
ExprPath(..) => ExprPrecedence::Path,
1219+
ExprAddrOf(..) => ExprPrecedence::AddrOf,
1220+
ExprBreak(..) => ExprPrecedence::Break,
1221+
ExprAgain(..) => ExprPrecedence::Continue,
1222+
ExprRet(..) => ExprPrecedence::Ret,
1223+
ExprInlineAsm(..) => ExprPrecedence::InlineAsm,
1224+
ExprStruct(..) => ExprPrecedence::Struct,
1225+
ExprRepeat(..) => ExprPrecedence::Repeat,
1226+
ExprYield(..) => ExprPrecedence::Yield,
1227+
}
1228+
}
1229+
}
1230+
11691231
impl fmt::Debug for Expr {
11701232
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11711233
write!(f, "expr({}: {})", self.id,

src/librustc/hir/print.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ impl<'a> State<'a> {
11041104
}
11051105

11061106
pub fn print_expr_maybe_paren(&mut self, expr: &hir::Expr, prec: i8) -> io::Result<()> {
1107-
let needs_par = expr_precedence(expr) < prec;
1107+
let needs_par = expr.precedence().order() < prec;
11081108
if needs_par {
11091109
self.popen()?;
11101110
}
@@ -2318,55 +2318,6 @@ fn stmt_ends_with_semi(stmt: &hir::Stmt_) -> bool {
23182318
}
23192319
}
23202320

2321-
2322-
fn expr_precedence(expr: &hir::Expr) -> i8 {
2323-
use syntax::util::parser::*;
2324-
2325-
match expr.node {
2326-
hir::ExprClosure(..) => PREC_CLOSURE,
2327-
2328-
hir::ExprBreak(..) |
2329-
hir::ExprAgain(..) |
2330-
hir::ExprRet(..) |
2331-
hir::ExprYield(..) => PREC_JUMP,
2332-
2333-
// Binop-like expr kinds, handled by `AssocOp`.
2334-
hir::ExprBinary(op, _, _) => bin_op_to_assoc_op(op.node).precedence() as i8,
2335-
2336-
hir::ExprCast(..) => AssocOp::As.precedence() as i8,
2337-
hir::ExprType(..) => AssocOp::Colon.precedence() as i8,
2338-
2339-
hir::ExprAssign(..) |
2340-
hir::ExprAssignOp(..) => AssocOp::Assign.precedence() as i8,
2341-
2342-
// Unary, prefix
2343-
hir::ExprBox(..) |
2344-
hir::ExprAddrOf(..) |
2345-
hir::ExprUnary(..) => PREC_PREFIX,
2346-
2347-
// Unary, postfix
2348-
hir::ExprCall(..) |
2349-
hir::ExprMethodCall(..) |
2350-
hir::ExprField(..) |
2351-
hir::ExprTupField(..) |
2352-
hir::ExprIndex(..) |
2353-
hir::ExprInlineAsm(..) => PREC_POSTFIX,
2354-
2355-
// Never need parens
2356-
hir::ExprArray(..) |
2357-
hir::ExprRepeat(..) |
2358-
hir::ExprTup(..) |
2359-
hir::ExprLit(..) |
2360-
hir::ExprPath(..) |
2361-
hir::ExprIf(..) |
2362-
hir::ExprWhile(..) |
2363-
hir::ExprLoop(..) |
2364-
hir::ExprMatch(..) |
2365-
hir::ExprBlock(..) |
2366-
hir::ExprStruct(..) => PREC_PAREN,
2367-
}
2368-
}
2369-
23702321
fn bin_op_to_assoc_op(op: hir::BinOp_) -> AssocOp {
23712322
use hir::BinOp_::*;
23722323
match op {

0 commit comments

Comments
 (0)