Skip to content

Commit

Permalink
Auto merge of #43968 - petrochenkov:span2, r=michaelwoerister
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 30, 2017
2 parents c66e7fa + a0c3264 commit ca9cf35
Show file tree
Hide file tree
Showing 60 changed files with 325 additions and 346 deletions.
15 changes: 6 additions & 9 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ impl FromStr for TokenStream {
// notify the expansion info that it is unhygienic
let mark = Mark::fresh(mark);
mark.set_expn_info(expn_info);
let span = syntax_pos::Span {
ctxt: SyntaxContext::empty().apply_mark(mark),
..call_site
};
let span = call_site.with_ctxt(SyntaxContext::empty().apply_mark(mark));
let stream = parse::parse_stream_from_source_str(name, src, sess, Some(span));
Ok(__internal::token_stream_wrap(stream))
})
Expand Down Expand Up @@ -177,10 +174,10 @@ pub struct Span(syntax_pos::Span);
#[unstable(feature = "proc_macro", issue = "38356")]
impl Default for Span {
fn default() -> Span {
::__internal::with_sess(|(_, mark)| Span(syntax_pos::Span {
ctxt: SyntaxContext::empty().apply_mark(mark),
..mark.expn_info().unwrap().call_site
}))
::__internal::with_sess(|(_, mark)| {
let call_site = mark.expn_info().unwrap().call_site;
Span(call_site.with_ctxt(SyntaxContext::empty().apply_mark(mark)))
})
}
}

Expand Down Expand Up @@ -570,7 +567,7 @@ impl TokenTree {
}).into();
},
TokenNode::Term(symbol) => {
let ident = ast::Ident { name: symbol.0, ctxt: self.span.0.ctxt };
let ident = ast::Ident { name: symbol.0, ctxt: self.span.0.ctxt() };
let token =
if symbol.0.as_str().starts_with("'") { Lifetime(ident) } else { Ident(ident) };
return TokenTree::Token(self.span.0, token).into();
Expand Down
10 changes: 4 additions & 6 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@ impl<'a> LoweringContext<'a> {
Symbol::gensym(s)
}

fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, mut span: Span)
-> Span
fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, span: Span) -> Span
{
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(codemap::ExpnInfo {
Expand All @@ -438,8 +437,7 @@ impl<'a> LoweringContext<'a> {
allow_internal_unsafe: false,
},
});
span.ctxt = SyntaxContext::empty().apply_mark(mark);
span
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
}

fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
Expand Down Expand Up @@ -613,7 +611,7 @@ impl<'a> LoweringContext<'a> {
TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty)),
TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt)),
TyKind::Rptr(ref region, ref mt) => {
let span = Span { hi: t.span.lo, ..t.span };
let span = t.span.with_hi(t.span.lo());
let lifetime = match *region {
Some(ref lt) => self.lower_lifetime(lt),
None => self.elided_lifetime(span)
Expand Down Expand Up @@ -1237,7 +1235,7 @@ impl<'a> LoweringContext<'a> {
name: self.lower_ident(match f.ident {
Some(ident) => ident,
// FIXME(jseyfried) positional field hygiene
None => Ident { name: Symbol::intern(&index.to_string()), ctxt: f.span.ctxt },
None => Ident { name: Symbol::intern(&index.to_string()), ctxt: f.span.ctxt() },
}),
vis: self.lower_visibility(&f.vis, None),
ty: self.lower_ty(&f.ty),
Expand Down
44 changes: 22 additions & 22 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<'a> State<'a> {
indented: usize,
close_box: bool)
-> io::Result<()> {
self.maybe_print_comment(span.hi)?;
self.maybe_print_comment(span.hi())?;
self.break_offset_if_not_bol(1, -(indented as isize))?;
self.s.word("}")?;
if close_box {
Expand Down Expand Up @@ -324,12 +324,12 @@ impl<'a> State<'a> {
let len = elts.len();
let mut i = 0;
for elt in elts {
self.maybe_print_comment(get_span(elt).hi)?;
self.maybe_print_comment(get_span(elt).hi())?;
op(self, elt)?;
i += 1;
if i < len {
self.s.word(",")?;
self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi))?;
self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()))?;
self.space_if_not_bol()?;
}
}
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<'a> State<'a> {
}

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

pub fn print_foreign_item(&mut self, item: &hir::ForeignItem) -> io::Result<()> {
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(item.span.lo)?;
self.maybe_print_comment(item.span.lo())?;
self.print_outer_attributes(&item.attrs)?;
match item.node {
hir::ForeignItemFn(ref decl, ref arg_names, ref generics) => {
Expand Down Expand Up @@ -531,7 +531,7 @@ impl<'a> State<'a> {
/// Pretty-print an item
pub fn print_item(&mut self, item: &hir::Item) -> io::Result<()> {
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(item.span.lo)?;
self.maybe_print_comment(item.span.lo())?;
self.print_outer_attributes(&item.attrs)?;
self.ann.pre(self, NodeItem(item))?;
match item.node {
Expand Down Expand Up @@ -797,7 +797,7 @@ impl<'a> State<'a> {
self.bopen()?;
for v in variants {
self.space_if_not_bol()?;
self.maybe_print_comment(v.span.lo)?;
self.maybe_print_comment(v.span.lo())?;
self.print_outer_attributes(&v.node.attrs)?;
self.ibox(indent_unit)?;
self.print_variant(v)?;
Expand Down Expand Up @@ -842,7 +842,7 @@ impl<'a> State<'a> {
if struct_def.is_tuple() {
self.popen()?;
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
s.maybe_print_comment(field.span.lo)?;
s.maybe_print_comment(field.span.lo())?;
s.print_outer_attributes(&field.attrs)?;
s.print_visibility(&field.vis)?;
s.print_type(&field.ty)
Expand All @@ -863,7 +863,7 @@ impl<'a> State<'a> {

for field in struct_def.fields() {
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(field.span.lo)?;
self.maybe_print_comment(field.span.lo())?;
self.print_outer_attributes(&field.attrs)?;
self.print_visibility(&field.vis)?;
self.print_name(field.name)?;
Expand Down Expand Up @@ -908,7 +908,7 @@ impl<'a> State<'a> {
pub fn print_trait_item(&mut self, ti: &hir::TraitItem) -> io::Result<()> {
self.ann.pre(self, NodeSubItem(ti.id))?;
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(ti.span.lo)?;
self.maybe_print_comment(ti.span.lo())?;
self.print_outer_attributes(&ti.attrs)?;
match ti.node {
hir::TraitItemKind::Const(ref ty, default) => {
Expand Down Expand Up @@ -938,7 +938,7 @@ impl<'a> State<'a> {
pub fn print_impl_item(&mut self, ii: &hir::ImplItem) -> io::Result<()> {
self.ann.pre(self, NodeSubItem(ii.id))?;
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(ii.span.lo)?;
self.maybe_print_comment(ii.span.lo())?;
self.print_outer_attributes(&ii.attrs)?;
self.print_defaultness(ii.defaultness)?;

Expand All @@ -962,7 +962,7 @@ impl<'a> State<'a> {
}

pub fn print_stmt(&mut self, st: &hir::Stmt) -> io::Result<()> {
self.maybe_print_comment(st.span.lo)?;
self.maybe_print_comment(st.span.lo())?;
match st.node {
hir::StmtDecl(ref decl, _) => {
self.print_decl(&decl)?;
Expand Down Expand Up @@ -1017,7 +1017,7 @@ impl<'a> State<'a> {
hir::PopUnsafeBlock(..) => self.word_space("pop_unsafe")?,
hir::DefaultBlock => (),
}
self.maybe_print_comment(blk.span.lo)?;
self.maybe_print_comment(blk.span.lo())?;
self.ann.pre(self, NodeBlock(blk))?;
self.bopen()?;

Expand All @@ -1030,7 +1030,7 @@ impl<'a> State<'a> {
Some(ref expr) => {
self.space_if_not_bol()?;
self.print_expr(&expr)?;
self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi))?;
self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()))?;
}
_ => (),
}
Expand Down Expand Up @@ -1228,7 +1228,7 @@ impl<'a> State<'a> {
}

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

pub fn print_decl(&mut self, decl: &hir::Decl) -> io::Result<()> {
self.maybe_print_comment(decl.span.lo)?;
self.maybe_print_comment(decl.span.lo())?;
match decl.node {
hir::DeclLocal(ref loc) => {
self.space_if_not_bol()?;
Expand Down Expand Up @@ -1523,7 +1523,7 @@ impl<'a> State<'a> {
path: &hir::Path,
colons_before_params: bool)
-> io::Result<()> {
self.maybe_print_comment(path.span.lo)?;
self.maybe_print_comment(path.span.lo())?;

for (i, segment) in path.segments.iter().enumerate() {
if i > 0 {
Expand Down Expand Up @@ -1641,7 +1641,7 @@ impl<'a> State<'a> {
}

pub fn print_pat(&mut self, pat: &hir::Pat) -> io::Result<()> {
self.maybe_print_comment(pat.span.lo)?;
self.maybe_print_comment(pat.span.lo())?;
self.ann.pre(self, NodePat(pat))?;
// Pat isn't normalized, but the beauty of it
// is that it doesn't matter
Expand Down Expand Up @@ -1897,7 +1897,7 @@ impl<'a> State<'a> {
match decl.output {
hir::Return(ref ty) => {
self.print_type(&ty)?;
self.maybe_print_comment(ty.span.lo)
self.maybe_print_comment(ty.span.lo())
}
hir::DefaultReturn(..) => unreachable!(),
}
Expand Down Expand Up @@ -2074,7 +2074,7 @@ impl<'a> State<'a> {
self.end()?;

match decl.output {
hir::Return(ref output) => self.maybe_print_comment(output.span.lo),
hir::Return(ref output) => self.maybe_print_comment(output.span.lo()),
_ => Ok(()),
}
}
Expand Down Expand Up @@ -2124,13 +2124,13 @@ impl<'a> State<'a> {
if (*cmnt).style != comments::Trailing {
return Ok(());
}
let span_line = cm.lookup_char_pos(span.hi);
let span_line = cm.lookup_char_pos(span.hi());
let comment_line = cm.lookup_char_pos((*cmnt).pos);
let mut next = (*cmnt).pos + BytePos(1);
if let Some(p) = next_pos {
next = p;
}
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
if span.hi() < (*cmnt).pos && (*cmnt).pos < next &&
span_line.line == comment_line.line {
self.print_comment(cmnt)?;
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,17 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
// If this is not an empty or invalid span, we want to hash the last
// position that belongs to it, as opposed to hashing the first
// position past it.
let span_hi = if self.hi > self.lo {
let span_hi = if self.hi() > self.lo() {
// We might end up in the middle of a multibyte character here,
// but that's OK, since we are not trying to decode anything at
// this position.
self.hi - ::syntax_pos::BytePos(1)
self.hi() - ::syntax_pos::BytePos(1)
} else {
self.hi
self.hi()
};

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

if self.ctxt == SyntaxContext::empty() {
if self.ctxt() == SyntaxContext::empty() {
0u8.hash_stable(hcx, hasher);
} else {
1u8.hash_stable(hcx, hasher);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
fn explain_span<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
heading: &str, span: Span)
-> (String, Option<Span>) {
let lo = tcx.sess.codemap().lookup_char_pos_adj(span.lo);
let lo = tcx.sess.codemap().lookup_char_pos_adj(span.lo());
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1),
Some(span))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl CodeExtent {
// (This is the special case aluded to in the
// doc-comment for this method)
let stmt_span = blk.stmts[r.first_statement_index as usize].span;
Some(Span { lo: stmt_span.hi, hi: blk.span.hi, ctxt: stmt_span.ctxt })
Some(Span::new(stmt_span.hi(), blk.span.hi(), stmt_span.ctxt()))
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/librustc_allocator/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
allow_internal_unsafe: false,
}
});
let span = Span {
ctxt: SyntaxContext::empty().apply_mark(mark),
..item.span
};
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
let ecfg = ExpansionConfig::default(name.to_string());
let mut f = AllocFnFactory {
span,
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ impl EmitterWriter {
continue;
}

let lo = cm.lookup_char_pos(span_label.span.lo);
let mut hi = cm.lookup_char_pos(span_label.span.hi);
let lo = cm.lookup_char_pos(span_label.span.lo());
let mut hi = cm.lookup_char_pos(span_label.span.hi());

// Watch out for "empty spans". If we get a span like 6..6, we
// want to just display a `^` at 6, so convert that to
Expand Down Expand Up @@ -683,15 +683,15 @@ impl EmitterWriter {
if let Some(ref cm) = self.cm {
for primary_span in msp.primary_spans() {
if primary_span != &DUMMY_SP {
let hi = cm.lookup_char_pos(primary_span.hi);
let hi = cm.lookup_char_pos(primary_span.hi());
if hi.line > max {
max = hi.line;
}
}
}
for span_label in msp.span_labels() {
if span_label.span != DUMMY_SP {
let hi = cm.lookup_char_pos(span_label.span.hi);
let hi = cm.lookup_char_pos(span_label.span.hi());
if hi.line > max {
max = hi.line;
}
Expand Down Expand Up @@ -914,7 +914,7 @@ impl EmitterWriter {
let (primary_lo, cm) = if let (Some(cm), Some(ref primary_span)) =
(self.cm.as_ref(), msp.primary_span().as_ref()) {
if primary_span != &&DUMMY_SP {
(cm.lookup_char_pos(primary_span.lo), cm)
(cm.lookup_char_pos(primary_span.lo()), cm)
} else {
emit_to_destination(&buffer.render(), level, &mut self.dst)?;
return Ok(());
Expand Down Expand Up @@ -1091,7 +1091,7 @@ impl EmitterWriter {
Some(Style::HeaderMsg));

let suggestions = suggestion.splice_lines(cm.borrow());
let span_start_pos = cm.lookup_char_pos(primary_sub.span.lo);
let span_start_pos = cm.lookup_char_pos(primary_sub.span.lo());
let line_start = span_start_pos.line;
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
let mut row_num = 2;
Expand Down
Loading

0 comments on commit ca9cf35

Please sign in to comment.