Use getters to access Span fields

This commit is contained in:
Vadim Petrochenkov 2017-08-19 21:47:40 +03:00 committed by Nick Cameron
parent 4eca284c9f
commit 6e5c6f5ba3
18 changed files with 303 additions and 295 deletions

View File

@ -470,7 +470,7 @@ fn rewrite_method_call(
shape: Shape,
) -> Option<String> {
let (lo, type_str) = if types.is_empty() {
(args[0].span.hi, String::new())
(args[0].span.hi(), String::new())
} else {
let type_list: Vec<_> =
try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
@ -481,11 +481,11 @@ fn rewrite_method_call(
format!("::<{}>", type_list.join(", "))
};
(types.last().unwrap().span.hi, type_str)
(types.last().unwrap().span.hi(), type_str)
};
let callee_str = format!(".{}{}", method_name, type_str);
let span = mk_sp(lo, span.hi);
let span = mk_sp(lo, span.hi());
rewrite_call(context, &callee_str, &args[1..], span, shape)
}

View File

@ -50,7 +50,7 @@ impl SpanUtils for CodeMap {
let snippet = self.span_to_snippet(original).unwrap();
let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
original.lo + BytePos(offset as u32)
original.lo() + BytePos(offset as u32)
}
fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
@ -61,21 +61,21 @@ impl SpanUtils for CodeMap {
offset += additional_offset + needle.len();
}
original.lo + BytePos(offset as u32)
original.lo() + BytePos(offset as u32)
}
fn span_before(&self, original: Span, needle: &str) -> BytePos {
let snippet = self.span_to_snippet(original).unwrap();
let offset = snippet.find_uncommented(needle).unwrap();
original.lo + BytePos(offset as u32)
original.lo() + BytePos(offset as u32)
}
}
impl LineRangeUtils for CodeMap {
fn lookup_line_range(&self, span: Span) -> LineRange {
let lo = self.lookup_char_pos(span.lo);
let hi = self.lookup_char_pos(span.hi);
let lo = self.lookup_char_pos(span.lo());
let hi = self.lookup_char_pos(span.hi());
assert!(
lo.file.name == hi.file.name,

View File

@ -65,7 +65,7 @@ pub fn format_expr(
let expr_rw = match expr.node {
ast::ExprKind::Array(ref expr_vec) => rewrite_array(
expr_vec.iter().map(|e| &**e),
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi()),
context,
shape,
false,
@ -81,7 +81,7 @@ pub fn format_expr(
),
},
ast::ExprKind::Call(ref callee, ref args) => {
let inner_span = mk_sp(callee.span.hi, expr.span.hi);
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
rewrite_call_with_binary_search(
context,
&**callee,
@ -308,8 +308,8 @@ pub fn format_expr(
let attrs = outer_attributes(&expr.attrs);
let attrs_str = try_opt!(attrs.rewrite(context, shape));
let span = mk_sp(
attrs.last().map_or(expr.span.lo, |attr| attr.span.hi),
expr.span.lo,
attrs.last().map_or(expr.span.lo(), |attr| attr.span.hi()),
expr.span.lo(),
);
combine_strs_with_missing_comments(context, &attrs_str, &expr_str, span, shape, false)
})
@ -417,11 +417,11 @@ where
context.codemap,
expr_iter,
"]",
|item| item.span.lo,
|item| item.span.hi,
|item| item.span.lo(),
|item| item.span.hi(),
|item| item.rewrite(context, nested_shape),
span.lo,
span.hi,
span.lo(),
span.hi(),
false,
).collect::<Vec<_>>();
@ -536,7 +536,7 @@ fn rewrite_closure_fn_decl(
|arg| span_hi_for_arg(context, arg),
|arg| arg.rewrite(context, arg_shape),
context.codemap.span_after(span, "|"),
body.span.lo,
body.span.lo(),
false,
);
let item_vec = arg_items.collect::<Vec<_>>();
@ -837,9 +837,9 @@ fn rewrite_block_with_visitor(
ast::BlockCheckMode::Unsafe(..) => {
let snippet = context.snippet(block.span);
let open_pos = try_opt!(snippet.find_uncommented("{"));
visitor.last_pos = block.span.lo + BytePos(open_pos as u32)
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
}
ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo,
ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
}
visitor.visit_block(block, None);
@ -1193,7 +1193,7 @@ impl<'a> ControlFlow<'a> {
let cond_span = if let Some(cond) = self.cond {
cond.span
} else {
mk_sp(self.block.span.lo, self.block.span.lo)
mk_sp(self.block.span.lo(), self.block.span.lo())
};
// `for event in event`
@ -1204,8 +1204,8 @@ impl<'a> ControlFlow<'a> {
.codemap
.span_after(mk_sp(lo, self.span.hi), self.keyword.trim()),
self.pat
.map_or(cond_span.lo, |p| if self.matcher.is_empty() {
p.span.lo
.map_or(cond_span.lo(), |p| if self.matcher.is_empty() {
p.span.lo()
} else {
context.codemap.span_before(self.span, self.matcher.trim())
}),
@ -1214,7 +1214,7 @@ impl<'a> ControlFlow<'a> {
let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
let after_cond_comment =
extract_comment(mk_sp(cond_span.hi, self.block.span.lo), context, shape);
extract_comment(mk_sp(cond_span.hi(), self.block.span.lo()), context, shape);
let block_sep = if self.cond.is_none() && between_kwd_cond_comment.is_some() {
""
@ -1305,7 +1305,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
next_else_block.as_ref().map(|e| &**e),
false,
true,
mk_sp(else_block.span.lo, self.span.hi),
mk_sp(else_block.span.lo(), self.span.hi()),
).rewrite(context, shape)
}
ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
@ -1316,7 +1316,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
next_else_block.as_ref().map(|e| &**e),
false,
true,
mk_sp(else_block.span.lo, self.span.hi),
mk_sp(else_block.span.lo(), self.span.hi()),
).rewrite(context, shape)
}
_ => {
@ -1332,10 +1332,10 @@ impl<'a> Rewrite for ControlFlow<'a> {
};
let between_kwd_else_block = mk_sp(
self.block.span.hi,
self.block.span.hi(),
context
.codemap
.span_before(mk_sp(self.block.span.hi, else_block.span.lo), "else"),
.span_before(mk_sp(self.block.span.hi(), else_block.span.lo()), "else"),
);
let between_kwd_else_block_comment =
extract_comment(between_kwd_else_block, context, shape);
@ -1343,8 +1343,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
let after_else = mk_sp(
context
.codemap
.span_after(mk_sp(self.block.span.hi, else_block.span.lo), "else"),
else_block.span.lo,
.span_after(mk_sp(self.block.span.hi(), else_block.span.lo()), "else"),
else_block.span.lo(),
);
let after_else_comment = extract_comment(after_else, context, shape);
@ -1504,9 +1504,9 @@ fn rewrite_match(
let open_brace_pos = if inner_attrs.is_empty() {
context
.codemap
.span_after(mk_sp(cond.span.hi, arms[0].span().lo), "{")
.span_after(mk_sp(cond.span.hi(), arms[0].span().lo()), "{")
} else {
inner_attrs[inner_attrs.len() - 1].span().hi
inner_attrs[inner_attrs.len() - 1].span().hi()
};
let arm_indent_str = if context.config.indent_match_arms() {
@ -1571,11 +1571,11 @@ fn rewrite_match_arms(
.zip(is_last_iter)
.map(|(arm, is_last)| ArmWrapper::new(arm, is_last)),
"}",
|arm| arm.arm.span().lo,
|arm| arm.arm.span().hi,
|arm| arm.arm.span().lo(),
|arm| arm.arm.span().hi(),
|arm| arm.rewrite(context, arm_shape),
open_brace_pos,
span.hi,
span.hi(),
false,
);
let arms_vec: Vec<_> = items.collect();
@ -1611,7 +1611,7 @@ fn rewrite_match_arm(
));
}
(
mk_sp(arm.attrs[arm.attrs.len() - 1].span.hi, arm.pats[0].span.lo),
mk_sp(arm.attrs[arm.attrs.len() - 1].span.hi(), arm.pats[0].span.lo()),
try_opt!(arm.attrs.rewrite(context, shape)),
)
} else {
@ -1973,7 +1973,7 @@ fn string_requires_rewrite(
string: &str,
shape: Shape,
) -> bool {
if context.codemap.lookup_char_pos(span.lo).col.0 != shape.indent.width() {
if context.codemap.lookup_char_pos(span.lo()).col.0 != shape.indent.width() {
return true;
}
@ -2087,7 +2087,7 @@ where
).ok_or(Ordering::Greater)?;
let span_lo = context.codemap.span_after(span, "(");
let args_span = mk_sp(span_lo, span.hi);
let args_span = mk_sp(span_lo, span.hi());
let (extendable, list_str) = rewrite_call_args(
context,
@ -2146,11 +2146,11 @@ where
context.codemap,
args.iter(),
")",
|item| item.span().lo,
|item| item.span().hi,
|item| item.span().lo(),
|item| item.span().hi(),
|item| item.rewrite(context, shape),
span.lo,
span.hi,
span.lo(),
span.hi(),
true,
);
let mut item_vec: Vec<_> = items.collect();
@ -2569,7 +2569,7 @@ fn rewrite_struct_lit<'a>(
fields,
context,
shape,
mk_sp(body_lo, span.hi),
mk_sp(body_lo, span.hi()),
one_line_width,
))
} else {
@ -2579,17 +2579,17 @@ fn rewrite_struct_lit<'a>(
.chain(base.into_iter().map(StructLitField::Base));
let span_lo = |item: &StructLitField| match *item {
StructLitField::Regular(field) => field.span().lo,
StructLitField::Regular(field) => field.span().lo(),
StructLitField::Base(expr) => {
let last_field_hi = fields.last().map_or(span.lo, |field| field.span.hi);
let snippet = context.snippet(mk_sp(last_field_hi, expr.span.lo));
let last_field_hi = fields.last().map_or(span.lo(), |field| field.span.hi());
let snippet = context.snippet(mk_sp(last_field_hi, expr.span.lo()));
let pos = snippet.find_uncommented("..").unwrap();
last_field_hi + BytePos(pos as u32)
}
};
let span_hi = |item: &StructLitField| match *item {
StructLitField::Regular(field) => field.span().hi,
StructLitField::Base(expr) => expr.span.hi,
StructLitField::Regular(field) => field.span().hi(),
StructLitField::Base(expr) => expr.span.hi(),
};
let rewrite = |item: &StructLitField| match *item {
StructLitField::Regular(field) => {
@ -2611,7 +2611,7 @@ fn rewrite_struct_lit<'a>(
span_hi,
rewrite,
body_lo,
span.hi,
span.hi(),
false,
);
let item_vec = items.collect::<Vec<_>>();
@ -2760,11 +2760,11 @@ where
context.codemap,
items,
")",
|item| item.span().lo,
|item| item.span().hi,
|item| item.span().lo(),
|item| item.span().hi(),
|item| item.rewrite(context, nested_shape),
list_lo,
span.hi - BytePos(1),
span.hi() - BytePos(1),
false,
);
let item_vec: Vec<_> = items.collect();

View File

@ -198,9 +198,9 @@ impl<'a> FmtVisitor<'a> {
self.last_pos,
p_i.attrs
.iter()
.map(|attr| attr.span.lo)
.map(|attr| attr.span.lo())
.min()
.unwrap_or(p_i.span.lo),
.unwrap_or(p_i.span.lo()),
)
})
.unwrap_or(self.last_pos);
@ -211,7 +211,7 @@ impl<'a> FmtVisitor<'a> {
.iter()
.map(|p_i| {
let new_item = (&*p_i, last_pos_of_prev_use_item);
last_pos_of_prev_use_item = p_i.span.hi;
last_pos_of_prev_use_item = p_i.span.hi();
new_item
})
.collect::<Vec<_>>();
@ -275,23 +275,23 @@ impl<'a> FmtVisitor<'a> {
match rw {
Some(ref s) if s.is_empty() => {
// Format up to last newline
let prev_span = utils::mk_sp(self.last_pos, source!(self, span).lo);
let prev_span = utils::mk_sp(self.last_pos, source!(self, span).lo());
let span_end = match self.snippet(prev_span).rfind('\n') {
Some(offset) => self.last_pos + BytePos(offset as u32),
None => source!(self, span).lo,
None => source!(self, span).lo(),
};
self.format_missing(span_end);
self.last_pos = source!(self, span).hi;
self.last_pos = source!(self, span).hi();
}
Some(ref s) => {
let s = format!("{}use {};", vis, s);
self.format_missing_with_indent(source!(self, span).lo);
self.format_missing_with_indent(source!(self, span).lo());
self.buffer.push_str(&s);
self.last_pos = source!(self, span).hi;
self.last_pos = source!(self, span).hi();
}
None => {
self.format_missing_with_indent(source!(self, span).lo);
self.format_missing(source!(self, span).hi);
self.format_missing_with_indent(source!(self, span).lo());
self.format_missing(source!(self, span).hi());
}
}
}
@ -442,11 +442,11 @@ fn rewrite_use_list(
context.codemap,
path_list.iter(),
"}",
|vpi| vpi.span.lo,
|vpi| vpi.span.hi,
|vpi| vpi.span.lo(),
|vpi| vpi.span.hi(),
rewrite_path_item,
context.codemap.span_after(span, "{"),
span.hi,
span.hi(),
false,
);
items.extend(iter);

View File

@ -167,11 +167,11 @@ impl<'a> FmtVisitor<'a> {
if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
// FIXME: this skips comments between the extern keyword and the opening
// brace.
self.last_pos = item.span.lo + BytePos(brace_pos as u32 + 1);
self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
self.block_indent = self.block_indent.block_indent(self.config);
if item.body.is_empty() {
self.format_missing_no_indent(item.span.hi - BytePos(1));
self.format_missing_no_indent(item.span.hi() - BytePos(1));
self.block_indent = self.block_indent.block_unindent(self.config);
self.buffer
@ -182,12 +182,12 @@ impl<'a> FmtVisitor<'a> {
}
self.block_indent = self.block_indent.block_unindent(self.config);
self.format_missing_with_indent(item.span.hi - BytePos(1));
self.format_missing_with_indent(item.span.hi() - BytePos(1));
}
}
self.buffer.push_str("}");
self.last_pos = item.span.hi;
self.last_pos = item.span.hi();
}
fn format_body_element(&mut self, element: &BodyElement) {
@ -205,7 +205,7 @@ impl<'a> FmtVisitor<'a> {
fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
let rewrite = item.rewrite(&self.get_context(), self.shape());
self.push_rewrite(item.span(), rewrite);
self.last_pos = item.span.hi;
self.last_pos = item.span.hi();
}
pub fn rewrite_fn(
@ -224,7 +224,7 @@ impl<'a> FmtVisitor<'a> {
) -> Option<String> {
let context = self.get_context();
let block_snippet = self.snippet(mk_sp(block.span.lo, block.span.hi));
let block_snippet = self.snippet(mk_sp(block.span.lo(), block.span.hi()));
let has_body = !block_snippet[1..block_snippet.len() - 1].trim().is_empty() ||
!context.config.fn_empty_single_line();
let mut newline_brace = newline_for_brace(self.config, &generics.where_clause, has_body);
@ -277,7 +277,7 @@ impl<'a> FmtVisitor<'a> {
span: Span,
) -> Option<String> {
// Drop semicolon or it will be interpreted as comment.
let span = mk_sp(span.lo, span.hi - BytePos(1));
let span = mk_sp(span.lo(), span.hi() - BytePos(1));
let context = self.get_context();
let (mut result, _) = try_opt!(rewrite_fn_base(
@ -362,7 +362,7 @@ impl<'a> FmtVisitor<'a> {
let enum_snippet = self.snippet(span);
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
let body_start = span.lo + BytePos(brace_pos as u32 + 1);
let body_start = span.lo() + BytePos(brace_pos as u32 + 1);
let generics_str = format_generics(
&self.get_context(),
generics,
@ -371,7 +371,7 @@ impl<'a> FmtVisitor<'a> {
self.config.item_brace_style(),
enum_def.variants.is_empty(),
self.block_indent,
mk_sp(span.lo, body_start),
mk_sp(span.lo(), body_start),
last_line_width(&enum_header),
).unwrap();
self.buffer.push_str(&generics_str);
@ -379,13 +379,13 @@ impl<'a> FmtVisitor<'a> {
self.last_pos = body_start;
self.block_indent = self.block_indent.block_indent(self.config);
let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1));
let variant_list = self.format_variant_list(enum_def, body_start, span.hi() - BytePos(1));
match variant_list {
Some(ref body_str) => self.buffer.push_str(body_str),
None => if contains_comment(&enum_snippet[brace_pos..]) {
self.format_missing_no_indent(span.hi - BytePos(1))
self.format_missing_no_indent(span.hi() - BytePos(1))
} else {
self.format_missing(span.hi - BytePos(1))
self.format_missing(span.hi() - BytePos(1))
},
}
self.block_indent = self.block_indent.block_unindent(self.config);
@ -395,7 +395,7 @@ impl<'a> FmtVisitor<'a> {
.push_str(&self.block_indent.to_string(self.config));
}
self.buffer.push_str("}");
self.last_pos = span.hi;
self.last_pos = span.hi();
}
// Format the body of an enum definition
@ -418,11 +418,11 @@ impl<'a> FmtVisitor<'a> {
enum_def.variants.iter(),
"}",
|f| if !f.node.attrs.is_empty() {
f.node.attrs[0].span.lo
f.node.attrs[0].span.lo()
} else {
f.span.lo
f.span.lo()
},
|f| f.span.hi,
|f| f.span.hi(),
|f| self.format_variant(f),
body_lo,
body_hi,
@ -450,8 +450,8 @@ impl<'a> FmtVisitor<'a> {
// Variant of an enum.
fn format_variant(&self, field: &ast::Variant) -> Option<String> {
if contains_skip(&field.node.attrs) {
let lo = field.node.attrs[0].span.lo;
let span = mk_sp(lo, field.span.hi);
let lo = field.node.attrs[0].span.lo();
let span = mk_sp(lo, field.span.hi());
return Some(self.snippet(span));
}
@ -463,8 +463,8 @@ impl<'a> FmtVisitor<'a> {
.node
.attrs
.last()
.map_or(field.span.lo, |attr| attr.span.hi);
let span = mk_sp(lo, field.span.lo);
.map_or(field.span.lo(), |attr| attr.span.hi());
let span = mk_sp(lo, field.span.lo());
let variant_body = match field.node.data {
ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => {
@ -547,7 +547,7 @@ pub fn format_impl(
context.config.where_density(),
"{",
where_span_end,
self_ty.span.hi,
self_ty.span.hi(),
option,
));
@ -612,14 +612,14 @@ pub fn format_impl(
if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
visitor.block_indent = offset.block_only().block_indent(context.config);
visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
visitor.last_pos = item.span.lo() + BytePos(open_pos as u32);
visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
for item in items {
visitor.visit_impl_item(item);
}
visitor.format_missing(item.span.hi - BytePos(1));
visitor.format_missing(item.span.hi() - BytePos(1));
let inner_indent_str = visitor.block_indent.to_string(context.config);
let outer_indent_str = offset.block_only().to_string(context.config);
@ -683,8 +683,8 @@ fn format_impl_ref_and_type(
let lo = context.codemap.span_after(item.span, "impl");
let hi = match *trait_ref {
Some(ref tr) => tr.path.span.lo,
None => self_ty.span.lo,
Some(ref tr) => tr.path.span.lo(),
None => self_ty.span.lo(),
};
let shape = try_opt!(generics_shape_from_config(
context.config,
@ -891,7 +891,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
context,
generics,
shape,
mk_sp(item.span.lo, body_lo),
mk_sp(item.span.lo(), body_lo),
));
result.push_str(&generics_str);
@ -931,9 +931,9 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
.checked_sub(last_line_width(&result))
);
let pos_before_where = if type_param_bounds.is_empty() {
generics.where_clause.span.lo
generics.where_clause.span.lo()
} else {
type_param_bounds[type_param_bounds.len() - 1].span().hi
type_param_bounds[type_param_bounds.len() - 1].span().hi()
};
let option = WhereClauseOption::snuggled(&generics_str);
let where_clause_str = try_opt!(rewrite_where_clause(
@ -1009,13 +1009,13 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
visitor.block_indent = offset.block_only().block_indent(context.config);
visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
visitor.last_pos = item.span.lo() + BytePos(open_pos as u32);
for item in trait_items {
visitor.visit_trait_item(item);
}
visitor.format_missing(item.span.hi - BytePos(1));
visitor.format_missing(item.span.hi() - BytePos(1));
let inner_indent_str = visitor.block_indent.to_string(context.config);
let outer_indent_str = offset.block_only().to_string(context.config);
@ -1067,7 +1067,7 @@ pub fn format_struct_struct(
context.config.item_brace_style(),
fields.is_empty(),
offset,
mk_sp(span.lo, body_lo),
mk_sp(span.lo(), body_lo),
last_line_width(&result),
)),
None => {
@ -1103,7 +1103,7 @@ pub fn format_struct_struct(
}
if fields.is_empty() {
let snippet = context.snippet(mk_sp(body_lo, span.hi - BytePos(1)));
let snippet = context.snippet(mk_sp(body_lo, span.hi() - BytePos(1)));
if snippet.trim().is_empty() {
// `struct S {}`
} else if snippet.trim_right_matches(&[' ', '\t'][..]).ends_with('\n') {
@ -1131,7 +1131,7 @@ pub fn format_struct_struct(
fields,
context,
Shape::indented(offset, context.config),
mk_sp(body_lo, span.hi),
mk_sp(body_lo, span.hi()),
one_line_budget,
));
@ -1168,7 +1168,7 @@ fn format_tuple_struct(
let body_lo = if fields.is_empty() {
context.codemap.span_after(span, "(")
} else {
fields[0].span.lo
fields[0].span.lo()
};
let body_hi = if fields.is_empty() {
context.codemap.span_after(span, ")")
@ -1176,11 +1176,11 @@ fn format_tuple_struct(
// This is a dirty hack to work around a missing `)` from the span of the last field.
let last_arg_span = fields[fields.len() - 1].span;
if context.snippet(last_arg_span).ends_with(")") {
last_arg_span.hi
last_arg_span.hi()
} else {
context
.codemap
.span_after(mk_sp(last_arg_span.hi, span.hi), ")")
.span_after(mk_sp(last_arg_span.hi(), span.hi()), ")")
}
};
@ -1188,7 +1188,7 @@ fn format_tuple_struct(
Some(generics) => {
let budget = context.budget(last_line_width(&header_str));
let shape = Shape::legacy(budget, offset);
let g_span = mk_sp(span.lo, body_lo);
let g_span = mk_sp(span.lo(), body_lo);
let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
result.push_str(&generics_str);
@ -1279,7 +1279,7 @@ pub fn rewrite_type_alias(
// 2 = `= `
let shape = try_opt!(Shape::indented(indent + result.len(), context.config).sub_width(2));
let g_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
let g_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo());
let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
result.push_str(&generics_str);
@ -1297,8 +1297,8 @@ pub fn rewrite_type_alias(
Shape::legacy(where_budget, indent),
context.config.where_density(),
"=",
Some(span.hi),
generics.span.hi,
Some(span.hi()),
generics.span.hi(),
option,
));
result.push_str(&where_clause_str);
@ -1396,7 +1396,7 @@ pub fn rewrite_struct_field(
lhs_max_width: usize,
) -> Option<String> {
if contains_skip(&field.attrs) {
let span = context.snippet(mk_sp(field.attrs[0].span.lo, field.span.hi));
let span = context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi()));
return wrap_str(span, context.config.max_width(), shape);
}
@ -1407,9 +1407,9 @@ pub fn rewrite_struct_field(
let attrs_extendable = attrs_str.is_empty() ||
(context.config.attributes_on_same_line_as_field() && is_attributes_extendable(&attrs_str));
let missing_span = if field.attrs.is_empty() {
mk_sp(field.span.lo, field.span.lo)
mk_sp(field.span.lo(), field.span.lo())
} else {
mk_sp(field.attrs.last().unwrap().span.hi, field.span.lo)
mk_sp(field.attrs.last().unwrap().span.hi(), field.span.lo())
};
let mut spacing = String::from(if field.ident.is_some() {
type_annotation_spacing.1
@ -1706,17 +1706,17 @@ fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability {
pub fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
if is_named_arg(arg) {
arg.pat.span.lo
arg.pat.span.lo()
} else {
arg.ty.span.lo
arg.ty.span.lo()
}
}
pub fn span_hi_for_arg(context: &RewriteContext, arg: &ast::Arg) -> BytePos {
match arg.ty.node {
ast::TyKind::Infer if context.snippet(arg.ty.span) == "_" => arg.ty.span.hi,
ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi,
_ => arg.ty.span.hi,
ast::TyKind::Infer if context.snippet(arg.ty.span) == "_" => arg.ty.span.hi(),
ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi(),
_ => arg.ty.span.hi(),
}
}
@ -1782,7 +1782,7 @@ fn rewrite_fn_base(
indent: indent,
offset: used_width,
};
let g_span = mk_sp(span.lo, fd.output.span().lo);
let g_span = mk_sp(span.lo(), fd.output.span().lo());
let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
result.push_str(&generics_str);
@ -1850,15 +1850,19 @@ fn rewrite_fn_base(
let args_start = generics
.ty_params
.last()
.map_or(span.lo, |tp| end_typaram(tp));
.map_or(span.lo(), |tp| end_typaram(tp));
let args_end = if fd.inputs.is_empty() {
context.codemap.span_after(mk_sp(args_start, span.hi), ")")
context
.codemap
.span_after(mk_sp(args_start, span.hi()), ")")
} else {
let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi, span.hi);
let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi(), span.hi());
context.codemap.span_after(last_span, ")")
};
let args_span = mk_sp(
context.codemap.span_after(mk_sp(args_start, span.hi), "("),
context
.codemap
.span_after(mk_sp(args_start, span.hi()), "("),
args_end,
);
let arg_str = try_opt!(rewrite_args(
@ -1970,9 +1974,9 @@ fn rewrite_fn_base(
}
// Comment between return type and the end of the decl.
let snippet_lo = fd.output.span().hi;
let snippet_lo = fd.output.span().hi();
if where_clause.predicates.is_empty() {
let snippet_hi = span.hi;
let snippet_hi = span.hi();
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
// Try to preserve the layout of the original snippet.
let original_starts_with_newline = snippet
@ -2003,8 +2007,8 @@ fn rewrite_fn_base(
};
let pos_before_where = match fd.output {
ast::FunctionRetTy::Default(..) => args_span.hi,
ast::FunctionRetTy::Ty(ref ty) => ty.span.hi,
ast::FunctionRetTy::Default(..) => args_span.hi(),
ast::FunctionRetTy::Ty(ref ty) => ty.span.hi(),
};
if where_clause.predicates.len() == 1 && should_compress_where {
@ -2020,7 +2024,7 @@ fn rewrite_fn_base(
Shape::legacy(budget, indent),
Density::Compressed,
"{",
Some(span.hi),
Some(span.hi()),
pos_before_where,
WhereClauseOption::compressed(),
) {
@ -2038,7 +2042,7 @@ fn rewrite_fn_base(
Shape::indented(indent, context.config),
Density::Tall,
"{",
Some(span.hi),
Some(span.hi()),
pos_before_where,
option,
));
@ -2047,7 +2051,7 @@ fn rewrite_fn_base(
if where_clause_str.is_empty() {
if let ast::FunctionRetTy::Default(ret_span) = fd.output {
match recover_missing_comment_in_span(
mk_sp(args_span.hi, ret_span.hi),
mk_sp(args_span.hi(), ret_span.hi()),
shape,
context,
last_line_width(&result),
@ -2144,15 +2148,15 @@ fn rewrite_args(
if args.len() >= min_args || variadic {
let comment_span_start = if min_args == 2 {
let second_arg_start = if arg_has_pattern(&args[1]) {
args[1].pat.span.lo
args[1].pat.span.lo()
} else {
args[1].ty.span.lo
args[1].ty.span.lo()
};
let reduced_span = mk_sp(span.lo, second_arg_start);
let reduced_span = mk_sp(span.lo(), second_arg_start);
context.codemap.span_after_last(reduced_span, ",")
} else {
span.lo
span.lo()
};
enum ArgumentKind<'a> {
@ -2161,7 +2165,7 @@ fn rewrite_args(
}
let variadic_arg = if variadic {
let variadic_span = mk_sp(args.last().unwrap().ty.span.hi, span.hi);
let variadic_span = mk_sp(args.last().unwrap().ty.span.hi(), span.hi());
let variadic_start = context.codemap.span_after(variadic_span, "...") - BytePos(3);
Some(ArgumentKind::Variadic(variadic_start))
} else {
@ -2180,7 +2184,7 @@ fn rewrite_args(
ArgumentKind::Variadic(start) => start,
},
|arg| match *arg {
ArgumentKind::Regular(arg) => arg.ty.span.hi,
ArgumentKind::Regular(arg) => arg.ty.span.hi(),
ArgumentKind::Variadic(start) => start + BytePos(3),
},
|arg| match *arg {
@ -2188,7 +2192,7 @@ fn rewrite_args(
ArgumentKind::Variadic(..) => Some("...".to_owned()),
},
comment_span_start,
span.hi,
span.hi(),
false,
);
@ -2376,11 +2380,11 @@ fn rewrite_generics_inner(
// Extract comments between generics.
let lt_spans = lifetimes.iter().map(|l| {
let hi = if l.bounds.is_empty() {
l.lifetime.span.hi
l.lifetime.span.hi()
} else {
l.bounds[l.bounds.len() - 1].span.hi
l.bounds[l.bounds.len() - 1].span.hi()
};
mk_sp(l.lifetime.span.lo, hi)
mk_sp(l.lifetime.span.lo(), hi)
});
let ty_spans = tys.iter().map(|ty| ty.span());
@ -2388,12 +2392,12 @@ fn rewrite_generics_inner(
context.codemap,
lt_spans.chain(ty_spans).zip(lt_strs.chain(ty_strs)),
">",
|&(sp, _)| sp.lo,
|&(sp, _)| sp.hi,
|&(sp, _)| sp.lo(),
|&(sp, _)| sp.hi(),
// FIXME: don't clone
|&(_, ref str)| str.clone(),
context.codemap.span_after(span, "<"),
span.hi,
span.hi(),
false,
);
format_generics_item_list(context, items, shape, one_line_width)
@ -2524,18 +2528,18 @@ fn rewrite_where_clause_rfc_style(
let clause_shape = block_shape.block_indent(context.config.tab_spaces());
// each clause on one line, trailing comma (except if suppress_comma)
let span_start = where_clause.predicates[0].span().lo;
let span_start = where_clause.predicates[0].span().lo();
// If we don't have the start of the next span, then use the end of the
// predicates, but that means we miss comments.
let len = where_clause.predicates.len();
let end_of_preds = where_clause.predicates[len - 1].span().hi;
let end_of_preds = where_clause.predicates[len - 1].span().hi();
let span_end = span_end.unwrap_or(end_of_preds);
let items = itemize_list(
context.codemap,
where_clause.predicates.iter(),
terminator,
|pred| pred.span().lo,
|pred| pred.span().hi,
|pred| pred.span().lo(),
|pred| pred.span().hi(),
|pred| pred.rewrite(context, block_shape),
span_start,
span_end,
@ -2626,18 +2630,18 @@ fn rewrite_where_clause(
// be out by a char or two.
let budget = context.config.max_width() - offset.width();
let span_start = where_clause.predicates[0].span().lo;
let span_start = where_clause.predicates[0].span().lo();
// If we don't have the start of the next span, then use the end of the
// predicates, but that means we miss comments.
let len = where_clause.predicates.len();
let end_of_preds = where_clause.predicates[len - 1].span().hi;
let end_of_preds = where_clause.predicates[len - 1].span().hi();
let span_end = span_end.unwrap_or(end_of_preds);
let items = itemize_list(
context.codemap,
where_clause.predicates.iter(),
terminator,
|pred| pred.span().lo,
|pred| pred.span().hi,
|pred| pred.span().lo(),
|pred| pred.span().hi(),
|pred| pred.rewrite(context, Shape::legacy(budget, offset)),
span_start,
span_end,
@ -2700,10 +2704,10 @@ fn missing_span_before_after_where(
before_item_span_end: BytePos,
where_clause: &ast::WhereClause,
) -> (Span, Span) {
let missing_span_before = mk_sp(before_item_span_end, where_clause.span.lo);
let missing_span_before = mk_sp(before_item_span_end, where_clause.span.lo());
// 5 = `where`
let pos_after_where = where_clause.span.lo + BytePos(5);
let missing_span_after = mk_sp(pos_after_where, where_clause.predicates[0].span().lo);
let pos_after_where = where_clause.span.lo() + BytePos(5);
let missing_span_after = mk_sp(pos_after_where, where_clause.predicates[0].span().lo());
(missing_span_before, missing_span_after)
}
@ -2754,8 +2758,8 @@ fn format_generics(
Shape::legacy(budget, offset.block_only()),
Density::Tall,
terminator,
Some(span.hi),
generics.span.hi,
Some(span.hi()),
generics.span.hi(),
option,
));
result.push_str(&where_clause_str);
@ -2796,7 +2800,7 @@ impl Rewrite for ast::ForeignItem {
let attrs_str = try_opt!(self.attrs.rewrite(context, shape));
// Drop semicolon or it will be interpreted as comment.
// FIXME: this may be a faulty span from libsyntax.
let span = mk_sp(self.span.lo, self.span.hi - BytePos(1));
let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
let item_str = try_opt!(match self.node {
ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => {
@ -2841,9 +2845,9 @@ impl Rewrite for ast::ForeignItem {
});
let missing_span = if self.attrs.is_empty() {
mk_sp(self.span.lo, self.span.lo)
mk_sp(self.span.lo(), self.span.lo())
} else {
mk_sp(self.attrs[self.attrs.len() - 1].span.hi, self.span.lo)
mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
};
combine_strs_with_missing_comments(
context,

View File

@ -86,7 +86,7 @@ macro_rules! span_with_attrs_lo_hi {
if attrs.is_empty() {
mk_sp($lo, $hi)
} else {
mk_sp(attrs[0].span.lo, $hi)
mk_sp(attrs[0].span.lo(), $hi)
}
}
}
@ -94,7 +94,7 @@ macro_rules! span_with_attrs_lo_hi {
macro_rules! span_with_attrs {
($this:ident) => {
span_with_attrs_lo_hi!($this, $this.span.lo, $this.span.hi)
span_with_attrs_lo_hi!($this, $this.span.lo(), $this.span.hi())
}
}
@ -121,7 +121,7 @@ impl Spanned for ast::Stmt {
ast::StmtKind::Local(ref local) => mk_sp(local.span().lo, self.span.hi),
ast::StmtKind::Item(ref item) => mk_sp(item.span().lo, self.span.hi),
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
mk_sp(expr.span().lo, self.span.hi)
mk_sp(expr.span().lo(), self.span.hi())
}
ast::StmtKind::Mac(ref mac) => {
let (_, _, ref attrs) = **mac;
@ -149,14 +149,14 @@ impl Spanned for ast::Ty {
impl Spanned for ast::Arm {
fn span(&self) -> Span {
span_with_attrs_lo_hi!(self, self.pats[0].span.lo, self.body.span.hi)
span_with_attrs_lo_hi!(self, self.pats[0].span.lo(), self.body.span.hi())
}
}
impl Spanned for ast::Arg {
fn span(&self) -> Span {
if items::is_named_arg(self) {
utils::mk_sp(self.pat.span.lo, self.ty.span.hi)
utils::mk_sp(self.pat.span.lo(), self.ty.span.hi())
} else {
self.ty.span
}
@ -165,7 +165,7 @@ impl Spanned for ast::Arg {
impl Spanned for ast::StructField {
fn span(&self) -> Span {
span_with_attrs_lo_hi!(self, self.span.lo, self.ty.span.hi)
span_with_attrs_lo_hi!(self, self.span.lo(), self.ty.span.hi())
}
}
@ -192,17 +192,17 @@ impl Spanned for ast::TyParam {
fn span(&self) -> Span {
// Note that ty.span is the span for ty.ident, not the whole item.
let lo = if self.attrs.is_empty() {
self.span.lo
self.span.lo()
} else {
self.attrs[0].span.lo
self.attrs[0].span.lo()
};
if let Some(ref def) = self.default {
return mk_sp(lo, def.span.hi);
return mk_sp(lo, def.span.hi());
}
if self.bounds.is_empty() {
return mk_sp(lo, self.span.hi);
return mk_sp(lo, self.span.hi());
}
let hi = self.bounds[self.bounds.len() - 1].span().hi;
let hi = self.bounds[self.bounds.len() - 1].span().hi();
mk_sp(lo, hi)
}
}
@ -705,7 +705,7 @@ where
}
{
let mut visitor = FmtVisitor::from_codemap(parse_session, config);
let filemap = visitor.codemap.lookup_char_pos(module.inner.lo).file;
let filemap = visitor.codemap.lookup_char_pos(module.inner.lo()).file;
// Format inner attributes if available.
if !krate.attrs.is_empty() && path == main_file {
visitor.visit_attrs(&krate.attrs, ast::AttrStyle::Inner);

View File

@ -234,7 +234,7 @@ pub fn rewrite_macro(
context
.codemap
.span_after(mac.span, original_style.opener()),
mac.span.hi - BytePos(1),
mac.span.hi() - BytePos(1),
),
context,
mac_shape,

View File

@ -82,14 +82,14 @@ impl<'a> FmtVisitor<'a> {
// Get a snippet from the file start to the span's hi without allocating.
// We need it to determine what precedes the current comment. If the comment
// follows code on the same line, we won't touch it.
let big_span_lo = self.codemap.lookup_char_pos(span.lo).file.start_pos;
let big_span_lo = self.codemap.lookup_char_pos(span.lo()).file.start_pos;
let local_begin = self.codemap.lookup_byte_offset(big_span_lo);
let local_end = self.codemap.lookup_byte_offset(span.hi);
let local_end = self.codemap.lookup_byte_offset(span.hi());
let start_index = local_begin.pos.to_usize();
let end_index = local_end.pos.to_usize();
let big_snippet = &local_begin.fm.src.as_ref().unwrap()[start_index..end_index];
let big_diff = (span.lo - big_span_lo).to_usize();
let big_diff = (span.lo() - big_span_lo).to_usize();
let snippet = self.snippet(span.clone());
debug!("write_snippet `{}`", snippet);
@ -114,7 +114,7 @@ impl<'a> FmtVisitor<'a> {
let mut last_wspace = None;
let mut rewrite_next_comment = true;
let char_pos = self.codemap.lookup_char_pos(span.lo);
let char_pos = self.codemap.lookup_char_pos(span.lo());
let file_name = &char_pos.file.name;
let mut cur_line = char_pos.line;

View File

@ -157,11 +157,11 @@ fn rewrite_struct_pat(
context.codemap,
fields.iter(),
terminator,
|f| f.span.lo,
|f| f.span.hi,
|f| f.span.lo(),
|f| f.span.hi(),
|f| f.node.rewrite(context, v_shape),
context.codemap.span_after(span, "{"),
span.hi,
span.hi(),
false,
);
let item_vec = items.collect::<Vec<_>>();
@ -266,24 +266,24 @@ fn rewrite_tuple_pat(
if let Some(pos) = dotdot_pos {
let prev = if pos == 0 {
span.lo
span.lo()
} else {
pats[pos - 1].span().hi
pats[pos - 1].span().hi()
};
let next = if pos + 1 >= pats.len() {
span.hi
span.hi()
} else {
pats[pos + 1].span().lo
pats[pos + 1].span().lo()
};
let dot_span = mk_sp(prev, next);
let snippet = context.snippet(dot_span);
let lo = dot_span.lo + BytePos(snippet.find_uncommented("..").unwrap() as u32);
let dotdot = TuplePatField::Dotdot(Span {
lo: lo,
let lo = dot_span.lo() + BytePos(snippet.find_uncommented("..").unwrap() as u32);
let dotdot = TuplePatField::Dotdot(Span::new(
lo,
// 2 == "..".len()
hi: lo + BytePos(2),
ctxt: codemap::NO_EXPANSION,
});
lo + BytePos(2),
codemap::NO_EXPANSION,
));
pat_vec.insert(pos, dotdot);
}
@ -297,9 +297,12 @@ fn rewrite_tuple_pat(
let new_item_count = 1 + pat_vec.len() - wildcard_suffix_len;
let sp = pat_vec[new_item_count - 1].span();
let snippet = context.snippet(sp);
let lo = sp.lo + BytePos(snippet.find_uncommented("_").unwrap() as u32);
let lo = sp.lo() + BytePos(snippet.find_uncommented("_").unwrap() as u32);
pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp(lo, lo + BytePos(1)));
(&pat_vec[..new_item_count], mk_sp(span.lo, lo + BytePos(1)))
(
&pat_vec[..new_item_count],
mk_sp(span.lo(), lo + BytePos(1)),
)
} else {
(&pat_vec[..], span)
};
@ -338,11 +341,11 @@ fn count_wildcard_suffix_len(
context.codemap,
patterns.iter(),
")",
|item| item.span().lo,
|item| item.span().hi,
|item| item.span().lo(),
|item| item.span().hi(),
|item| item.rewrite(context, shape),
context.codemap.span_after(span, "("),
span.hi - BytePos(1),
span.hi() - BytePos(1),
false,
).collect();

View File

@ -50,7 +50,7 @@ pub fn rewrite_path(
String::new()
};
let mut span_lo = path.span.lo;
let mut span_lo = path.span.lo();
if let Some(qself) = qself {
result.push('<');
@ -76,7 +76,7 @@ pub fn rewrite_path(
result,
path.segments.iter().take(skip_count),
span_lo,
path.span.hi,
path.span.hi(),
context,
shape,
));
@ -87,7 +87,7 @@ pub fn rewrite_path(
}
result.push_str(">::");
span_lo = qself.ty.span.hi + BytePos(1);
span_lo = qself.ty.span.hi() + BytePos(1);
}
rewrite_path_segments(
@ -95,7 +95,7 @@ pub fn rewrite_path(
result,
path.segments.iter().skip(skip_count),
span_lo,
path.span.hi,
path.span.hi(),
context,
shape,
)
@ -218,7 +218,7 @@ fn rewrite_segment(
.chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
.collect::<Vec<_>>();
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
let next_span_lo = param_list.last().unwrap().get_span().hi() + BytePos(1);
let list_lo = context.codemap.span_after(mk_sp(*span_lo, span_hi), "<");
let separator = if path_context == PathContext::Expr {
"::"
@ -236,8 +236,8 @@ fn rewrite_segment(
context.codemap,
param_list.into_iter(),
">",
|param| param.get_span().lo,
|param| param.get_span().hi,
|param| param.get_span().lo(),
|param| param.get_span().hi(),
|seg| seg.rewrite(context, generics_shape),
list_lo,
span_hi,
@ -332,11 +332,11 @@ where
.chain(variadic_arg),
")",
|arg| match *arg {
ArgumentKind::Regular(ref ty) => ty.span().lo,
ArgumentKind::Regular(ref ty) => ty.span().lo(),
ArgumentKind::Variadic(start) => start,
},
|arg| match *arg {
ArgumentKind::Regular(ref ty) => ty.span().hi,
ArgumentKind::Regular(ref ty) => ty.span().hi(),
ArgumentKind::Variadic(start) => start + BytePos(3),
},
|arg| match *arg {
@ -344,7 +344,7 @@ where
ArgumentKind::Variadic(_) => Some("...".to_owned()),
},
list_lo,
span.hi,
span.hi(),
false,
);

View File

@ -208,7 +208,7 @@ pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
ast::RegionTyParamBound(ref lt) => lt.span,
ast::TraitTyParamBound(ref prt, _) => prt.span,
})
.hi
.hi()
}
#[inline]
@ -363,11 +363,7 @@ macro_rules! source {
}
pub fn mk_sp(lo: BytePos, hi: BytePos) -> Span {
Span {
lo,
hi,
ctxt: NO_EXPANSION,
}
Span::new(lo, hi, NO_EXPANSION)
}
// Return true if the given span does not intersect with file lines.

View File

@ -49,9 +49,9 @@ impl AlignedItem for ast::StructField {
fn rewrite_prefix(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
let attrs_str = try_opt!(self.attrs.rewrite(context, shape));
let missing_span = if self.attrs.is_empty() {
mk_sp(self.span.lo, self.span.lo)
mk_sp(self.span.lo(), self.span.lo())
} else {
mk_sp(self.attrs.last().unwrap().span.hi, self.span.lo)
mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
};
rewrite_struct_field_prefix(context, self).and_then(|field_str| {
combine_strs_with_missing_comments(
@ -88,9 +88,9 @@ impl AlignedItem for ast::Field {
let attrs_str = try_opt!(self.attrs.rewrite(context, shape));
let name = &self.ident.node.to_string();
let missing_span = if self.attrs.is_empty() {
mk_sp(self.span.lo, self.span.lo)
mk_sp(self.span.lo(), self.span.lo())
} else {
mk_sp(self.attrs.last().unwrap().span.hi, self.span.lo)
mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
};
combine_strs_with_missing_comments(
context,
@ -127,15 +127,15 @@ pub fn rewrite_with_alignment<T: AlignedItem>(
let init = &fields[0..group_index + 1];
let rest = &fields[group_index + 1..];
let init_last_pos = if rest.is_empty() {
span.hi
span.hi()
} else {
// Decide whether the missing comments should stick to init or rest.
let init_hi = init[init.len() - 1].get_span().hi;
let rest_lo = rest[0].get_span().lo;
let init_hi = init[init.len() - 1].get_span().hi();
let rest_lo = rest[0].get_span().lo();
let missing_span = mk_sp(init_hi, rest_lo);
let missing_span = mk_sp(
context.codemap.span_after(missing_span, ","),
missing_span.hi,
missing_span.hi(),
);
let snippet = context.snippet(missing_span);
@ -158,10 +158,10 @@ pub fn rewrite_with_alignment<T: AlignedItem>(
init_hi + BytePos(offset as u32 + 2)
} else {
missing_span.lo
missing_span.lo()
}
};
let init_span = mk_sp(span.lo, init_last_pos);
let init_span = mk_sp(span.lo(), init_last_pos);
let one_line_width = if rest.is_empty() { one_line_width } else { 0 };
let result = try_opt!(rewrite_aligned_items_inner(
context,
@ -173,7 +173,7 @@ pub fn rewrite_with_alignment<T: AlignedItem>(
if rest.is_empty() {
Some(result + spaces)
} else {
let rest_span = mk_sp(init_last_pos, span.hi);
let rest_span = mk_sp(init_last_pos, span.hi());
let rest_str = try_opt!(rewrite_with_alignment(
rest,
context,
@ -239,11 +239,11 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
context.codemap,
fields.iter(),
"}",
|field| field.get_span().lo,
|field| field.get_span().hi,
|field| field.get_span().lo(),
|field| field.get_span().hi(),
|field| field.rewrite_aligned_item(context, item_shape, field_prefix_max_width),
span.lo,
span.hi,
span.lo(),
span.hi(),
false,
).collect::<Vec<_>>();
@ -277,7 +277,7 @@ fn group_aligned_items<T: AlignedItem>(
return ("", index);
}
// See if there are comments or empty lines between fields.
let span = mk_sp(fields[i].get_span().hi, fields[i + 1].get_span().lo);
let span = mk_sp(fields[i].get_span().hi(), fields[i + 1].get_span().lo());
let snippet = context
.snippet(span)
.lines()

View File

@ -65,8 +65,8 @@ impl<'a> FmtVisitor<'a> {
fn visit_stmt(&mut self, stmt: &ast::Stmt) {
debug!(
"visit_stmt: {:?} {:?}",
self.codemap.lookup_char_pos(stmt.span.lo),
self.codemap.lookup_char_pos(stmt.span.hi)
self.codemap.lookup_char_pos(stmt.span.lo()),
self.codemap.lookup_char_pos(stmt.span.hi())
);
match stmt.node {
@ -93,7 +93,7 @@ impl<'a> FmtVisitor<'a> {
} else {
self.visit_mac(mac, None, MacroPosition::Statement);
}
self.format_missing(stmt.span.hi);
self.format_missing(stmt.span.hi());
}
}
}
@ -101,8 +101,8 @@ impl<'a> FmtVisitor<'a> {
pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribute]>) {
debug!(
"visit_block: {:?} {:?}",
self.codemap.lookup_char_pos(b.span.lo),
self.codemap.lookup_char_pos(b.span.hi)
self.codemap.lookup_char_pos(b.span.lo()),
self.codemap.lookup_char_pos(b.span.hi())
);
// Check if this block has braces.
@ -118,7 +118,7 @@ impl<'a> FmtVisitor<'a> {
if let Some(first_stmt) = b.stmts.first() {
let attr_lo = inner_attrs
.and_then(|attrs| {
inner_attributes(attrs).first().map(|attr| attr.span.lo)
inner_attributes(attrs).first().map(|attr| attr.span.lo())
})
.or_else(|| {
// Attributes for an item in a statement position
@ -130,8 +130,8 @@ impl<'a> FmtVisitor<'a> {
}.and_then(|attr| {
// Some stmts can have embedded attributes.
// e.g. `match { #![attr] ... }`
let attr_lo = attr.span.lo;
if attr_lo < first_stmt.span.lo {
let attr_lo = attr.span.lo();
if attr_lo < first_stmt.span.lo() {
Some(attr_lo)
} else {
None
@ -139,8 +139,10 @@ impl<'a> FmtVisitor<'a> {
})
});
let snippet =
self.snippet(mk_sp(self.last_pos, attr_lo.unwrap_or(first_stmt.span.lo)));
let snippet = self.snippet(mk_sp(
self.last_pos,
attr_lo.unwrap_or(first_stmt.span.lo()),
));
let len = CommentCodeSlices::new(&snippet).nth(0).and_then(
|(kind, _, s)| if kind == CodeCharKind::Normal {
s.rfind('\n')
@ -175,8 +177,8 @@ impl<'a> FmtVisitor<'a> {
if self.config.remove_blank_lines_at_start_or_end_of_block() {
if let Some(stmt) = b.stmts.last() {
let snippet = self.snippet(mk_sp(
stmt.span.hi,
source!(self, b.span).hi - brace_compensation,
stmt.span.hi(),
source!(self, b.span).hi() - brace_compensation,
));
let len = CommentCodeSlices::new(&snippet)
.last()
@ -203,12 +205,14 @@ impl<'a> FmtVisitor<'a> {
if unindent_comment {
self.block_indent = self.block_indent.block_unindent(self.config);
}
self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation - remove_len);
self.format_missing_with_indent(
source!(self, b.span).hi() - brace_compensation - remove_len,
);
if unindent_comment {
self.block_indent = self.block_indent.block_indent(self.config);
}
self.close_block(unindent_comment);
self.last_pos = source!(self, b.span).hi;
self.last_pos = source!(self, b.span).hi();
}
// FIXME: this is a terrible hack to indent the comments between the last
@ -255,7 +259,7 @@ impl<'a> FmtVisitor<'a> {
defaultness,
abi,
vis,
mk_sp(s.lo, b.span.lo),
mk_sp(s.lo(), b.span.lo()),
&b,
)
}
@ -271,7 +275,7 @@ impl<'a> FmtVisitor<'a> {
defaultness,
sig.abi,
vis.unwrap_or(&ast::Visibility::Inherited),
mk_sp(s.lo, b.span.lo),
mk_sp(s.lo(), b.span.lo()),
&b,
)
}
@ -279,19 +283,19 @@ impl<'a> FmtVisitor<'a> {
};
if let Some(fn_str) = rewrite {
self.format_missing_with_indent(source!(self, s).lo);
self.format_missing_with_indent(source!(self, s).lo());
self.buffer.push_str(&fn_str);
if let Some(c) = fn_str.chars().last() {
if c == '}' {
self.last_pos = source!(self, block.span).hi;
self.last_pos = source!(self, block.span).hi();
return;
}
}
} else {
self.format_missing(source!(self, block.span).lo);
self.format_missing(source!(self, block.span).lo());
}
self.last_pos = source!(self, block.span).lo;
self.last_pos = source!(self, block.span).lo();
self.visit_block(block, inner_attrs)
}
@ -305,8 +309,8 @@ impl<'a> FmtVisitor<'a> {
let mut attrs = item.attrs.clone();
match item.node {
ast::ItemKind::Mod(ref m) => {
let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
let outer_file = self.codemap.lookup_char_pos(item.span.lo()).file;
let inner_file = self.codemap.lookup_char_pos(m.inner.lo()).file;
if outer_file.name == inner_file.name {
// Module is inline, in this case we treat modules like any
// other item.
@ -323,7 +327,7 @@ impl<'a> FmtVisitor<'a> {
let filterd_attrs = item.attrs
.iter()
.filter_map(|a| {
let attr_file = self.codemap.lookup_char_pos(a.span.lo).file;
let attr_file = self.codemap.lookup_char_pos(a.span.lo()).file;
if attr_file.name == outer_file.name {
Some(a.clone())
} else {
@ -352,24 +356,24 @@ impl<'a> FmtVisitor<'a> {
let snippet = self.snippet(item.span);
let where_span_end = snippet
.find_uncommented("{")
.map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
.map(|x| (BytePos(x as u32)) + source!(self, item.span).lo());
if let Some(impl_str) =
format_impl(&self.get_context(), item, self.block_indent, where_span_end)
{
self.buffer.push_str(&impl_str);
self.last_pos = source!(self, item.span).hi;
self.last_pos = source!(self, item.span).hi();
}
}
ast::ItemKind::Trait(..) => {
self.format_missing_with_indent(item.span.lo);
self.format_missing_with_indent(item.span.lo());
if let Some(trait_str) = format_trait(&self.get_context(), item, self.block_indent)
{
self.buffer.push_str(&trait_str);
self.last_pos = source!(self, item.span).hi;
self.last_pos = source!(self, item.span).hi();
}
}
ast::ItemKind::ExternCrate(_) => {
self.format_missing_with_indent(source!(self, item.span).lo);
self.format_missing_with_indent(source!(self, item.span).lo());
let new_str = self.snippet(item.span);
if contains_comment(&new_str) {
self.buffer.push_str(&new_str)
@ -379,7 +383,7 @@ impl<'a> FmtVisitor<'a> {
self.buffer
.push_str(&Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"));
}
self.last_pos = source!(self, item.span).hi;
self.last_pos = source!(self, item.span).hi();
}
ast::ItemKind::Struct(ref def, ref generics) => {
let rewrite = {
@ -403,19 +407,19 @@ impl<'a> FmtVisitor<'a> {
self.push_rewrite(item.span, rewrite);
}
ast::ItemKind::Enum(ref def, ref generics) => {
self.format_missing_with_indent(source!(self, item.span).lo);
self.format_missing_with_indent(source!(self, item.span).lo());
self.visit_enum(item.ident, &item.vis, def, generics, item.span);
self.last_pos = source!(self, item.span).hi;
self.last_pos = source!(self, item.span).hi();
}
ast::ItemKind::Mod(ref module) => {
self.format_missing_with_indent(source!(self, item.span).lo);
self.format_missing_with_indent(source!(self, item.span).lo());
self.format_mod(module, &item.vis, item.span, item.ident, &attrs);
}
ast::ItemKind::Mac(ref mac) => {
self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
}
ast::ItemKind::ForeignMod(ref foreign_mod) => {
self.format_missing_with_indent(source!(self, item.span).lo);
self.format_missing_with_indent(source!(self, item.span).lo());
self.format_foreign_mod(foreign_mod, item.span);
}
ast::ItemKind::Static(ref ty, mutability, ref expr) => {
@ -619,10 +623,10 @@ impl<'a> FmtVisitor<'a> {
}
pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
self.format_missing_with_indent(source!(self, span).lo);
self.format_missing_with_indent(source!(self, span).lo());
let result = rewrite.unwrap_or_else(|| self.snippet(span));
self.buffer.push_str(&result);
self.last_pos = source!(self, span).hi;
self.last_pos = source!(self, span).hi();
}
pub fn from_codemap(parse_session: &'a ParseSess, config: &'a Config) -> FmtVisitor<'a> {
@ -643,8 +647,8 @@ impl<'a> FmtVisitor<'a> {
Err(_) => {
println!(
"Couldn't make snippet for span {:?}->{:?}",
self.codemap.lookup_char_pos(span.lo),
self.codemap.lookup_char_pos(span.hi)
self.codemap.lookup_char_pos(span.lo()),
self.codemap.lookup_char_pos(span.hi())
);
"".to_owned()
}
@ -750,7 +754,7 @@ impl<'a> FmtVisitor<'a> {
// Decide whether this is an inline mod or an external mod.
let local_file_name = self.codemap.span_to_filename(s);
let inner_span = source!(self, m.inner);
let is_internal = !(inner_span.lo.0 == 0 && inner_span.hi.0 == 0) &&
let is_internal = !(inner_span.lo().0 == 0 && inner_span.hi().0 == 0) &&
local_file_name == self.codemap.span_to_filename(inner_span);
self.buffer.push_str(&*utils::format_visibility(vis));
@ -765,7 +769,8 @@ impl<'a> FmtVisitor<'a> {
}
// Hackery to account for the closing }.
let mod_lo = self.codemap.span_after(source!(self, s), "{");
let body_snippet = self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1)));
let body_snippet =
self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi() - BytePos(1)));
let body_snippet = body_snippet.trim();
if body_snippet.is_empty() {
self.buffer.push_str("}");
@ -774,13 +779,13 @@ impl<'a> FmtVisitor<'a> {
self.block_indent = self.block_indent.block_indent(self.config);
self.visit_attrs(attrs, ast::AttrStyle::Inner);
self.walk_mod_items(m);
self.format_missing_with_indent(source!(self, m.inner).hi - BytePos(1));
self.format_missing_with_indent(source!(self, m.inner).hi() - BytePos(1));
self.close_block(false);
}
self.last_pos = source!(self, m.inner).hi;
self.last_pos = source!(self, m.inner).hi();
} else {
self.buffer.push_str(";");
self.last_pos = source!(self, s).hi;
self.last_pos = source!(self, s).hi();
}
}
@ -828,11 +833,11 @@ impl Rewrite for ast::MetaItem {
context.codemap,
list.iter(),
")",
|nested_meta_item| nested_meta_item.span.lo,
|nested_meta_item| nested_meta_item.span.hi,
|nested_meta_item| nested_meta_item.span.lo(),
|nested_meta_item| nested_meta_item.span.hi(),
|nested_meta_item| nested_meta_item.rewrite(context, item_shape),
self.span.lo,
self.span.hi,
self.span.lo(),
self.span.hi(),
false,
);
let item_vec = items.collect::<Vec<_>>();
@ -909,7 +914,7 @@ impl<'a> Rewrite for [ast::Attribute] {
// Write comments and blank lines between attributes.
if i > 0 {
let comment = context.snippet(mk_sp(self[i - 1].span.hi, a.span.lo));
let comment = context.snippet(mk_sp(self[i - 1].span.hi(), a.span.lo()));
// This particular horror show is to preserve line breaks in between doc
// comments. An alternative would be to force such line breaks to start
// with the usual doc comment token.

View File

@ -13,9 +13,9 @@ fn main() {
let loooooooooooooong_name = |field| {
// format comments.
if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo
if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo()
} else {
field.span.lo
field.span.lo()
}};
let unblock_me = |trivial| {
@ -85,7 +85,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
fn foo() {
lifetimes_iter___map(|lasdfasfd| {
let hi = if l.bounds.is_empty() {
l.lifetime.span.hi
l.lifetime.span.hi()
};
});
}

View File

@ -83,8 +83,8 @@ fn function_calls() {
let items = itemize_list(context.codemap,
args.iter(),
")",
|item| item.span.lo,
|item| item.span.hi,
|item| item.span.lo(),
|item| item.span.hi(),
|item| {
item.rewrite(context,
Shape {
@ -92,14 +92,14 @@ fn function_calls() {
..nested_shape
})
},
span.lo,
span.hi);
span.lo(),
span.hi());
itemize_list(context.codemap,
args.iter(),
")",
|item| item.span.lo,
|item| item.span.hi,
|item| item.span.lo(),
|item| item.span.hi(),
|item| {
item.rewrite(context,
Shape {
@ -107,8 +107,8 @@ fn function_calls() {
..nested_shape
})
},
span.lo,
span.hi)
span.lo(),
span.hi())
}
fn macros() {

View File

@ -24,9 +24,9 @@ fn main() {
let loooooooooooooong_name = |field| {
// format comments.
if field.node.attrs.len() > 0 {
field.node.attrs[0].span.lo
field.node.attrs[0].span.lo()
} else {
field.span.lo
field.span.lo()
}
};
@ -106,7 +106,7 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
fn foo() {
lifetimes_iter___map(|lasdfasfd| {
let hi = if l.bounds.is_empty() {
l.lifetime.span.hi
l.lifetime.span.hi()
};
});
}

View File

@ -112,8 +112,8 @@ fn function_calls() {
context.codemap,
args.iter(),
")",
|item| item.span.lo,
|item| item.span.hi,
|item| item.span.lo(),
|item| item.span.hi(),
|item| {
item.rewrite(
context,
@ -123,16 +123,16 @@ fn function_calls() {
},
)
},
span.lo,
span.hi,
span.lo(),
span.hi(),
);
itemize_list(
context.codemap,
args.iter(),
")",
|item| item.span.lo,
|item| item.span.hi,
|item| item.span.lo(),
|item| item.span.hi(),
|item| {
item.rewrite(
context,
@ -142,8 +142,8 @@ fn function_calls() {
},
)
},
span.lo,
span.hi,
span.lo(),
span.hi(),
)
}

View File

@ -5,17 +5,17 @@ fn main() {
field_iter,
"}",
|item| match *item {
StructLitField::Regular(ref field) => field.span.lo,
StructLitField::Regular(ref field) => field.span.lo(),
StructLitField::Base(ref expr) => {
let last_field_hi = fields.last().map_or(span.lo, |field| field.span.hi);
let snippet = context.snippet(mk_sp(last_field_hi, expr.span.lo));
let last_field_hi = fields.last().map_or(span.lo(), |field| field.span.hi());
let snippet = context.snippet(mk_sp(last_field_hi, expr.span.lo()));
let pos = snippet.find_uncommented("..").unwrap();
last_field_hi + BytePos(pos as u32)
}
},
|item| match *item {
StructLitField::Regular(ref field) => field.span.hi,
StructLitField::Base(ref expr) => expr.span.hi,
StructLitField::Regular(ref field) => field.span.hi(),
StructLitField::Base(ref expr) => expr.span.hi(),
},
|item| {
match *item {
@ -34,7 +34,7 @@ fn main() {
}
},
context.codemap.span_after(span, "{"),
span.hi,
span.hi(),
);
// #1580