Skip to content

Commit 468ced6

Browse files
committed
Introduce hir::Lit not keeping the original token
1 parent bbee088 commit 468ced6

File tree

7 files changed

+74
-10
lines changed

7 files changed

+74
-10
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4124,7 +4124,7 @@ impl<'a> LoweringContext<'a> {
41244124
let ohs = P(self.lower_expr(ohs));
41254125
hir::ExprKind::Unary(op, ohs)
41264126
}
4127-
ExprKind::Lit(ref l) => hir::ExprKind::Lit((*l).clone()),
4127+
ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.node.clone())),
41284128
ExprKind::Cast(ref expr, ref ty) => {
41294129
let expr = P(self.lower_expr(expr));
41304130
hir::ExprKind::Cast(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))

src/librustc/hir/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
2020
use syntax::source_map::Spanned;
2121
use rustc_target::spec::abi::Abi;
2222
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, AsmDialect};
23-
use syntax::ast::{Attribute, Label, Lit, StrStyle, FloatTy, IntTy, UintTy};
23+
use syntax::ast::{Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
2424
use syntax::attr::{InlineAttr, OptimizeAttr};
2525
use syntax::ext::hygiene::SyntaxContext;
2626
use syntax::ptr::P;
@@ -1331,6 +1331,9 @@ impl BodyOwnerKind {
13311331
}
13321332
}
13331333

1334+
/// A literal.
1335+
pub type Lit = Spanned<LitKind>;
1336+
13341337
/// A constant (expression) that's not an item or associated item,
13351338
/// but needs its own `DefId` for type-checking, const-eval, etc.
13361339
/// These are usually found nested inside types (e.g., array lengths)
@@ -1353,7 +1356,7 @@ pub struct Expr {
13531356

13541357
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
13551358
#[cfg(target_arch = "x86_64")]
1356-
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 80);
1359+
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 72);
13571360

