diff options
Diffstat (limited to 'compiler/rustc_builtin_macros')
| -rw-r--r-- | compiler/rustc_builtin_macros/src/alloc_error_handler.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/assert/context.rs | 16 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/concat.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/concat_bytes.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/deriving/clone.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs | 51 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/format.rs | 17 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/global_allocator.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/lib.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/test.rs | 12 |
11 files changed, 107 insertions, 83 deletions
diff --git a/compiler/rustc_builtin_macros/src/alloc_error_handler.rs b/compiler/rustc_builtin_macros/src/alloc_error_handler.rs index 82bae9157e7..070d50708ff 100644 --- a/compiler/rustc_builtin_macros/src/alloc_error_handler.rs +++ b/compiler/rustc_builtin_macros/src/alloc_error_handler.rs @@ -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); diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs index 0682d48aca6..6733b9e56ad 100644 --- a/compiler/rustc_builtin_macros/src/assert/context.rs +++ b/compiler/rustc_builtin_macros/src/assert/context.rs @@ -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. // diff --git a/compiler/rustc_builtin_macros/src/concat.rs b/compiler/rustc_builtin_macros/src/concat.rs index 9695fb4fee1..6b8330bfdaf 100644 --- a/compiler/rustc_builtin_macros/src/concat.rs +++ b/compiler/rustc_builtin_macros/src/concat.rs @@ -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}")), diff --git a/compiler/rustc_builtin_macros/src/concat_bytes.rs b/compiler/rustc_builtin_macros/src/concat_bytes.rs index 6a1586f071c..c4f5af384c1 100644 --- a/compiler/rustc_builtin_macros/src/concat_bytes.rs +++ b/compiler/rustc_builtin_macros/src/concat_bytes.rs @@ -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) { diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index b468abe3249..1649cc76c8d 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -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>; diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs index a000e4895d1..8a6d219379f 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs @@ -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>; diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs index a5b3a504e38..f3164bd2c2a 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs @@ -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]) diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index 8397b5e4221..4b5c777f4ad 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -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 => {} diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs index 1bec00add55..33392edf060 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -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); diff --git a/compiler/rustc_builtin_macros/src/lib.rs b/compiler/rustc_builtin_macros/src/lib.rs index ebf1448f55c..35b615697f7 100644 --- a/compiler/rustc_builtin_macros/src/lib.rs +++ b/compiler/rustc_builtin_macros/src/lib.rs @@ -71,33 +71,35 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) { } register_bang! { + // tidy-alphabetical-start asm: asm::expand_asm, assert: assert::expand_assert, cfg: cfg::expand_cfg, column: source_util::expand_column, compile_error: compile_error::expand_compile_error, + concat: concat::expand_concat, concat_bytes: concat_bytes::expand_concat_bytes, concat_idents: concat_idents::expand_concat_idents, - concat: concat::expand_concat, + const_format_args: format::expand_format_args, + core_panic: edition_panic::expand_panic, env: env::expand_env, file: source_util::expand_file, - format_args_nl: format::expand_format_args_nl, format_args: format::expand_format_args, - const_format_args: format::expand_format_args, + format_args_nl: format::expand_format_args_nl, global_asm: asm::expand_global_asm, + include: source_util::expand_include, include_bytes: source_util::expand_include_bytes, include_str: source_util::expand_include_str, - include: source_util::expand_include, line: source_util::expand_line, log_syntax: log_syntax::expand_log_syntax, module_path: source_util::expand_mod, option_env: env::expand_option_env, - core_panic: edition_panic::expand_panic, std_panic: edition_panic::expand_panic, - unreachable: edition_panic::expand_unreachable, stringify: source_util::expand_stringify, trace_macros: trace_macros::expand_trace_macros, type_ascribe: type_ascribe::expand_type_ascribe, + unreachable: edition_panic::expand_unreachable, + // tidy-alphabetical-end } register_attr! { diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 1580a6f6dd3..6d55603c708 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -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![]; |
