Skip to content

Commit ca9cf35

Browse files
committed
Auto merge of #43968 - petrochenkov:span2, r=michaelwoerister
Make fields of `Span` private I actually tried to intern spans and benchmark the result<sup>*</sup>, and this was a prerequisite. This kind of encapsulation will be a prerequisite for any other attempt to compress span's representation, so I decided to submit this change alone. The issue #43088 seems relevant, but it looks like `SpanId` won't be able to reuse this interface, unless the tables are global (like interner that I tried) and are not a part of HIR. r? @michaelwoerister anyway <sup>*</sup> Interning means 2-3 times more space is required for a single span, but duplicates are free. In practice it turned out that duplicates are not *that* common, so more memory was wasted by interning rather than saved.
2 parents c66e7fa + a0c3264 commit ca9cf35

File tree

60 files changed

+325
-346
lines changed

Some content is hidden

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

60 files changed

+325
-346
lines changed

src/libproc_macro/lib.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ impl FromStr for TokenStream {
8989
// notify the expansion info that it is unhygienic
9090
let mark = Mark::fresh(mark);
9191
mark.set_expn_info(expn_info);
92-
let span = syntax_pos::Span {
93-
ctxt: SyntaxContext::empty().apply_mark(mark),
94-
..call_site
95-
};
92+
let span = call_site.with_ctxt(SyntaxContext::empty().apply_mark(mark));
9693
let stream = parse::parse_stream_from_source_str(name, src, sess, Some(span));
9794
Ok(__internal::token_stream_wrap(stream))
9895
})
@@ -177,10 +174,10 @@ pub struct Span(syntax_pos::Span);
177174
#[unstable(feature = "proc_macro", issue = "38356")]
178175
impl Default for Span {
179176
fn default() -> Span {
180-
::__internal::with_sess(|(_, mark)| Span(syntax_pos::Span {
181-
ctxt: SyntaxContext::empty().apply_mark(mark),
182-
..mark.expn_info().unwrap().call_site
183-
}))
177+
::__internal::with_sess(|(_, mark)| {
178+
let call_site = mark.expn_info().unwrap().call_site;
179+
Span(call_site.with_ctxt(SyntaxContext::empty().apply_mark(mark)))
180+
})
184181
}
185182
}
186183

@@ -570,7 +567,7 @@ impl TokenTree {
570567
}).into();
571568
},
572569
TokenNode::Term(symbol) => {
573-
let ident = ast::Ident { name: symbol.0, ctxt: self.span.0.ctxt };
570+
let ident = ast::Ident { name: symbol.0, ctxt: self.span.0.ctxt() };
574571
let token =
575572
if symbol.0.as_str().starts_with("'") { Lifetime(ident) } else { Ident(ident) };
576573
return TokenTree::Token(self.span.0, token).into();

src/librustc/hir/lowering.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ impl<'a> LoweringContext<'a> {
425425
Symbol::gensym(s)
426426
}
427427

428-
fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, mut span: Span)
429-
-> Span
428+
fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, span: Span) -> Span
430429
{
431430
let mark = Mark::fresh(Mark::root());
432431
mark.set_expn_info(codemap::ExpnInfo {
@@ -438,8 +437,7 @@ impl<'a> LoweringContext<'a> {
438437
allow_internal_unsafe: false,
439438
},
440439
});
441-
span.ctxt = SyntaxContext::empty().apply_mark(mark);
442-
span
440+
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
443441
}
444442

