2015-08-16 15:56:09 +02:00
|
|
|
#![feature(rustc_private)]
|
2015-08-14 17:14:54 +02:00
|
|
|
|
2016-08-17 18:35:25 +02:00
|
|
|
extern crate clippy_lints;
|
2015-08-16 15:56:09 +02:00
|
|
|
extern crate rustc;
|
2016-03-16 12:38:26 +01:00
|
|
|
extern crate rustc_const_eval;
|
2016-03-31 17:05:43 +02:00
|
|
|
extern crate rustc_const_math;
|
|
|
|
extern crate syntax;
|
2015-08-14 17:14:54 +02:00
|
|
|
|
2016-08-17 18:35:25 +02:00
|
|
|
use clippy_lints::consts::{constant_simple, Constant, FloatWidth};
|
2016-03-31 17:05:43 +02:00
|
|
|
use rustc_const_math::ConstInt;
|
2016-04-07 17:46:48 +02:00
|
|
|
use rustc::hir::*;
|
2016-09-22 02:51:12 +02:00
|
|
|
use syntax::ast::{LitIntType, LitKind, NodeId, StrStyle};
|
2016-03-31 17:05:43 +02:00
|
|
|
use syntax::codemap::{Spanned, COMMAND_LINE_SP};
|
2016-11-23 21:19:03 +01:00
|
|
|
use syntax::symbol::Symbol;
|
2015-08-16 15:56:09 +02:00
|
|
|
use syntax::ptr::P;
|
2016-06-28 15:54:23 +02:00
|
|
|
use syntax::util::ThinVec;
|
2015-08-17 11:43:36 +02:00
|
|
|
|
2015-08-17 16:24:57 +02:00
|
|
|
fn spanned<T>(t: T) -> Spanned<T> {
|
2016-06-06 01:42:39 +02:00
|
|
|
Spanned {
|
|
|
|
node: t,
|
|
|
|
span: COMMAND_LINE_SP,
|
|
|
|
}
|
2015-08-17 16:24:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expr(n: Expr_) -> Expr {
|
2016-06-06 01:42:39 +02:00
|
|
|
Expr {
|
2016-09-22 02:51:12 +02:00
|
|
|
id: NodeId::new(1),
|
2015-08-17 16:24:57 +02:00
|
|
|
node: n,
|
2015-08-17 11:43:36 +02:00
|
|
|
span: COMMAND_LINE_SP,
|
2016-06-28 15:54:23 +02:00
|
|
|
attrs: ThinVec::new(),
|
2015-08-17 11:43:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:35:44 +01:00
|
|
|
fn lit(l: LitKind) -> Expr {
|
2015-08-17 16:24:57 +02:00
|
|
|
expr(ExprLit(P(spanned(l))))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn binop(op: BinOp_, l: Expr, r: Expr) -> Expr {
|
|
|
|
expr(ExprBinary(spanned(op), P(l), P(r)))
|
|
|
|
}
|
|
|
|
|
2015-08-17 17:51:30 +02:00
|
|
|
fn check(expect: Constant, expr: &Expr) {
|
|
|
|
assert_eq!(Some(expect), constant_simple(expr))
|
2015-08-16 15:56:09 +02:00
|
|
|
}
|
2015-08-14 17:14:54 +02:00
|
|
|
|
2016-06-06 01:42:39 +02:00
|
|
|
const TRUE: Constant = Constant::Bool(true);
|
|
|
|
const FALSE: Constant = Constant::Bool(false);
|
|
|
|
const ZERO: Constant = Constant::Int(ConstInt::Infer(0));
|
|
|
|
const ONE: Constant = Constant::Int(ConstInt::Infer(1));
|
|
|
|
const TWO: Constant = Constant::Int(ConstInt::Infer(2));
|
2015-08-17 16:24:57 +02:00
|
|
|
|
2015-08-14 17:14:54 +02:00
|
|
|
#[test]
|
|
|
|
fn test_lit() {
|
2016-02-12 18:35:44 +01:00
|
|
|
check(TRUE, &lit(LitKind::Bool(true)));
|
|
|
|
check(FALSE, &lit(LitKind::Bool(false)));
|
|
|
|
check(ZERO, &lit(LitKind::Int(0, LitIntType::Unsuffixed)));
|
2016-06-06 01:42:39 +02:00
|
|
|
check(Constant::Str("cool!".into(), StrStyle::Cooked),
|
2016-11-23 21:19:03 +01:00
|
|
|
&lit(LitKind::Str(Symbol::intern("cool!"), StrStyle::Cooked)));
|
2015-08-14 17:14:54 +02:00
|
|
|
}
|
2015-08-17 16:24:57 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ops() {
|
2016-02-12 18:35:44 +01:00
|
|
|
check(TRUE, &binop(BiOr, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
|
|
|
|
check(FALSE, &binop(BiAnd, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
|
2015-08-17 16:24:57 +02:00
|
|
|
|
2016-02-12 18:35:44 +01:00
|
|
|
let litzero = lit(LitKind::Int(0, LitIntType::Unsuffixed));
|
|
|
|
let litone = lit(LitKind::Int(1, LitIntType::Unsuffixed));
|
2015-08-17 16:24:57 +02:00
|
|
|
check(TRUE, &binop(BiEq, litzero.clone(), litzero.clone()));
|
|
|
|
check(TRUE, &binop(BiGe, litzero.clone(), litzero.clone()));
|
|
|
|
check(TRUE, &binop(BiLe, litzero.clone(), litzero.clone()));
|
|
|
|
check(FALSE, &binop(BiNe, litzero.clone(), litzero.clone()));
|
|
|
|
check(FALSE, &binop(BiGt, litzero.clone(), litzero.clone()));
|
|
|
|
check(FALSE, &binop(BiLt, litzero.clone(), litzero.clone()));
|
2015-08-19 11:58:59 +02:00
|
|
|
|
|
|
|
check(ZERO, &binop(BiAdd, litzero.clone(), litzero.clone()));
|
|
|
|
check(TWO, &binop(BiAdd, litone.clone(), litone.clone()));
|
|
|
|
check(ONE, &binop(BiSub, litone.clone(), litzero.clone()));
|
|
|
|
check(ONE, &binop(BiMul, litone.clone(), litone.clone()));
|
|
|
|
check(ONE, &binop(BiDiv, litone.clone(), litone.clone()));
|
2016-02-12 15:51:55 +01:00
|
|
|
|
2016-02-15 17:00:06 +01:00
|
|
|
let half_any = Constant::Float("0.5".into(), FloatWidth::Any);
|
|
|
|
let half32 = Constant::Float("0.5".into(), FloatWidth::F32);
|
|
|
|
let half64 = Constant::Float("0.5".into(), FloatWidth::F64);
|
2016-07-13 18:55:16 +02:00
|
|
|
let pos_zero = Constant::Float("0.0".into(), FloatWidth::F64);
|
|
|
|
let neg_zero = Constant::Float("-0.0".into(), FloatWidth::F64);
|
|
|
|
|
|
|
|
assert_eq!(pos_zero, pos_zero);
|
|
|
|
assert_eq!(neg_zero, neg_zero);
|
|
|
|
assert_eq!(None, pos_zero.partial_cmp(&neg_zero));
|
2016-02-12 15:51:55 +01:00
|
|
|
|
|
|
|
assert_eq!(half_any, half32);
|
|
|
|
assert_eq!(half_any, half64);
|
2017-01-17 19:23:32 +01:00
|
|
|
assert_eq!(half32, half64); // for transitivity
|
2016-03-16 16:28:31 +01:00
|
|
|
|
|
|
|
assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::U8(0)));
|
|
|
|
assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::I8(0)));
|
|
|
|
assert_eq!(Constant::Int(ConstInt::InferSigned(-1)), Constant::Int(ConstInt::I8(-1)));
|
2015-08-17 16:24:57 +02:00
|
|
|
}
|