2015-08-16 15:56:09 +02:00
|
|
|
#![allow(plugin_as_library)]
|
|
|
|
#![feature(rustc_private)]
|
2015-08-14 17:14:54 +02:00
|
|
|
|
|
|
|
extern crate clippy;
|
2015-08-16 15:56:09 +02:00
|
|
|
extern crate syntax;
|
|
|
|
extern crate rustc;
|
2015-09-03 16:42:17 +02:00
|
|
|
extern crate rustc_front;
|
2015-08-14 17:14:54 +02:00
|
|
|
|
2015-09-03 16:42:17 +02:00
|
|
|
use rustc_front::hir::*;
|
2015-08-17 13:18:14 +02:00
|
|
|
use syntax::parse::token::InternedString;
|
2015-08-16 15:56:09 +02:00
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax::codemap::{Spanned, COMMAND_LINE_SP};
|
|
|
|
|
2015-09-17 02:01:41 +02:00
|
|
|
use syntax::ast::Lit_::*;
|
|
|
|
use syntax::ast::Lit_;
|
|
|
|
use syntax::ast::LitIntType::*;
|
|
|
|
use syntax::ast::StrStyle::*;
|
|
|
|
use syntax::ast::Sign::*;
|
|
|
|
|
2015-08-17 17:51:30 +02:00
|
|
|
use clippy::consts::{constant_simple, Constant};
|
2015-08-17 11:43:36 +02:00
|
|
|
|
2015-08-17 16:24:57 +02:00
|
|
|
fn spanned<T>(t: T) -> Spanned<T> {
|
|
|
|
Spanned{ node: t, span: COMMAND_LINE_SP }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr(n: Expr_) -> Expr {
|
2015-08-17 11:43:36 +02:00
|
|
|
Expr{
|
|
|
|
id: 1,
|
2015-08-17 16:24:57 +02:00
|
|
|
node: n,
|
2015-08-17 11:43:36 +02:00
|
|
|
span: COMMAND_LINE_SP,
|
2015-12-05 13:25:04 +01:00
|
|
|
attrs: None
|
2015-08-17 11:43:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 16:24:57 +02:00
|
|
|
fn lit(l: Lit_) -> Expr {
|
|
|
|
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-02-01 12:51:33 +01:00
|
|
|
const TRUE : Constant = Constant::Bool(true);
|
|
|
|
const FALSE : Constant = Constant::Bool(false);
|
|
|
|
const ZERO : Constant = Constant::Int(0, UnsuffixedIntLit(Plus));
|
|
|
|
const ONE : Constant = Constant::Int(1, UnsuffixedIntLit(Plus));
|
|
|
|
const TWO : Constant = Constant::Int(2, UnsuffixedIntLit(Plus));
|
2015-08-17 16:24:57 +02:00
|
|
|
|
2015-08-14 17:14:54 +02:00
|
|
|
#[test]
|
|
|
|
fn test_lit() {
|
2015-08-17 16:24:57 +02:00
|
|
|
check(TRUE, &lit(LitBool(true)));
|
|
|
|
check(FALSE, &lit(LitBool(false)));
|
|
|
|
check(ZERO, &lit(LitInt(0, UnsuffixedIntLit(Plus))));
|
2016-02-01 12:51:33 +01:00
|
|
|
check(Constant::Str("cool!".into(), CookedStr), &lit(LitStr(
|
2015-08-17 13:18:14 +02:00
|
|
|
InternedString::new("cool!"), CookedStr)));
|
2015-08-14 17:14:54 +02:00
|
|
|
}
|
2015-08-17 16:24:57 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ops() {
|
|
|
|
check(TRUE, &binop(BiOr, lit(LitBool(false)), lit(LitBool(true))));
|
|
|
|
check(FALSE, &binop(BiAnd, lit(LitBool(false)), lit(LitBool(true))));
|
|
|
|
|
|
|
|
let litzero = lit(LitInt(0, UnsuffixedIntLit(Plus)));
|
2015-08-19 11:58:59 +02:00
|
|
|
let litone = lit(LitInt(1, UnsuffixedIntLit(Plus)));
|
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()));
|
2015-08-17 16:24:57 +02:00
|
|
|
}
|