Skip to content

Commit 751ae5a

Browse files
committed
Introduce hir::Lit not keeping the original token
1 parent f2834a4 commit 751ae5a

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
@@ -4100,7 +4100,7 @@ impl<'a> LoweringContext<'a> {
41004100
let ohs = P(self.lower_expr(ohs));
41014101
hir::ExprKind::Unary(op, ohs)
41024102
}
4103-
ExprKind::Lit(ref l) => hir::ExprKind::Lit((*l).clone()),
4103+
ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.node.clone())),
41044104
ExprKind::Cast(ref expr, ref ty) => {
41054105
let expr = P(self.lower_expr(expr));
41064106
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};
@@ -1276,6 +1277,64 @@ impl<'a> State<'a> {
12761277
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
12771278
}
12781279

1280+
fn print_literal(&mut self, lit: &hir::Lit) -> io::Result<()> {
1281+
self.maybe_print_comment(lit.span.lo())?;
1282+
if let Some(ltrl) = self.next_lit(lit.span.lo()) {
1283+
return self.writer().word(ltrl.lit.clone());
1284+
}
1285+
match lit.node {
1286+
hir::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
1287+
hir::LitKind::Err(st) => {
1288+
let st = st.as_str().escape_debug().to_string();
1289+
let mut res = String::with_capacity(st.len() + 2);
1290+
res.push('\'');
1291+
res.push_str(&st);
1292+
res.push('\'');
1293+
self.writer().word(res)
1294+
}
1295+
hir::LitKind::Byte(byte) => {
1296+
let mut res = String::from("b'");
1297+
res.extend(ascii::escape_default(byte).map(|c| c as char));
1298+
res.push('\'');
1299+
self.writer().word(res)
1300+
}
1301+
hir::LitKind::Char(ch) => {
1302+
let mut res = String::from("'");
1303+
res.extend(ch.escape_default());
1304+
res.push('\'');
1305+
self.writer().word(res)
1306+
}
1307+
hir::LitKind::Int(i, t) => {
1308+
match t {
1309+
ast::LitIntType::Signed(st) => {
1310+
self.writer().word(st.val_to_string(i as i128))
1311+
}
1312+
ast::LitIntType::Unsigned(ut) => {
1313+
self.writer().word(ut.val_to_string(i))
1314+
}
1315+
ast::LitIntType::Unsuffixed => {
1316+
self.writer().word(i.to_string())
1317+
}
1318+
}
1319+
}
1320+
hir::LitKind::Float(ref f, t) => {
1321+
self.writer().word(format!("{}{}", &f, t.ty_to_string()))
1322+
}
1323+
hir::LitKind::FloatUnsuffixed(ref f) => self.writer().word(f.as_str().to_string()),
1324+
hir::LitKind::Bool(val) => {
1325+
if val { self.writer().word("true") } else { self.writer().word("false") }
1326+
}
1327+
hir::LitKind::ByteStr(ref v) => {
1328+
let mut escaped: String = String::new();
1329+
for &ch in v.iter() {
1330+
escaped.extend(ascii::escape_default(ch)
1331+
.map(|c| c as char));
1332+
}
1333+
self.writer().word(format!("b\"{}\"", escaped))
1334+
}
1335+
}
1336+
}
1337+
12791338
pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {
12801339
self.maybe_print_comment(expr.span.lo())?;
12811340
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
@@ -3083,7 +3083,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30833083

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

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)