With working test now

This commit is contained in:
llogiq 2015-08-16 15:56:09 +02:00
parent f23af0cfd5
commit 03c7d7074d
2 changed files with 28 additions and 14 deletions

View File

@ -1,6 +1,4 @@
#[cfg(test)]
use rustc::lint::Context;
use rustc::middle::const_eval::lookup_const_by_id;
use rustc::middle::def::PathResolution;
use rustc::middle::def::Def::*;
@ -12,9 +10,6 @@ use std::ops::Deref;
use self::ConstantVariant::*;
use self::FloatWidth::*;
#[cfg(not(test))]
pub struct Context;
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum FloatWidth {
Fw32,
@ -33,8 +28,8 @@ impl From<FloatTy> for FloatWidth {
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Constant {
constant: ConstantVariant,
needed_resolution: bool
pub constant: ConstantVariant,
pub needed_resolution: bool
}
impl Constant {
@ -187,11 +182,7 @@ fn constant_tup<E: Deref<Target=Expr> + Sized>(cx: &Context, tup: &[E]) -> Optio
})
}
#[cfg(test)]
fn fetch_path(_cx: &Context, _expr: &Expr) -> Option<Constant> { None }
/// lookup a possibly constant expression from a ExprPath
#[cfg(not(test))]
fn fetch_path(cx: &Context, e: &Expr) -> Option<Constant> {
if let Some(&PathResolution { base_def: DefConst(id), ..}) =
cx.tcx.def_map.borrow().get(&e.id) {

View File

@ -1,11 +1,34 @@
#![allow(plugin_as_library)]
#![feature(rustc_private)]
extern crate clippy;
extern crate syntax;
extern crate rustc;
use clippy::consts;
use clippy::consts::constant;
use clippy::consts::ConstantVariant::*;
use syntax::ast::*;
use syntax::ptr::P;
use syntax::codemap::{Spanned, COMMAND_LINE_SP};
use std::mem;
use rustc::lint::Context;
fn ctx() -> &'static Context<'static, 'static> {
unsafe {
let x : *const Context<'static, 'static> = std::ptr::null();
mem::transmute(x)
}
}
#[test]
fn test_lit() {
assert_eq!(ConstantBool(true), constant(&Context,
Expr{ node_id: 1, node: ExprLit(LitBool(true)), span: default() }));
assert_eq!(Some(ConstantBool(true)), constant(ctx(),
&Expr{
id: 1,
node: ExprLit(P(Spanned{
node: LitBool(true),
span: COMMAND_LINE_SP,
})),
span: COMMAND_LINE_SP,
}).map(|x| x.constant));
}