Skip to content

Commit e5e1e0c

Browse files
committed
Fix compile errors from breaking changes in libsyntax
cc rust-lang/rust#48149.
1 parent ceda367 commit e5e1e0c

File tree

7 files changed

+120
-113
lines changed

7 files changed

+120
-113
lines changed

src/chains.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use expr::rewrite_call;
7070
use macros::convert_try_mac;
7171
use rewrite::{Rewrite, RewriteContext};
7272
use shape::Shape;
73+
use spanned::Spanned;
7374
use utils::{
7475
first_line_width, last_line_extendable, last_line_width, mk_sp, trimmed_last_line_width,
7576
wrap_str,
@@ -436,9 +437,9 @@ fn rewrite_chain_subexpr(
436437

437438
match expr.node {
438439
ast::ExprKind::MethodCall(ref segment, ref expressions) => {
439-
let types = match segment.parameters {
440+
let types = match segment.args {
440441
Some(ref params) => match **params {
441-
ast::PathParameters::AngleBracketed(ref data) => &data.types[..],
442+
ast::GenericArgs::AngleBracketed(ref data) => &data.args[..],
442443
_ => &[],
443444
},
444445
_ => &[],
@@ -484,7 +485,7 @@ fn is_try(expr: &ast::Expr) -> bool {
484485

485486
fn rewrite_method_call(
486487
method_name: ast::Ident,
487-
types: &[ptr::P<ast::Ty>],
488+
types: &[ast::GenericArg],
488489
args: &[ptr::P<ast::Expr>],
489490
span: Span,
490491
context: &RewriteContext,
@@ -500,7 +501,7 @@ fn rewrite_method_call(
500501

501502
let type_str = format!("::<{}>", type_list.join(", "));
502503

503-
(types.last().unwrap().span.hi(), type_str)
504+
(types.last().unwrap().span().hi(), type_str)
504505
};
505506

506507
let callee_str = format!(".{}{}", method_name, type_str);

src/expr.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -2014,8 +2014,8 @@ fn rewrite_assignment(
20142014
pub enum RhsTactics {
20152015
/// Use heuristics.
20162016
Default,
2017-
/// Put the rhs on the next line if it uses multiple line.
2018-
ForceNextLine,
2017+
/// Put the rhs on the next line if it uses multiple line, without extra indentation.
2018+
ForceNextLineWithoutIndent,
20192019
}
20202020

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

20812082
match (orig_rhs, new_rhs) {
20822083
(Some(ref orig_rhs), Some(ref new_rhs))
@@ -2098,8 +2099,22 @@ fn choose_rhs<R: Rewrite>(
20982099
}
20992100
}
21002101

2102+
fn shape_from_rhs_tactic(
2103+
context: &RewriteContext,
2104+
shape: Shape,
2105+
rhs_tactic: RhsTactics,
2106+
) -> Option<Shape> {
2107+
match rhs_tactic {
2108+
RhsTactics::ForceNextLineWithoutIndent => Some(shape.with_max_width(context.config)),
2109+
RhsTactics::Default => {
2110+
Shape::indented(shape.indent.block_indent(context.config), context.config)
2111+
.sub_width(shape.rhs_overhead(context.config))
2112+
}
2113+
}
2114+
}
2115+
21012116
pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTactics) -> bool {
2102-
rhs_tactics == RhsTactics::ForceNextLine
2117+
rhs_tactics == RhsTactics::ForceNextLineWithoutIndent
21032118
|| !next_line_rhs.contains('\n')
21042119
|| count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
21052120
}

src/imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl UseTree {
346346
.collect(),
347347
));
348348
}
349-
UseTreeKind::Simple(ref rename) => {
349+
UseTreeKind::Simple(ref rename, ..) => {
350350
let mut name = (*path_to_imported_ident(&a.prefix).name.as_str()).to_owned();
351351
let alias = rename.and_then(|ident| {
352352
if ident == path_to_imported_ident(&a.prefix) {

src/items.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use overflow;
3636
use rewrite::{Rewrite, RewriteContext};
3737
use shape::{Indent, Shape};
3838
use spanned::Spanned;
39-
use types::TraitTyParamBounds;
4039
use utils::*;
4140
use vertical::rewrite_with_alignment;
4241
use visitor::FmtVisitor;
@@ -971,7 +970,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
971970
is_auto,
972971
unsafety,
973972
ref generics,
974-
ref type_param_bounds,
973+
ref generic_bounds,
975974
ref trait_items,
976975
) = item.node
977976
{
@@ -997,23 +996,22 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
997996
result.push_str(&generics_str);
998997

999998
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1000-
if !type_param_bounds.is_empty() {
999+
if !generic_bounds.is_empty() {
10011000
let ident_hi = context
10021001
.snippet_provider
10031002
.span_after(item.span, &format!("{}", item.ident));
1004-
let bound_hi = type_param_bounds.last().unwrap().span().hi();
1003+
let bound_hi = generic_bounds.last().unwrap().span().hi();
10051004
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
10061005
if contains_comment(snippet) {
10071006
return None;
10081007
}
1009-
}
1010-
if !type_param_bounds.is_empty() {
1008+
10111009
result = rewrite_assign_rhs_with(
10121010
context,
10131011
result + ":",
1014-
&TraitTyParamBounds::new(type_param_bounds),
1012+
generic_bounds,
10151013
shape,
1016-
RhsTactics::ForceNextLine,
1014+
RhsTactics::ForceNextLineWithoutIndent,
10171015
)?;
10181016
}
10191017

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

10281026
let where_budget = context.budget(last_line_width(&result));
1029-
let pos_before_where = if type_param_bounds.is_empty() {
1027+
let pos_before_where = if generic_bounds.is_empty() {
10301028
generics.where_clause.span.lo()
10311029
} else {
1032-
type_param_bounds[type_param_bounds.len() - 1].span().hi()
1030+
generic_bounds[generic_bounds.len() - 1].span().hi()
10331031
};
10341032
let option = WhereClauseOption::snuggled(&generics_str);
10351033
let where_clause_str = rewrite_where_clause(
@@ -1134,7 +1132,7 @@ pub fn format_trait_alias(
11341132
context: &RewriteContext,
11351133
ident: ast::Ident,
11361134
generics: &ast::Generics,
1137-
ty_param_bounds: &ast::TyParamBounds,
1135+
generic_bounds: &ast::GenericBounds,
11381136
shape: Shape,
11391137
) -> Option<String> {
11401138
let alias = ident.name.as_str();
@@ -1143,7 +1141,7 @@ pub fn format_trait_alias(
11431141
let generics_str = rewrite_generics(context, &alias, generics, g_shape, generics.span)?;
11441142
let lhs = format!("trait {} =", generics_str);
11451143
// 1 = ";"
1146-
rewrite_assign_rhs(context, lhs, ty_param_bounds, shape.sub_width(1)?).map(|s| s + ";")
1144+
rewrite_assign_rhs(context, lhs, generic_bounds, shape.sub_width(1)?).map(|s| s + ";")
11471145
}
11481146

11491147
fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option<String> {
@@ -1671,13 +1669,13 @@ fn rewrite_static(
16711669
pub fn rewrite_associated_type(
16721670
ident: ast::Ident,
16731671
ty_opt: Option<&ptr::P<ast::Ty>>,
1674-
ty_param_bounds_opt: Option<&ast::TyParamBounds>,
1672+
generic_bounds_opt: Option<&ast::GenericBounds>,
16751673
context: &RewriteContext,
16761674
indent: Indent,
16771675
) -> Option<String> {
16781676
let prefix = format!("type {}", ident);
16791677

1680-
let type_bounds_str = if let Some(bounds) = ty_param_bounds_opt {
1678+
let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
16811679
if bounds.is_empty() {
16821680
String::new()
16831681
} else {
@@ -1703,11 +1701,11 @@ pub fn rewrite_associated_impl_type(
17031701
ident: ast::Ident,
17041702
defaultness: ast::Defaultness,
17051703
ty_opt: Option<&ptr::P<ast::Ty>>,
1706-
ty_param_bounds_opt: Option<&ast::TyParamBounds>,
1704+
generic_bounds_opt: Option<&ast::GenericBounds>,
17071705
context: &RewriteContext,
17081706
indent: Indent,
17091707
) -> Option<String> {
1710-
let result = rewrite_associated_type(ident, ty_opt, ty_param_bounds_opt, context, indent)?;
1708+
let result = rewrite_associated_type(ident, ty_opt, generic_bounds_opt, context, indent)?;
17111709

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

2807-
impl Rewrite for ast::GenericParam {
2808-
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
2809-
match *self {
2810-
ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.rewrite(context, shape),
2811-
ast::GenericParam::Type(ref ty) => ty.rewrite(context, shape),
2812-
}
2813-
}
2814-
}
2815-
28162805
/// Rewrite an inline mod.
28172806
pub fn rewrite_mod(item: &ast::Item) -> String {
28182807
let mut result = String::with_capacity(32);

src/spanned.rs

+28-32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use syntax::codemap::Span;
1414
use macros::MacroArg;
1515
use utils::{mk_sp, outer_attributes};
1616

17+
use std::cmp::max;
18+
1719
/// Spanned returns a span including attributes, if available.
1820
pub trait Spanned {
1921
fn span(&self) -> Span;
@@ -110,10 +112,25 @@ impl Spanned for ast::Arg {
110112

111113
impl Spanned for ast::GenericParam {
112114
fn span(&self) -> Span {
113-
match *self {
114-
ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.span(),
115-
ast::GenericParam::Type(ref ty) => ty.span(),
116-
}
115+
let lo = if self.attrs.is_empty() {
116+
self.ident.span.lo()
117+
} else {
118+
self.attrs[0].span.lo()
119+
};
120+
let hi = if self.bounds.is_empty() {
121+
self.ident.span.hi()
122+
} else {
123+
self.bounds.last().unwrap().span().hi()
124+
};
125+
let ty_hi = if let ast::GenericParamKind::Type {
126+
default: Some(ref ty),
127+
} = self.kind
128+
{
129+
ty.span().hi()
130+
} else {
131+
hi
132+
};
133+
mk_sp(lo, max(hi, ty_hi))
117134
}
118135
}
119136

@@ -142,45 +159,24 @@ impl Spanned for ast::FunctionRetTy {
142159
}
143160
}
144161

145-
impl Spanned for ast::TyParam {
162+
impl Spanned for ast::GenericArg {
146163
fn span(&self) -> Span {
147-
// Note that ty.span is the span for ty.ident, not the whole item.
148-
let lo = if self.attrs.is_empty() {
149-
self.ident.span.lo()
150-
} else {
151-
self.attrs[0].span.lo()
152-
};
153-
if let Some(ref def) = self.default {
154-
return mk_sp(lo, def.span.hi());
155-
}
156-
if self.bounds.is_empty() {
157-
return mk_sp(lo, self.ident.span.hi());
164+
match *self {
165+
ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
166+
ast::GenericArg::Type(ref ty) => ty.span(),
158167
}
159-
let hi = self.bounds[self.bounds.len() - 1].span().hi();
160-
mk_sp(lo, hi)
161168
}
162169
}
163170

164-
impl Spanned for ast::TyParamBound {
171+
impl Spanned for ast::GenericBound {
165172
fn span(&self) -> Span {
166173
match *self {
167-
ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span,
168-
ast::TyParamBound::RegionTyParamBound(ref l) => l.ident.span,
174+
ast::GenericBound::Trait(ref ptr, _) => ptr.span,
175+
ast::GenericBound::Outlives(ref l) => l.ident.span,
169176
}
170177
}
171178
}
172179

173-
impl Spanned for ast::LifetimeDef {
174-
fn span(&self) -> Span {
175-
let hi = if self.bounds.is_empty() {
176-
self.lifetime.ident.span.hi()
177-
} else {
178-
self.bounds[self.bounds.len() - 1].ident.span.hi()
179-
};
180-
mk_sp(self.lifetime.ident.span.lo(), hi)
181-
}
182-
}
183-
184180
impl Spanned for MacroArg {
185181
fn span(&self) -> Span {
186182
match *self {

0 commit comments

Comments
 (0)