|
1 | 1 | use std::str::FromStr;
|
2 | 2 |
|
3 | 3 | use rustc_abi::ExternAbi;
|
4 |
| -use rustc_ast::attr::list_contains_name; |
5 | 4 | use rustc_ast::expand::autodiff_attrs::{
|
6 | 5 | AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ret_activity,
|
7 | 6 | };
|
@@ -377,24 +376,20 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
377 | 376 | let segments =
|
378 | 377 | set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
|
379 | 378 | match segments.as_slice() {
|
380 |
| - [sym::arm, sym::a32] | [sym::arm, sym::t32] => { |
381 |
| - if !tcx.sess.target.has_thumb_interworking { |
382 |
| - struct_span_code_err!( |
383 |
| - tcx.dcx(), |
384 |
| - attr.span, |
385 |
| - E0779, |
386 |
| - "target does not support `#[instruction_set]`" |
387 |
| - ) |
388 |
| - .emit(); |
389 |
| - None |
390 |
| - } else if segments[1] == sym::a32 { |
391 |
| - Some(InstructionSetAttr::ArmA32) |
392 |
| - } else if segments[1] == sym::t32 { |
393 |
| - Some(InstructionSetAttr::ArmT32) |
394 |
| - } else { |
395 |
| - unreachable!() |
396 |
| - } |
| 379 | + [sym::arm, sym::a32 | sym::t32] |
| 380 | + if !tcx.sess.target.has_thumb_interworking => |
| 381 | + { |
| 382 | + struct_span_code_err!( |
| 383 | + tcx.dcx(), |
| 384 | + attr.span, |
| 385 | + E0779, |
| 386 | + "target does not support `#[instruction_set]`" |
| 387 | + ) |
| 388 | + .emit(); |
| 389 | + None |
397 | 390 | }
|
| 391 | + [sym::arm, sym::a32] => Some(InstructionSetAttr::ArmA32), |
| 392 | + [sym::arm, sym::t32] => Some(InstructionSetAttr::ArmT32), |
398 | 393 | _ => {
|
399 | 394 | struct_span_code_err!(
|
400 | 395 | tcx.dcx(),
|
@@ -435,7 +430,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
435 | 430 | && let Some((sym::align, literal)) = item.singleton_lit_list()
|
436 | 431 | {
|
437 | 432 | rustc_attr_parsing::parse_alignment(&literal.kind)
|
438 |
| - .map_err(|msg| { |
| 433 | + .inspect_err(|msg| { |
439 | 434 | struct_span_code_err!(
|
440 | 435 | tcx.dcx(),
|
441 | 436 | literal.span,
|
@@ -536,25 +531,27 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
536 | 531 | }
|
537 | 532 |
|
538 | 533 | if attr.is_word() {
|
539 |
| - InlineAttr::Hint |
540 |
| - } else if let Some(ref items) = attr.meta_item_list() { |
541 |
| - inline_span = Some(attr.span); |
542 |
| - if items.len() != 1 { |
543 |
| - struct_span_code_err!(tcx.dcx(), attr.span, E0534, "expected one argument").emit(); |
544 |
| - InlineAttr::None |
545 |
| - } else if list_contains_name(items, sym::always) { |
546 |
| - InlineAttr::Always |
547 |
| - } else if list_contains_name(items, sym::never) { |
548 |
| - InlineAttr::Never |
549 |
| - } else { |
550 |
| - struct_span_code_err!(tcx.dcx(), items[0].span(), E0535, "invalid argument") |
551 |
| - .with_help("valid inline arguments are `always` and `never`") |
552 |
| - .emit(); |
| 534 | + return InlineAttr::Hint; |
| 535 | + } |
| 536 | + let Some(ref items) = attr.meta_item_list() else { |
| 537 | + return ia; |
| 538 | + }; |
553 | 539 |
|
554 |
| - InlineAttr::None |
555 |
| - } |
| 540 | + inline_span = Some(attr.span); |
| 541 | + let [item] = &items[..] else { |
| 542 | + struct_span_code_err!(tcx.dcx(), attr.span, E0534, "expected one argument").emit(); |
| 543 | + return InlineAttr::None; |
| 544 | + }; |
| 545 | + if item.has_name(sym::always) { |
| 546 | + InlineAttr::Always |
| 547 | + } else if item.has_name(sym::never) { |
| 548 | + InlineAttr::Never |
556 | 549 | } else {
|
557 |
| - ia |
| 550 | + struct_span_code_err!(tcx.dcx(), item.span(), E0535, "invalid argument") |
| 551 | + .with_help("valid inline arguments are `always` and `never`") |
| 552 | + .emit(); |
| 553 | + |
| 554 | + InlineAttr::None |
558 | 555 | }
|
559 | 556 | });
|
560 | 557 | codegen_fn_attrs.inline = attrs.iter().fold(codegen_fn_attrs.inline, |ia, attr| {
|
@@ -586,23 +583,25 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
586 | 583 | let err = |sp, s| struct_span_code_err!(tcx.dcx(), sp, E0722, "{}", s).emit();
|
587 | 584 | if attr.is_word() {
|
588 | 585 | err(attr.span, "expected one argument");
|
589 |
| - ia |
590 |
| - } else if let Some(ref items) = attr.meta_item_list() { |
591 |
| - inline_span = Some(attr.span); |
592 |
| - if items.len() != 1 { |
593 |
| - err(attr.span, "expected one argument"); |
594 |
| - OptimizeAttr::Default |
595 |
| - } else if list_contains_name(items, sym::size) { |
596 |
| - OptimizeAttr::Size |
597 |
| - } else if list_contains_name(items, sym::speed) { |
598 |
| - OptimizeAttr::Speed |
599 |
| - } else if list_contains_name(items, sym::none) { |
600 |
| - OptimizeAttr::DoNotOptimize |
601 |
| - } else { |
602 |
| - err(items[0].span(), "invalid argument"); |
603 |
| - OptimizeAttr::Default |
604 |
| - } |
| 586 | + return ia; |
| 587 | + } |
| 588 | + let Some(ref items) = attr.meta_item_list() else { |
| 589 | + return OptimizeAttr::Default; |
| 590 | + }; |
| 591 | + |
| 592 | + inline_span = Some(attr.span); |
| 593 | + let [item] = &items[..] else { |
| 594 | + err(attr.span, "expected one argument"); |
| 595 | + return OptimizeAttr::Default; |
| 596 | + }; |
| 597 | + if item.has_name(sym::size) { |
| 598 | + OptimizeAttr::Size |
| 599 | + } else if item.has_name(sym::speed) { |
| 600 | + OptimizeAttr::Speed |
| 601 | + } else if item.has_name(sym::none) { |
| 602 | + OptimizeAttr::DoNotOptimize |
605 | 603 | } else {
|
| 604 | + err(item.span(), "invalid argument"); |
606 | 605 | OptimizeAttr::Default
|
607 | 606 | }
|
608 | 607 | });
|
@@ -644,25 +643,20 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
644 | 643 | // llvm/llvm-project#70563).
|
645 | 644 | if !codegen_fn_attrs.target_features.is_empty()
|
646 | 645 | && matches!(codegen_fn_attrs.inline, InlineAttr::Always)
|
| 646 | + && let Some(span) = inline_span |
647 | 647 | {
|
648 |
| - if let Some(span) = inline_span { |
649 |
| - tcx.dcx().span_err(span, "cannot use `#[inline(always)]` with `#[target_feature]`"); |
650 |
| - } |
| 648 | + tcx.dcx().span_err(span, "cannot use `#[inline(always)]` with `#[target_feature]`"); |
651 | 649 | }
|
652 | 650 |
|
653 |
| - if !codegen_fn_attrs.no_sanitize.is_empty() && codegen_fn_attrs.inline.always() { |
654 |
| - if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) { |
655 |
| - let hir_id = tcx.local_def_id_to_hir_id(did); |
656 |
| - tcx.node_span_lint( |
657 |
| - lint::builtin::INLINE_NO_SANITIZE, |
658 |
| - hir_id, |
659 |
| - no_sanitize_span, |
660 |
| - |lint| { |
661 |
| - lint.primary_message("`no_sanitize` will have no effect after inlining"); |
662 |
| - lint.span_note(inline_span, "inlining requested here"); |
663 |
| - }, |
664 |
| - ) |
665 |
| - } |
| 651 | + if !codegen_fn_attrs.no_sanitize.is_empty() |
| 652 | + && codegen_fn_attrs.inline.always() |
| 653 | + && let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) |
| 654 | + { |
| 655 | + let hir_id = tcx.local_def_id_to_hir_id(did); |
| 656 | + tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| { |
| 657 | + lint.primary_message("`no_sanitize` will have no effect after inlining"); |
| 658 | + lint.span_note(inline_span, "inlining requested here"); |
| 659 | + }) |
666 | 660 | }
|
667 | 661 |
|
668 | 662 | // Weak lang items have the same semantics as "std internal" symbols in the
|
@@ -692,10 +686,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
692 | 686 | // Any linkage to LLVM intrinsics for now forcibly marks them all as never
|
693 | 687 | // unwinds since LLVM sometimes can't handle codegen which `invoke`s
|
694 | 688 | // intrinsic functions.
|
695 |
| - if let Some(name) = &codegen_fn_attrs.link_name { |
696 |
| - if name.as_str().starts_with("llvm.") { |
697 |
| - codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND; |
698 |
| - } |
| 689 | + if let Some(name) = &codegen_fn_attrs.link_name |
| 690 | + && name.as_str().starts_with("llvm.") |
| 691 | + { |
| 692 | + codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND; |
699 | 693 | }
|
700 | 694 |
|
701 | 695 | if let Some(features) = check_tied_features(
|
@@ -756,18 +750,13 @@ fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
756 | 750 |
|
757 | 751 | fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &hir::Attribute) -> Option<u16> {
|
758 | 752 | use rustc_ast::{LitIntType, LitKind, MetaItemLit};
|
759 |
| - let meta_item_list = attr.meta_item_list(); |
760 |
| - let meta_item_list = meta_item_list.as_deref(); |
761 |
| - let sole_meta_list = match meta_item_list { |
762 |
| - Some([item]) => item.lit(), |
763 |
| - Some(_) => { |
764 |
| - tcx.dcx().emit_err(errors::InvalidLinkOrdinalNargs { span: attr.span }); |
765 |
| - return None; |
766 |
| - } |
767 |
| - _ => None, |
| 753 | + let meta_item_list = attr.meta_item_list()?; |
| 754 | + let [sole_meta_list] = &meta_item_list[..] else { |
| 755 | + tcx.dcx().emit_err(errors::InvalidLinkOrdinalNargs { span: attr.span }); |
| 756 | + return None; |
768 | 757 | };
|
769 | 758 | if let Some(MetaItemLit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) =
|
770 |
| - sole_meta_list |
| 759 | + sole_meta_list.lit() |
771 | 760 | {
|
772 | 761 | // According to the table at
|
773 | 762 | // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-header, the
|
|
0 commit comments