let ohs = P(self.lower_expr(ohs));
hir::ExprKind::Unary(op, ohs)
}
- ExprKind::Lit(ref l) => hir::ExprKind::Lit((*l).clone()),
+ ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.node.clone())),
ExprKind::Cast(ref expr, ref ty) => {
let expr = P(self.lower_expr(expr));
hir::ExprKind::Cast(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))
use syntax::source_map::Spanned;
use rustc_target::spec::abi::Abi;
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, AsmDialect};
-use syntax::ast::{Attribute, Label, Lit, StrStyle, FloatTy, IntTy, UintTy};
+use syntax::ast::{Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
use syntax::attr::{InlineAttr, OptimizeAttr};
use syntax::ext::hygiene::SyntaxContext;
use syntax::ptr::P;
}
}
+/// A literal.
+pub type Lit = Spanned<LitKind>;
+
/// A constant (expression) that's not an item or associated item,
/// but needs its own `DefId` for type-checking, const-eval, etc.
/// These are usually found nested inside types (e.g., array lengths)
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(target_arch = "x86_64")]
-static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 80);
+static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 72);
impl Expr {
pub fn precedence(&self) -> ExprPrecedence {
use crate::hir::{PatKind, GenericBound, TraitBoundModifier, RangeEnd};
use crate::hir::{GenericParam, GenericParamKind, GenericArg};
+use std::ascii;
use std::borrow::Cow;
use std::cell::Cell;
use std::io::{self, Write, Read};
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
}
+ fn print_literal(&mut self, lit: &hir::Lit) -> io::Result<()> {
+ self.maybe_print_comment(lit.span.lo())?;
+ if let Some(ltrl) = self.next_lit(lit.span.lo()) {
+ return self.writer().word(ltrl.lit.clone());
+ }
+ match lit.node {
+ hir::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
+ hir::LitKind::Err(st) => {
+ let st = st.as_str().escape_debug().to_string();
+ let mut res = String::with_capacity(st.len() + 2);
+ res.push('\'');
+ res.push_str(&st);
+ res.push('\'');
+ self.writer().word(res)
+ }
+ hir::LitKind::Byte(byte) => {
+ let mut res = String::from("b'");
+ res.extend(ascii::escape_default(byte).map(|c| c as char));
+ res.push('\'');
+ self.writer().word(res)
+ }
+ hir::LitKind::Char(ch) => {
+ let mut res = String::from("'");
+ res.extend(ch.escape_default());
+ res.push('\'');
+ self.writer().word(res)
+ }
+ hir::LitKind::Int(i, t) => {
+ match t {
+ ast::LitIntType::Signed(st) => {
+ self.writer().word(st.val_to_string(i as i128))
+ }
+ ast::LitIntType::Unsigned(ut) => {
+ self.writer().word(ut.val_to_string(i))
+ }
+ ast::LitIntType::Unsuffixed => {
+ self.writer().word(i.to_string())
+ }
+ }
+ }
+ hir::LitKind::Float(ref f, t) => {
+ self.writer().word(format!("{}{}", &f, t.ty_to_string()))
+ }
+ hir::LitKind::FloatUnsuffixed(ref f) => self.writer().word(f.as_str().to_string()),
+ hir::LitKind::Bool(val) => {
+ if val { self.writer().word("true") } else { self.writer().word("false") }
+ }
+ hir::LitKind::ByteStr(ref v) => {
+ let mut escaped: String = String::new();
+ for &ch in v.iter() {
+ escaped.extend(ascii::escape_default(ch)
+ .map(|c| c as char));
+ }
+ self.writer().word(format!("b\"{}\"", escaped))
+ }
+ }
+ }
+
pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {
self.maybe_print_comment(expr.span.lo())?;
self.print_outer_attributes(&expr.attrs)?;
Bool(value)
});
+impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
+
impl_stable_hash_for!(enum ::syntax::ast::IntTy { Isize, I8, I16, I32, I64, I128 });
impl_stable_hash_for!(enum ::syntax::ast::UintTy { Usize, U8, U16, U32, U64, U128 });
impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
/// Returns `true` iff the lint was overridden.
fn lint_overflowing_range_endpoint<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
- lit: &ast::Lit,
+ lit: &hir::Lit,
lit_val: u128,
max: u128,
expr: &'tcx hir::Expr,
}
}
-fn get_bin_hex_repr(cx: &LateContext<'_, '_>, lit: &ast::Lit) -> Option<String> {
+fn get_bin_hex_repr(cx: &LateContext<'_, '_>, lit: &hir::Lit) -> Option<String> {
let src = cx.sess().source_map().span_to_snippet(lit.span).ok()?;
let firstch = src.chars().next()?;
cx: &LateContext<'a, 'tcx>,
type_limits: &TypeLimits,
e: &'tcx hir::Expr,
- lit: &ast::Lit,
+ lit: &hir::Lit,
t: ast::IntTy,
v: u128,
) {
fn lint_uint_literal<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
e: &'tcx hir::Expr,
- lit: &ast::Lit,
+ lit: &hir::Lit,
t: ast::UintTy,
) {
let uint_type = if let ast::UintTy::Usize = t {
cx: &LateContext<'a, 'tcx>,
type_limits: &TypeLimits,
e: &'tcx hir::Expr,
- lit: &ast::Lit,
+ lit: &hir::Lit,
) {
match cx.tables.node_type(e.hir_id).sty {
ty::Int(t) => {
// AST fragment checking
fn check_lit(&self,
- lit: &ast::Lit,
+ lit: &hir::Lit,
expected: Expectation<'tcx>)
-> Ty<'tcx>
{
}
/// A literal.
-#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash, PartialEq)]
+#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Lit {
pub node: LitKind,
pub token: token::Lit,