13581361
impl Expr {
13591362
pub fn precedence(&self) -> ExprPrecedence {

src/librustc/hir/print.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::hir;
1515
use crate::hir::{PatKind, GenericBound, TraitBoundModifier, RangeEnd};
1616
use crate::hir::{GenericParam, GenericParamKind, GenericArg};
1717

18+
use std::ascii;
1819
use std::borrow::Cow;
1920
use std::cell::Cell;
2021
use std::io::{self, Write, Read};
@@ -1335,6 +1336,64 @@ impl<'a> State<'a> {
13351336
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
13361337
}
13371338

1339+
fn print_literal(&mut self, lit: &hir::Lit) -> io::Result<()> {
1340+
self.maybe_print_comment(lit.span.lo())?;
1341+
if let Some(ltrl) = self.next_lit(lit.span.lo()) {
1342+
return self.writer().word(ltrl.lit.clone());
1343+
}
1344+
match lit.node {
1345+
hir::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
1346+
hir::LitKind::Err(st) => {
1347+
let st = st.as_str().escape_debug().to_string();
1348+
let mut res = String::with_capacity(st.len() + 2);
1349+
res.push('\'');
1350+
res.push_str(&st);
1351+
res.push('\'');
1352+
self.writer().word(res)
1353+
}
1354+
hir::LitKind::Byte(byte) => {
1355+
let mut res = String::from("b'");
1356+
res.extend(ascii::escape_default(byte).map(|c| c as char));
1357+
res.push('\'');
1358+
self.writer().word(res)
1359+
}
1360+
hir::LitKind::Char(ch) => {
1361+
let mut res = String::from("'");
1362+
res.extend(ch.escape_default());
1363+
res.push('\'');
1364+
self.writer().word(res)
1365+
}
1366+
hir::LitKind::Int(i, t) => {
1367+
match t {
1368+
ast::LitIntType::Signed(st) => {
1369+
self.writer().word(st.val_to_string(i as i128))
1370+
}
1371+
ast::LitIntType::Unsigned(ut) => {
1372+
self.writer().word(ut.val_to_string(i))
1373+
}
1374+
ast::LitIntType::Unsuffixed => {
1375+
self.writer().word(i.to_string())
1376+
}
1377+
}
1378+
}
1379+
hir::LitKind::Float(ref f, t) => {
1380+
self.writer().word(format!("{}{}", &f, t.ty_to_string()))
1381+
}
1382+
hir::LitKind::FloatUnsuffixed(ref f) => self.writer().word(f.as_str().to_string()),
1383+
hir::LitKind::Bool(val) => {
1384+
if val { self.writer().word("true") } else { self.writer().word("false") }
1385+
}
1386+
hir::LitKind::ByteStr(ref v) => {
1387+
let mut escaped: String = String::new();
1388+
for &ch in v.iter() {
1389+
escaped.extend(ascii::escape_default(ch)
1390+
.map(|c| c as char));
1391+
}
1392+
self.writer().word(format!("b\"{}\"", escaped))
1393+
}
1394+
}
1395+
}
1396+
13381397
pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {
13391398
self.maybe_print_comment(expr.span.lo())?;
13401399
self.print_outer_attributes(&expr.attrs)?;

src/librustc/ich/impls_syntax.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ impl_stable_hash_for!(enum ::syntax::ast::LitKind {
181181
Bool(value)
182182
});
183183

184+
impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
185+
184186
impl_stable_hash_for!(enum ::syntax::ast::IntTy { Isize, I8, I16, I32, I64, I128 });
185187
impl_stable_hash_for!(enum ::syntax::ast::UintTy { Usize, U8, U16, U32, U64, U128 });
186188
impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });

src/librustc_lint/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl TypeLimits {
6262
/// Returns `true` iff the lint was overridden.
6363
fn lint_overflowing_range_endpoint<'a, 'tcx>(
6464
cx: &LateContext<'a, 'tcx>,
65-
lit: &ast::Lit,
65+
lit: &hir::Lit,
6666
lit_val: u128,
6767
max: u128,
6868
expr: &'tcx hir::Expr,
@@ -132,7 +132,7 @@ fn uint_ty_range(uint_ty: ast::UintTy) -> (u128, u128) {
132132
}
133133
}
134134

135-
fn get_bin_hex_repr(cx: &LateContext<'_, '_>, lit: &ast::Lit) -> Option<String> {
135+
fn get_bin_hex_repr(cx: &LateContext<'_, '_>, lit: &hir::Lit) -> Option<String> {
136136
let src = cx.sess().source_map().span_to_snippet(lit.span).ok()?;
137137
let firstch = src.chars().next()?;
138138

@@ -249,7 +249,7 @@ fn lint_int_literal<'a, 'tcx>(
249249
cx: &LateContext<'a, 'tcx>,
250250
type_limits: &TypeLimits,
251251
e: &'tcx hir::Expr,
252-
lit: &ast::Lit,
252+
lit: &hir::Lit,
253253
t: ast::IntTy,
254254
v: u128,
255255
) {
@@ -301,7 +301,7 @@ fn lint_int_literal<'a, 'tcx>(
301301
fn lint_uint_literal<'a, 'tcx>(
302302
cx: &LateContext<'a, 'tcx>,
303303
e: &'tcx hir::Expr,
304-
lit: &ast::Lit,
304+
lit: &hir::Lit,
305305
t: ast::UintTy,
306306
) {
307307
let uint_type = if let ast::UintTy::Usize = t {
@@ -363,7 +363,7 @@ fn lint_literal<'a, 'tcx>(
363363
cx: &LateContext<'a, 'tcx>,
364364
type_limits: &TypeLimits,
365365
e: &'tcx hir::Expr,
366-
lit: &ast::Lit,
366+
lit: &hir::Lit,
367367
) {
368368
match cx.tables.node_type(e.hir_id).sty {
369369
ty::Int(t) => {

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3084,7 +3084,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30843084

30853085
// AST fragment checking
30863086
fn check_lit(&self,
3087-
lit: &ast::Lit,
3087+
lit: &hir::Lit,
30883088
expected: Expectation<'tcx>)
30893089
-> Ty<'tcx>
30903090
{

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ pub enum StrStyle {
13521352
}
13531353

13541354
/// A literal.
1355-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash, PartialEq)]
1355+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
13561356
pub struct Lit {
13571357
pub node: LitKind,
13581358
pub token: token::Lit,

0 commit comments

Comments
 (0)