Skip to content

Commit 865451e

Browse files
committed
Eliminate comments::Literal
1 parent 468ced6 commit 865451e

File tree

7 files changed

+61
-210
lines changed

7 files changed

+61
-210
lines changed

src/librustc/hir/print.rs

+7-38
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::ascii;
1919
use std::borrow::Cow;
2020
use std::cell::Cell;
2121
use std::io::{self, Write, Read};
22-
use std::iter::Peekable;
2322
use std::vec;
2423

2524
pub enum AnnNode<'a> {
@@ -77,7 +76,6 @@ pub struct State<'a> {
7776
pub s: pp::Printer<'a>,
7877
cm: Option<&'a SourceMap>,
7978
comments: Option<Vec<comments::Comment>>,
80-
literals: Peekable<vec::IntoIter<comments::Literal>>,
8179
cur_cmnt: usize,
8280
boxes: Vec<pp::Breaks>,
8381
ann: &'a (dyn PpAnn + 'a),
@@ -99,14 +97,6 @@ impl<'a> PrintState<'a> for State<'a> {
9997
fn cur_cmnt(&mut self) -> &mut usize {
10098
&mut self.cur_cmnt
10199
}
102-
103-
fn cur_lit(&mut self) -> Option<&comments::Literal> {
104-
self.literals.peek()
105-
}
106-
107-
fn bump_lit(&mut self) -> Option<comments::Literal> {
108-
self.literals.next()
109-
}
110100
}
111101

112102
#[allow(non_upper_case_globals)]
@@ -117,18 +107,16 @@ pub const default_columns: usize = 78;
117107

118108

119109
/// Requires you to pass an input filename and reader so that
120-
/// it can scan the input text for comments and literals to
121-
/// copy forward.
110+
/// it can scan the input text for comments to copy forward.
122111
pub fn print_crate<'a>(cm: &'a SourceMap,
123112
sess: &ParseSess,
124113
krate: &hir::Crate,
125114
filename: FileName,
126115
input: &mut dyn Read,
127116
out: Box<dyn Write + 'a>,
128-
ann: &'a dyn PpAnn,
129-
is_expanded: bool)
117+
ann: &'a dyn PpAnn)
130118
-> io::Result<()> {
131-
let mut s = State::new_from_input(cm, sess, filename, input, out, ann, is_expanded);
119+
let mut s = State::new_from_input(cm, sess, filename, input, out, ann);
132120

133121
// When printing the AST, we sometimes need to inject `#[no_std]` here.
134122
// Since you can't compile the HIR, it's not necessary.
@@ -144,36 +132,21 @@ impl<'a> State<'a> {
144132
filename: FileName,
145133
input: &mut dyn Read,
146134
out: Box<dyn Write + 'a>,
147-
ann: &'a dyn PpAnn,
148-
is_expanded: bool)
135+
ann: &'a dyn PpAnn)
149136
-> State<'a> {
150-
let (cmnts, lits) = comments::gather_comments_and_literals(sess, filename, input);
151-
152-
State::new(cm,
153-
out,
154-
ann,
155-
Some(cmnts),
156-
// If the code is post expansion, don't use the table of
157-
// literals, since it doesn't correspond with the literals
158-
// in the AST anymore.
159-
if is_expanded {
160-
None
161-
} else {
162-
Some(lits)
163-
})
137+
let comments = comments::gather_comments(sess, filename, input);
138+
State::new(cm, out, ann, Some(comments))
164139
}
165140

