Skip to content

Commit 826fb78

Browse files
committed
Use token::Lit in ast::ExprKind::Lit.
Instead of `ast::Lit`. Literal lowering now happens at two different times. Expression literals are lowered when HIR is crated. Attribute literals are lowered during parsing. This commit changes the language very slightly. Some programs that used to not compile now will compile. This is because some invalid literals that are removed by `cfg` or attribute macros will no longer trigger errors. See this comment for more details: rust-lang#102944 (comment)
1 parent 660e535 commit 826fb78

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/attr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ impl Rewrite for ast::NestedMetaItem {
260260
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
261261
match self {
262262
ast::NestedMetaItem::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
263-
ast::NestedMetaItem::Literal(ref l) => rewrite_literal(context, l, shape),
263+
ast::NestedMetaItem::Literal(ref l) => {
264+
rewrite_literal(context, l.token_lit, l.span, shape)
265+
}
264266
}
265267
}
266268
}
@@ -318,7 +320,7 @@ impl Rewrite for ast::MetaItem {
318320
// we might be better off ignoring the fact that the attribute
319321
// is longer than the max width and continue on formatting.
320322
// See #2479 for example.
321-
let value = rewrite_literal(context, literal, lit_shape)
323+
let value = rewrite_literal(context, literal.token_lit, literal.span, lit_shape)
322324
.unwrap_or_else(|| context.snippet(literal.span).to_owned());
323325
format!("{} = {}", path, value)
324326
}

src/expr.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cmp::min;
33

44
use itertools::Itertools;
55
use rustc_ast::token::{Delimiter, LitKind};
6-
use rustc_ast::{ast, ptr};
6+
use rustc_ast::{ast, ptr, token};
77
use rustc_span::{BytePos, Span};
88

99
use crate::chains::rewrite_chain;
@@ -75,12 +75,12 @@ pub(crate) fn format_expr(
7575
choose_separator_tactic(context, expr.span),
7676
None,
7777
),
78-
ast::ExprKind::Lit(ref l) => {
79-
if let Some(expr_rw) = rewrite_literal(context, l, shape) {
78+
ast::ExprKind::Lit(token_lit) => {
79+
if let Some(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
8080
Some(expr_rw)
8181
} else {
82-
if let LitKind::StrRaw(_) = l.token_lit.kind {
83-
Some(context.snippet(l.span).trim().into())
82+
if let LitKind::StrRaw(_) = token_lit.kind {
83+
Some(context.snippet(expr.span).trim().into())
8484
} else {
8585
None
8686
}
@@ -274,9 +274,9 @@ pub(crate) fn format_expr(
274274

275275
fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool {
276276
match lhs.kind {
277-
ast::ExprKind::Lit(ref lit) => match lit.kind {
278-
ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) => {
279-
context.snippet(lit.span).ends_with('.')
277+
ast::ExprKind::Lit(token_lit) => match token_lit.kind {
278+
token::LitKind::Float if token_lit.suffix.is_none() => {
279+
context.snippet(lhs.span).ends_with('.')
280280
}
281281
_ => false,
282282
},
@@ -1185,14 +1185,15 @@ pub(crate) fn is_unsafe_block(block: &ast::Block) -> bool {
11851185

11861186
pub(crate) fn rewrite_literal(
11871187
context: &RewriteContext<'_>,
1188-
l: &ast::Lit,
1188+
token_lit: token::Lit,
1189+
span: Span,
11891190
shape: Shape,
11901191
) -> Option<String> {
1191-
match l.kind {
1192-
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
1193-
ast::LitKind::Int(..) => rewrite_int_lit(context, l, shape),
1192+
match token_lit.kind {
1193+
token::LitKind::Str => rewrite_string_lit(context, span, shape),
1194+
token::LitKind::Integer => rewrite_int_lit(context, token_lit, span, shape),
11941195
_ => wrap_str(
1195-
context.snippet(l.span).to_owned(),
1196+
context.snippet(span).to_owned(),
11961197
context.config.max_width(),
11971198
shape,
11981199
),
@@ -1225,9 +1226,13 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12251226
)
12261227
}
12271228

1228-
fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -> Option<String> {
1229-
let span = lit.span;
1230-
let symbol = lit.token_lit.symbol.as_str();
1229+
fn rewrite_int_lit(
1230+
context: &RewriteContext<'_>,
1231+
token_lit: token::Lit,
1232+
span: Span,
1233+
shape: Shape,
1234+
) -> Option<String> {
1235+
let symbol = token_lit.symbol.as_str();
12311236

12321237
if let Some(symbol_stripped) = symbol.strip_prefix("0x") {
12331238
let hex_lit = match context.config.hex_literal_case() {
@@ -1240,9 +1245,7 @@ fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -
12401245
format!(
12411246
"0x{}{}",
12421247
hex_lit,
1243-
lit.token_lit
1244-
.suffix
1245-
.map_or(String::new(), |s| s.to_string())
1248+
token_lit.suffix.map_or(String::new(), |s| s.to_string())
12461249
),
12471250
context.config.max_width(),
12481251
shape,

0 commit comments

Comments
 (0)