1
Fork 0

Format all the let chains in compiler

This commit is contained in:
Michael Goulet 2023-10-13 08:58:33 +00:00
parent 2763ca50da
commit b2d2184ede
206 changed files with 3120 additions and 2228 deletions

View file

@ -21,20 +21,22 @@ pub fn expand(
// Allow using `#[alloc_error_handler]` on an item statement
// FIXME - if we get deref patterns, use them to reduce duplication here
let (item, is_stmt, sig_span) =
if let Annotatable::Item(item) = &item
&& let ItemKind::Fn(fn_kind) = &item.kind
{
(item, false, ecx.with_def_site_ctxt(fn_kind.sig.span))
} else if let Annotatable::Stmt(stmt) = &item
&& let StmtKind::Item(item) = &stmt.kind
&& let ItemKind::Fn(fn_kind) = &item.kind
{
(item, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
} else {
ecx.sess.parse_sess.span_diagnostic.emit_err(errors::AllocErrorMustBeFn {span: item.span() });
return vec![orig_item];
};
let (item, is_stmt, sig_span) = if let Annotatable::Item(item) = &item
&& let ItemKind::Fn(fn_kind) = &item.kind
{
(item, false, ecx.with_def_site_ctxt(fn_kind.sig.span))
} else if let Annotatable::Stmt(stmt) = &item
&& let StmtKind::Item(item) = &stmt.kind
&& let ItemKind::Fn(fn_kind) = &item.kind
{
(item, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
} else {
ecx.sess
.parse_sess
.span_diagnostic
.emit_err(errors::AllocErrorMustBeFn { span: item.span() });
return vec![orig_item];
};
// Generate a bunch of new items using the AllocFnFactory
let span = ecx.with_def_site_ctxt(item.span);

View file

@ -193,10 +193,9 @@ impl<'cx, 'a> Context<'cx, 'a> {
fn manage_cond_expr(&mut self, expr: &mut P<Expr>) {
match &mut expr.kind {
ExprKind::AddrOf(_, mutability, local_expr) => {
self.with_is_consumed_management(
matches!(mutability, Mutability::Mut),
|this| this.manage_cond_expr(local_expr)
);
self.with_is_consumed_management(matches!(mutability, Mutability::Mut), |this| {
this.manage_cond_expr(local_expr)
});
}
ExprKind::Array(local_exprs) => {
for local_expr in local_exprs {
@ -223,7 +222,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
|this| {
this.manage_cond_expr(lhs);
this.manage_cond_expr(rhs);
}
},
);
}
ExprKind::Call(_, local_exprs) => {
@ -285,10 +284,9 @@ impl<'cx, 'a> Context<'cx, 'a> {
}
}
ExprKind::Unary(un_op, local_expr) => {
self.with_is_consumed_management(
matches!(un_op, UnOp::Neg | UnOp::Not),
|this| this.manage_cond_expr(local_expr)
);
self.with_is_consumed_management(matches!(un_op, UnOp::Neg | UnOp::Not), |this| {
this.manage_cond_expr(local_expr)
});
}
// Expressions that are not worth or can not be captured.
//

View file

@ -33,7 +33,7 @@ pub fn expand_concat(
accumulator.push_str(&b.to_string());
}
Ok(ast::LitKind::CStr(..)) => {
cx.emit_err(errors::ConcatCStrLit{ span: e.span});
cx.emit_err(errors::ConcatCStrLit { span: e.span });
has_errors = true;
}
Ok(ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..)) => {
@ -49,7 +49,9 @@ pub fn expand_concat(
}
},
// We also want to allow negative numeric literals.
ast::ExprKind::Unary(ast::UnOp::Neg, ref expr) if let ast::ExprKind::Lit(token_lit) = expr.kind => {
ast::ExprKind::Unary(ast::UnOp::Neg, ref expr)
if let ast::ExprKind::Lit(token_lit) = expr.kind =>
{
match ast::LitKind::from_token_lit(token_lit) {
Ok(ast::LitKind::Int(i, _)) => accumulator.push_str(&format!("-{i}")),
Ok(ast::LitKind::Float(f, _)) => accumulator.push_str(&format!("-{f}")),

View file

@ -140,8 +140,8 @@ pub fn expand_concat_bytes(
}
ast::ExprKind::Repeat(expr, count) => {
if let ast::ExprKind::Lit(token_lit) = count.value.kind
&& let Ok(ast::LitKind::Int(count_val, _)) =
ast::LitKind::from_token_lit(token_lit)
&& let Ok(ast::LitKind::Int(count_val, _)) =
ast::LitKind::from_token_lit(token_lit)
{
if let Some(elem) =
handle_array_element(cx, &mut has_errors, &mut missing_literals, expr)
@ -151,7 +151,7 @@ pub fn expand_concat_bytes(
}
}
} else {
cx.emit_err(errors::ConcatBytesBadRepeat {span: count.value.span });
cx.emit_err(errors::ConcatBytesBadRepeat { span: count.value.span });
}
}
&ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {

View file

@ -106,7 +106,9 @@ fn cs_clone_simple(
// This basic redundancy checking only prevents duplication of
// assertions like `AssertParamIsClone<Foo>` where the type is a
// simple name. That's enough to get a lot of cases, though.
if let Some(name) = field.ty.kind.is_simple_path() && !seen_type_names.insert(name) {
if let Some(name) = field.ty.kind.is_simple_path()
&& !seen_type_names.insert(name)
{
// Already produced an assertion for this type.
} else {
// let _: AssertParamIsClone<FieldTy>;

View file

@ -73,7 +73,9 @@ fn cs_total_eq_assert(
// This basic redundancy checking only prevents duplication of
// assertions like `AssertParamIsEq<Foo>` where the type is a
// simple name. That's enough to get a lot of cases, though.
if let Some(name) = field.ty.kind.is_simple_path() && !seen_type_names.insert(name) {
if let Some(name) = field.ty.kind.is_simple_path()
&& !seen_type_names.insert(name)
{
// Already produced an assertion for this type.
} else {
// let _: AssertParamIsEq<FieldTy>;

View file

@ -21,25 +21,26 @@ pub fn expand_deriving_partial_ord(
// Order in which to perform matching
let tag_then_data = if let Annotatable::Item(item) = item
&& let ItemKind::Enum(def, _) = &item.kind {
let dataful: Vec<bool> = def.variants.iter().map(|v| !v.data.fields().is_empty()).collect();
match dataful.iter().filter(|&&b| b).count() {
// No data, placing the tag check first makes codegen simpler
0 => true,
1..=2 => false,
_ => {
(0..dataful.len()-1).any(|i| {
if dataful[i] && let Some(idx) = dataful[i+1..].iter().position(|v| *v) {
idx >= 2
} else {
false
}
})
&& let ItemKind::Enum(def, _) = &item.kind
{
let dataful: Vec<bool> = def.variants.iter().map(|v| !v.data.fields().is_empty()).collect();
match dataful.iter().filter(|&&b| b).count() {
// No data, placing the tag check first makes codegen simpler
0 => true,
1..=2 => false,
_ => (0..dataful.len() - 1).any(|i| {
if dataful[i]
&& let Some(idx) = dataful[i + 1..].iter().position(|v| *v)
{
idx >= 2
} else {
false
}
}
} else {
true
};
}),
}
} else {
true
};
let partial_cmp_def = MethodDef {
name: sym::partial_cmp,
generics: Bounds::empty(),
@ -133,12 +134,16 @@ fn cs_partial_cmp(
if !tag_then_data
&& let ExprKind::Match(_, arms) = &mut expr1.kind
&& let Some(last) = arms.last_mut()
&& let PatKind::Wild = last.pat.kind {
last.body = expr2;
expr1
&& let PatKind::Wild = last.pat.kind
{
last.body = expr2;
expr1
} else {
let eq_arm =
cx.arm(span, cx.pat_some(span, cx.pat_path(span, equal_path.clone())), expr1);
let eq_arm = cx.arm(
span,
cx.pat_some(span, cx.pat_path(span, equal_path.clone())),
expr1,
);
let neq_arm =
cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
cx.expr_match(span, expr2, thin_vec![eq_arm, neq_arm])

View file

@ -73,7 +73,9 @@ fn parse_args<'a>(ecx: &mut ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<
let first_token = &p.token;
let fmtstr = if let token::Literal(lit) = first_token.kind && matches!(lit.kind, token::Str | token::StrRaw(_)) {
let fmtstr = if let token::Literal(lit) = first_token.kind
&& matches!(lit.kind, token::Str | token::StrRaw(_))
{
// This allows us to properly handle cases when the first comma
// after the format string is mistakenly replaced with any operator,
// which cause the expression parser to eat too much tokens.
@ -176,7 +178,7 @@ fn make_format_args(
&& block.stmts.len() == 1
&& let StmtKind::Expr(expr) = &block.stmts[0].kind
&& let ExprKind::Path(None, path) = &expr.kind
&& path.is_potential_trivial_const_arg()
&& path.is_potential_trivial_const_arg()
{
err.multipart_suggestion(
"quote your inlined format argument to use as string literal",
@ -184,7 +186,7 @@ fn make_format_args(
(unexpanded_fmt_span.shrink_to_hi(), "\"".to_string()),
(unexpanded_fmt_span.shrink_to_lo(), "\"".to_string()),
],
Applicability::MaybeIncorrect,
Applicability::MaybeIncorrect,
);
} else {
let sugg_fmt = match args.explicit_args().len() {
@ -257,8 +259,13 @@ fn make_format_args(
if let Some(note) = err.note {
e.note_ = Some(errors::InvalidFormatStringNote { note });
}
if let Some((label, span)) = err.secondary_label && is_source_literal {
e.label_ = Some(errors::InvalidFormatStringLabel { span: fmt_span.from_inner(InnerSpan::new(span.start, span.end)), label } );
if let Some((label, span)) = err.secondary_label
&& is_source_literal
{
e.label_ = Some(errors::InvalidFormatStringLabel {
span: fmt_span.from_inner(InnerSpan::new(span.start, span.end)),
label,
});
}
match err.suggestion {
parse::Suggestion::None => {}

View file

@ -24,20 +24,22 @@ pub fn expand(
// Allow using `#[global_allocator]` on an item statement
// FIXME - if we get deref patterns, use them to reduce duplication here
let (item, is_stmt, ty_span) =
if let Annotatable::Item(item) = &item
&& let ItemKind::Static(box ast::StaticItem { ty, ..}) = &item.kind
{
(item, false, ecx.with_def_site_ctxt(ty.span))
} else if let Annotatable::Stmt(stmt) = &item
&& let StmtKind::Item(item) = &stmt.kind
&& let ItemKind::Static(box ast::StaticItem { ty, ..}) = &item.kind
{
(item, true, ecx.with_def_site_ctxt(ty.span))
} else {
ecx.sess.parse_sess.span_diagnostic.emit_err(errors::AllocMustStatics{span: item.span()});
return vec![orig_item];
};
let (item, is_stmt, ty_span) = if let Annotatable::Item(item) = &item
&& let ItemKind::Static(box ast::StaticItem { ty, .. }) = &item.kind
{
(item, false, ecx.with_def_site_ctxt(ty.span))
} else if let Annotatable::Stmt(stmt) = &item
&& let StmtKind::Item(item) = &stmt.kind
&& let ItemKind::Static(box ast::StaticItem { ty, .. }) = &item.kind
{
(item, true, ecx.with_def_site_ctxt(ty.span))
} else {
ecx.sess
.parse_sess
.span_diagnostic
.emit_err(errors::AllocMustStatics { span: item.span() });
return vec![orig_item];
};
// Generate a bunch of new items using the AllocFnFactory
let span = ecx.with_def_site_ctxt(item.span);

View file

@ -35,11 +35,13 @@ pub fn expand_test_case(
let sp = ecx.with_def_site_ctxt(attr_sp);
let (mut item, is_stmt) = match anno_item {
Annotatable::Item(item) => (item, false),
Annotatable::Stmt(stmt) if let ast::StmtKind::Item(_) = stmt.kind => if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
(i, true)
} else {
unreachable!()
},
Annotatable::Stmt(stmt) if let ast::StmtKind::Item(_) = stmt.kind => {
if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
(i, true)
} else {
unreachable!()
}
}
_ => {
ecx.emit_err(errors::TestCaseNonItem { span: anno_item.span() });
return vec![];