166141
pub fn new(cm: &'a SourceMap,
167142
out: Box<dyn Write + 'a>,
168143
ann: &'a dyn PpAnn,
169-
comments: Option<Vec<comments::Comment>>,
170-
literals: Option<Vec<comments::Literal>>)
144+
comments: Option<Vec<comments::Comment>>)
171145
-> State<'a> {
172146
State {
173147
s: pp::mk_printer(out, default_columns),
174148
cm: Some(cm),
175149
comments,
176-
literals: literals.unwrap_or_default().into_iter().peekable(),
177150
cur_cmnt: 0,
178151
boxes: Vec::new(),
179152
ann,
@@ -190,7 +163,6 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
190163
s: pp::mk_printer(Box::new(&mut wr), default_columns),
191164
cm: None,
192165
comments: None,
193-
literals: vec![].into_iter().peekable(),
194166
cur_cmnt: 0,
195167
boxes: Vec::new(),
196168
ann,
@@ -1338,9 +1310,6 @@ impl<'a> State<'a> {
13381310

13391311
fn print_literal(&mut self, lit: &hir::Lit) -> io::Result<()> {
13401312
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-
}
13441313
match lit.node {
13451314
hir::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
13461315
hir::LitKind::Err(st) => {

src/librustc_driver/pretty.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,7 @@ pub fn print_after_hir_lowering<'tcx>(
806806
src_name,
807807
&mut rdr,
808808
box out,
809-
annotation.pp_ann(),
810-
true)
809+
annotation.pp_ann())
811810
})
812811
}
813812

