Skip to content

Commit 30baa4c

Browse files
tests: fix some changes for rustc-ap update
1 parent e3e04af commit 30baa4c

File tree

14 files changed

+201
-115
lines changed

14 files changed

+201
-115
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rustfmt-core/rustfmt-lib/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ version = "638.0.0"
3838
package = "rustc-ap-rustc_errors"
3939
version = "638.0.0"
4040

41-
[dependencies.rustc_error_codes]
42-
package = "rustc-ap-rustc_error_codes"
43-
version = "638.0.0"
44-
4541
[dependencies.rustc_parse]
4642
package = "rustc-ap-rustc_parse"
4743
version = "638.0.0"

rustfmt-core/rustfmt-lib/src/expr.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ pub(crate) fn format_expr(
252252
fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool {
253253
match lhs.kind {
254254
ast::ExprKind::Lit(ref lit) => match lit.kind {
255-
ast::LitKind::Float(_, ref float_type) if ast::LitFloatType::Unsuffixed == *float_type =>
255+
ast::LitKind::Float(_, ref float_type)
256+
if ast::LitFloatType::Unsuffixed == *float_type =>
256257
{
257258
context.snippet(lit.span).ends_with('.')
258259
}
@@ -1336,8 +1337,12 @@ pub(crate) fn can_be_overflowed_expr(
13361337
|| (context.use_block_indent() && args_len == 1)
13371338
}
13381339
ast::ExprKind::Mac(ref mac) => {
1339-
match (syntax::ast::MacDelimiter::from_token(mac.args.delim()), context.config.overflow_delimited_expr()) {
1340-
(Some(ast::MacDelimiter::Bracket), true) | (Some(ast::MacDelimiter::Brace), true) => true,
1340+
match (
1341+
syntax::ast::MacDelimiter::from_token(mac.args.delim()),
1342+
context.config.overflow_delimited_expr(),
1343+
) {
1344+
(Some(ast::MacDelimiter::Bracket), true)
1345+
| (Some(ast::MacDelimiter::Brace), true) => true,
13411346
_ => context.use_block_indent() && args_len == 1,
13421347
}
13431348
}

rustfmt-core/rustfmt-lib/src/items.rs

+59-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::cmp::{max, min, Ordering};
55

66
use regex::Regex;
77
use rustc_span::{BytePos, DUMMY_SP, source_map, Span, symbol};
8-
use rustc_target::spec::abi;
98
use syntax::visit;
109
use syntax::{ast, ptr};
1110

@@ -195,7 +194,11 @@ impl<'a> Item<'a> {
195194
fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
196195
Item {
197196
keyword: "",
198-
abi: format_extern(ast::Extern::from_abi(fm.abi), config.force_explicit_abi(), true),
197+
abi: format_extern(
198+
ast::Extern::from_abi(fm.abi),
199+
config.force_explicit_abi(),
200+
true,
201+
),
199202
vis: None,
200203
body: fm
201204
.items
@@ -220,7 +223,6 @@ pub(crate) struct FnSig<'a> {
220223
decl: &'a ast::FnDecl,
221224
generics: &'a ast::Generics,
222225
ext: ast::Extern,
223-
// abi: abi::Abi,
224226
is_async: Cow<'a, ast::IsAsync>,
225227
constness: ast::Constness,
226228
defaultness: ast::Defaultness,
@@ -250,11 +252,6 @@ impl<'a> FnSig<'a> {
250252
method_sig: &'a ast::FnSig,
251253
generics: &'a ast::Generics,
252254
) -> FnSig<'a> {
253-
// let abi = match method_sig.header.ext {
254-
// ast::Extern::None => abi::Abi::Rust,
255-
// ast::Extern::Implicit => abi::Abi::C,
256-
// ast::Extern::Explicit(abi) => self.lower_abi(abi),
257-
// };
258255
FnSig {
259256
unsafety: method_sig.header.unsafety,
260257
is_async: Cow::Borrowed(&method_sig.header.asyncness.node),
@@ -646,21 +643,70 @@ impl<'a> FmtVisitor<'a> {
646643
buffer.push((self.buffer.clone(), item.clone()));
647644
self.buffer.clear();
648645
}
646+
647+
fn is_type(ty: &Option<syntax::ptr::P<ast::Ty>>) -> bool {
648+
match ty {
649+
None => true,
650+
Some(lty) => match lty.kind.opaque_top_hack() {
651+
None => true,
652+
Some(_) => false,
653+
},
654+
}
655+
}
656+
657+
fn is_opaque(ty: &Option<syntax::ptr::P<ast::Ty>>) -> bool {
658+
match ty {
659+
None => false,
660+
Some(lty) => match lty.kind.opaque_top_hack() {
661+
None => false,
662+
Some(_) => true,
663+
},
664+
}
665+
}
666+
667+
fn both_type(
668+
a: &Option<syntax::ptr::P<ast::Ty>>,
669+
b: &Option<syntax::ptr::P<ast::Ty>>,
670+
) -> bool {
671+
is_type(a) && is_type(b)
672+
}
673+
674+
fn both_opaque(
675+
a: &Option<syntax::ptr::P<ast::Ty>>,
676+
b: &Option<syntax::ptr::P<ast::Ty>>,
677+
) -> bool {
678+
is_opaque(a) && is_opaque(b)
679+
}
680+
681+
// In rustc-ap-v638 the `OpaqueTy` AssocItemKind was removed but
682+
// we still need to differentiate to maintain sorting order.
683+
649684
// type -> opaque -> const -> macro -> method
650685
use crate::ast::AssocItemKind::*;
651686
fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
652687
match (a, b) {
653-
(TyAlias(..), TyAlias(..))
654-
| (Const(..), Const(..)) => false,
688+
(TyAlias(_, ref lty), TyAlias(_, ref rty))
689+
if both_type(lty, rty) || both_opaque(lty, rty) =>
690+
{
691+
false
692+
}
693+
(Const(..), Const(..)) => false,
655694
_ => true,
656695
}
657696
}
658697

659698
buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
660-
(TyAlias(..), TyAlias(..))
661-
| (Const(..), Const(..))
662-
| (Macro(..), Macro(..)) => a.ident.as_str().cmp(&b.ident.as_str()),
699+
(TyAlias(_, ref lty), TyAlias(_, ref rty))
700+
if both_type(lty, rty) || both_opaque(lty, rty) =>
701+
{
702+
a.ident.as_str().cmp(&b.ident.as_str())
703+
}
704+
(Const(..), Const(..)) | (Macro(..), Macro(..)) => {
705+
a.ident.as_str().cmp(&b.ident.as_str())
706+
}
663707
(Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
708+
(TyAlias(_, ref ty), _) if is_type(ty) => Ordering::Less,
709+
(_, TyAlias(_, ref ty)) if is_type(ty) => Ordering::Greater,
664710
(TyAlias(..), _) => Ordering::Less,
665711
(_, TyAlias(..)) => Ordering::Greater,
666712
(Const(..), _) => Ordering::Less,

rustfmt-core/rustfmt-lib/src/macros.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Rewrite for MacroArg {
8181
MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
8282
MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
8383
MacroArg::Item(ref item) => item.rewrite(context, shape),
84-
MacroArg::Keyword(ident, _) => Some(ident.to_string()),
84+
MacroArg::Keyword(ident, _) => Some(ident.name.to_string()),
8585
}
8686
}
8787
}
@@ -261,12 +261,7 @@ fn rewrite_macro_inner(
261261
} else {
262262
original_style
263263
};
264-
265-
let ts: TokenStream = match *mac.args {
266-
ast::MacArgs::Empty => TokenStream::default(),
267-
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
268-
ast::MacArgs::Eq(_, token_stream) => token_stream,
269-
};
264+
let ts = mac.args.inner_tokens();
270265

271266
let has_comment = contains_comment(context.snippet(mac.span()));
272267
if ts.is_empty() && !has_comment {
@@ -493,8 +488,8 @@ pub(crate) fn rewrite_macro_def(
493488
}
494489
let ts: TokenStream = match *def.body {
495490
ast::MacArgs::Empty => TokenStream::default(),
496-
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
497-
ast::MacArgs::Eq(_, token_stream) => token_stream,
491+
ast::MacArgs::Delimited(_, _, ref token_stream) => token_stream.clone(),
492+
ast::MacArgs::Eq(_, ref token_stream) => token_stream.clone(),
498493
};
499494
let mut parser = MacroParser::new(ts.into_trees());
500495
let parsed_def = match parser.parse() {
@@ -1201,11 +1196,12 @@ pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> O
12011196
// https://github.com/rust-lang/rust/pull/62672
12021197
let path = &pprust::path_to_string(&mac.path);
12031198
if path == "try" || path == "r#try" {
1204-
let ts: TokenStream = match *mac.args {
1205-
ast::MacArgs::Empty => TokenStream::default(),
1206-
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
1207-
ast::MacArgs::Eq(_, token_stream) => token_stream,
1208-
};
1199+
// let ts: TokenStream = match *mac.args {
1200+
// ast::MacArgs::Empty => TokenStream::default(),
1201+
// ast::MacArgs::Delimited(_, _, ref token_stream) => token_stream.clone(),
1202+
// ast::MacArgs::Eq(_, ref token_stream) => token_stream.clone(),
1203+
// };
1204+
let ts = mac.args.inner_tokens();
12091205
let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect());
12101206

12111207
let mut kind = parser.parse_expr().ok()?;
@@ -1223,7 +1219,7 @@ pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> O
12231219
}
12241220
}
12251221

1226-
fn macro_style(mac: &ast::Mac, context: &RewriteContext<'_>) -> DelimToken {
1222+
pub(crate) fn macro_style(mac: &ast::Mac, context: &RewriteContext<'_>) -> DelimToken {
12271223
let snippet = context.snippet(mac.span());
12281224
let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
12291225
let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
@@ -1476,7 +1472,10 @@ fn format_lazy_static(
14761472

14771473
while parser.token.kind != TokenKind::Eof {
14781474
// Parse a `lazy_static!` item.
1479-
let vis = crate::utils::format_visibility(context, &parse_or!(parse_visibility, rustc_parse::parser::FollowedByType::No));
1475+
let vis = crate::utils::format_visibility(
1476+
context,
1477+
&parse_or!(parse_visibility, rustc_parse::parser::FollowedByType::No),
1478+
);
14801479
parser.eat_keyword(kw::Static);
14811480
parser.eat_keyword(kw::Ref);
14821481
let id = parse_or!(parse_ident);

rustfmt-core/rustfmt-lib/src/patterns.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -179,25 +179,28 @@ impl Rewrite for Pat {
179179
None
180180
}
181181
}
182-
PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
183-
let infix = match end_kind.node {
184-
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
185-
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
186-
RangeEnd::Excluded => "..",
187-
};
188-
let infix = if context.config.spaces_around_ranges() {
189-
format!(" {} ", infix)
190-
} else {
191-
infix.to_owned()
192-
};
193-
rewrite_pair(
194-
&lhs.unwrap(),
195-
&rhs.unwrap(),
196-
PairParts::infix(&infix),
197-
context,
198-
shape,
199-
SeparatorPlace::Front,
200-
)
182+
PatKind::Range(ref lhs, ref rhs, ref end_kind) => match (lhs, rhs) {
183+
(Some(lhs), Some(rhs)) => {
184+
let infix = match end_kind.node {
185+
RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
186+
RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
187+
RangeEnd::Excluded => "..",
188+
};
189+
let infix = if context.config.spaces_around_ranges() {
190+
format!(" {} ", infix)
191+
} else {
192+
infix.to_owned()
193+
};
194+
rewrite_pair(
195+
&**lhs,
196+
&**rhs,
197+
PairParts::infix(&infix),
198+
context,
199+
shape,
200+
SeparatorPlace::Front,
201+
)
202+
},
203+
(_, _) => unimplemented!(),
201204
}
202205
PatKind::Ref(ref pat, mutability) => {
203206
let prefix = format!("&{}", format_mutability(mutability));

rustfmt-core/rustfmt-lib/src/skip.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ impl SkipContext {
2929
}
3030
}
3131
for attr in attrs {
32-
if is_skip_attr_with(&attr.get_normal_item().path.segments, |s| s == sym!(macros)) {
33-
get_skip_names(&mut self.macros, attr)
34-
} else if is_skip_attr_with(&attr.get_normal_item().path.segments, |s| s == sym::attributes) {
35-
get_skip_names(&mut self.attributes, attr)
32+
match &attr.kind {
33+
syntax::ast::AttrKind::Normal(ref attr_item) => {
34+
if is_skip_attr_with(&attr_item.path.segments, |s| s == sym!(macros)) {
35+
get_skip_names(&mut self.macros, attr)
36+
} else if is_skip_attr_with(&attr_item.path.segments, |s| s == sym::attributes) {
37+
get_skip_names(&mut self.attributes, attr)
38+
}
39+
}
40+
_ => (),
3641
}
3742
}
3843
}

rustfmt-core/rustfmt-lib/src/syntux/parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ impl<'a> Parser<'a> {
285285
) -> Result<Vec<ast::Item>, &'static str> {
286286
let token_stream: syntax::tokenstream::TokenStream = match *mac.args {
287287
ast::MacArgs::Empty => syntax::tokenstream::TokenStream::default(),
288-
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
289-
ast::MacArgs::Eq(_, token_stream) => token_stream,
288+
ast::MacArgs::Delimited(_, _, ref token_stream) => token_stream.clone(),
289+
ast::MacArgs::Eq(_, ref token_stream) => token_stream.clone(),
290290
};
291291
let mut parser = rustc_parse::stream_to_parser_with_base_dir(
292292
sess.inner(),

rustfmt-core/rustfmt-lib/src/syntux/session.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ pub(crate) struct ParseSess {
2727
struct SilentEmitter;
2828

2929
impl Emitter for SilentEmitter {
30-
fn source_map(&self) -> Option<&Lrc<SourceMap>> { None }
30+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
31+
None
32+
}
3133
fn emit_diagnostic(&mut self, _db: &Diagnostic) {}
3234
}
3335

@@ -53,7 +55,9 @@ impl SilentOnIgnoredFilesEmitter {
5355
}
5456

5557
impl Emitter for SilentOnIgnoredFilesEmitter {
56-
fn source_map(&self) -> Option<&Lrc<SourceMap>> { None }
58+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
59+
None
60+
}
5761
fn emit_diagnostic(&mut self, db: &Diagnostic) {
5862
if db.level == DiagnosticLevel::Fatal {
5963
return self.handle_non_ignoreable_error(db);
@@ -271,13 +275,16 @@ mod tests {
271275
use crate::is_nightly_channel;
272276
use crate::utils::mk_sp;
273277
use std::path::PathBuf;
274-
use rustc_span::{MultiSpan, FileName as SourceMapFileName};
278+
use rustc_span::{DUMMY_SP, MultiSpan, FileName as SourceMapFileName};
275279

276280
struct TestEmitter {
277281
num_emitted_errors: Rc<RefCell<u32>>,
278282
}
279283

280284
impl Emitter for TestEmitter {
285+
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
286+
None
287+
}
281288
fn emit_diagnostic(&mut self, _db: &Diagnostic) {
282289
*self.num_emitted_errors.borrow_mut() += 1;
283290
}
@@ -291,6 +298,7 @@ mod tests {
291298
children: vec![],
292299
suggestions: vec![],
293300
span: span.unwrap_or_else(MultiSpan::new),
301+
sort_span: DUMMY_SP,
294302
}
295303
}
296304

rustfmt-core/rustfmt-lib/src/utils.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,19 @@ pub(crate) fn format_mutability(mutability: ast::Mutability) -> &'static str {
133133
}
134134

135135
#[inline]
136-
pub(crate) fn format_extern(ext: ast::Extern, explicit_abi: bool, is_mod: bool) -> Cow<'static, str> {
137-
match (ext, explicit_abi, is_mod) {
138-
(ast::Extern::None, _, false) => Cow::from(""),
139-
(ast::Extern::Implicit, false, _) => Cow::from("extern "),
140-
(ast::Extern::Explicit(abi), _, _) => Cow::from(format!("extern {} ", abi.symbol_unescaped.as_str())),
141-
(_, _, _) => unreachable!(),
142-
}
143-
}
136+
pub(crate) fn format_extern(
137+
ext: ast::Extern,
138+
explicit_abi: bool,
139+
is_mod: bool,
140+
) -> Cow<'static, str> {
141+
let abi = match ext {
142+
ast::Extern::None => abi::Abi::Rust,
143+
ast::Extern::Implicit => abi::Abi::C,
144+
ast::Extern::Explicit(abi) => {
145+
abi::lookup(&abi.symbol_unescaped.as_str()).unwrap_or(abi::Abi::Rust)
146+
}
147+
};
144148

145-
#[inline]
146-
pub(crate) fn format_abi(abi: abi::Abi, explicit_abi: bool, is_mod: bool) -> Cow<'static, str> {
147149
if abi == abi::Abi::Rust && !is_mod {
148150
Cow::from("")
149151
} else if abi == abi::Abi::C && !explicit_abi {
@@ -428,7 +430,7 @@ pub(crate) fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr {
428430
| ast::ExprKind::Binary(_, ref e, _)
429431
| ast::ExprKind::Cast(ref e, _)
430432
| ast::ExprKind::Type(ref e, _)
431-
| ast::ExprKind::Assign(ref e,_, _)
433+
| ast::ExprKind::Assign(ref e, _, _)
432434
| ast::ExprKind::AssignOp(_, ref e, _)
433435
| ast::ExprKind::Field(ref e, _)
434436
| ast::ExprKind::Index(ref e, _)

0 commit comments

Comments
 (0)