Skip to content

Commit 6d478fd

Browse files
committed
Remove the span from ast::Lit.
In the most common case, the `Lit` is stored within an `Expr`, which records the same span. For other cases, we can store the span next to the `Lit`.
1 parent 78a891d commit 6d478fd

File tree

41 files changed

+226
-206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+226
-206
lines changed

compiler/rustc_ast/src/ast.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ pub enum NestedMetaItem {
521521
/// A literal.
522522
///
523523
/// E.g., `"foo"`, `64`, `true`.
524-
Literal(Lit),
524+
Literal(Lit, Span),
525525
}
526526

527527
/// A spanned compile-time attribute item.
@@ -550,7 +550,9 @@ pub enum MetaItemKind {
550550
/// Name value meta item.
551551
///
552552
/// E.g., `feature = "foo"` as in `#[feature = "foo"]`.
553-
NameValue(Lit),
553+
///
554+
/// The span is that of the literal on the RHS.
555+
NameValue(Lit, Span),
554556
}
555557

556558
/// A block (`{ .. }`).
@@ -1605,7 +1607,7 @@ pub enum MacArgs {
16051607
#[derive(Clone, Encodable, Decodable, Debug)]
16061608
pub enum MacArgsEq {
16071609
Ast(P<Expr>),
1608-
Hir(Lit),
1610+
Hir(Lit, Span),
16091611
}
16101612

16111613
impl MacArgs {
@@ -1621,7 +1623,7 @@ impl MacArgs {
16211623
MacArgs::Empty => None,
16221624
MacArgs::Delimited(dspan, ..) => Some(dspan.entire()),
16231625
MacArgs::Eq(eq_span, MacArgsEq::Ast(expr)) => Some(eq_span.to(expr.span)),
1624-
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
1626+
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
16251627
unreachable!("in literal form when getting span: {:?}", lit);
16261628
}
16271629
}
@@ -1634,7 +1636,7 @@ impl MacArgs {
16341636
MacArgs::Empty => TokenStream::default(),
16351637
MacArgs::Delimited(.., tokens) => tokens.clone(),
16361638
MacArgs::Eq(_, MacArgsEq::Ast(expr)) => TokenStream::from_ast(expr),
1637-
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
1639+
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
16381640
unreachable!("in literal form when getting inner tokens: {:?}", lit)
16391641
}
16401642
}
@@ -1663,9 +1665,10 @@ where
16631665
MacArgs::Eq(_eq_span, MacArgsEq::Ast(expr)) => {
16641666
unreachable!("hash_stable {:?}", expr);
16651667
}
1666-
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit)) => {
1668+
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit, lit_span)) => {
16671669
eq_span.hash_stable(ctx, hasher);
16681670
lit.hash_stable(ctx, hasher);
1671+
lit_span.hash_stable(ctx, hasher);
16691672
}
16701673
}
16711674
}
@@ -1717,6 +1720,8 @@ pub enum StrStyle {
17171720
}
17181721

