Skip to content

Commit 88130f1

Browse files
author
Matthew Russo
committed
updates all Filename variants to take a fingerprint
1 parent 6ee4d3c commit 88130f1

File tree

12 files changed

+93
-51
lines changed

12 files changed

+93
-51
lines changed

src/librustc/ich/impls_syntax.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind {
417417
impl_stable_hash_for!(enum ::syntax_pos::FileName {
418418
Real(pb),
419419
Macros(s),
420-
QuoteExpansion,
421-
Anon,
422-
MacroExpansion,
423-
ProcMacroSourceCode,
424-
CliCrateAttr,
425-
CfgSpec,
420+
QuoteExpansion(s),
421+
Anon(s),
422+
MacroExpansion(s),
423+
ProcMacroSourceCode(s),
424+
CliCrateAttr(s),
425+
CfgSpec(s),
426426
Custom(s)
427427
});
428428

src/librustc/session/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1756,8 +1756,8 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
17561756
.into_iter()
17571757
.map(|s| {
17581758
let sess = parse::ParseSess::new(FilePathMapping::empty());
1759-
let mut parser =
1760-
parse::new_parser_from_source_str(&sess, FileName::CfgSpec, s.to_string());
1759+
let filename = FileName::cfg_spec_source_code(&s);
1760+
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
17611761

17621762
macro_rules! error {($reason: expr) => {
17631763
early_error(ErrorOutputType::default(),

src/librustc_driver/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option
594594
} else {
595595
None
596596
};
597-
Some((Input::Str { name: FileName::Anon, input: src },
597+
Some((Input::Str { name: FileName::anon_source_code(&src), input: src },
598598
None, err))
599599
} else {
600600
Some((Input::File(PathBuf::from(ifile)),

src/librustc_driver/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn test_env_with_pool<F>(
129129
let cstore = CStore::new(::get_codegen_backend(&sess).metadata_loader());
130130
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
131131
let input = config::Input::Str {
132-
name: FileName::Anon,
132+
name: FileName::anon_source_code(&source_string),
133133
input: source_string.to_string(),
134134
};
135135
let krate =

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3008,7 +3008,7 @@ pub struct Span {
30083008
impl Span {
30093009
pub fn empty() -> Span {
30103010
Span {
3011-
filename: FileName::Anon,
3011+
filename: FileName::Anon(0),
30123012
loline: 0, locol: 0,
30133013
hiline: 0, hicol: 0,
30143014
}

src/libsyntax/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
803803
for raw_attr in attrs {
804804
let mut parser = parse::new_parser_from_source_str(
805805
parse_sess,
806-
FileName::CliCrateAttr,
806+
FileName::cli_crate_attr_source_code(&raw_attr),
807807
raw_attr.clone(),
808808
);
809809

src/libsyntax/ext/quote.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -353,27 +353,27 @@ pub mod rt {
353353
impl<'a> ExtParseUtils for ExtCtxt<'a> {
354354
fn parse_item(&self, s: String) -> P<ast::Item> {
355355
panictry!(parse::parse_item_from_source_str(
356-
FileName::QuoteExpansion,
356+
FileName::quote_expansion_source_code(&s),
357357
s,
358358
self.parse_sess())).expect("parse error")
359359
}
360360

361361
fn parse_stmt(&self, s: String) -> ast::Stmt {
362362
panictry!(parse::parse_stmt_from_source_str(
363-
FileName::QuoteExpansion,
363+
FileName::quote_expansion_source_code(&s),
364364
s,
365365
self.parse_sess())).expect("parse error")
366366
}
367367

368368
fn parse_expr(&self, s: String) -> P<ast::Expr> {
369369
panictry!(parse::parse_expr_from_source_str(
370-
FileName::QuoteExpansion,
370+
FileName::quote_expansion_source_code(&s),
371371
s,
372372
self.parse_sess()))
373373
}
374374

375375
fn parse_tts(&self, s: String) -> Vec<TokenTree> {
376-
let source_name = FileName::QuoteExpansion;
376+
let source_name = FileName::quote_expansion_source_code(&s);
377377
parse::parse_stream_from_source_str(source_name, s, self.parse_sess(), None)
378378
.into_trees().collect()
379379
}

src/libsyntax/parse/token.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ impl Token {
545545
let tokens_for_real = nt.1.force(|| {
546546
// FIXME(#43081): Avoid this pretty-print + reparse hack
547547
let source = pprust::token_to_string(self);
548-
parse_stream_from_source_str(FileName::MacroExpansion, source, sess, Some(span))
548+
let filename = FileName::macro_expansion_source_code(&source);
549+
parse_stream_from_source_str(filename, source, sess, Some(span))
549550
});
550551

551552
// During early phases of the compiler the AST could get modified
@@ -781,10 +782,12 @@ fn prepend_attrs(sess: &ParseSess,
781782
assert_eq!(attr.style, ast::AttrStyle::Outer,
782783
"inner attributes should prevent cached tokens from existing");
783784

785+
let source = pprust::attr_to_string(attr);
786+
let macro_filename = FileName::macro_expansion_source_code(&source);
784787
if attr.is_sugared_doc {
785788
let stream = parse_stream_from_source_str(
786-
FileName::MacroExpansion,
787-
pprust::attr_to_string(attr),
789+
macro_filename,
790+
source,
788791
sess,
789792
Some(span),
790793
);
@@ -805,8 +808,8 @@ fn prepend_attrs(sess: &ParseSess,
805808
// should eventually be removed.
806809
} else {
807810
let stream = parse_stream_from_source_str(
808-
FileName::MacroExpansion,
809-
pprust::path_to_string(&attr.path),
811+
macro_filename,
812+
source,
810813
sess,
811814
Some(span),
812815
);

src/libsyntax/source_map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ pub struct StableSourceFileId(u128);
110110

111111
impl StableSourceFileId {
112112
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
113-
StableFilemapId::new_from_pieces(&source_file.name,
113+
StableSourceFileId::new_from_pieces(&source_file.name,
114114
source_file.name_was_remapped,
115115
source_file.unmapped_path.as_ref())
116116
}
117117

118118
pub fn new_from_pieces(name: &FileName,
119119
name_was_remapped: bool,
120-
unmapped_path: Option<&FileName>) -> StableFilemapId {
120+
unmapped_path: Option<&FileName>) -> StableSourceFileId {
121121
let mut hasher = StableHasher::new();
122122

123123
name.hash(&mut hasher);
@@ -236,7 +236,7 @@ impl SourceMap {
236236
other => (other, false),
237237
};
238238

239-
let file_id = StableFilemapId::new_from_pieces(&filename,
239+
let file_id = StableSourceFileId::new_from_pieces(&filename,
240240
was_remapped,
241241
Some(&unmapped_path));
242242

src/libsyntax_pos/lib.rs

+61-24
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ pub enum FileName {
9090
/// A macro. This includes the full name of the macro, so that there are no clashes.
9191
Macros(String),
9292
/// call to `quote!`
93-
QuoteExpansion,
93+
QuoteExpansion(u64),
9494
/// Command line
95-
Anon,
95+
Anon(u64),
9696
/// Hack in src/libsyntax/parse.rs
9797
/// FIXME(jseyfried)
98-
MacroExpansion,
99-
ProcMacroSourceCode,
98+
MacroExpansion(u64),
99+
ProcMacroSourceCode(u64),
100100
/// Strings provided as --cfg [cfgspec] stored in a crate_cfg
101-
CfgSpec,
101+
CfgSpec(u64),
102102
/// Strings provided as crate attributes in the CLI
103-
CliCrateAttr,
103+
CliCrateAttr(u64),
104104
/// Custom sources for explicit parser calls from plugins and drivers
105105
Custom(String),
106106
}
@@ -111,12 +111,13 @@ impl std::fmt::Display for FileName {
111111
match *self {
112112
Real(ref path) => write!(fmt, "{}", path.display()),
113113
Macros(ref name) => write!(fmt, "<{} macros>", name),
114-
QuoteExpansion => write!(fmt, "<quote expansion>"),
115-
MacroExpansion => write!(fmt, "<macro expansion>"),
116-
Anon => write!(fmt, "<anon>"),
117-
ProcMacroSourceCode => write!(fmt, "<proc-macro source code>"),
118-
CfgSpec => write!(fmt, "cfgspec"),
119-
CliCrateAttr => write!(fmt, "<crate attribute>"),
114+
QuoteExpansion(_) => write!(fmt, "<quote expansion>"),
115+
MacroExpansion(_) => write!(fmt, "<macro expansion>"),
116+
Anon(_) => write!(fmt, "<anon>"),
117+
ProcMacroSourceCode(_) =>
118+
write!(fmt, "<proc-macro source code>"),
119+
CfgSpec(_) => write!(fmt, "<cfgspec>"),
120+
CliCrateAttr(_) => write!(fmt, "<crate attribute>"),
120121
Custom(ref s) => write!(fmt, "<{}>", s),
121122
}
122123
}
@@ -135,30 +136,66 @@ impl FileName {
135136
match *self {
136137
Real(_) => true,
137138
Macros(_) |
138-
Anon |
139-
MacroExpansion |
140-
ProcMacroSourceCode |
141-
CfgSpec |
142-
CliCrateAttr |
139+
Anon(_) |
140+
MacroExpansion(_) |
141+
ProcMacroSourceCode(_) |
142+
CfgSpec(_) |
143+
CliCrateAttr(_) |
143144
Custom(_) |
144-
QuoteExpansion => false,
145+
QuoteExpansion(_) => false,
145146
}
146147
}
147148

148149
pub fn is_macros(&self) -> bool {
149150
use self::FileName::*;
150151
match *self {
151152
Real(_) |
152-
Anon |
153-
MacroExpansion |
154-
ProcMacroSourceCode |
155-
CfgSpec |
156-
CliCrateAttr |
153+
Anon(_) |
154+
MacroExpansion(_) |
155+
ProcMacroSourceCode(_) |
156+
CfgSpec(_) |
157+
CliCrateAttr(_) |
157158
Custom(_) |
158-
QuoteExpansion => false,
159+
QuoteExpansion(_) => false,
159160
Macros(_) => true,
160161
}
161162
}
163+
164+
pub fn quote_expansion_source_code(src: &str) -> FileName {
165+
let mut hasher = StableHasher::new();
166+
src.hash(&mut hasher);
167+
FileName::QuoteExpansion(hasher.finish())
168+
}
169+
170+
pub fn macro_expansion_source_code(src: &str) -> FileName {
171+
let mut hasher = StableHasher::new();
172+
src.hash(&mut hasher);
173+
FileName::MacroExpansion(hasher.finish())
174+
}
175+
176+
pub fn anon_source_code(src: &str) -> FileName {
177+
let mut hasher = StableHasher::new();
178+
src.hash(&mut hasher);
179+
FileName::Anon(hasher.finish())
180+
}
181+
182+
pub fn proc_macro_source_code(src: &str) -> FileName {
183+
let mut hasher = StableHasher::new();
184+
src.hash(&mut hasher);
185+
FileName::ProcMacroSourceCode(hasher.finish())
186+
}
187+
188+
pub fn cfg_spec_source_code(src: &str) -> FileName {
189+
let mut hasher = StableHasher::new();
190+
src.hash(&mut hasher);
191+
FileName::QuoteExpansion(hasher.finish())
192+
}
193+
194+
pub fn cli_crate_attr_source_code(src: &str) -> FileName {
195+
let mut hasher = StableHasher::new();
196+
src.hash(&mut hasher);
197+
FileName::CliCrateAttr(hasher.finish())
198+
}
162199
}
163200

164201
/// Spans represent a region of code, used for error reporting. Positions in spans

src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::fmt;
3333
// Copied out of syntax::util::parser_testing
3434

3535
pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
36-
new_parser_from_source_str(ps, FileName::Custom("bogofile".to_owned()), source_str)
36+
new_parser_from_source_str(ps, FileName::Custom(source_str.clone()), source_str)
3737
}
3838

3939
fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> PResult<'a, T> where

src/test/run-pass-fulldeps/pprust-expr-roundtrip.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ use syntax::ptr::P;
4444

4545

4646
fn parse_expr(ps: &ParseSess, src: &str) -> P<Expr> {
47+
let src_as_string = src.to_string();
48+
4749
let mut p = parse::new_parser_from_source_str(ps,
48-
FileName::Custom("expr".to_owned()),
49-
src.to_owned());
50+
FileName::Custom(src_as_string.clone()),
51+
src_as_string);
5052
p.parse_expr().unwrap()
5153
}
5254

0 commit comments

Comments
 (0)