@@ -830,8 +829,7 @@ pub fn print_after_hir_lowering<'tcx>(
830829
src_name,
831830
&mut rdr,
832831
box out,
833-
annotation.pp_ann(),
834-
true);
832+
annotation.pp_ann());
835833
for node_id in uii.all_matching_node_ids(hir_map) {
836834
let node = hir_map.get(node_id);
837835
pp_state.print_node(node)?;

src/librustdoc/clean/cfg.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,9 @@ impl<'a> fmt::Display for Html<'a> {
414414
mod test {
415415
use super::Cfg;
416416

417-
use syntax::symbol::Symbol;
418-
use syntax::ast::*;
419-
use syntax::source_map::dummy_spanned;
420417
use syntax_pos::DUMMY_SP;
418+
use syntax::ast::*;
419+
use syntax::symbol::Symbol;
421420
use syntax::with_globals;
422421

423422
fn word_cfg(s: &str) -> Cfg {
@@ -592,12 +591,11 @@ mod test {
592591
let mi = dummy_meta_item_word("all");
593592
assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all")));
594593

594+
let node = LitKind::Str(Symbol::intern("done"), StrStyle::Cooked);
595+
let (token, suffix) = node.lit_token();
595596
let mi = MetaItem {
596597
path: Path::from_ident(Ident::from_str("all")),
597-
node: MetaItemKind::NameValue(dummy_spanned(LitKind::Str(
598-
Symbol::intern("done"),
599-
StrStyle::Cooked,
600-
))),
598+
node: MetaItemKind::NameValue(Lit { node, token, suffix, span: DUMMY_SP }),
601599
span: DUMMY_SP,
602600
};
603601
assert_eq!(Cfg::parse(&mi), Ok(name_value_cfg("all", "done")));
@@ -627,9 +625,11 @@ mod test {
627625
#[test]
628626
fn test_parse_err() {
629627
with_globals(|| {
628+
let node = LitKind::Bool(false);
629+
let (token, suffix) = node.lit_token();
630630
let mi = MetaItem {
631631
path: Path::from_ident(Ident::from_str("foo")),
632-
node: MetaItemKind::NameValue(dummy_spanned(LitKind::Bool(false))),
632+
node: MetaItemKind::NameValue(Lit { node, token, suffix, span: DUMMY_SP }),
633633
span: DUMMY_SP,
634634
};
635635
assert!(Cfg::parse(&mi).is_err());

src/libsyntax/attr/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,9 @@ impl MetaItemKind {
565565
Some(TokenTree::Token(_, token::Eq)) => {
566566
tokens.next();
567567
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
568-
LitKind::from_token(token)
569-
.map(|(node, token, suffix)| MetaItemKind::NameValue(Lit { node, token, suffix, span }))
568+
LitKind::from_token(token).map(|(node, token, suffix)| {
569+
MetaItemKind::NameValue(Lit { node, token, suffix, span })
570+
})
570571
} else {
571572
None
572573
};
@@ -635,7 +636,7 @@ impl LitKind {
635636
}
636637
}
637638

638-
pub(crate) fn lit_token(&self) -> (token::Lit, Option<Symbol>) {
639+
pub fn lit_token(&self) -> (token::Lit, Option<Symbol>) {
639640
use std::ascii;
640641

641642
match *self {

src/libsyntax/parse/lexer/comments.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ pub use CommentStyle::*;
33
use crate::ast;
44
use crate::source_map::SourceMap;
55
use crate::parse::lexer::{is_block_doc_comment, is_pattern_whitespace};
6-
use crate::parse::lexer::{self, ParseSess, StringReader, TokenAndSpan};
7-
use crate::print::pprust;
6+
use crate::parse::lexer::{self, ParseSess, StringReader};
87

98
use syntax_pos::{BytePos, CharPos, Pos, FileName};
109
use log::debug;
@@ -339,16 +338,9 @@ fn consume_comment(rdr: &mut StringReader<'_>,
339338
debug!("<<< consume comment");
340339
}
341340

342-
#[derive(Clone)]
343-
pub struct Literal {
344-
pub lit: String,
345-
pub pos: BytePos,
346-
}
347-
348341
// it appears this function is called only from pprust... that's
349342
// probably not a good thing.
350-
pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut dyn Read)
351-
-> (Vec<Comment>, Vec<Literal>)
343+
pub fn gather_comments(sess: &ParseSess, path: FileName, srdr: &mut dyn Read) -> Vec<Comment>
352344
{
353345
let mut src = String::new();
354346
srdr.read_to_string(&mut src).unwrap();
@@ -357,7 +349,6 @@ pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut
357349
let mut rdr = lexer::StringReader::new_raw(sess, source_file, None);
358350

359351
let mut comments: Vec<Comment> = Vec::new();
360-
let mut literals: Vec<Literal> = Vec::new();
361352
let mut code_to_the_left = false; // Only code
362353
let mut anything_to_the_left = false; // Code or comments
363354

@@ -382,26 +373,12 @@ pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut
382373
}
383374
}
384375

385-
let bstart = rdr.pos;
386376
rdr.next_token();
387-
// discard, and look ahead; we're working with internal state
388-
let TokenAndSpan { tok, sp } = rdr.peek();
389-
if tok.is_lit() {
390-
rdr.with_str_from(bstart, |s| {
391-
debug!("tok lit: {}", s);
392-
literals.push(Literal {
393-
lit: s.to_string(),
394-
pos: sp.lo(),
395-
});
396-
})
397-
} else {
398-
debug!("tok: {}", pprust::token_to_string(&tok));
399-
}
400377
code_to_the_left = true;
401378
anything_to_the_left = true;
402379
}
403380

404-
(comments, literals)
381+
comments
405382
}
406383

407384
#[cfg(test)]

src/libsyntax/parse/parser.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2116,11 +2116,11 @@ impl<'a> Parser<'a> {
21162116
Applicability::MachineApplicable,
21172117
);
21182118
err.emit();
2119-
return Ok(match float_suffix {
2120-
"f32" => (ast::LitKind::Float(val, ast::FloatTy::F32), token::Float(val), suffix),
2121-
"f64" => (ast::LitKind::Float(val, ast::FloatTy::F64), token::Float(val), suffix),
2122-
_ => (ast::LitKind::FloatUnsuffixed(val), token::Float(val), suffix),
2123-
});
2119+
return Ok((match float_suffix {
2120+
"f32" => ast::LitKind::Float(val, ast::FloatTy::F32),
2121+
"f64" => ast::LitKind::Float(val, ast::FloatTy::F64),
2122+
_ => ast::LitKind::FloatUnsuffixed(val),
2123+
}, token::Float(val), suffix));
21242124
} else {
21252125
unreachable!();
21262126
};

0 commit comments

Comments
 (0)