// 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))
})
#[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)))
+ })
}
}
}).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();
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 {
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
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)
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),
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 {
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()?;
}
}
}
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) => {
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) => {
/// 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 {
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)?;
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)
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)?;
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) => {
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)?;
}
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)?;
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()?;
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()))?;
}
_ => (),
}
}
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))?;
}
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()?;
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 {
}
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
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!(),
}
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(()),
}
}
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)?;
}
// 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));
}
}
- if self.ctxt == SyntaxContext::empty() {
+ if self.ctxt() == SyntaxContext::empty() {
0u8.hash_stable(hcx, hasher);
} else {
1u8.hash_stable(hcx, hasher);
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))
}
// (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()))
}
}
}
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,
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
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;
}
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(());
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;
// Assumption: all spans are in the same file, and all spans
// are disjoint. Sort in ascending order.
- primary_spans.sort_by_key(|sp| sp.0.lo);
+ primary_spans.sort_by_key(|sp| sp.0.lo());
// Find the bounding span.
- let lo = primary_spans.iter().map(|sp| sp.0.lo).min().unwrap();
- let hi = primary_spans.iter().map(|sp| sp.0.hi).min().unwrap();
- let bounding_span = Span {
- lo,
- hi,
- ctxt: NO_EXPANSION,
- };
+ let lo = primary_spans.iter().map(|sp| sp.0.lo()).min().unwrap();
+ let hi = primary_spans.iter().map(|sp| sp.0.hi()).min().unwrap();
+ let bounding_span = Span::new(lo, hi, NO_EXPANSION);
let lines = cm.span_to_lines(bounding_span).unwrap();
assert!(!lines.lines.is_empty());
//
// Finally push the trailing line segment of the last span
let fm = &lines.file;
- let mut prev_hi = cm.lookup_char_pos(bounding_span.lo);
+ let mut prev_hi = cm.lookup_char_pos(bounding_span.lo());
prev_hi.col = CharPos::from_usize(0);
let mut prev_line = fm.get_line(lines.lines[0].line_index);
let mut bufs = vec![(String::new(), false); self.substitutions()];
for (sp, substitutes) in primary_spans {
- let cur_lo = cm.lookup_char_pos(sp.lo);
+ let cur_lo = cm.lookup_char_pos(sp.lo());
for (&mut (ref mut buf, ref mut underline), substitute) in bufs.iter_mut()
.zip(substitutes) {
if prev_hi.line == cur_lo.line {
}
buf.push_str(substitute);
}
- prev_hi = cm.lookup_char_pos(sp.hi);
+ prev_hi = cm.lookup_char_pos(sp.hi());
prev_line = fm.get_line(prev_hi.line - 1);
}
for &mut (ref mut buf, _) in &mut bufs {
let source_name = format!("<{} macros>", name);
let filemap = sess.parse_sess.codemap().new_filemap(source_name, def.body);
- let local_span = Span { lo: filemap.start_pos, hi: filemap.end_pos, ctxt: NO_EXPANSION };
+ let local_span = Span::new(filemap.start_pos, filemap.end_pos, NO_EXPANSION);
let body = filemap_to_stream(&sess.parse_sess, filemap, None);
// Mark the attrs as used
let sess = if let Some(sess) = self.sess {
sess
} else {
- return Ok(Span { lo: lo, hi: hi, ctxt: NO_EXPANSION });
+ return Ok(Span::new(lo, hi, NO_EXPANSION));
};
let (lo, hi) = if lo > hi {
let lo = (lo - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
let hi = (hi - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
- Ok(Span { lo: lo, hi: hi, ctxt: NO_EXPANSION })
+ Ok(Span::new(lo, hi, NO_EXPANSION))
}
}
builder.args_and_body(block, &arguments, arg_extent, &body.value)
}));
// Attribute epilogue to function's closing brace
- let fn_end = Span { lo: span.hi, ..span };
+ let fn_end = span.with_lo(span.hi());
let source_info = builder.source_info(fn_end);
let return_block = builder.return_block();
builder.cfg.terminate(block, source_info,
let tcx = self.hir.tcx();
let extent_span = extent.span(&tcx.hir).unwrap();
// Attribute scope exit drops to scope's closing brace
- let scope_end = Span { lo: extent_span.hi, .. extent_span};
+ let scope_end = extent_span.with_lo(extent_span.hi());
scope.drops.push(DropData {
span: scope_end,
location: lvalue.clone(),
impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
// Checks that a field is accessible.
fn check_field(&mut self, span: Span, def: &'tcx ty::AdtDef, field: &'tcx ty::FieldDef) {
- let ident = Ident { ctxt: span.ctxt.modern(), ..keywords::Invalid.ident() };
+ let ident = Ident { ctxt: span.ctxt().modern(), ..keywords::Invalid.ident() };
let def_id = self.tcx.adjust_ident(ident, def.did, self.current_item).1;
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
// don't suggest placing a use before the prelude
// import or other generated ones
if item.span == DUMMY_SP {
- let mut span = item.span;
- span.hi = span.lo;
- self.span = Some(span);
+ self.span = Some(item.span.with_hi(item.span.lo()));
self.found_use = true;
return;
}
ItemKind::ExternCrate(_) => {}
// but place them before the first other item
_ => if self.span.map_or(true, |span| item.span < span ) {
- let mut span = item.span;
- span.hi = span.lo;
- self.span = Some(span);
+ self.span = Some(item.span.with_hi(item.span.lo()));
},
}
}
fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
let mut module = self.get_module(module.normal_ancestor_id);
- while module.span.ctxt.modern() != *ctxt {
+ while module.span.ctxt().modern() != *ctxt {
let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
module = self.get_module(parent.normal_ancestor_id);
}
sp = sp.next_point();
if let Ok(snippet) = cm.span_to_snippet(sp.to(sp.next_point())) {
debug!("snippet {:?}", snippet);
- let line_sp = cm.lookup_char_pos(sp.hi).line;
- let line_base_sp = cm.lookup_char_pos(base_span.lo).line;
+ let line_sp = cm.lookup_char_pos(sp.hi()).line;
+ let line_base_sp = cm.lookup_char_pos(base_span.lo()).line;
debug!("{:?} {:?}", line_sp, line_base_sp);
if snippet == ":" {
err.span_label(base_span,
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
let module = binding.module().unwrap();
let mut ident = ident;
- if ident.ctxt.glob_adjust(module.expansion, binding.span.ctxt.modern()).is_none() {
+ if ident.ctxt.glob_adjust(module.expansion, binding.span.ctxt().modern()).is_none() {
continue
}
if self.resolve_ident_in_module_unadjusted(module, ident, ns, false, false, module.span)
new_binding: &NameBinding,
old_binding: &NameBinding) {
// Error on the second of two conflicting names
- if old_binding.span.lo > new_binding.span.lo {
+ if old_binding.span.lo() > new_binding.span.lo() {
return self.report_conflict(parent, ident, ns, old_binding, new_binding);
}
}
let module = unwrap_or!(directive.imported_module.get(), return Err(Undetermined));
let (orig_current_module, mut ident) = (self.current_module, ident.modern());
- match ident.ctxt.glob_adjust(module.expansion, directive.span.ctxt.modern()) {
+ match ident.ctxt.glob_adjust(module.expansion, directive.span.ctxt().modern()) {
Some(Some(def)) => self.current_module = self.macro_def_scope(def),
Some(None) => {}
None => continue,
for directive in module.glob_importers.borrow_mut().iter() {
let mut ident = ident.modern();
let scope = match ident.ctxt.reverse_glob_adjust(module.expansion,
- directive.span.ctxt.modern()) {
+ directive.span.ctxt().modern()) {
Some(Some(def)) => self.macro_def_scope(def),
Some(None) => directive.parent,
None => continue,
}).collect::<Vec<_>>();
for ((mut ident, ns), binding) in bindings {
let scope = match ident.ctxt.reverse_glob_adjust(module.expansion,
- directive.span.ctxt.modern()) {
+ directive.span.ctxt().modern()) {
Some(Some(def)) => self.macro_def_scope(def),
Some(None) => self.current_module,
None => continue,
use rls_span::{Row, Column};
let cm = self.tcx.sess.codemap();
- let start = cm.lookup_char_pos(span.lo);
- let end = cm.lookup_char_pos(span.hi);
+ let start = cm.lookup_char_pos(span.lo());
+ let end = cm.lookup_char_pos(span.hi());
SpanData {
file_name: start.file.name.clone().into(),
- byte_start: span.lo.0,
- byte_end: span.hi.0,
+ byte_start: span.lo().0,
+ byte_end: span.hi().0,
line_start: Row::new_one_indexed(start.line as u32),
line_end: Row::new_one_indexed(end.line as u32),
column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
continue;
}
};
- let lo_loc = self.span_utils.sess.codemap().lookup_char_pos(span.lo);
+ let lo_loc = self.span_utils.sess.codemap().lookup_char_pos(span.lo());
result.push(ExternalCrateData {
name: self.tcx.sess.cstore.crate_name(n).to_string(),
num: n.as_u32(),
// Helper function to determine if a span came from a
// macro expansion or syntax extension.
fn generated_code(span: Span) -> bool {
- span.ctxt != NO_EXPANSION || span == DUMMY_SP
+ span.ctxt() != NO_EXPANSION || span == DUMMY_SP
}
// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
prev = next;
}
if angle_count != 0 || bracket_count != 0 {
- let loc = self.sess.codemap().lookup_char_pos(span.lo);
+ let loc = self.sess.codemap().lookup_char_pos(span.lo());
span_bug!(span,
"Mis-counted brackets when breaking path? Parsing '{}' \
in {}, line {}",
};
//If the span comes from a fake filemap, filter it.
- if !self.sess.codemap().lookup_char_pos(parent.lo).file.is_real_file() {
+ if !self.sess.codemap().lookup_char_pos(parent.lo()).file.is_real_file() {
return true;
}
// Deterministically select one of the spans for error reporting
let span = match (span1, span2) {
(Some(span1), Some(span2)) => {
- Some(if span1.lo.0 > span2.lo.0 {
+ Some(if span1.lo().0 > span2.lo().0 {
span1
} else {
span2
/// Return syntax_pos::Loc corresponding to the beginning of the span
pub fn span_start(cx: &CrateContext, span: Span) -> syntax_pos::Loc {
- cx.sess().codemap().lookup_char_pos(span.lo)
+ cx.sess().codemap().lookup_char_pos(span.lo())
}
pub fn size_and_align_of(cx: &CrateContext, llvm_type: Type) -> (u64, u32) {
self.set_debug_loc(&bcx, terminator.source_info);
// Get the location information.
- let loc = bcx.sess().codemap().lookup_char_pos(span.lo);
+ let loc = bcx.sess().codemap().lookup_char_pos(span.lo());
let filename = Symbol::intern(&loc.file.name).as_str();
let filename = C_str_slice(bcx.ccx, filename);
let line = C_u32(bcx.ccx, loc.line as u32);
// In order to have a good line stepping behavior in debugger, we overwrite debug
// locations of macro expansions with that of the outermost expansion site
// (unless the crate is being compiled with `-Z debug-macros`).
- if source_info.span.ctxt == NO_EXPANSION ||
+ if source_info.span.ctxt() == NO_EXPANSION ||
self.ccx.sess().opts.debugging_opts.debug_macros {
- let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo);
+ let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo());
(scope, source_info.span)
} else {
// Walk up the macro expansion chain until we reach a non-expanded span.
// We also stop at the function body level because no line stepping can occur
// at the level above that.
let mut span = source_info.span;
- while span.ctxt != NO_EXPANSION && span.ctxt != self.mir.span.ctxt {
- if let Some(info) = span.ctxt.outer().expn_info() {
+ while span.ctxt() != NO_EXPANSION && span.ctxt() != self.mir.span.ctxt() {
+ if let Some(info) = span.ctxt().outer().expn_info() {
span = info.call_site;
} else {
break;
}
}
- let scope = self.scope_metadata_for_loc(source_info.scope, span.lo);
+ let scope = self.scope_metadata_for_loc(source_info.scope, span.lo());
// Use span of the outermost expansion site, while keeping the original lexical scope.
(scope, span)
}
err.span_label(def_s, "defined here");
}
if sugg_unit {
- let mut sugg_span = sp.end_point();
+ let sugg_span = sp.end_point();
// remove closing `)` from the span
- sugg_span.hi = sugg_span.lo;
+ let sugg_span = sugg_span.with_hi(sugg_span.lo());
err.span_suggestion(
sugg_span,
"expected the unit value `()`. You can create one with a pair of parenthesis",
debug!("tuple struct named {:?}", base_t);
let ident = ast::Ident {
name: Symbol::intern(&idx.node.to_string()),
- ctxt: idx.span.ctxt.modern(),
+ ctxt: idx.span.ctxt().modern(),
};
let (ident, def_scope) =
self.tcx.adjust_ident(ident, base_def.did, self.body_id);
return;
}
let original_span = original_sp(last_stmt.span, blk.span);
- let span_semi = Span {
- lo: original_span.hi - BytePos(1),
- hi: original_span.hi,
- ctxt: original_span.ctxt,
- };
+ let span_semi = original_span.with_lo(original_span.hi() - BytePos(1));
err.span_suggestion(span_semi, "consider removing this semicolon", "".to_string());
}
// the outer `mod` item for the source code.
let whence = {
let cm = cx.sess().codemap();
- let outer = cm.lookup_char_pos(self.where_outer.lo);
- let inner = cm.lookup_char_pos(self.where_inner.lo);
+ let outer = cm.lookup_char_pos(self.where_outer.lo());
+ let inner = cm.lookup_char_pos(self.where_inner.lo());
if outer.file.start_pos == inner.file.start_pos {
// mod foo { ... }
self.where_outer
let cm = cx.sess().codemap();
let filename = cm.span_to_filename(*self);
- let lo = cm.lookup_char_pos(self.lo);
- let hi = cm.lookup_char_pos(self.hi);
+ let lo = cm.lookup_char_pos(self.lo());
+ let hi = cm.lookup_char_pos(self.hi());
Span {
filename: filename.to_string(),
loline: lo.line,
pub fn get_line(&self) -> usize {
if let Some(ref codemap) = self.codemap {
- let line = self.position.lo.to_usize();
+ let line = self.position.lo().to_usize();
let line = codemap.lookup_char_pos(BytePos(line as u32)).line;
if line > 0 { line - 1 } else { line }
} else {
}
pub fn crate_root(span: Span) -> Self {
PathSegment {
- identifier: Ident { ctxt: span.ctxt, ..keywords::CrateRoot.ident() },
+ identifier: Ident { ctxt: span.ctxt(), ..keywords::CrateRoot.ident() },
span,
parameters: None,
}
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
where I: Iterator<Item = TokenTree>,
{
- let (mut span, name) = match tokens.next() {
+ let (span, name) = match tokens.next() {
Some(TokenTree::Token(span, Token::Ident(ident))) => (span, ident.name),
Some(TokenTree::Token(_, Token::Interpolated(ref nt))) => match nt.0 {
token::Nonterminal::NtIdent(ident) => (ident.span, ident.node.name),
},
_ => return None,
};
- let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi);
+ let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
let node = match MetaItemKind::from_tokens(tokens) {
Some(node) => node,
_ => return None,
};
- span.hi = match node {
- MetaItemKind::NameValue(ref lit) => lit.span.hi,
- MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(span.hi),
- _ => span.hi,
+ let hi = match node {
+ MetaItemKind::NameValue(ref lit) => lit.span.hi(),
+ MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(span.hi()),
+ _ => span.hi(),
};
- Some(MetaItem { name: name, span: span, node: node })
+ Some(MetaItem { name, node, span: span.with_hi(hi) })
}
}
/// otherwise return the call site span up to the `enclosing_sp` by
/// following the `expn_info` chain.
pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
- let call_site1 = sp.ctxt.outer().expn_info().map(|ei| ei.call_site);
- let call_site2 = enclosing_sp.ctxt.outer().expn_info().map(|ei| ei.call_site);
+ let call_site1 = sp.ctxt().outer().expn_info().map(|ei| ei.call_site);
+ let call_site2 = enclosing_sp.ctxt().outer().expn_info().map(|ei| ei.call_site);
match (call_site1, call_site2) {
(None, _) => sp,
(Some(call_site1), Some(call_site2)) if call_site1 == call_site2 => sp,
}
pub fn mk_substr_filename(&self, sp: Span) -> String {
- let pos = self.lookup_char_pos(sp.lo);
+ let pos = self.lookup_char_pos(sp.lo());
(format!("<{}:{}:{}>",
pos.file.name,
pos.line,
/// * the lhs span needs to end on the same line the rhs span begins
/// * the lhs span must start at or before the rhs span
pub fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
- use std::cmp;
-
// make sure we're at the same expansion id
- if sp_lhs.ctxt != sp_rhs.ctxt {
+ if sp_lhs.ctxt() != sp_rhs.ctxt() {
return None;
}
- let lhs_end = match self.lookup_line(sp_lhs.hi) {
+ let lhs_end = match self.lookup_line(sp_lhs.hi()) {
Ok(x) => x,
Err(_) => return None
};
- let rhs_begin = match self.lookup_line(sp_rhs.lo) {
+ let rhs_begin = match self.lookup_line(sp_rhs.lo()) {
Ok(x) => x,
Err(_) => return None
};
}
// ensure these follow the expected order and we don't overlap
- if (sp_lhs.lo <= sp_rhs.lo) && (sp_lhs.hi <= sp_rhs.lo) {
- Some(Span {
- lo: cmp::min(sp_lhs.lo, sp_rhs.lo),
- hi: cmp::max(sp_lhs.hi, sp_rhs.hi),
- ctxt: sp_lhs.ctxt,
- })
+ if (sp_lhs.lo() <= sp_rhs.lo()) && (sp_lhs.hi() <= sp_rhs.lo()) {
+ Some(sp_lhs.to(sp_rhs))
} else {
None
}
return "no-location".to_string();
}
- let lo = self.lookup_char_pos_adj(sp.lo);
- let hi = self.lookup_char_pos_adj(sp.hi);
+ let lo = self.lookup_char_pos_adj(sp.lo());
+ let hi = self.lookup_char_pos_adj(sp.hi());
return (format!("{}:{}:{}: {}:{}",
lo.filename,
lo.line,
}
pub fn span_to_filename(&self, sp: Span) -> FileName {
- self.lookup_char_pos(sp.lo).file.name.to_string()
+ self.lookup_char_pos(sp.lo()).file.name.to_string()
}
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
debug!("span_to_lines(sp={:?})", sp);
- if sp.lo > sp.hi {
+ if sp.lo() > sp.hi() {
return Err(SpanLinesError::IllFormedSpan(sp));
}
- let lo = self.lookup_char_pos(sp.lo);
+ let lo = self.lookup_char_pos(sp.lo());
debug!("span_to_lines: lo={:?}", lo);
- let hi = self.lookup_char_pos(sp.hi);
+ let hi = self.lookup_char_pos(sp.hi());
debug!("span_to_lines: hi={:?}", hi);
if lo.file.start_pos != hi.file.start_pos {
}
pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
- if sp.lo > sp.hi {
+ if sp.lo() > sp.hi() {
return Err(SpanSnippetError::IllFormedSpan(sp));
}
- let local_begin = self.lookup_byte_offset(sp.lo);
- let local_end = self.lookup_byte_offset(sp.hi);
+ let local_begin = self.lookup_byte_offset(sp.lo());
+ let local_end = self.lookup_byte_offset(sp.hi());
if local_begin.fm.start_pos != local_end.fm.start_pos {
return Err(SpanSnippetError::DistinctSources(DistinctSources {
Ok(snippet) => {
let snippet = snippet.split(c).nth(0).unwrap_or("").trim_right();
if !snippet.is_empty() && !snippet.contains('\n') {
- Span { hi: BytePos(sp.lo.0 + snippet.len() as u32), ..sp }
+ sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
} else {
sp
}
fn t7() {
// Test span_to_lines for a span ending at the end of filemap
let cm = init_code_map();
- let span = Span {lo: BytePos(12), hi: BytePos(23), ctxt: NO_EXPANSION};
+ let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION);
let file_lines = cm.span_to_lines(span).unwrap();
assert_eq!(file_lines.file.name, "blork.rs");
assert_eq!(input.len(), selection.len());
let left_index = selection.find('~').unwrap() as u32;
let right_index = selection.rfind('~').map(|x|x as u32).unwrap_or(left_index);
- Span { lo: BytePos(left_index), hi: BytePos(right_index + 1), ctxt: NO_EXPANSION }
+ Span::new(BytePos(left_index), BytePos(right_index + 1), NO_EXPANSION)
}
/// Test span_to_snippet and span_to_lines for a span converting 3
fn t8() {
// Test span_to_snippet for a span ending at the end of filemap
let cm = init_code_map();
- let span = Span {lo: BytePos(12), hi: BytePos(23), ctxt: NO_EXPANSION};
+ let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION);
let snippet = cm.span_to_snippet(span);
assert_eq!(snippet, Ok("second line".to_string()));
fn t9() {
// Test span_to_str for a span ending at the end of filemap
let cm = init_code_map();
- let span = Span {lo: BytePos(12), hi: BytePos(23), ctxt: NO_EXPANSION};
+ let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION);
let sstr = cm.span_to_string(span);
assert_eq!(sstr, "blork.rs:2:1: 2:12");
let lo = hi + offset;
hi = lo + substring.len();
if i == n {
- let span = Span {
- lo: BytePos(lo as u32 + file.start_pos.0),
- hi: BytePos(hi as u32 + file.start_pos.0),
- ctxt: NO_EXPANSION,
- };
+ let span = Span::new(
+ BytePos(lo as u32 + file.start_pos.0),
+ BytePos(hi as u32 + file.start_pos.0),
+ NO_EXPANSION,
+ );
assert_eq!(&self.span_to_snippet(span).unwrap()[..],
substring);
return span;
use ast;
use codemap::Spanned;
use parse::{token, ParseSess};
-use syntax_pos::Span;
use ptr::P;
use util::small_vector::SmallVector;
parser.expect(&token::OpenDelim(token::Paren))?;
let cfg = parser.parse_meta_item()?;
parser.expect(&token::Comma)?;
- let lo = parser.span.lo;
+ let lo = parser.span.lo();
let (path, tokens) = parser.parse_path_and_tokens()?;
parser.expect(&token::CloseDelim(token::Paren))?;
- Ok((cfg, path, tokens, Span { lo: lo, ..parser.prev_span }))
+ Ok((cfg, path, tokens, parser.prev_span.with_lo(lo)))
}) {
Ok(result) => result,
Err(mut e) => {
impl ErrorLocation {
/// Create an error location from a span.
pub fn from_span(ecx: &ExtCtxt, sp: Span) -> ErrorLocation {
- let loc = ecx.codemap().lookup_char_pos_adj(sp.lo);
+ let loc = ecx.codemap().lookup_char_pos_adj(sp.lo());
ErrorLocation {
filename: loc.filename,
line: loc.line
// Stop going up the backtrace once include! is encountered
return None;
}
- ctxt = info.call_site.ctxt;
+ ctxt = info.call_site.ctxt();
last_macro = Some(info.call_site);
Some(())
}).is_none() {
-> Option<Spanned<(Symbol, ast::StrStyle)>> {
// Update `expr.span`'s ctxt now in case expr is an `include!` macro invocation.
let expr = expr.map(|mut expr| {
- expr.span.ctxt = expr.span.ctxt.apply_mark(cx.current_expansion.mark);
+ expr.span = expr.span.with_ctxt(expr.span.ctxt().apply_mark(cx.current_expansion.mark));
expr
});
}
fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
- let loc = self.codemap().lookup_char_pos(span.lo);
+ let loc = self.codemap().lookup_char_pos(span.lo());
let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name));
let expr_line = self.expr_u32(span, loc.line as u32);
let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
},
});
- let span = Span { ctxt: cx.backtrace(), ..span };
+ let span = span.with_ctxt(cx.backtrace());
item.map_attrs(|mut attrs| {
if names.contains(&Symbol::intern("Eq")) && names.contains(&Symbol::intern("PartialEq")) {
let meta = cx.meta_word(span, Symbol::intern("structural_match"));
match *ext {
ProcMacroDerive(ref ext, _) => {
invoc.expansion_data.mark.set_expn_info(expn_info);
- let span = Span { ctxt: self.cx.backtrace(), ..span };
+ let span = span.with_ctxt(self.cx.backtrace());
let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this
name: keywords::Invalid.name(),
span: DUMMY_SP,
BuiltinDerive(func) => {
expn_info.callee.allow_internal_unstable = true;
invoc.expansion_data.mark.set_expn_info(expn_info);
- let span = Span { ctxt: self.cx.backtrace(), ..span };
+ let span = span.with_ctxt(self.cx.backtrace());
let mut items = Vec::new();
func(self.cx, span, &attr.meta().unwrap(), &item, &mut |a| items.push(a));
kind.expect_from_annotatables(items)
if self.token != token::Eof {
let msg = format!("macro expansion ignores token `{}` and any following",
self.this_token_to_string());
- let mut def_site_span = self.span;
- def_site_span.ctxt = SyntaxContext::empty(); // Avoid emitting backtrace info twice.
+ // Avoid emitting backtrace info twice.
+ let def_site_span = self.span.with_ctxt(SyntaxContext::empty());
let mut err = self.diagnostic().struct_span_err(def_site_span, &msg);
let msg = format!("caused by the macro expansion here; the usage \
of `{}!` is likely invalid in {} context",
ident
}
- fn new_span(&mut self, mut span: Span) -> Span {
- span.ctxt = span.ctxt.apply_mark(self.0);
- span
+ fn new_span(&mut self, span: Span) -> Span {
+ span.with_ctxt(span.ctxt().apply_mark(self.0))
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
base::check_zero_tts(cx, sp, tts, "line!");
let topmost = cx.expansion_cause().unwrap_or(sp);
- let loc = cx.codemap().lookup_char_pos(topmost.lo);
+ let loc = cx.codemap().lookup_char_pos(topmost.lo());
base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
}
base::check_zero_tts(cx, sp, tts, "column!");
let topmost = cx.expansion_cause().unwrap_or(sp);
- let loc = cx.codemap().lookup_char_pos(topmost.lo);
+ let loc = cx.codemap().lookup_char_pos(topmost.lo());
base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32))
}
base::check_zero_tts(cx, sp, tts, "file!");
let topmost = cx.expansion_cause().unwrap_or(sp);
- let loc = cx.codemap().lookup_char_pos(topmost.lo);
+ let loc = cx.codemap().lookup_char_pos(topmost.lo());
base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name)))
}
// Only touch the binders we have actually bound
for idx in item.match_lo..item.match_hi {
let sub = item.matches[idx].clone();
- new_pos.push_match(idx, MatchedSeq(sub, Span { lo: item.sp_lo, ..span }));
+ let span = span.with_lo(item.sp_lo);
+ new_pos.push_match(idx, MatchedSeq(sub, span));
}
new_pos.match_cur = item.match_hi;
match_cur: item.match_cur,
match_hi: item.match_cur + seq.num_captures,
up: Some(item),
- sp_lo: sp.lo,
+ sp_lo: sp.lo(),
top_elts: Tt(TokenTree::Sequence(sp, seq)),
}));
}
recurse_into_modules: bool)
-> NamedParseResult {
let mut parser = Parser::new(sess, tts, directory, recurse_into_modules, true);
- let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo));
+ let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo()));
let mut next_items = Vec::new(); // or proceed normally
loop {
tts = tts.map_enumerated(|i, tt| {
let mut tt = tt.clone();
let mut sp = rhs_spans[i];
- sp.ctxt = tt.span().ctxt;
+ sp = sp.with_ctxt(tt.span().ctxt());
tt.set_span(sp);
tt
});
macro_ident: name
})
}
- Failure(sp, tok) => if sp.lo >= best_fail_spot.lo {
+ Failure(sp, tok) => if sp.lo() >= best_fail_spot.lo() {
best_fail_spot = sp;
best_fail_tok = Some(tok);
},
let open_span = if span == DUMMY_SP {
DUMMY_SP
} else {
- Span { hi: span.lo + BytePos(self.delim.len() as u32), ..span }
+ span.with_lo(span.lo() + BytePos(self.delim.len() as u32))
};
TokenTree::Token(open_span, self.open_token())
}
let close_span = if span == DUMMY_SP {
DUMMY_SP
} else {
- Span { lo: span.hi - BytePos(self.delim.len() as u32), ..span }
+ span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
};
TokenTree::Token(close_span, self.close_token())
}
Some(tokenstream::TokenTree::Token(span, token::Colon)) => match trees.next() {
Some(tokenstream::TokenTree::Token(end_sp, ref tok)) => match tok.ident() {
Some(kind) => {
- let span = Span { lo: start_sp.lo, ..end_sp };
+ let span = end_sp.with_lo(start_sp.lo());
result.push(TokenTree::MetaVarDecl(span, ident, kind));
continue
}
}
Some(tokenstream::TokenTree::Token(ident_span, ref token)) if token.is_ident() => {
let ident = token.ident().unwrap();
- let span = Span { lo: span.lo, ..ident_span };
+ let span = ident_span.with_lo(span.lo());
if ident.name == keywords::Crate.name() {
let ident = ast::Ident { name: keywords::DollarCrate.name(), ..ident };
TokenTree::Token(span, token::Ident(ident))
if let NtTT(ref tt) = **nt {
result.push(tt.clone().into());
} else {
- sp.ctxt = sp.ctxt.apply_mark(cx.current_expansion.mark);
+ sp = sp.with_ctxt(sp.ctxt().apply_mark(cx.current_expansion.mark));
let token = TokenTree::Token(sp, Token::interpolated((**nt).clone()));
result.push(token.into());
}
} else {
let ident =
Ident { ctxt: ident.ctxt.apply_mark(cx.current_expansion.mark), ..ident };
- sp.ctxt = sp.ctxt.apply_mark(cx.current_expansion.mark);
+ sp = sp.with_ctxt(sp.ctxt().apply_mark(cx.current_expansion.mark));
result.push(TokenTree::Token(sp, token::Dollar).into());
result.push(TokenTree::Token(sp, token::Ident(ident)).into());
}
}
quoted::TokenTree::Delimited(mut span, delimited) => {
- span.ctxt = span.ctxt.apply_mark(cx.current_expansion.mark);
+ span = span.with_ctxt(span.ctxt().apply_mark(cx.current_expansion.mark));
stack.push(Frame::Delimited { forest: delimited, idx: 0, span: span });
result_stack.push(mem::replace(&mut result, Vec::new()));
}
mut backtrace: vec::IntoIter<MacroBacktrace>,
je: &JsonEmitter)
-> DiagnosticSpan {
- let start = je.cm.lookup_char_pos(span.lo);
- let end = je.cm.lookup_char_pos(span.hi);
+ let start = je.cm.lookup_char_pos(span.lo());
+ let end = je.cm.lookup_char_pos(span.hi());
let backtrace_step = backtrace.next().map(|bt| {
let call_site =
Self::from_span_full(bt.call_site,
});
DiagnosticSpan {
file_name: start.file.name.clone(),
- byte_start: span.lo.0 - start.file.start_pos.0,
- byte_end: span.hi.0 - start.file.start_pos.0,
+ byte_start: span.lo().0 - start.file.start_pos.0,
+ byte_end: span.hi().0 - start.file.start_pos.0,
line_start: start.line,
line_end: end.line,
column_start: start.col.0 + 1,
debug!("tok lit: {}", s);
literals.push(Literal {
lit: s.to_string(),
- pos: sp.lo,
+ pos: sp.lo(),
});
})
} else {
impl<'a> StringReader<'a> {
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
- unwrap_or!(self.override_span, Span { lo: lo, hi: hi, ctxt: NO_EXPANSION})
+ unwrap_or!(self.override_span, Span::new(lo, hi, NO_EXPANSION))
}
fn next_token(&mut self) -> TokenAndSpan where Self: Sized {
}
pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
- let begin = sess.codemap().lookup_byte_offset(span.lo);
- let end = sess.codemap().lookup_byte_offset(span.hi);
+ let begin = sess.codemap().lookup_byte_offset(span.lo());
+ let end = sess.codemap().lookup_byte_offset(span.hi());
// Make the range zero-length if the span is invalid.
- if span.lo > span.hi || begin.fm.start_pos != end.fm.start_pos {
- span.hi = span.lo;
+ if span.lo() > span.hi() || begin.fm.start_pos != end.fm.start_pos {
+ span = span.with_hi(span.lo());
}
let mut sr = StringReader::new_raw_internal(sess, begin.fm);
// Seek the lexer to the right byte range.
sr.save_new_lines_and_multibyte = false;
- sr.next_pos = span.lo;
- sr.terminator = Some(span.hi);
+ sr.next_pos = span.lo();
+ sr.terminator = Some(span.hi());
sr.bump();
let tok1 = string_reader.next_token();
let tok2 = TokenAndSpan {
tok: token::Ident(id),
- sp: Span {
- lo: BytePos(21),
- hi: BytePos(23),
- ctxt: NO_EXPANSION,
- },
+ sp: Span::new(BytePos(21), BytePos(23), NO_EXPANSION),
};
assert_eq!(tok1, tok2);
assert_eq!(string_reader.next_token().tok, token::Whitespace);
let tok3 = string_reader.next_token();
let tok4 = TokenAndSpan {
tok: token::Ident(Ident::from_str("main")),
- sp: Span {
- lo: BytePos(24),
- hi: BytePos(28),
- ctxt: NO_EXPANSION,
- },
+ sp: Span::new(BytePos(24), BytePos(28), NO_EXPANSION),
};
assert_eq!(tok3, tok4);
// the lparen is already read:
let mut lexer = setup(&cm, &sh, "// test\r\n/// test\r\n".to_string());
let comment = lexer.next_token();
assert_eq!(comment.tok, token::Comment);
- assert_eq!((comment.sp.lo, comment.sp.hi), (BytePos(0), BytePos(7)));
+ assert_eq!((comment.sp.lo(), comment.sp.hi()), (BytePos(0), BytePos(7)));
assert_eq!(lexer.next_token().tok, token::Whitespace);
assert_eq!(lexer.next_token().tok,
token::DocComment(Symbol::intern("/// test")));
use print::pprust::token_to_string;
use parse::lexer::StringReader;
use parse::{token, PResult};
-use syntax_pos::Span;
use tokenstream::{Delimited, TokenStream, TokenTree};
impl<'a> StringReader<'a> {
let mut tts = Vec::new();
while self.token != token::Eof {
let tree = self.parse_token_tree()?;
- let is_joint = tree.span().hi == self.span.lo && token::is_op(&self.token);
+ let is_joint = tree.span().hi() == self.span.lo() && token::is_op(&self.token);
tts.push(if is_joint { tree.joint() } else { tree.into() });
}
Ok(TokenStream::concat(tts))
return TokenStream::concat(tts);
}
};
- let is_joint = tree.span().hi == self.span.lo && token::is_op(&self.token);
+ let is_joint = tree.span().hi() == self.span.lo() && token::is_op(&self.token);
tts.push(if is_joint { tree.joint() } else { tree.into() });
}
}
let tts = self.parse_token_trees_until_close_delim();
// Expand to cover the entire delimited token tree
- let span = Span { hi: self.span.hi, ..pre_span };
+ let span = pre_span.with_hi(self.span.hi());
match self.token {
// Correct delimiter.
.iter()
.find(|&&(c, _, _)| c == ch)
.map(|&(_, u_name, ascii_char)| {
- let span = Span { lo: reader.pos, hi: reader.next_pos, ctxt: NO_EXPANSION };
+ let span = Span::new(reader.pos, reader.next_pos, NO_EXPANSION);
match ASCII_ARRAY.iter().find(|&&(c, _)| c == ascii_char) {
Some(&(ascii_char, ascii_name)) => {
let msg =
let mut parser = stream_to_parser(sess, filemap_to_stream(sess, filemap, None));
if parser.token == token::Eof && parser.span == syntax_pos::DUMMY_SP {
- parser.span = Span { lo: end_pos, hi: end_pos, ctxt: NO_EXPANSION };
+ parser.span = Span::new(end_pos, end_pos, NO_EXPANSION);
}
parser
// produce a syntax_pos::span
fn sp(a: u32, b: u32) -> Span {
- Span {lo: BytePos(a), hi: BytePos(b), ctxt: NO_EXPANSION}
+ Span::new(BytePos(a), BytePos(b), NO_EXPANSION)
}
fn str2seg(s: &str, lo: u32, hi: u32) -> ast::PathSegment {
for &src in &srcs {
let spans = get_spans_of_pat_idents(src);
- let Span{ lo, hi, .. } = spans[0];
+ let (lo, hi) = (spans[0].lo(), spans[0].hi());
assert!("self" == &src[lo.to_usize()..hi.to_usize()],
"\"{}\" != \"self\". src=\"{}\"",
&src[lo.to_usize()..hi.to_usize()], src)
Ok(())
}
token::AndAnd => {
- let span = self.span;
- let lo = span.lo + BytePos(1);
- Ok(self.bump_with(token::BinOp(token::And), Span { lo: lo, ..span }))
+ let span = self.span.with_lo(self.span.lo() + BytePos(1));
+ Ok(self.bump_with(token::BinOp(token::And), span))
}
_ => self.unexpected()
}
true
}
token::BinOp(token::Shl) => {
- let span = self.span;
- let lo = span.lo + BytePos(1);
- self.bump_with(token::Lt, Span { lo: lo, ..span });
+ let span = self.span.with_lo(self.span.lo() + BytePos(1));
+ self.bump_with(token::Lt, span);
true
}
_ => false,
Ok(())
}
token::BinOp(token::Shr) => {
- let span = self.span;
- let lo = span.lo + BytePos(1);
- Ok(self.bump_with(token::Gt, Span { lo: lo, ..span }))
+ let span = self.span.with_lo(self.span.lo() + BytePos(1));
+ Ok(self.bump_with(token::Gt, span))
}
token::BinOpEq(token::Shr) => {
- let span = self.span;
- let lo = span.lo + BytePos(1);
- Ok(self.bump_with(token::Ge, Span { lo: lo, ..span }))
+ let span = self.span.with_lo(self.span.lo() + BytePos(1));
+ Ok(self.bump_with(token::Ge, span))
}
token::Ge => {
- let span = self.span;
- let lo = span.lo + BytePos(1);
- Ok(self.bump_with(token::Eq, Span { lo: lo, ..span }))
+ let span = self.span.with_lo(self.span.lo() + BytePos(1));
+ Ok(self.bump_with(token::Eq, span))
}
_ => self.unexpected()
}
/// Advance the parser using provided token as a next one. Use this when
/// consuming a part of a token. For example a single `<` from `<<`.
pub fn bump_with(&mut self, next: token::Token, span: Span) {
- self.prev_span = Span { hi: span.lo, ..self.span };
+ self.prev_span = self.span.with_hi(span.lo());
// It would be incorrect to record the kind of the current token, but
// fortunately for tokens currently using `bump_with`, the
// prev_token_kind will be of no use anyway.
if self.eat(&token::RArrow) {
Ok(FunctionRetTy::Ty(self.parse_ty_no_plus()?))
} else {
- Ok(FunctionRetTy::Default(Span { hi: self.span.lo, ..self.span }))
+ Ok(FunctionRetTy::Default(self.span.with_hi(self.span.lo())))
}
}
pub fn process_potential_macro_variable(&mut self) {
let ident = match self.token {
- token::Dollar if self.span.ctxt != syntax_pos::hygiene::SyntaxContext::empty() &&
+ token::Dollar if self.span.ctxt() != syntax_pos::hygiene::SyntaxContext::empty() &&
self.look_ahead(1, |t| t.is_ident()) => {
self.bump();
let name = match self.token { token::Ident(ident) => ident, _ => unreachable!() };
err.span_label(self.span,
"expecting a type here because of type ascription");
let cm = self.sess.codemap();
- let cur_pos = cm.lookup_char_pos(self.span.lo);
- let op_pos = cm.lookup_char_pos(cur_op_span.hi);
+ let cur_pos = cm.lookup_char_pos(self.span.lo());
+ let op_pos = cm.lookup_char_pos(cur_op_span.hi());
if cur_pos.line != op_pos.line {
err.span_suggestion_short(cur_op_span,
"did you mean to use `;` here?",
let mut stmt_span = stmt.span;
// expand the span to include the semicolon, if it exists
if self.eat(&token::Semi) {
- stmt_span.hi = self.prev_span.hi;
+ stmt_span = stmt_span.with_hi(self.prev_span.hi());
}
let sugg = pprust::to_string(|s| {
use print::pprust::{PrintState, INDENT_UNIT};
stmt = stmt.add_trailing_semicolon();
}
- stmt.span.hi = self.prev_span.hi;
+ stmt.span = stmt.span.with_hi(self.prev_span.hi());
Ok(Some(stmt))
}
}
fn print_literal(&mut self, lit: &ast::Lit) -> io::Result<()> {
- self.maybe_print_comment(lit.span.lo)?;
- if let Some(ltrl) = self.next_lit(lit.span.lo) {
+ self.maybe_print_comment(lit.span.lo())?;
+ if let Some(ltrl) = self.next_lit(lit.span.lo()) {
return self.writer().word(<rl.lit);
}
match lit.node {
if !is_inline {
self.hardbreak_if_not_bol()?;
}
- self.maybe_print_comment(attr.span.lo)?;
+ self.maybe_print_comment(attr.span.lo())?;
if attr.is_sugared_doc {
self.writer().word(&attr.value_str().unwrap().as_str())?;
self.writer().hardbreak()
}
pub fn bclose_maybe_open(&mut self, span: syntax_pos::Span,
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 {
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))?;
+ Some(get_span(&elts[i]).hi()))?;
self.space_if_not_bol()?;
}
}
}
pub fn print_type(&mut self, ty: &ast::Ty) -> io::Result<()> {
- self.maybe_print_comment(ty.span.lo)?;
+ self.maybe_print_comment(ty.span.lo())?;
self.ibox(0)?;
match ty.node {
ast::TyKind::Slice(ref ty) => {
pub fn print_foreign_item(&mut self,
item: &ast::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 {
ast::ForeignItemKind::Fn(ref decl, ref generics) => {
/// Pretty-print an item
pub fn print_item(&mut self, item: &ast::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 {
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)?;
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)
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_ident(field.ident.unwrap())?;
-> 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 {
ast::TraitItemKind::Const(ref ty, ref default) => {
pub fn print_impl_item(&mut self, ii: &ast::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)?;
match ii.node {
}
pub fn print_stmt(&mut self, st: &ast::Stmt) -> io::Result<()> {
- self.maybe_print_comment(st.span.lo)?;
+ self.maybe_print_comment(st.span.lo())?;
match st.node {
ast::StmtKind::Local(ref loc) => {
self.print_outer_attributes(&loc.attrs)?;
BlockCheckMode::Unsafe(..) => self.word_space("unsafe")?,
BlockCheckMode::Default => ()
}
- self.maybe_print_comment(blk.span.lo)?;
+ self.maybe_print_comment(blk.span.lo())?;
self.ann.pre(self, NodeBlock(blk))?;
self.bopen()?;
for (i, st) in blk.stmts.iter().enumerate() {
match st.node {
ast::StmtKind::Expr(ref expr) if i == blk.stmts.len() - 1 => {
- self.maybe_print_comment(st.span.lo)?;
+ self.maybe_print_comment(st.span.lo())?;
self.space_if_not_bol()?;
self.print_expr_outer_attr_style(expr, false)?;
- self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi))?;
+ self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()))?;
}
_ => self.print_stmt(st)?,
}
fn print_expr_outer_attr_style(&mut self,
expr: &ast::Expr,
is_inline: bool) -> io::Result<()> {
- self.maybe_print_comment(expr.span.lo)?;
+ self.maybe_print_comment(expr.span.lo())?;
let attrs = &expr.attrs;
if is_inline {
defaults_to_global: bool)
-> io::Result<()>
{
- self.maybe_print_comment(path.span.lo)?;
+ self.maybe_print_comment(path.span.lo())?;
let mut segments = path.segments[..path.segments.len()-depth].iter();
if defaults_to_global && path.is_global() {
}
pub fn print_pat(&mut self, pat: &ast::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 */
}
self.cbox(INDENT_UNIT)?;
self.ibox(0)?;
- self.maybe_print_comment(arm.pats[0].span.lo)?;
+ self.maybe_print_comment(arm.pats[0].span.lo())?;
self.print_outer_attributes(&arm.attrs)?;
let mut first = true;
for p in &arm.pats {
match decl.output {
ast::FunctionRetTy::Ty(ref ty) => {
self.print_type(ty)?;
- self.maybe_print_comment(ty.span.lo)
+ self.maybe_print_comment(ty.span.lo())
}
ast::FunctionRetTy::Default(..) => unreachable!(),
}
self.end()?;
match decl.output {
- ast::FunctionRetTy::Ty(ref output) => self.maybe_print_comment(output.span.lo),
+ ast::FunctionRetTy::Ty(ref output) => self.maybe_print_comment(output.span.lo()),
_ => Ok(())
}
}
};
if let Some(ref cmnt) = self.next_comment() {
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 next = next_pos.unwrap_or(cmnt.pos + BytePos(1));
- if span.hi < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
+ if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line {
self.print_comment(cmnt)?;
}
}
allow_internal_unsafe: false,
}
});
- Span { ctxt: SyntaxContext::empty().apply_mark(mark), ..sp }
+ sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
}
pub fn injected_crate_name(krate: &ast::Crate) -> Option<&'static str> {
/// call to codemap's `is_internal` check.
/// The expanded code calls some unstable functions in the test crate.
fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
- Span { ctxt: cx.ctxt, ..sp }
+ sp.with_ctxt(cx.ctxt)
}
#[derive(PartialEq)]
let start = make_pos(file_text, start);
let end = make_pos(file_text, end) + end.string.len(); // just after matching thing ends
assert!(start <= end);
- Span {
- lo: BytePos(start as u32),
- hi: BytePos(end as u32),
- ctxt: NO_EXPANSION,
- }
+ Span::new(BytePos(start as u32), BytePos(end as u32), NO_EXPANSION)
}
fn make_pos(file_text: &str, pos: &Position) -> usize {
let open_span = if span == DUMMY_SP {
DUMMY_SP
} else {
- Span { hi: span.lo + BytePos(self.delim.len() as u32), ..span }
+ span.with_hi(span.lo() + BytePos(self.delim.len() as u32))
};
TokenTree::Token(open_span, self.open_token())
}
let close_span = if span == DUMMY_SP {
DUMMY_SP
} else {
- Span { lo: span.hi - BytePos(self.delim.len() as u32), ..span }
+ span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
};
TokenTree::Token(close_span, self.close_token())
}
}
fn sp(a: u32, b: u32) -> Span {
- Span {
- lo: BytePos(a),
- hi: BytePos(b),
- ctxt: NO_EXPANSION,
- }
+ Span::new(BytePos(a), BytePos(b), NO_EXPANSION)
}
#[test]
ty: P<ast::Ty>, span: Span, helper_name: &str) {
// Generate statement `let _: helper_name<ty>;`,
// set the expn ID so we can use the unstable struct.
- let span = Span { ctxt: cx.backtrace(), ..span};
+ let span = span.with_ctxt(cx.backtrace());
let assert_path = cx.path_all(span, true,
cx.std_path(&["clone", helper_name]),
vec![], vec![ty], vec![]);
ty: P<ast::Ty>, span: Span, helper_name: &str) {
// Generate statement `let _: helper_name<ty>;`,
// set the expn ID so we can use the unstable struct.
- let span = Span { ctxt: cx.backtrace(), ..span };
+ let span = span.with_ctxt(cx.backtrace());
let assert_path = cx.path_all(span, true,
cx.std_path(&["cmp", helper_name]),
vec![], vec![ty], vec![]);
};
// We want to make sure we have the ctxt set so that we can use unstable methods
- let span = Span { ctxt: cx.backtrace(), ..span };
+ let span = span.with_ctxt(cx.backtrace());
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
let builder = Ident::from_str("builder");
let builder_expr = cx.expr_ident(span, builder.clone());
}
fn visit_mac(&mut self, mac: &ast::Mac) {
- let span = Span { ctxt: self.span.ctxt, ..mac.span };
+ let span = mac.span.with_ctxt(self.span.ctxt());
self.cx.span_err(span, "`derive` cannot be used on items with type macros");
}
}
.iter()
.map(|v| {
let ident = v.node.name;
- let sp = Span { ctxt: trait_.span.ctxt, ..v.span };
+ let sp = v.span.with_ctxt(trait_.span.ctxt());
let summary = trait_.summarise_struct(cx, &v.node.data);
(ident, sp, summary)
})
let mut named_idents = Vec::new();
let mut just_spans = Vec::new();
for field in struct_def.fields() {
- let sp = Span { ctxt: self.span.ctxt, ..field.span };
+ let sp = field.span.with_ctxt(self.span.ctxt());
match field.ident {
Some(ident) => named_idents.push((ident, sp)),
_ => just_spans.push(sp),
let mut paths = Vec::new();
let mut ident_exprs = Vec::new();
for (i, struct_field) in struct_def.fields().iter().enumerate() {
- let sp = Span { ctxt: self.span.ctxt, ..struct_field.span };
+ let sp = struct_field.span.with_ctxt(self.span.ctxt());
let ident = cx.ident_of(&format!("{}_{}", prefix, i));
paths.push(codemap::Spanned {
span: sp,
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");
}
codemap::Spanned {
- span: Span { ctxt: self.span.ctxt, ..pat.span },
+ span: pat.span.with_ctxt(self.span.ctxt()),
node: ast::FieldPat {
ident: ident.unwrap(),
pat,
mutbl: ast::Mutability)
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>, &'a [ast::Attribute])>) {
let variant_ident = variant.node.name;
- let sp = Span { ctxt: self.span.ctxt, ..variant.span };
+ let sp = variant.span.with_ctxt(self.span.ctxt());
let variant_path = cx.path(sp, vec![enum_ident, variant_ident]);
self.create_struct_pattern(cx, variant_path, &variant.node.data, prefix, mutbl)
}
args: Vec<P<ast::Expr>>)
-> P<ast::Expr> {
if cx.current_expansion.mark.expn_info().unwrap().callee.allow_internal_unstable {
- span.ctxt = cx.backtrace();
+ span = span.with_ctxt(cx.backtrace());
} else { // Avoid instability errors with user defined curstom derives, cc #36316
let mut info = cx.current_expansion.mark.expn_info().unwrap();
info.callee.allow_internal_unstable = true;
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(info);
- span.ctxt = SyntaxContext::empty().apply_mark(mark);
+ span = span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
}
let path = cx.std_path(&["intrinsics", intrinsic]);
let call = cx.expr_call_global(span, path, args);
// passed to this function.
for (i, e) in self.args.into_iter().enumerate() {
let name = self.ecx.ident_of(&format!("__arg{}", i));
- let span = Span {
- ctxt: e.span.ctxt.apply_mark(self.ecx.current_expansion.mark),
- ..DUMMY_SP
- };
+ let span =
+ DUMMY_SP.with_ctxt(e.span.ctxt().apply_mark(self.ecx.current_expansion.mark));
pats.push(self.ecx.pat_ident(span, name));
for ref arg_ty in self.arg_unique_types[i].iter() {
locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, name));
ty: &ArgumentType,
arg: ast::Ident)
-> P<ast::Expr> {
- sp.ctxt = sp.ctxt.apply_mark(ecx.current_expansion.mark);
+ sp = sp.with_ctxt(sp.ctxt().apply_mark(ecx.current_expansion.mark));
let arg = ecx.expr_ident(sp, arg);
let trait_ = match *ty {
Placeholder(ref tyname) => {
mut sp: Span,
tts: &[tokenstream::TokenTree])
-> Box<base::MacResult + 'cx> {
- sp.ctxt = sp.ctxt.apply_mark(ecx.current_expansion.mark);
+ sp = sp.with_ctxt(sp.ctxt().apply_mark(ecx.current_expansion.mark));
match parse_args(ecx, sp, tts) {
Some((efmt, args, names)) => {
MacEager::expr(expand_preparsed_format_args(ecx, sp, efmt, args, names))
let arg_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect();
let arg_unique_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect();
let mut macsp = ecx.call_site();
- macsp.ctxt = macsp.ctxt.apply_mark(ecx.current_expansion.mark);
+ macsp = macsp.with_ctxt(macsp.ctxt().apply_mark(ecx.current_expansion.mark));
let msg = "format argument must be a string literal.";
let fmt = match expr_to_spanned_string(ecx, efmt, msg) {
Some(fmt) => fmt,
allow_internal_unsafe: false,
}
});
- let span = Span { ctxt: SyntaxContext::empty().apply_mark(mark), ..DUMMY_SP };
+ let span = DUMMY_SP.with_ctxt(SyntaxContext::empty().apply_mark(mark));
let proc_macro = Ident::from_str("proc_macro");
let krate = cx.item(span,
/// range between files.
#[derive(Clone, Copy, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct Span {
- pub lo: BytePos,
- pub hi: BytePos,
+ lo: BytePos,
+ hi: BytePos,
/// Information about where the macro came from, if this piece of
/// code was created by a macro expansion.
- pub ctxt: SyntaxContext,
+ ctxt: SyntaxContext,
}
+pub const DUMMY_SP: Span = Span { lo: BytePos(0), hi: BytePos(0), ctxt: NO_EXPANSION };
+
/// A collection of spans. Spans have two orthogonal attributes:
///
/// - they can be *primary spans*. In this case they are the locus of
}
impl Span {
+ #[inline]
+ pub fn new(lo: BytePos, hi: BytePos, ctxt: SyntaxContext) -> Self {
+ Span { lo, hi, ctxt }
+ }
+
+ #[inline]
+ pub fn lo(self) -> BytePos {
+ self.lo
+ }
+ #[inline]
+ pub fn with_lo(self, lo: BytePos) -> Span {
+ Span::new(lo, self.hi(), self.ctxt())
+ }
+ #[inline]
+ pub fn hi(self) -> BytePos {
+ self.hi
+ }
+ #[inline]
+ pub fn with_hi(self, hi: BytePos) -> Span {
+ Span::new(self.lo(), hi, self.ctxt())
+ }
+ #[inline]
+ pub fn ctxt(self) -> SyntaxContext {
+ self.ctxt
+ }
+ #[inline]
+ pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
+ Span::new(self.lo(), self.hi(), ctxt)
+ }
+
/// Returns a new span representing just the end-point of this span
pub fn end_point(self) -> Span {
- let lo = cmp::max(self.hi.0 - 1, self.lo.0);
- Span { lo: BytePos(lo), ..self }
+ let lo = cmp::max(self.hi().0 - 1, self.lo().0);
+ self.with_lo(BytePos(lo))
}
/// Returns a new span representing the next character after the end-point of this span
pub fn next_point(self) -> Span {
- let lo = cmp::max(self.hi.0, self.lo.0 + 1);
- Span { lo: BytePos(lo), hi: BytePos(lo), ..self }
+ let lo = cmp::max(self.hi().0, self.lo().0 + 1);
+ Span::new(BytePos(lo), BytePos(lo), self.ctxt())
}
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
/// Return true if `self` fully encloses `other`.
pub fn contains(self, other: Span) -> bool {
- self.lo <= other.lo && other.hi <= self.hi
+ self.lo() <= other.lo() && other.hi() <= self.hi()
}
/// Return true if the spans are equal with regards to the source text.
/// Use this instead of `==` when either span could be generated code,
/// and you only care that they point to the same bytes of source text.
pub fn source_equal(&self, other: &Span) -> bool {
- self.lo == other.lo && self.hi == other.hi
+ self.lo() == other.lo() && self.hi() == other.hi()
}
/// Returns `Some(span)`, where the start is trimmed by the end of `other`
pub fn trim_start(self, other: Span) -> Option<Span> {
- if self.hi > other.hi {
- Some(Span { lo: cmp::max(self.lo, other.hi), .. self })
+ if self.hi() > other.hi() {
+ Some(self.with_lo(cmp::max(self.lo(), other.hi())))
} else {
None
}
/// Return the source span - this is either the supplied span, or the span for
/// the macro callsite that expanded to it.
pub fn source_callsite(self) -> Span {
- self.ctxt.outer().expn_info().map(|info| info.call_site.source_callsite()).unwrap_or(self)
+ self.ctxt().outer().expn_info().map(|info| info.call_site.source_callsite()).unwrap_or(self)
}
/// Return the source callee.
/// corresponding to the source callsite.
pub fn source_callee(self) -> Option<NameAndSpan> {
fn source_callee(info: ExpnInfo) -> NameAndSpan {
- match info.call_site.ctxt.outer().expn_info() {
+ match info.call_site.ctxt().outer().expn_info() {
Some(info) => source_callee(info),
None => info.callee,
}
}
- self.ctxt.outer().expn_info().map(source_callee)
+ self.ctxt().outer().expn_info().map(source_callee)
}
/// Check if a span is "internal" to a macro in which #[unstable]
/// items can be used (that is, a macro marked with
/// `#[allow_internal_unstable]`).
pub fn allows_unstable(&self) -> bool {
- match self.ctxt.outer().expn_info() {
+ match self.ctxt().outer().expn_info() {
Some(info) => info.callee.allow_internal_unstable,
None => false,
}
/// Check if this span arises from a compiler desugaring of kind `kind`.
pub fn is_compiler_desugaring(&self, kind: CompilerDesugaringKind) -> bool {
- match self.ctxt.outer().expn_info() {
+ match self.ctxt().outer().expn_info() {
Some(info) => match info.callee.format {
ExpnFormat::CompilerDesugaring(k) => k == kind,
_ => false,
/// can be used without triggering the `unsafe_code` lint
// (that is, a macro marked with `#[allow_internal_unsafe]`).
pub fn allows_unsafe(&self) -> bool {
- match self.ctxt.outer().expn_info() {
+ match self.ctxt().outer().expn_info() {
Some(info) => info.callee.allow_internal_unsafe,
None => false,
}
let mut prev_span = DUMMY_SP;
let mut result = vec![];
loop {
- let info = match self.ctxt.outer().expn_info() {
+ let info = match self.ctxt().outer().expn_info() {
Some(info) => info,
None => break,
};
/// Return a `Span` that would enclose both `self` and `end`.
pub fn to(self, end: Span) -> Span {
- Span {
- lo: cmp::min(self.lo, end.lo),
- hi: cmp::max(self.hi, end.hi),
+ Span::new(
+ cmp::min(self.lo(), end.lo()),
+ cmp::max(self.hi(), end.hi()),
// FIXME(jseyfried): self.ctxt should always equal end.ctxt here (c.f. issue #23480)
- ctxt: if self.ctxt == SyntaxContext::empty() {
- end.ctxt
- } else {
- self.ctxt
- },
- }
+ if self.ctxt() == SyntaxContext::empty() { end.ctxt() } else { self.ctxt() },
+ )
}
/// Return a `Span` between the end of `self` to the beginning of `end`.
pub fn between(self, end: Span) -> Span {
- Span {
- lo: self.hi,
- hi: end.lo,
- ctxt: if end.ctxt == SyntaxContext::empty() {
- end.ctxt
- } else {
- self.ctxt
- }
- }
+ Span::new(
+ self.hi(),
+ end.lo(),
+ if end.ctxt() == SyntaxContext::empty() { end.ctxt() } else { self.ctxt() },
+ )
}
/// Return a `Span` between the beginning of `self` to the beginning of `end`.
pub fn until(self, end: Span) -> Span {
- Span {
- lo: self.lo,
- hi: end.lo,
- ctxt: if end.ctxt == SyntaxContext::empty() {
- end.ctxt
- } else {
- self.ctxt
- }
- }
+ Span::new(
+ self.lo(),
+ end.lo(),
+ if end.ctxt() == SyntaxContext::empty() { end.ctxt() } else { self.ctxt() },
+ )
}
}
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_struct("Span", 2, |s| {
s.emit_struct_field("lo", 0, |s| {
- self.lo.encode(s)
+ self.lo().encode(s)
})?;
s.emit_struct_field("hi", 1, |s| {
- self.hi.encode(s)
+ self.hi().encode(s)
})
})
}
d.read_struct("Span", 2, |d| {
let lo = d.read_struct_field("lo", 0, Decodable::decode)?;
let hi = d.read_struct_field("hi", 1, Decodable::decode)?;
- Ok(Span { lo: lo, hi: hi, ctxt: NO_EXPANSION })
+ Ok(Span::new(lo, hi, NO_EXPANSION))
})
}
}
fn default_span_debug(span: Span, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Span {{ lo: {:?}, hi: {:?}, ctxt: {:?} }}",
- span.lo, span.hi, span.ctxt)
+ span.lo(), span.hi(), span.ctxt())
}
impl fmt::Debug for Span {
}
}
-pub const DUMMY_SP: Span = Span { lo: BytePos(0), hi: BytePos(0), ctxt: NO_EXPANSION };
-
impl MultiSpan {
pub fn new() -> MultiSpan {
MultiSpan {
fn main() {
// The AST of `exported_generic` stored in crate_with_invalid_spans's
- // metadata should contain an invalid span where span.lo > span.hi.
+ // metadata should contain an invalid span where span.lo() > span.hi().
// Let's make sure the compiler doesn't crash when encountering this.
let _ = crate_with_invalid_spans::exported_generic(32u32, 7u32);
}