Skip to content

Commit

Permalink
Fix compile errors from breaking changes in libsyntax
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Jun 25, 2018
1 parent ceda367 commit e5e1e0c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 113 deletions.
9 changes: 5 additions & 4 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ use expr::rewrite_call;
use macros::convert_try_mac;
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use spanned::Spanned;
use utils::{
first_line_width, last_line_extendable, last_line_width, mk_sp, trimmed_last_line_width,
wrap_str,
Expand Down Expand Up @@ -436,9 +437,9 @@ fn rewrite_chain_subexpr(

match expr.node {
ast::ExprKind::MethodCall(ref segment, ref expressions) => {
let types = match segment.parameters {
let types = match segment.args {
Some(ref params) => match **params {
ast::PathParameters::AngleBracketed(ref data) => &data.types[..],
ast::GenericArgs::AngleBracketed(ref data) => &data.args[..],
_ => &[],
},
_ => &[],
Expand Down Expand Up @@ -484,7 +485,7 @@ fn is_try(expr: &ast::Expr) -> bool {

fn rewrite_method_call(
method_name: ast::Ident,
types: &[ptr::P<ast::Ty>],
types: &[ast::GenericArg],
args: &[ptr::P<ast::Expr>],
span: Span,
context: &RewriteContext,
Expand All @@ -500,7 +501,7 @@ fn rewrite_method_call(

let type_str = 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);
Expand Down
29 changes: 22 additions & 7 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2014,8 +2014,8 @@ fn rewrite_assignment(
pub enum RhsTactics {
/// Use heuristics.
Default,
/// Put the rhs on the next line if it uses multiple line.
ForceNextLine,
/// Put the rhs on the next line if it uses multiple line, without extra indentation.
ForceNextLineWithoutIndent,
}

// The left hand side must contain everything up to, and including, the
Expand Down Expand Up @@ -2072,11 +2072,12 @@ fn choose_rhs<R: Rewrite>(
_ => {
// Expression did not fit on the same line as the identifier.
// Try splitting the line and see if that works better.
let new_shape =
Shape::indented(shape.indent.block_indent(context.config), context.config)
.sub_width(shape.rhs_overhead(context.config))?;
let new_shape = shape_from_rhs_tactic(context, shape, rhs_tactics)?;
let new_rhs = expr.rewrite(context, new_shape);
let new_indent_str = &new_shape.indent.to_string_with_newline(context.config);
let new_indent_str = &shape
.indent
.block_indent(context.config)
.to_string_with_newline(context.config);

match (orig_rhs, new_rhs) {
(Some(ref orig_rhs), Some(ref new_rhs))
Expand All @@ -2098,8 +2099,22 @@ fn choose_rhs<R: Rewrite>(
}
}

fn shape_from_rhs_tactic(
context: &RewriteContext,
shape: Shape,
rhs_tactic: RhsTactics,
) -> Option<Shape> {
match rhs_tactic {
RhsTactics::ForceNextLineWithoutIndent => Some(shape.with_max_width(context.config)),
RhsTactics::Default => {
Shape::indented(shape.indent.block_indent(context.config), context.config)
.sub_width(shape.rhs_overhead(context.config))
}
}
}

pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTactics) -> bool {
rhs_tactics == RhsTactics::ForceNextLine
rhs_tactics == RhsTactics::ForceNextLineWithoutIndent
|| !next_line_rhs.contains('\n')
|| count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
}
Expand Down
2 changes: 1 addition & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl UseTree {
.collect(),
));
}
UseTreeKind::Simple(ref rename) => {
UseTreeKind::Simple(ref rename, ..) => {
let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
let alias = rename.and_then(|ident| {
if ident == path_to_imported_ident(&a.prefix) {
Expand Down
41 changes: 15 additions & 26 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use overflow;
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
use spanned::Spanned;
use types::TraitTyParamBounds;
use utils::*;
use vertical::rewrite_with_alignment;
use visitor::FmtVisitor;
Expand Down Expand Up @@ -971,7 +970,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
is_auto,
unsafety,
ref generics,
ref type_param_bounds,
ref generic_bounds,
ref trait_items,
) = item.node
{
Expand All @@ -997,23 +996,22 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
result.push_str(&generics_str);

// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
if !type_param_bounds.is_empty() {
if !generic_bounds.is_empty() {
let ident_hi = context
.snippet_provider
.span_after(item.span, &format!("{}", item.ident));
let bound_hi = type_param_bounds.last().unwrap().span().hi();
let bound_hi = generic_bounds.last().unwrap().span().hi();
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
if contains_comment(snippet) {
return None;
}
}
if !type_param_bounds.is_empty() {

result = rewrite_assign_rhs_with(
context,
result + ":",
&TraitTyParamBounds::new(type_param_bounds),
generic_bounds,
shape,
RhsTactics::ForceNextLine,
RhsTactics::ForceNextLineWithoutIndent,
)?;
}

Expand All @@ -1026,10 +1024,10 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
};

let where_budget = context.budget(last_line_width(&result));
let pos_before_where = if type_param_bounds.is_empty() {
let pos_before_where = if generic_bounds.is_empty() {
generics.where_clause.span.lo()
} else {
type_param_bounds[type_param_bounds.len() - 1].span().hi()
generic_bounds[generic_bounds.len() - 1].span().hi()
};
let option = WhereClauseOption::snuggled(&generics_str);
let where_clause_str = rewrite_where_clause(
Expand Down Expand Up @@ -1134,7 +1132,7 @@ pub fn format_trait_alias(
context: &RewriteContext,
ident: ast::Ident,
generics: &ast::Generics,
ty_param_bounds: &ast::TyParamBounds,
generic_bounds: &ast::GenericBounds,
shape: Shape,
) -> Option<String> {
let alias = ident.name.as_str();
Expand All @@ -1143,7 +1141,7 @@ pub fn format_trait_alias(
let generics_str = rewrite_generics(context, &alias, generics, g_shape, generics.span)?;
let lhs = format!("trait {} =", generics_str);
// 1 = ";"
rewrite_assign_rhs(context, lhs, ty_param_bounds, shape.sub_width(1)?).map(|s| s + ";")
rewrite_assign_rhs(context, lhs, generic_bounds, shape.sub_width(1)?).map(|s| s + ";")
}

fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option<String> {
Expand Down Expand Up @@ -1671,13 +1669,13 @@ fn rewrite_static(
pub fn rewrite_associated_type(
ident: ast::Ident,
ty_opt: Option<&ptr::P<ast::Ty>>,
ty_param_bounds_opt: Option<&ast::TyParamBounds>,
generic_bounds_opt: Option<&ast::GenericBounds>,
context: &RewriteContext,
indent: Indent,
) -> Option<String> {
let prefix = format!("type {}", ident);

let type_bounds_str = if let Some(bounds) = ty_param_bounds_opt {
let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
if bounds.is_empty() {
String::new()
} else {
Expand All @@ -1703,11 +1701,11 @@ pub fn rewrite_associated_impl_type(
ident: ast::Ident,
defaultness: ast::Defaultness,
ty_opt: Option<&ptr::P<ast::Ty>>,
ty_param_bounds_opt: Option<&ast::TyParamBounds>,
generic_bounds_opt: Option<&ast::GenericBounds>,
context: &RewriteContext,
indent: Indent,
) -> Option<String> {
let result = rewrite_associated_type(ident, ty_opt, ty_param_bounds_opt, context, indent)?;
let result = rewrite_associated_type(ident, ty_opt, generic_bounds_opt, context, indent)?;

match defaultness {
ast::Defaultness::Default => Some(format!("default {}", result)),
Expand Down Expand Up @@ -2698,7 +2696,7 @@ fn format_generics(
}
// If the generics are not parameterized then generics.span.hi() == 0,
// so we use span.lo(), which is the position after `struct Foo`.
let span_end_before_where = if generics.is_parameterized() {
let span_end_before_where = if !generics.params.is_empty() {
generics.span.hi()
} else {
span.lo()
Expand Down Expand Up @@ -2804,15 +2802,6 @@ impl Rewrite for ast::ForeignItem {
}
}

impl Rewrite for ast::GenericParam {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
match *self {
ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.rewrite(context, shape),
ast::GenericParam::Type(ref ty) => ty.rewrite(context, shape),
}
}
}

/// Rewrite an inline mod.
pub fn rewrite_mod(item: &ast::Item) -> String {
let mut result = String::with_capacity(32);
Expand Down
60 changes: 28 additions & 32 deletions src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use syntax::codemap::Span;
use macros::MacroArg;
use utils::{mk_sp, outer_attributes};

use std::cmp::max;

/// Spanned returns a span including attributes, if available.
pub trait Spanned {
fn span(&self) -> Span;
Expand Down Expand Up @@ -110,10 +112,25 @@ impl Spanned for ast::Arg {

impl Spanned for ast::GenericParam {
fn span(&self) -> Span {
match *self {
ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.span(),
ast::GenericParam::Type(ref ty) => ty.span(),
}
let lo = if self.attrs.is_empty() {
self.ident.span.lo()
} else {
self.attrs[0].span.lo()
};
let hi = if self.bounds.is_empty() {
self.ident.span.hi()
} else {
self.bounds.last().unwrap().span().hi()
};
let ty_hi = if let ast::GenericParamKind::Type {
default: Some(ref ty),
} = self.kind
{
ty.span().hi()
} else {
hi
};
mk_sp(lo, max(hi, ty_hi))
}
}

Expand Down Expand Up @@ -142,45 +159,24 @@ impl Spanned for ast::FunctionRetTy {
}
}

impl Spanned for ast::TyParam {
impl Spanned for ast::GenericArg {
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.ident.span.lo()
} else {
self.attrs[0].span.lo()
};
if let Some(ref def) = self.default {
return mk_sp(lo, def.span.hi());
}
if self.bounds.is_empty() {
return mk_sp(lo, self.ident.span.hi());
match *self {
ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
ast::GenericArg::Type(ref ty) => ty.span(),
}
let hi = self.bounds[self.bounds.len() - 1].span().hi();
mk_sp(lo, hi)
}
}

impl Spanned for ast::TyParamBound {
impl Spanned for ast::GenericBound {
fn span(&self) -> Span {
match *self {
ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span,
ast::TyParamBound::RegionTyParamBound(ref l) => l.ident.span,
ast::GenericBound::Trait(ref ptr, _) => ptr.span,
ast::GenericBound::Outlives(ref l) => l.ident.span,
}
}
}

impl Spanned for ast::LifetimeDef {
fn span(&self) -> Span {
let hi = if self.bounds.is_empty() {
self.lifetime.ident.span.hi()
} else {
self.bounds[self.bounds.len() - 1].ident.span.hi()
};
mk_sp(self.lifetime.ident.span.lo(), hi)
}
}

impl Spanned for MacroArg {
fn span(&self) -> Span {
match *self {
Expand Down
Loading

0 comments on commit e5e1e0c

Please sign in to comment.