Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Bump rustc-ap crates #4561

Merged
merged 7 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 111 additions & 91 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,36 @@ rustc-workspace-hack = "1.0.0"

[dependencies.rustc_ast]
package = "rustc-ap-rustc_ast"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_ast_pretty]
package = "rustc-ap-rustc_ast_pretty"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_attr]
package = "rustc-ap-rustc_attr"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_data_structures]
package = "rustc-ap-rustc_data_structures"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_errors]
package = "rustc-ap-rustc_errors"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_expand]
package = "rustc-ap-rustc_expand"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_parse]
package = "rustc-ap-rustc_parse"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_session]
package = "rustc-ap-rustc_session"
version = "686.0.0"
version = "691.0.0"

[dependencies.rustc_span]
package = "rustc-ap-rustc_span"
version = "686.0.0"
version = "691.0.0"
46 changes: 28 additions & 18 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ pub(crate) fn format_expr(
})
}
ast::ExprKind::Unary(op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape),
ast::ExprKind::Struct(ref path, ref fields, ref base) => rewrite_struct_lit(
ast::ExprKind::Struct(ref path, ref fields, ref struct_rest) => rewrite_struct_lit(
context,
path,
fields,
base.as_ref().map(|e| &**e),
struct_rest,
&expr.attrs,
expr.span,
shape,
Expand Down Expand Up @@ -384,6 +384,7 @@ pub(crate) fn format_expr(
}
}
ast::ExprKind::Await(_) => rewrite_chain(expr, context, shape),
ast::ExprKind::Underscore => Some("_".to_owned()),
ast::ExprKind::Err => None,
};

Expand Down Expand Up @@ -1510,19 +1511,15 @@ fn rewrite_index(
}
}

fn struct_lit_can_be_aligned(fields: &[ast::Field], base: Option<&ast::Expr>) -> bool {
if base.is_some() {
return false;
}

fields.iter().all(|field| !field.is_shorthand)
fn struct_lit_can_be_aligned(fields: &[ast::Field], has_base: bool) -> bool {
!has_base && fields.iter().all(|field| !field.is_shorthand)
}

fn rewrite_struct_lit<'a>(
context: &RewriteContext<'_>,
path: &ast::Path,
fields: &'a [ast::Field],
base: Option<&'a ast::Expr>,
struct_rest: &ast::StructRest,
attrs: &[ast::Attribute],
span: Span,
shape: Shape,
Expand All @@ -1532,22 +1529,28 @@ fn rewrite_struct_lit<'a>(
enum StructLitField<'a> {
Regular(&'a ast::Field),
Base(&'a ast::Expr),
Rest(&'a Span),
}

// 2 = " {".len()
let path_shape = shape.sub_width(2)?;
let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;

if fields.is_empty() && base.is_none() {
return Some(format!("{} {{}}", path_str));
}
let has_base = match struct_rest {
ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)),
ast::StructRest::Rest(_) if fields.is_empty() => {
return Some(format!("{} {{ .. }}", path_str));
}
ast::StructRest::Base(_) => true,
_ => false,
};

// Foo { a: Foo } - indent is +3, width is -5.
let (h_shape, v_shape) = struct_lit_shape(shape, context, path_str.len() + 3, 2)?;

let one_line_width = h_shape.map_or(0, |shape| shape.width);
let body_lo = context.snippet_provider.span_after(span, "{");
let fields_str = if struct_lit_can_be_aligned(fields, base)
let fields_str = if struct_lit_can_be_aligned(fields, has_base)
&& context.config.struct_field_align_threshold() > 0
{
rewrite_with_alignment(
Expand All @@ -1558,10 +1561,14 @@ fn rewrite_struct_lit<'a>(
one_line_width,
)?
} else {
let field_iter = fields
.iter()
.map(StructLitField::Regular)
.chain(base.into_iter().map(StructLitField::Base));
let field_iter = fields.iter().map(StructLitField::Regular).chain(
match struct_rest {
ast::StructRest::Base(expr) => Some(StructLitField::Base(&**expr)),
ast::StructRest::Rest(span) => Some(StructLitField::Rest(span)),
ast::StructRest::None => None,
}
.into_iter(),
);

let span_lo = |item: &StructLitField<'_>| match *item {
StructLitField::Regular(field) => field.span().lo(),
Expand All @@ -1571,10 +1578,12 @@ fn rewrite_struct_lit<'a>(
let pos = snippet.find_uncommented("..").unwrap();
last_field_hi + BytePos(pos as u32)
}
StructLitField::Rest(span) => span.lo(),
};
let span_hi = |item: &StructLitField<'_>| match *item {
StructLitField::Regular(field) => field.span().hi(),
StructLitField::Base(expr) => expr.span.hi(),
StructLitField::Rest(span) => span.hi(),
};
let rewrite = |item: &StructLitField<'_>| match *item {
StructLitField::Regular(field) => {
Expand All @@ -1586,6 +1595,7 @@ fn rewrite_struct_lit<'a>(
expr.rewrite(context, v_shape.offset_left(2)?)
.map(|s| format!("..{}", s))
}
StructLitField::Rest(_) => Some("..".to_owned()),
};

let items = itemize_list(
Expand All @@ -1612,7 +1622,7 @@ fn rewrite_struct_lit<'a>(
nested_shape,
tactic,
context,
force_no_trailing_comma || base.is_some() || !context.use_block_indent(),
force_no_trailing_comma || has_base || !context.use_block_indent(),
);