445443
fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
@@ -613,7 +611,7 @@ impl<'a> LoweringContext<'a> {
613611
TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty)),
614612
TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt)),
615613
TyKind::Rptr(ref region, ref mt) => {
616-
let span = Span { hi: t.span.lo, ..t.span };
614+
let span = t.span.with_hi(t.span.lo());
617615
let lifetime = match *region {
618616
Some(ref lt) => self.lower_lifetime(lt),
619617
None => self.elided_lifetime(span)
@@ -1237,7 +1235,7 @@ impl<'a> LoweringContext<'a> {
12371235
name: self.lower_ident(match f.ident {
12381236
Some(ident) => ident,
12391237
// FIXME(jseyfried) positional field hygiene
1240-
None => Ident { name: Symbol::intern(&index.to_string()), ctxt: f.span.ctxt },
1238+
None => Ident { name: Symbol::intern(&index.to_string()), ctxt: f.span.ctxt() },
12411239
}),
12421240
vis: self.lower_visibility(&f.vis, None),
12431241
ty: self.lower_ty(&f.ty),

src/librustc/hir/print.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl<'a> State<'a> {
262262
indented: usize,
263263
close_box: bool)
264264
-> io::Result<()> {
265-
self.maybe_print_comment(span.hi)?;
265+
self.maybe_print_comment(span.hi())?;
266266
self.break_offset_if_not_bol(1, -(indented as isize))?;
267267
self.s.word("}")?;
268268
if close_box {
@@ -324,12 +324,12 @@ impl<'a> State<'a> {
324324
let len = elts.len();
325325
let mut i = 0;
326326
for elt in elts {
327-
self.maybe_print_comment(get_span(elt).hi)?;
327+
self.maybe_print_comment(get_span(elt).hi())?;
328328
op(self, elt)?;
329329
i += 1;
330330
if i < len {
331331
self.s.word(",")?;
332-
self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi))?;
332+
self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()))?;
333333
self.space_if_not_bol()?;
334334
}
335335
}
@@ -368,7 +368,7 @@ impl<'a> State<'a> {
368368
}
369369

370370
pub fn print_type(&mut self, ty: &hir::Ty) -> io::Result<()> {
371-
self.maybe_print_comment(ty.span.lo)?;
371+
self.maybe_print_comment(ty.span.lo())?;
372372
self.ibox(0)?;
373373
match ty.node {
374374
hir::TySlice(ref ty) => {
@@ -458,7 +458,7 @@ impl<'a> State<'a> {
458458

459459
pub fn print_foreign_item(&mut self, item: &hir::ForeignItem) -> io::Result<()> {
460460
self.hardbreak_if_not_bol()?;
461-
self.maybe_print_comment(item.span.lo)?;
461+
self.maybe_print_comment(item.span.lo())?;
462462
self.print_outer_attributes(&item.attrs)?;
463463
match item.node {
464464
hir::ForeignItemFn(ref decl, ref arg_names, ref generics) => {
@@ -531,7 +531,7 @@ impl<'a> State<'a> {
531531
/// Pretty-print an item
532532
pub fn print_item(&mut self, item: &hir::Item) -> io::Result<()> {
533533
self.hardbreak_if_not_bol()?;
534-
self.maybe_print_comment(item.span.lo)?;
534+
self.maybe_print_comment(item.span.lo())?;
535535
self.print_outer_attributes(&item.attrs)?;
536536
self.ann.pre(self, NodeItem(item))?;
537537
match item.node {
@@ -797,7 +797,7 @@ impl<'a> State<'a> {
797797
self.bopen()?;
798798
for v in variants {
799799
self.space_if_not_bol()?;
800-
self.maybe_print_comment(v.span.lo)?;
800+
self.maybe_print_comment(v.span.lo())?;
801801
self.print_outer_attributes(&v.node.attrs)?;
802802
self.ibox(indent_unit)?;
803803
self.print_variant(v)?;
@@ -842,7 +842,7 @@ impl<'a> State<'a> {
842842
if struct_def.is_tuple() {
843843
self.popen()?;
844844
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
845-
s.maybe_print_comment(field.span.lo)?;
845+
s.maybe_print_comment(field.span.lo())?;
846846
s.print_outer_attributes(&field.attrs)?;
847847
s.print_visibility(&field.vis)?;
848848
s.print_type(&field.ty)
@@ -863,7 +863,7 @@ impl<'a> State<'a> {
863863

864864
for field in struct_def.fields() {
865865
self.hardbreak_if_not_bol()?;
866-
self.maybe_print_comment(field.span.lo)?;
866+
self.maybe_print_comment(field.span.lo())?;
867867
self.print_outer_attributes(&field.attrs)?;
868868
self.print_visibility(&field.vis)?;
869869
self.print_name(field.name)?;
@@ -908,7 +908,7 @@ impl<'a> State<'a> {
908908
pub fn print_trait_item(&mut self, ti: &hir::TraitItem) -> io::Result<()> {
909909
self.ann.pre(self, NodeSubItem(ti.id))?;
910910
self.hardbreak_if_not_bol()?;
911-
self.maybe_print_comment(ti.span.lo)?;
911+
self.maybe_print_comment(ti.span.lo())?;
912912
self.print_outer_attributes(&ti.attrs)?;
913913
match ti.node {
914914
hir::TraitItemKind::Const(ref ty, default) => {
@@ -938,7 +938,7 @@ impl<'a> State<'a> {
938938
pub fn print_impl_item(&mut self, ii: &hir::ImplItem) -> io::Result<()> {
939939
self.ann.pre(self, NodeSubItem(ii.id))?;
940940
self.hardbreak_if_not_bol()?;
941-
self.maybe_print_comment(ii.span.lo)?;
941+
self.maybe_print_comment(ii.span.lo())?;
942942
self.print_outer_attributes(&ii.attrs)?;
943943
self.print_defaultness(ii.defaultness)?;
944944

@@ -962,7 +962,7 @@ impl<'a> State<'a> {
962962
}
963963

964964
pub fn print_stmt(&mut self, st: &hir::Stmt) -> io::Result<()> {
965-
self.maybe_print_comment(st.span.lo)?;
965+
self.maybe_print_comment(st.span.lo())?;
966966
match st.node {
967967
hir::StmtDecl(ref decl, _) => {
968968
self.print_decl(&decl)?;
@@ -1017,7 +1017,7 @@ impl<'a> State<'a> {
10171017
hir::PopUnsafeBlock(..) => self.word_space("pop_unsafe")?,
10181018
hir::DefaultBlock => (),
10191019
}
1020-
self.maybe_print_comment(blk.span.lo)?;
1020+
self.maybe_print_comment(blk.span.lo())?;
10211021
self.ann.pre(self, NodeBlock(blk))?;
10221022
self.bopen()?;
10231023

@@ -1030,7 +1030,7 @@ impl<'a> State<'a> {
10301030
Some(ref expr) => {
10311031
self.space_if_not_bol()?;
10321032
self.print_expr(&expr)?;
1033-
self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi))?;
1033+
self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()))?;
10341034
}
10351035
_ => (),
10361036
}
@@ -1228,7 +1228,7 @@ impl<'a> State<'a> {
12281228
}
12291229

12301230
pub fn print_expr(&mut self, expr: &hir::Expr) -> io::Result<()> {
1231-
self.maybe_print_comment(expr.span.lo)?;
1231+
self.maybe_print_comment(expr.span.lo())?;
12321232
self.print_outer_attributes(&expr.attrs)?;
12331233
self.ibox(indent_unit)?;
12341234
self.ann.pre(self, NodeExpr(expr))?;
@@ -1480,7 +1480,7 @@ impl<'a> State<'a> {
14801480
}
14811481

14821482
pub fn print_decl(&mut self, decl: &hir::Decl) -> io::Result<()> {
1483-
self.maybe_print_comment(decl.span.lo)?;
1483+
self.maybe_print_comment(decl.span.lo())?;
14841484
match decl.node {
14851485
hir::DeclLocal(ref loc) => {
14861486
self.space_if_not_bol()?;
@@ -1523,7 +1523,7 @@ impl<'a> State<'a> {
15231523
path: &hir::Path,
15241524
colons_before_params: bool)
15251525
-> io::Result<()> {
1526-
self.maybe_print_comment(path.span.lo)?;
1526+
self.maybe_print_comment(path.span.lo())?;
15271527

15281528
for (i, segment) in path.segments.iter().enumerate() {
15291529
if i > 0 {
@@ -1641,7 +1641,7 @@ impl<'a> State<'a> {
16411641
}
16421642

16431643
pub fn print_pat(&mut self, pat: &hir::Pat) -> io::Result<()> {
1644-
self.maybe_print_comment(pat.span.lo)?;
1644+
self.maybe_print_comment(pat.span.lo())?;
16451645
self.ann.pre(self, NodePat(pat))?;
16461646
// Pat isn't normalized, but the beauty of it
16471647
// is that it doesn't matter
@@ -1897,7 +1897,7 @@ impl<'a> State<'a> {
18971897
match decl.output {
18981898
hir::Return(ref ty) => {
18991899
self.print_type(&ty)?;
1900-
self.maybe_print_comment(ty.span.lo)
1900+
self.maybe_print_comment(ty.span.lo())
19011901
}
19021902
hir::DefaultReturn(..) => unreachable!(),
19031903
}
@@ -2074,7 +2074,7 @@ impl<'a> State<'a> {
20742074
self.end()?;
20752075

20762076
match decl.output {
2077-
hir::Return(ref output) => self.maybe_print_comment(output.span.lo),
2077+
hir::Return(ref output) => self.maybe_print_comment(output.span.lo()),
20782078
_ => Ok(()),
20792079
}
20802080
}
@@ -2124,13 +2124,13 @@ impl<'a> State<'a> {
21242124
if (*cmnt).style != comments::Trailing {
21252125
return Ok(());
21262126
}
2127-
let span_line = cm.lookup_char_pos(span.hi);
2127+
let span_line = cm.lookup_char_pos(span.hi());
21282128
let comment_line = cm.lookup_char_pos((*cmnt).pos);
21292129
let mut next = (*cmnt).pos + BytePos(1);
21302130
if let Some(p) = next_pos {
21312131
next = p;
21322132
}
2133-
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
2133+
if span.hi() < (*cmnt).pos && (*cmnt).pos < next &&
21342134
span_line.line == comment_line.line {
21352135
self.print_comment(cmnt)?;
21362136
}

src/librustc/ich/hcx.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,17 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
253253
// If this is not an empty or invalid span, we want to hash the last
254254
// position that belongs to it, as opposed to hashing the first
255255
// position past it.
256-
let span_hi = if self.hi > self.lo {
256+
let span_hi = if self.hi() > self.lo() {
257257
// We might end up in the middle of a multibyte character here,
258258
// but that's OK, since we are not trying to decode anything at
259259
// this position.
260-
self.hi - ::syntax_pos::BytePos(1)
260+
self.hi() - ::syntax_pos::BytePos(1)
261261
} else {
262-
self.hi
262+
self.hi()
263263
};
264264

265265
{
266-
let loc1 = hcx.codemap().byte_pos_to_line_and_col(self.lo);
266+
let loc1 = hcx.codemap().byte_pos_to_line_and_col(self.lo());
267267
let loc1 = loc1.as_ref()
268268
.map(|&(ref fm, line, col)| (&fm.name[..], line, col.to_usize()))
269269
.unwrap_or(("???", 0, 0));
@@ -296,7 +296,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
296296
}
297297
}
298298

299-
if self.ctxt == SyntaxContext::empty() {
299+
if self.ctxt() == SyntaxContext::empty() {
300300
0u8.hash_stable(hcx, hasher);
301301
} else {
302302
1u8.hash_stable(hcx, hasher);

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
118118
fn explain_span<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
119119
heading: &str, span: Span)
120120
-> (String, Option<Span>) {
121-
let lo = tcx.sess.codemap().lookup_char_pos_adj(span.lo);
121+
let lo = tcx.sess.codemap().lookup_char_pos_adj(span.lo());
122122
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1),
123123
Some(span))
124124
}

src/librustc/middle/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl CodeExtent {
180180
// (This is the special case aluded to in the
181181
// doc-comment for this method)
182182
let stmt_span = blk.stmts[r.first_statement_index as usize].span;
183-
Some(Span { lo: stmt_span.hi, hi: blk.span.hi, ctxt: stmt_span.ctxt })
183+
Some(Span::new(stmt_span.hi(), blk.span.hi(), stmt_span.ctxt()))
184184
}
185185
}
186186
}

src/librustc_allocator/expand.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
8282
allow_internal_unsafe: false,
8383
}
8484
});
85-
let span = Span {
86-
ctxt: SyntaxContext::empty().apply_mark(mark),
87-
..item.span
88-
};
85+
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
8986
let ecfg = ExpansionConfig::default(name.to_string());
9087
let mut f = AllocFnFactory {
9188
span,

src/librustc_errors/emitter.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ impl EmitterWriter {
183183
continue;
184184
}
185185

186-
let lo = cm.lookup_char_pos(span_label.span.lo);
187-
let mut hi = cm.lookup_char_pos(span_label.span.hi);
186+
let lo = cm.lookup_char_pos(span_label.span.lo());
187+
let mut hi = cm.lookup_char_pos(span_label.span.hi());
188188

189189
// Watch out for "empty spans". If we get a span like 6..6, we
190190
// want to just display a `^` at 6, so convert that to
@@ -683,15 +683,15 @@ impl EmitterWriter {
683683
if let Some(ref cm) = self.cm {
684684
for primary_span in msp.primary_spans() {
685685
if primary_span != &DUMMY_SP {
686-
let hi = cm.lookup_char_pos(primary_span.hi);
686+
let hi = cm.lookup_char_pos(primary_span.hi());
687687
if hi.line > max {
688688
max = hi.line;
689689
}
690690
}
691691
}
692692
for span_label in msp.span_labels() {
693693
if span_label.span != DUMMY_SP {
694-
let hi = cm.lookup_char_pos(span_label.span.hi);
694+
let hi = cm.lookup_char_pos(span_label.span.hi());
695695
if hi.line > max {
696696
max = hi.line;
697697
}
@@ -914,7 +914,7 @@ impl EmitterWriter {
914914
let (primary_lo, cm) = if let (Some(cm), Some(ref primary_span)) =
915915
(self.cm.as_ref(), msp.primary_span().as_ref()) {
916916
if primary_span != &&DUMMY_SP {
917-
(cm.lookup_char_pos(primary_span.lo), cm)
917+
(cm.lookup_char_pos(primary_span.lo()), cm)
918918
} else {
919919
emit_to_destination(&buffer.render(), level, &mut self.dst)?;
920920
return Ok(());
@@ -1091,7 +1091,7 @@ impl EmitterWriter {
10911091
Some(Style::HeaderMsg));
10921092

10931093
let suggestions = suggestion.splice_lines(cm.borrow());
1094-
let span_start_pos = cm.lookup_char_pos(primary_sub.span.lo);
1094+
let span_start_pos = cm.lookup_char_pos(primary_sub.span.lo());
10951095
let line_start = span_start_pos.line;
10961096
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
10971097
let mut row_num = 2;

0 commit comments

Comments
 (0)