17191722
/// An AST literal.
1723+
///
1724+
/// Consult the enclosing type for the span (e.g. `ExprKind::Lit`).
17201725
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
17211726
pub struct Lit {
17221727
/// The original literal token as written in source code.
@@ -1725,7 +1730,6 @@ pub struct Lit {
17251730
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
17261731
/// FIXME: Remove this and only create the semantic representation during lowering to HIR.
17271732
pub kind: LitKind,
1728-
pub span: Span,
17291733
}
17301734

17311735
/// Same as `Lit`, but restricted to string literals.
@@ -1749,7 +1753,6 @@ impl StrLit {
17491753
};
17501754
Lit {
17511755
token_lit: token::Lit::new(token_kind, self.symbol, self.suffix),
1752-
span: self.span,
17531756
kind: LitKind::Str(self.symbol_unescaped, self.style),
17541757
}
17551758
}
@@ -3084,7 +3087,7 @@ mod size_asserts {
30843087
static_assert_size!(Impl, 200);
30853088
static_assert_size!(Item, 184);
30863089
static_assert_size!(ItemKind, 112);
3087-
static_assert_size!(Lit, 48);
3090+
static_assert_size!(Lit, 40);
30883091
static_assert_size!(LitKind, 24);
30893092
static_assert_size!(Local, 72);
30903093
static_assert_size!(Param, 40);

compiler/rustc_ast/src/attr/mod.rs

+37-32
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl NestedMetaItem {
4949
/// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
5050
pub fn literal(&self) -> Option<&Lit> {
5151
match *self {
52-
NestedMetaItem::Literal(ref lit) => Some(lit),
52+
NestedMetaItem::Literal(ref lit, _) => Some(lit),
5353
_ => None,
5454
}
5555
}
@@ -174,10 +174,24 @@ impl MetaItem {
174174

175175
// Example:
176176
// #[attribute(name = "value")]
177-
// ^^^^^^^^^^^^^^
177+
// ^^^^^^^
178178
pub fn name_value_literal(&self) -> Option<&Lit> {
179179
match &self.kind {
180-
MetaItemKind::NameValue(v) => Some(v),
180+
MetaItemKind::NameValue(v, _) => Some(v),
181+
_ => None,
182+
}
183+
}
184+
185+
pub fn name_value_literal_and_span(&self) -> Option<(&Lit, Span)> {
186+
match &self.kind {
187+
MetaItemKind::NameValue(v, v_span) => Some((v, *v_span)),
188+
_ => None,
189+
}
190+
}
191+
192+
pub fn name_value_literal_span(&self) -> Option<Span> {
193+
match self.kind {
194+
MetaItemKind::NameValue(_, v_span) => Some(v_span),
181195
_ => None,
182196
}
183197
}
@@ -200,17 +214,6 @@ impl MetaItem {
200214
pub fn has_name(&self, name: Symbol) -> bool {
201215
self.path == name
202216
}
203-
204-
/// This is used in case you want the value span instead of the whole attribute. Example:
205-
///
206-
/// ```text
207-
/// #[doc(alias = "foo")]
208-
/// ```
209-
///
210-
/// In here, it'll return a span for `"foo"`.
211-
pub fn name_value_literal_span(&self) -> Option<Span> {
212-
Some(self.name_value_literal()?.span)
213-
}
214217
}
215218

216219
impl AttrItem {
@@ -320,9 +323,9 @@ pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> Meta
320323
}
321324

322325
pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
323-
let lit = Lit::from_lit_kind(lit_kind, lit_span);
326+
let lit = Lit::from_lit_kind(lit_kind);
324327
let span = ident.span.to(lit_span);
325-
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
328+
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit, lit_span) }
326329
}
327330

