1 #![allow(plugin_as_library)]
2 #![feature(rustc_private)]
6 extern crate rustc_const_eval;
7 extern crate rustc_const_math;
10 use clippy::consts::{constant_simple, Constant, FloatWidth};
11 use rustc_const_math::ConstInt;
13 use syntax::ast::{LitIntType, LitKind, StrStyle};
14 use syntax::codemap::{Spanned, COMMAND_LINE_SP};
15 use syntax::parse::token::InternedString;
18 fn spanned<T>(t: T) -> Spanned<T> {
19 Spanned{ node: t, span: COMMAND_LINE_SP }
22 fn expr(n: Expr_) -> Expr {
26 span: COMMAND_LINE_SP,
31 fn lit(l: LitKind) -> Expr {
32 expr(ExprLit(P(spanned(l))))
35 fn binop(op: BinOp_, l: Expr, r: Expr) -> Expr {
36 expr(ExprBinary(spanned(op), P(l), P(r)))
39 fn check(expect: Constant, expr: &Expr) {
40 assert_eq!(Some(expect), constant_simple(expr))
43 const TRUE : Constant = Constant::Bool(true);
44 const FALSE : Constant = Constant::Bool(false);
45 const ZERO : Constant = Constant::Int(ConstInt::Infer(0));
46 const ONE : Constant = Constant::Int(ConstInt::Infer(1));
47 const TWO : Constant = Constant::Int(ConstInt::Infer(2));
51 check(TRUE, &lit(LitKind::Bool(true)));
52 check(FALSE, &lit(LitKind::Bool(false)));
53 check(ZERO, &lit(LitKind::Int(0, LitIntType::Unsuffixed)));
54 check(Constant::Str("cool!".into(), StrStyle::Cooked), &lit(LitKind::Str(
55 InternedString::new("cool!"), StrStyle::Cooked)));
60 check(TRUE, &binop(BiOr, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
61 check(FALSE, &binop(BiAnd, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
63 let litzero = lit(LitKind::Int(0, LitIntType::Unsuffixed));
64 let litone = lit(LitKind::Int(1, LitIntType::Unsuffixed));
65 check(TRUE, &binop(BiEq, litzero.clone(), litzero.clone()));
66 check(TRUE, &binop(BiGe, litzero.clone(), litzero.clone()));
67 check(TRUE, &binop(BiLe, litzero.clone(), litzero.clone()));
68 check(FALSE, &binop(BiNe, litzero.clone(), litzero.clone()));
69 check(FALSE, &binop(BiGt, litzero.clone(), litzero.clone()));
70 check(FALSE, &binop(BiLt, litzero.clone(), litzero.clone()));
72 check(ZERO, &binop(BiAdd, litzero.clone(), litzero.clone()));
73 check(TWO, &binop(BiAdd, litone.clone(), litone.clone()));
74 check(ONE, &binop(BiSub, litone.clone(), litzero.clone()));
75 check(ONE, &binop(BiMul, litone.clone(), litone.clone()));
76 check(ONE, &binop(BiDiv, litone.clone(), litone.clone()));
78 let half_any = Constant::Float("0.5".into(), FloatWidth::Any);
79 let half32 = Constant::Float("0.5".into(), FloatWidth::F32);
80 let half64 = Constant::Float("0.5".into(), FloatWidth::F64);
82 assert_eq!(half_any, half32);
83 assert_eq!(half_any, half64);
84 assert_eq!(half32, half64); // for transitivity
86 assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::U8(0)));
87 assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::I8(0)));
88 assert_eq!(Constant::Int(ConstInt::InferSigned(-1)), Constant::Int(ConstInt::I8(-1)));