write_list(&item_vec, &fmt)?
Expand Down
6 changes: 2 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use std::collections::HashMap;
use std::panic::{catch_unwind, AssertUnwindSafe};

use rustc_ast::token::{BinOpToken, DelimToken, Token, TokenKind};
use rustc_ast::tokenstream::{
Cursor, LazyTokenStream, LazyTokenStreamInner, TokenStream, TokenTree,
};
use rustc_ast::tokenstream::{Cursor, LazyTokenStream, TokenStream, TokenTree};
use rustc_ast::{ast, ptr};
use rustc_ast_pretty::pprust;
use rustc_parse::parser::Parser;
Expand Down Expand Up @@ -1214,7 +1212,7 @@ pub(crate) fn convert_try_mac(
kind: ast::ExprKind::Try(parser.parse_expr().ok()?),
span: mac.span(), // incorrect span, but shouldn't matter too much
attrs: ast::AttrVec::new(),
tokens: Some(LazyTokenStream::new(LazyTokenStreamInner::Ready(ts))),
tokens: Some(LazyTokenStream::new(ts)),
})
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion src/modules/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> CfgIfVisitor<'a> {
}

impl<'a, 'ast: 'a> Visitor<'ast> for CfgIfVisitor<'a> {
fn visit_mac(&mut self, mac: &'ast ast::MacCall) {
fn visit_mac_call(&mut self, mac: &'ast ast::MacCall) {
match self.visit_mac_inner(mac) {
Ok(()) => (),
Err(e) => debug!("{}", e),
Expand Down
2 changes: 1 addition & 1 deletion src/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn get_skip_names(kind: &str, attrs: &[ast::Attribute]) -> Vec<String> {
for attr in attrs {
// rustc_ast::ast::Path is implemented partialEq
// but it is designed for segments.len() == 1
if let ast::AttrKind::Normal(attr_item) = &attr.kind {
if let ast::AttrKind::Normal(attr_item, _) = &attr.kind {
if pprust::path_to_string(&attr_item.path) != path {
continue;
}
Expand Down
10 changes: 9 additions & 1 deletion src/syntux/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,16 @@ impl<'a> Parser<'a> {
if !parser.eat_keyword(kw::If) {
return Err("Expected `if`");
}
// Inner attributes are not actually syntactically permitted here, but we don't
// care about inner vs outer attributes in this position. Our purpose with this
// special case parsing of cfg_if macros is to ensure we can correctly resolve
// imported modules that may have a custom `path` defined.
//
// As such, we just need to advance the parser past the attribute and up to
// to the opening brace.
// See also https://github.com/rust-lang/rust/pull/79433
parser
.parse_attribute(false)
.parse_attribute(rustc_parse::parser::attr::InnerAttrPolicy::Permitted)
.map_err(|_| "Failed to parse attributes")?;
}

Expand Down
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
| ast::ExprKind::Ret(..)
| ast::ExprKind::Tup(..)
| ast::ExprKind::Type(..)
| ast::ExprKind::Yield(None) => false,
| ast::ExprKind::Yield(None)
| ast::ExprKind::Underscore => false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
);
} else {
match &attr.kind {
ast::AttrKind::Normal(ref attribute_item)
ast::AttrKind::Normal(ref attribute_item, _)
if self.is_unknown_rustfmt_attr(&attribute_item.path.segments) =>
{
let file_name = self.parse_sess.span_to_filename(attr.span);
Expand Down
10 changes: 10 additions & 0 deletions tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,13 @@ fn foo() {
}
.await;
}

fn underscore() {
_= 1;
_;
[ _,a,_ ] = [1, 2, 3];
(a, _) = (8, 9);
TupleStruct( _, a) = TupleStruct(2, 2);

let _ : usize = foo(_, _);
}
11 changes: 11 additions & 0 deletions tests/source/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ pub struct Foo {
pub i: TypeForPublicField
}

// Destructuring
fn foo() {
S { x: 5,
..};
Struct {..} = Struct { a: 1, b: 4 };
Struct { a, .. } = Struct { a: 1, b: 2, c: 3};
TupleStruct(a,.., b) = TupleStruct(1, 2);
TupleStruct( ..) = TupleStruct(3, 4);
TupleStruct(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, .., bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) = TupleStruct(1, 2);
}

// #1095
struct S<T: /* comment */> {
t: T,
Expand Down
10 changes: 10 additions & 0 deletions tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,13 @@ fn foo() {
}
.await;
}

fn underscore() {
_ = 1;
_;
[_, a, _] = [1, 2, 3];
(a, _) = (8, 9);
TupleStruct(_, a) = TupleStruct(2, 2);

let _: usize = foo(_, _);
}
14 changes: 14 additions & 0 deletions tests/target/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ pub struct Foo {
pub i: TypeForPublicField,
}

// Destructuring
fn foo() {
S { x: 5, .. };
Struct { .. } = Struct { a: 1, b: 4 };
Struct { a, .. } = Struct { a: 1, b: 2, c: 3 };
TupleStruct(a, .., b) = TupleStruct(1, 2);
TupleStruct(..) = TupleStruct(3, 4);
TupleStruct(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
..,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
) = TupleStruct(1, 2);
}

// #1095
struct S<T /* comment */> {
t: T,
Expand Down