328331
pub fn mk_list_item(ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
@@ -458,7 +461,7 @@ impl MetaItem {
458461
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
459462
let kind = MetaItemKind::from_tokens(tokens)?;
460463
let hi = match kind {
461-
MetaItemKind::NameValue(ref lit) => lit.span.hi(),
464+
MetaItemKind::NameValue(_, lit_span) => lit_span.hi(),
462465
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
463466
_ => path.span.hi(),
464467
};
@@ -470,7 +473,7 @@ impl MetaItem {
470473
impl MetaItemKind {
471474
pub fn value_str(&self) -> Option<Symbol> {
472475
match self {
473-
MetaItemKind::NameValue(ref v) => match v.kind {
476+
MetaItemKind::NameValue(ref v, _) => match v.kind {
474477
LitKind::Str(ref s, _) => Some(*s),
475478
_ => None,
476479
},
@@ -481,11 +484,11 @@ impl MetaItemKind {
481484
pub fn mac_args(&self, span: Span) -> MacArgs {
482485
match self {
483486
MetaItemKind::Word => MacArgs::Empty,
484-
MetaItemKind::NameValue(lit) => {
487+
MetaItemKind::NameValue(lit, lit_span) => {
485488
let expr = P(ast::Expr {
486489
id: ast::DUMMY_NODE_ID,
487490
kind: ast::ExprKind::Lit(lit.clone()),
488-
span: lit.span,
491+
span: *lit_span,
489492
attrs: ast::AttrVec::new(),
490493
tokens: None,
491494
});
@@ -511,10 +514,10 @@ impl MetaItemKind {
511514
fn token_trees(&self, span: Span) -> Vec<TokenTree> {
512515
match *self {
513516
MetaItemKind::Word => vec![],
514-
MetaItemKind::NameValue(ref lit) => {
517+
MetaItemKind::NameValue(ref lit, lit_span) => {
515518
vec![
516519
TokenTree::token_alone(token::Eq, span),
517-
TokenTree::Token(lit.to_token(), Spacing::Alone),
520+
TokenTree::Token(lit.to_token(lit_span), Spacing::Alone),
518521
]
519522
}
520523
MetaItemKind::List(ref list) => {
@@ -555,9 +558,9 @@ impl MetaItemKind {
555558
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
556559
MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
557560
}
558-
Some(TokenTree::Token(token, _)) => {
559-
Lit::from_token(&token).ok().map(MetaItemKind::NameValue)
560-
}
561+
Some(TokenTree::Token(token, _)) => Lit::from_token(&token)
562+
.ok()
563+
.map(|(lit, lit_span)| MetaItemKind::NameValue(lit, lit_span)),
561564
_ => None,
562565
}
563566
}
@@ -570,10 +573,12 @@ impl MetaItemKind {
570573
}
571574
MacArgs::Delimited(..) => None,
572575
MacArgs::Eq(_, MacArgsEq::Ast(expr)) => match &expr.kind {
573-
ast::ExprKind::Lit(lit) => Some(MetaItemKind::NameValue(lit.clone())),
576+
ast::ExprKind::Lit(lit) => Some(MetaItemKind::NameValue(lit.clone(), expr.span)),
574577
_ => None,
575578
},
576-
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
579+
MacArgs::Eq(_, MacArgsEq::Hir(lit, span)) => {
580+
Some(MetaItemKind::NameValue(lit.clone(), *span))
581+
}
577582
}
578583
}
579584

@@ -600,15 +605,15 @@ impl NestedMetaItem {
600605
pub fn span(&self) -> Span {
601606
match *self {
602607
NestedMetaItem::MetaItem(ref item) => item.span,
603-
NestedMetaItem::Literal(ref lit) => lit.span,
608+
NestedMetaItem::Literal(_, lit_span) => lit_span,
604609
}
605610
}
606611

607612
fn token_trees(&self) -> Vec<TokenTree> {
608613
match *self {
609614
NestedMetaItem::MetaItem(ref item) => item.token_trees(),
610-
NestedMetaItem::Literal(ref lit) => {
611-
vec![TokenTree::Token(lit.to_token(), Spacing::Alone)]
615+
NestedMetaItem::Literal(ref lit, lit_span) => {
616+
vec![TokenTree::Token(lit.to_token(lit_span), Spacing::Alone)]
612617
}
613618
}
614619
}
@@ -619,10 +624,10 @@ impl NestedMetaItem {
619624
{
620625
match tokens.peek() {
621626
Some(TokenTree::Token(token, _))
622-
if let Ok(lit) = Lit::from_token(token) =>
627+
if let Ok((lit, lit_span)) = Lit::from_token(token) =>
623628
{
624629
tokens.next();
625-
return Some(NestedMetaItem::Literal(lit));
630+
return Some(NestedMetaItem::Literal(lit, lit_span));
626631
}
627632
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
628633
let inner_tokens = inner_tokens.clone();

compiler/rustc_ast/src/mut_visit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pub fn visit_mac_args<T: MutVisitor>(args: &mut MacArgs, vis: &mut T) {
372372
vis.visit_span(eq_span);
373373
vis.visit_expr(expr);
374374
}
375-
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
375+
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
376376
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
377377
}
378378
}
@@ -617,7 +617,7 @@ pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T
617617
pub fn noop_visit_meta_list_item<T: MutVisitor>(li: &mut NestedMetaItem, vis: &mut T) {
618618
match li {
619619
NestedMetaItem::MetaItem(mi) => vis.visit_meta_item(mi),
620-
NestedMetaItem::Literal(_lit) => {}
620+
NestedMetaItem::Literal(_lit, _lit_span) => {}
621621
}
622622
}
623623

@@ -626,7 +626,7 @@ pub fn noop_visit_meta_item<T: MutVisitor>(mi: &mut MetaItem, vis: &mut T) {
626626
match kind {
627627
MetaItemKind::Word => {}
628628
MetaItemKind::List(mis) => visit_vec(mis, |mi| vis.visit_meta_list_item(mi)),
629-
MetaItemKind::NameValue(_s) => {}
629+
MetaItemKind::NameValue(_s, _) => {}
630630
}
631631
vis.visit_span(span);
632632
}

compiler/rustc_ast/src/util/literal.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ impl LitKind {
209209

210210
impl Lit {
211211
/// Converts literal token into an AST literal.
212-
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<Lit, LitError> {
213-
Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
212+
pub fn from_token_lit(token_lit: token::Lit) -> Result<Lit, LitError> {
213+
Ok(Lit { token_lit, kind: LitKind::from_token_lit(token_lit)? })
214214
}
215215

216216
/// Converts arbitrary token into an AST literal.
217217
///
218218
/// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation.
219-
pub fn from_token(token: &Token) -> Result<Lit, LitError> {
219+
pub fn from_token(token: &Token) -> Result<(Lit, Span), LitError> {
220220
let lit = match token.uninterpolate().kind {
221221
token::Ident(name, false) if name.is_bool_lit() => {
222222
token::Lit::new(token::Bool, name, None)
@@ -226,30 +226,30 @@ impl Lit {
226226
if let token::NtExpr(expr) | token::NtLiteral(expr) = &**nt
227227
&& let ast::ExprKind::Lit(lit) = &expr.kind
228228
{
229-
return Ok(lit.clone());
229+
return Ok((lit.clone(), expr.span));
230230
}
231231
return Err(LitError::NotLiteral);
232232
}
233233
_ => return Err(LitError::NotLiteral),
234234
};
235235

236-
Lit::from_token_lit(lit, token.span)
236+
Ok((Lit::from_token_lit(lit)?, token.span))
237237
}
238238

239239
/// Attempts to recover an AST literal from semantic literal.
240240
/// This function is used when the original token doesn't exist (e.g. the literal is created
241241
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
242-
pub fn from_lit_kind(kind: LitKind, span: Span) -> Lit {
243-
Lit { token_lit: kind.to_token_lit(), kind, span }
242+
pub fn from_lit_kind(kind: LitKind) -> Lit {
243+
Lit { token_lit: kind.to_token_lit(), kind }
244244
}
245245

246246
/// Losslessly convert an AST literal into a token.
247-
pub fn to_token(&self) -> Token {
247+
pub fn to_token(&self, span: Span) -> Token {
248248
let kind = match self.token_lit.kind {
249249
token::Bool => token::Ident(self.token_lit.symbol, false),
250250
_ => token::Literal(self.token_lit),
251251
};
252-
Token::new(kind, self.span)
252+
Token::new(kind, span)
253253
}
254254
}
255255

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ pub fn walk_mac_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a MacArgs) {
939939
MacArgs::Empty => {}
940940
MacArgs::Delimited(_dspan, _delim, _tokens) => {}
941941
MacArgs::Eq(_eq_span, MacArgsEq::Ast(expr)) => visitor.visit_expr(expr),
942-
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
942+
MacArgs::Eq(_, MacArgsEq::Hir(lit, _)) => {
943943
unreachable!("in literal form when walking mac args eq: {:?}", lit)
944944
}
945945
}

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
8585
hir::ExprKind::Unary(op, ohs)
8686
}
8787
ExprKind::Lit(ref l) => {
88-
hir::ExprKind::Lit(respan(self.lower_span(l.span), l.kind.clone()))
88+
hir::ExprKind::Lit(respan(self.lower_span(e.span), l.kind.clone()))
8989
}
9090
ExprKind::Cast(ref expr, ref ty) => {
9191
let expr = self.lower_expr(expr);

compiler/rustc_ast_lowering/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -928,12 +928,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
928928
Lit {
929929
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
930930
kind: LitKind::Err,
931-
span: DUMMY_SP,
932931
}
933932
};
934-
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit))
933+
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit, expr.span))
935934
}
936-
MacArgs::Eq(_, MacArgsEq::Hir(ref lit)) => {
935+
MacArgs::Eq(_, MacArgsEq::Hir(ref lit, _)) => {
937936
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
938937
}
939938
}

0 commit comments

Comments
 (0)