From 6e190fa993ddcf04c978c918cfc785d1e2f26f8b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 25 Mar 2024 20:36:13 +0100 Subject: [PATCH 1/7] panic_str only exists for the migration to 2021 panic macros --- compiler/rustc_lint/src/non_fmt_panic.rs | 2 +- compiler/rustc_span/src/symbol.rs | 2 +- library/core/src/option.rs | 4 ++-- library/core/src/panic.rs | 4 ++-- library/core/src/panicking.rs | 27 +++++++++++++++--------- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_lint/src/non_fmt_panic.rs b/compiler/rustc_lint/src/non_fmt_panic.rs index a2d07fff5067c..e81790a7348e2 100644 --- a/compiler/rustc_lint/src/non_fmt_panic.rs +++ b/compiler/rustc_lint/src/non_fmt_panic.rs @@ -53,7 +53,7 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt { if Some(def_id) == cx.tcx.lang_items().begin_panic_fn() || Some(def_id) == cx.tcx.lang_items().panic_fn() - || f_diagnostic_name == Some(sym::panic_str) + || f_diagnostic_name == Some(sym::panic_str_2015) { if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id { if matches!( diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 73fcd2a76dfc0..8276f11fbcbda 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1306,7 +1306,7 @@ symbols! { panic_misaligned_pointer_dereference, panic_nounwind, panic_runtime, - panic_str, + panic_str_2015, panic_unwind, panicking, param_attrs, diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 631e1654ce075..1e3ed0f7c49f1 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -554,7 +554,7 @@ #![stable(feature = "rust1", since = "1.0.0")] use crate::iter::{self, FusedIterator, TrustedLen}; -use crate::panicking::{panic, panic_str}; +use crate::panicking::{panic, panic_display}; use crate::pin::Pin; use crate::{ cmp, convert, hint, mem, @@ -1991,7 +1991,7 @@ const fn unwrap_failed() -> ! { #[track_caller] #[rustc_const_unstable(feature = "const_option", issue = "67441")] const fn expect_failed(msg: &str) -> ! { - panic_str(msg) + panic_display(&msg) } ///////////////////////////////////////////////////////////////////////////// diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index b520efe93f90d..8771f40f9b42b 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -27,9 +27,9 @@ pub macro panic_2015 { ($msg:literal $(,)?) => ( $crate::panicking::panic($msg) ), - // Use `panic_str` instead of `panic_display::<&str>` for non_fmt_panic lint. + // Use `panic_str_2015` instead of `panic_display::<&str>` for non_fmt_panic lint. ($msg:expr $(,)?) => ({ - $crate::panicking::panic_str($msg); + $crate::panicking::panic_str_2015($msg); }), // Special-case the single-argument case for const_panic. ("{}", $arg:expr $(,)?) => ({ diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index a8940d9cd1eb0..7f7f06866543f 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -124,8 +124,8 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo // above. /// The underlying implementation of core's `panic!` macro when no formatting is used. -// never inline unless panic_immediate_abort to avoid code -// bloat at the call sites as much as possible +// Never inline unless panic_immediate_abort to avoid code +// bloat at the call sites as much as possible. #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)] #[cfg_attr(feature = "panic_immediate_abort", inline)] #[track_caller] @@ -138,6 +138,11 @@ pub const fn panic(expr: &'static str) -> ! { // truncation and padding (even though none is used here). Using // Arguments::new_const may allow the compiler to omit Formatter::pad from the // output binary, saving up to a few kilobytes. + // However, this optimization only works for `'static` strings: `new_const` also makes this + // message return `Some` from `Arguments::as_str`, which means it can become part of the panic + // payload without any allocation or copying. Shorter-lived strings would become invalid as + // stack frames get popped during unwinding, and couldn't be directly referenced from the + // payload. panic_fmt(fmt::Arguments::new_const(&[expr])); } @@ -160,14 +165,6 @@ pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! { panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), /* force_no_backtrace */ true); } -#[inline] -#[track_caller] -#[rustc_diagnostic_item = "panic_str"] -#[rustc_const_unstable(feature = "panic_internals", issue = "none")] -pub const fn panic_str(expr: &str) -> ! { - panic_display(&expr); -} - #[track_caller] #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)] #[cfg_attr(feature = "panic_immediate_abort", inline)] @@ -183,6 +180,16 @@ pub fn unreachable_display(x: &T) -> ! { panic_fmt(format_args!("internal error: entered unreachable code: {}", *x)); } +/// This exists solely for the 2015 edition `panic!` macro to trigger +/// a lint on `panic!(my_str_variable);`. +#[inline] +#[track_caller] +#[rustc_diagnostic_item = "panic_str_2015"] +#[rustc_const_unstable(feature = "panic_internals", issue = "none")] +pub const fn panic_str_2015(expr: &str) -> ! { + panic_display(&expr); +} + #[inline] #[track_caller] #[rustc_do_not_const_check] // hooked by const-eval From d101971ab10ebc95ad0664b704b730ce792d8fc5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Apr 2024 12:17:29 +0200 Subject: [PATCH 2/7] weak lang items are not allowed to be #[track_caller] --- compiler/rustc_passes/messages.ftl | 13 +++++++++-- compiler/rustc_passes/src/check_attr.rs | 23 +++++++++++++++++-- compiler/rustc_passes/src/errors.rs | 10 ++++++++ .../panic-handler-with-target-feature.rs | 2 +- .../panic-handler-with-target-feature.stderr | 4 ++-- .../panic-handler-with-track-caller.rs | 14 +++++++++++ .../panic-handler-with-track-caller.stderr | 11 +++++++++ 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 tests/ui/panic-handler/panic-handler-with-track-caller.rs create mode 100644 tests/ui/panic-handler/panic-handler-with-track-caller.stderr diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index 7fc523ffe0dea..8cd744925f541 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -394,9 +394,18 @@ passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export] passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments +passes_lang_item_fn = {$name -> + [panic_impl] `#[panic_handler]` + *[other] `{$name}` language item +} function + passes_lang_item_fn_with_target_feature = - `{$name}` language item function is not allowed to have `#[target_feature]` - .label = `{$name}` language item function is not allowed to have `#[target_feature]` + {passes_lang_item_fn} is not allowed to have `#[target_feature]` + .label = {passes_lang_item_fn} is not allowed to have `#[target_feature]` + +passes_lang_item_fn_with_track_caller = + {passes_lang_item_fn} is not allowed to have `#[track_caller]` + .label = {passes_lang_item_fn} is not allowed to have `#[target_feature]` passes_lang_item_on_incorrect_target = `{$name}` language item must be applied to a {$expected_target} diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 1254ae8cfc8db..ca41c5165b259 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -11,9 +11,9 @@ use rustc_data_structures::fx::FxHashMap; use rustc_errors::StashKey; use rustc_errors::{Applicability, DiagCtxt, IntoDiagArg, MultiSpan}; use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; -use rustc_hir as hir; use rustc_hir::def_id::LocalModDefId; use rustc_hir::intravisit::{self, Visitor}; +use rustc_hir::{self as hir}; use rustc_hir::{ self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID, }; @@ -519,7 +519,26 @@ impl<'tcx> CheckAttrVisitor<'tcx> { self.dcx().emit_err(errors::NakedTrackedCaller { attr_span }); false } - Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true, + Target::Fn => { + // `#[track_caller]` is not valid on weak lang items because they are called via + // `extern` declarations and `#[track_caller]` would alter their ABI. + if let Some((lang_item, _)) = hir::lang_items::extract(attrs) + && let Some(item) = hir::LangItem::from_name(lang_item) + && item.is_weak() + { + let sig = self.tcx.hir_node(hir_id).fn_sig().unwrap(); + + self.dcx().emit_err(errors::LangItemWithTrackCaller { + attr_span, + name: lang_item, + sig_span: sig.span, + }); + false + } else { + true + } + } + Target::Method(..) | Target::ForeignFn | Target::Closure => true, // FIXME(#80564): We permit struct fields, match arms and macro defs to have an // `#[track_caller]` attribute with just a lint, because we previously // erroneously allowed it and some crates used it accidentally, to be compatible diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 3f26ea4507d41..19b5e5495d6c6 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -819,6 +819,16 @@ pub struct MissingLangItem { pub name: Symbol, } +#[derive(Diagnostic)] +#[diag(passes_lang_item_fn_with_track_caller)] +pub struct LangItemWithTrackCaller { + #[primary_span] + pub attr_span: Span, + pub name: Symbol, + #[label] + pub sig_span: Span, +} + #[derive(Diagnostic)] #[diag(passes_lang_item_fn_with_target_feature)] pub struct LangItemWithTargetFeature { diff --git a/tests/ui/panic-handler/panic-handler-with-target-feature.rs b/tests/ui/panic-handler/panic-handler-with-target-feature.rs index 3dfdd2847bf48..8d5ea0703afa5 100644 --- a/tests/ui/panic-handler/panic-handler-with-target-feature.rs +++ b/tests/ui/panic-handler/panic-handler-with-target-feature.rs @@ -9,7 +9,7 @@ use core::panic::PanicInfo; #[panic_handler] #[target_feature(enable = "avx2")] -//~^ ERROR `panic_impl` language item function is not allowed to have `#[target_feature]` +//~^ ERROR `#[panic_handler]` function is not allowed to have `#[target_feature]` fn panic(info: &PanicInfo) -> ! { unimplemented!(); } diff --git a/tests/ui/panic-handler/panic-handler-with-target-feature.stderr b/tests/ui/panic-handler/panic-handler-with-target-feature.stderr index c38feab49c3b1..cb17da3a4efbe 100644 --- a/tests/ui/panic-handler/panic-handler-with-target-feature.stderr +++ b/tests/ui/panic-handler/panic-handler-with-target-feature.stderr @@ -1,11 +1,11 @@ -error: `panic_impl` language item function is not allowed to have `#[target_feature]` +error: `#[panic_handler]` function is not allowed to have `#[target_feature]` --> $DIR/panic-handler-with-target-feature.rs:11:1 | LL | #[target_feature(enable = "avx2")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn panic(info: &PanicInfo) -> ! { - | ------------------------------- `panic_impl` language item function is not allowed to have `#[target_feature]` + | ------------------------------- `#[panic_handler]` function is not allowed to have `#[target_feature]` error: aborting due to 1 previous error diff --git a/tests/ui/panic-handler/panic-handler-with-track-caller.rs b/tests/ui/panic-handler/panic-handler-with-track-caller.rs new file mode 100644 index 0000000000000..09c94139e1697 --- /dev/null +++ b/tests/ui/panic-handler/panic-handler-with-track-caller.rs @@ -0,0 +1,14 @@ +//@ compile-flags:-C panic=abort +//@ only-x86_64 + +#![no_std] +#![no_main] + +use core::panic::PanicInfo; + +#[panic_handler] +#[track_caller] +//~^ ERROR `#[panic_handler]` function is not allowed to have `#[track_caller]` +fn panic(info: &PanicInfo) -> ! { + unimplemented!(); +} diff --git a/tests/ui/panic-handler/panic-handler-with-track-caller.stderr b/tests/ui/panic-handler/panic-handler-with-track-caller.stderr new file mode 100644 index 0000000000000..9ed387fc8d1f4 --- /dev/null +++ b/tests/ui/panic-handler/panic-handler-with-track-caller.stderr @@ -0,0 +1,11 @@ +error: `#[panic_handler]` function is not allowed to have `#[track_caller]` + --> $DIR/panic-handler-with-track-caller.rs:10:1 + | +LL | #[track_caller] + | ^^^^^^^^^^^^^^^ +LL | +LL | fn panic(info: &PanicInfo) -> ! { + | ------------------------------- `#[panic_handler]` function is not allowed to have `#[target_feature]` + +error: aborting due to 1 previous error + From 8b35be741fd1e46f81a3614478279d718701783e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Apr 2024 12:30:45 +0200 Subject: [PATCH 3/7] consistency rename: language item -> lang item --- .../rustc_error_codes/src/error_codes/E0522.md | 2 +- compiler/rustc_feature/src/builtin_attrs.rs | 2 +- compiler/rustc_hir/src/lang_items.rs | 10 +++++----- .../rustc_hir_analysis/src/check/intrinsic.rs | 6 +++--- compiler/rustc_metadata/src/rmeta/decoder.rs | 2 +- compiler/rustc_middle/src/middle/lang_items.rs | 2 +- .../src/deduce_param_attrs.rs | 2 +- .../rustc_mir_transform/src/lower_slice_len.rs | 2 +- compiler/rustc_passes/messages.ftl | 12 ++++++------ compiler/rustc_passes/src/check_attr.rs | 2 +- compiler/rustc_passes/src/lang_items.rs | 2 +- compiler/rustc_passes/src/weak_lang_items.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 2 +- tests/ui/assoc-lang-items.stderr | 16 ++++++++-------- tests/ui/error-codes/E0522.rs | 2 +- tests/ui/error-codes/E0522.stderr | 4 ++-- tests/ui/error-codes/E0718.rs | 2 +- tests/ui/error-codes/E0718.stderr | 2 +- .../ui/feature-gates/feature-gate-lang-items.rs | 4 ++-- .../feature-gates/feature-gate-lang-items.stderr | 6 +++--- tests/ui/lang-items/issue-83471.rs | 6 +++--- tests/ui/lang-items/issue-83471.stderr | 6 +++--- tests/ui/lang-items/issue-87573.rs | 4 ++-- tests/ui/lang-items/issue-87573.stderr | 4 ++-- .../lang-items/lang-item-generic-requirements.rs | 12 ++++++------ .../lang-item-generic-requirements.stderr | 12 ++++++------ .../start_lang_item_with_target_feature.rs | 2 +- .../start_lang_item_with_target_feature.stderr | 4 ++-- .../panic-handler-wrong-location.rs | 2 +- .../panic-handler-wrong-location.stderr | 2 +- tests/ui/unknown-language-item.rs | 2 +- tests/ui/unknown-language-item.stderr | 4 ++-- 32 files changed, 72 insertions(+), 72 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0522.md b/compiler/rustc_error_codes/src/error_codes/E0522.md index 83272314a8708..b0dd5a775ac14 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0522.md +++ b/compiler/rustc_error_codes/src/error_codes/E0522.md @@ -6,7 +6,7 @@ Erroneous code example: #![feature(lang_items)] #[lang = "cookie"] -fn cookie() -> ! { // error: definition of an unknown language item: `cookie` +fn cookie() -> ! { // error: definition of an unknown lang item: `cookie` loop {} } ``` diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index 4f62323231a73..23b3f31b43704 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -798,7 +798,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ // ========================================================================== gated!( lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, EncodeCrossCrate::No, lang_items, - "language items are subject to change", + "lang items are subject to change", ), rustc_attr!( rustc_pass_by_value, Normal, template!(Word), ErrorFollowing, diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index 2a796ca5465c9..c205791b25a3b 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -1,4 +1,4 @@ -//! Defines language items. +//! Defines lang items. //! //! Language items are items that represent concepts intrinsic to the language //! itself. Examples are: @@ -16,7 +16,7 @@ use rustc_macros::HashStable_Generic; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; -/// All of the language items, defined or not. +/// All of the lang items, defined or not. /// Defined lang items can come from the current crate or its dependencies. #[derive(HashStable_Generic, Debug)] pub struct LanguageItems { @@ -57,7 +57,7 @@ macro_rules! language_item_table { ) => { enum_from_u32! { - /// A representation of all the valid language items in Rust. + /// A representation of all the valid lang items in Rust. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)] pub enum LangItem { $( @@ -165,7 +165,7 @@ language_item_table! { CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1); DispatchFromDyn, sym::dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1); - // language items relating to transmutability + // lang items relating to transmutability TransmuteOpts, sym::transmute_opts, transmute_opts, Target::Struct, GenericRequirement::Exact(0); TransmuteTrait, sym::transmute_trait, transmute_trait, Target::Trait, GenericRequirement::Exact(2); @@ -291,7 +291,7 @@ language_item_table! { OwnedBox, sym::owned_box, owned_box, Target::Struct, GenericRequirement::Minimum(1); GlobalAlloc, sym::global_alloc_ty, global_alloc_ty, Target::Struct, GenericRequirement::None; - // Experimental language item for Miri + // Experimental lang item for Miri PtrUnique, sym::ptr_unique, ptr_unique, Target::Struct, GenericRequirement::Exact(1); PhantomData, sym::phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1); diff --git a/compiler/rustc_hir_analysis/src/check/intrinsic.rs b/compiler/rustc_hir_analysis/src/check/intrinsic.rs index bd64621f07738..9ceac20be726d 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsic.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsic.rs @@ -534,7 +534,7 @@ pub fn check_intrinsic_type( sym::va_start | sym::va_end => match mk_va_list_ty(hir::Mutability::Mut) { Some((va_list_ref_ty, _)) => (0, 0, vec![va_list_ref_ty], Ty::new_unit(tcx)), - None => bug!("`va_list` language item needed for C-variadic intrinsics"), + None => bug!("`va_list` lang item needed for C-variadic intrinsics"), }, sym::va_copy => match mk_va_list_ty(hir::Mutability::Not) { @@ -542,12 +542,12 @@ pub fn check_intrinsic_type( let va_list_ptr_ty = Ty::new_mut_ptr(tcx, va_list_ty); (0, 0, vec![va_list_ptr_ty, va_list_ref_ty], Ty::new_unit(tcx)) } - None => bug!("`va_list` language item needed for C-variadic intrinsics"), + None => bug!("`va_list` lang item needed for C-variadic intrinsics"), }, sym::va_arg => match mk_va_list_ty(hir::Mutability::Mut) { Some((va_list_ref_ty, _)) => (1, 0, vec![va_list_ref_ty], param(0)), - None => bug!("`va_list` language item needed for C-variadic intrinsics"), + None => bug!("`va_list` lang item needed for C-variadic intrinsics"), }, sym::nontemporal_store => { diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 596da58b091f4..c4ab4eb2657d6 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1208,7 +1208,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { tcx.arena.alloc_from_iter(self.root.stability_implications.decode(self)) } - /// Iterates over the language items in the given crate. + /// Iterates over the lang items in the given crate. fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, LangItem)] { tcx.arena.alloc_from_iter( self.root diff --git a/compiler/rustc_middle/src/middle/lang_items.rs b/compiler/rustc_middle/src/middle/lang_items.rs index a4e193ba2c942..4fd1c1f4a1b7d 100644 --- a/compiler/rustc_middle/src/middle/lang_items.rs +++ b/compiler/rustc_middle/src/middle/lang_items.rs @@ -1,4 +1,4 @@ -//! Detecting language items. +//! Detecting lang items. //! //! Language items are items that represent concepts intrinsic to the language //! itself. Examples are: diff --git a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs b/compiler/rustc_mir_transform/src/deduce_param_attrs.rs index ca63f5550ae2e..370e930b74098 100644 --- a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs +++ b/compiler/rustc_mir_transform/src/deduce_param_attrs.rs @@ -160,7 +160,7 @@ pub fn deduced_param_attrs<'tcx>( return &[]; } - // If the Freeze language item isn't present, then don't bother. + // If the Freeze lang item isn't present, then don't bother. if tcx.lang_items().freeze_trait().is_none() { return &[]; } diff --git a/compiler/rustc_mir_transform/src/lower_slice_len.rs b/compiler/rustc_mir_transform/src/lower_slice_len.rs index 8137525a33243..2267a621a834a 100644 --- a/compiler/rustc_mir_transform/src/lower_slice_len.rs +++ b/compiler/rustc_mir_transform/src/lower_slice_len.rs @@ -21,7 +21,7 @@ impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls { pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { let language_items = tcx.lang_items(); let Some(slice_len_fn_item_def_id) = language_items.slice_len_fn() else { - // there is no language item to compare to :) + // there is no lang item to compare to :) return; }; diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index 8cd744925f541..a545c170297de 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -353,7 +353,7 @@ passes_incorrect_meta_item = expected a quoted string literal passes_incorrect_meta_item_suggestion = consider surrounding this with quotes passes_incorrect_target = - `{$name}` language item must be applied to a {$kind} with {$at_least -> + `{$name}` lang item must be applied to a {$kind} with {$at_least -> [true] at least {$num} *[false] {$num} } generic {$num -> @@ -396,7 +396,7 @@ passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can onl passes_lang_item_fn = {$name -> [panic_impl] `#[panic_handler]` - *[other] `{$name}` language item + *[other] `{$name}` lang item } function passes_lang_item_fn_with_target_feature = @@ -408,7 +408,7 @@ passes_lang_item_fn_with_track_caller = .label = {passes_lang_item_fn} is not allowed to have `#[target_feature]` passes_lang_item_on_incorrect_target = - `{$name}` language item must be applied to a {$expected_target} + `{$name}` lang item must be applied to a {$expected_target} .label = attribute should be applied to a {$expected_target}, not a {$actual_target} passes_layout_abi = @@ -464,7 +464,7 @@ passes_missing_const_stab_attr = {$descr} has missing const stability attribute passes_missing_lang_item = - language item required, but not found: `{$name}` + lang item required, but not found: `{$name}` .note = this can occur when a binary crate with `#![no_std]` is compiled for a target where `{$name}` is defined in the standard library .help = you may be able to compile for a target that doesn't need `{$name}`, specify a target with `--target` or in `.cargo/config` @@ -705,8 +705,8 @@ passes_unknown_feature = unknown feature `{$feature}` passes_unknown_lang_item = - definition of an unknown language item: `{$name}` - .label = definition of unknown language item `{$name}` + definition of an unknown lang item: `{$name}` + .label = definition of unknown lang item `{$name}` passes_unlabeled_cf_in_while_condition = `break` or `continue` with no label in the condition of a `while` loop diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index ca41c5165b259..a2388b3bd5227 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -621,7 +621,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { ) -> bool { match target { Target::Fn => { - // `#[target_feature]` is not allowed in language items. + // `#[target_feature]` is not allowed in lang items. if let Some((lang_item, _)) = hir::lang_items::extract(attrs) // Calling functions with `#[target_feature]` is // not unsafe on WASM, see #84988 diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs index d1368267224a7..c1da8928f3021 100644 --- a/compiler/rustc_passes/src/lang_items.rs +++ b/compiler/rustc_passes/src/lang_items.rs @@ -1,4 +1,4 @@ -//! Detecting language items. +//! Detecting lang items. //! //! Language items are items that represent concepts intrinsic to the language //! itself. Examples are: diff --git a/compiler/rustc_passes/src/weak_lang_items.rs b/compiler/rustc_passes/src/weak_lang_items.rs index 4eb0c6c275e61..90691ca17908b 100644 --- a/compiler/rustc_passes/src/weak_lang_items.rs +++ b/compiler/rustc_passes/src/weak_lang_items.rs @@ -14,7 +14,7 @@ use crate::errors::{ }; /// Checks the crate for usage of weak lang items, returning a vector of all the -/// language items required by this crate, but not defined yet. +/// lang items required by this crate, but not defined yet. pub fn check_crate(tcx: TyCtxt<'_>, items: &mut lang_items::LanguageItems, krate: &ast::Crate) { // These are never called by user code, they're generated by the compiler. // They will never implicitly be added to the `missing` array unless we do diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index b8221d9d7f9f0..b5b6d899cc58e 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1042,7 +1042,7 @@ pub struct Resolver<'a, 'tcx> { block_map: NodeMap>, /// A fake module that contains no definition and no prelude. Used so that /// some AST passes can generate identifiers that only resolve to local or - /// language items. + /// lang items. empty_module: Module<'a>, module_map: FxHashMap>, binding_parent_modules: FxHashMap, Module<'a>>, diff --git a/tests/ui/assoc-lang-items.stderr b/tests/ui/assoc-lang-items.stderr index 040792fb1cd5f..59aec8e3fdc6e 100644 --- a/tests/ui/assoc-lang-items.stderr +++ b/tests/ui/assoc-lang-items.stderr @@ -1,26 +1,26 @@ -error[E0522]: definition of an unknown language item: `dummy_lang_item_1` +error[E0522]: definition of an unknown lang item: `dummy_lang_item_1` --> $DIR/assoc-lang-items.rs:4:5 | LL | #[lang = "dummy_lang_item_1"] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_1` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_1` -error[E0522]: definition of an unknown language item: `dummy_lang_item_2` +error[E0522]: definition of an unknown lang item: `dummy_lang_item_2` --> $DIR/assoc-lang-items.rs:7:5 | LL | #[lang = "dummy_lang_item_2"] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_2` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_2` -error[E0522]: definition of an unknown language item: `dummy_lang_item_3` +error[E0522]: definition of an unknown lang item: `dummy_lang_item_3` --> $DIR/assoc-lang-items.rs:10:5 | LL | #[lang = "dummy_lang_item_3"] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_3` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_3` -error[E0522]: definition of an unknown language item: `dummy_lang_item_4` +error[E0522]: definition of an unknown lang item: `dummy_lang_item_4` --> $DIR/assoc-lang-items.rs:17:5 | LL | #[lang = "dummy_lang_item_4"] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_4` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_4` error: aborting due to 4 previous errors diff --git a/tests/ui/error-codes/E0522.rs b/tests/ui/error-codes/E0522.rs index 1414e82408cef..ec9e6d257743e 100644 --- a/tests/ui/error-codes/E0522.rs +++ b/tests/ui/error-codes/E0522.rs @@ -2,7 +2,7 @@ #[lang = "cookie"] fn cookie() -> ! { -//~^^ ERROR definition of an unknown language item: `cookie` [E0522] +//~^^ ERROR definition of an unknown lang item: `cookie` [E0522] loop {} } diff --git a/tests/ui/error-codes/E0522.stderr b/tests/ui/error-codes/E0522.stderr index 66359cbacc846..82bbf07959405 100644 --- a/tests/ui/error-codes/E0522.stderr +++ b/tests/ui/error-codes/E0522.stderr @@ -1,8 +1,8 @@ -error[E0522]: definition of an unknown language item: `cookie` +error[E0522]: definition of an unknown lang item: `cookie` --> $DIR/E0522.rs:3:1 | LL | #[lang = "cookie"] - | ^^^^^^^^^^^^^^^^^^ definition of unknown language item `cookie` + | ^^^^^^^^^^^^^^^^^^ definition of unknown lang item `cookie` error: aborting due to 1 previous error diff --git a/tests/ui/error-codes/E0718.rs b/tests/ui/error-codes/E0718.rs index 909cae0ba25a2..358bc348ec76d 100644 --- a/tests/ui/error-codes/E0718.rs +++ b/tests/ui/error-codes/E0718.rs @@ -1,7 +1,7 @@ #![feature(lang_items)] // Box is expected to be a struct, so this will error. -#[lang = "owned_box"] //~ ERROR language item must be applied to a struct +#[lang = "owned_box"] //~ ERROR lang item must be applied to a struct static X: u32 = 42; fn main() {} diff --git a/tests/ui/error-codes/E0718.stderr b/tests/ui/error-codes/E0718.stderr index 9a3db136d23c0..ec7462765f81f 100644 --- a/tests/ui/error-codes/E0718.stderr +++ b/tests/ui/error-codes/E0718.stderr @@ -1,4 +1,4 @@ -error[E0718]: `owned_box` language item must be applied to a struct +error[E0718]: `owned_box` lang item must be applied to a struct --> $DIR/E0718.rs:4:1 | LL | #[lang = "owned_box"] diff --git a/tests/ui/feature-gates/feature-gate-lang-items.rs b/tests/ui/feature-gates/feature-gate-lang-items.rs index 93262f2171bfe..d6fd5472a0c1d 100644 --- a/tests/ui/feature-gates/feature-gate-lang-items.rs +++ b/tests/ui/feature-gates/feature-gate-lang-items.rs @@ -1,5 +1,5 @@ -#[lang = "foo"] //~ ERROR language items are subject to change - //~^ ERROR definition of an unknown language item: `foo` +#[lang = "foo"] //~ ERROR lang items are subject to change + //~^ ERROR definition of an unknown lang item: `foo` trait Foo {} fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-lang-items.stderr b/tests/ui/feature-gates/feature-gate-lang-items.stderr index 54787e03784e4..c5caffbdc94dd 100644 --- a/tests/ui/feature-gates/feature-gate-lang-items.stderr +++ b/tests/ui/feature-gates/feature-gate-lang-items.stderr @@ -1,4 +1,4 @@ -error[E0658]: language items are subject to change +error[E0658]: lang items are subject to change --> $DIR/feature-gate-lang-items.rs:1:1 | LL | #[lang = "foo"] @@ -7,11 +7,11 @@ LL | #[lang = "foo"] = help: add `#![feature(lang_items)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error[E0522]: definition of an unknown language item: `foo` +error[E0522]: definition of an unknown lang item: `foo` --> $DIR/feature-gate-lang-items.rs:1:1 | LL | #[lang = "foo"] - | ^^^^^^^^^^^^^^^ definition of unknown language item `foo` + | ^^^^^^^^^^^^^^^ definition of unknown lang item `foo` error: aborting due to 2 previous errors diff --git a/tests/ui/lang-items/issue-83471.rs b/tests/ui/lang-items/issue-83471.rs index b32aa03415152..6be345ac50765 100644 --- a/tests/ui/lang-items/issue-83471.rs +++ b/tests/ui/lang-items/issue-83471.rs @@ -5,12 +5,12 @@ #![no_core] #[lang = "sized"] -//~^ ERROR: language items are subject to change [E0658] +//~^ ERROR: lang items are subject to change [E0658] trait Sized {} #[lang = "fn"] -//~^ ERROR: language items are subject to change [E0658] -//~| ERROR: `fn` language item must be applied to a trait with 1 generic argument +//~^ ERROR: lang items are subject to change [E0658] +//~| ERROR: `fn` lang item must be applied to a trait with 1 generic argument trait Fn { fn call(export_name); //~^ ERROR: expected type diff --git a/tests/ui/lang-items/issue-83471.stderr b/tests/ui/lang-items/issue-83471.stderr index 1f22d966dd7d7..244b2efeaf1e0 100644 --- a/tests/ui/lang-items/issue-83471.stderr +++ b/tests/ui/lang-items/issue-83471.stderr @@ -4,7 +4,7 @@ error[E0573]: expected type, found built-in attribute `export_name` LL | fn call(export_name); | ^^^^^^^^^^^ not a type -error[E0658]: language items are subject to change +error[E0658]: lang items are subject to change --> $DIR/issue-83471.rs:7:1 | LL | #[lang = "sized"] @@ -13,7 +13,7 @@ LL | #[lang = "sized"] = help: add `#![feature(lang_items)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error[E0658]: language items are subject to change +error[E0658]: lang items are subject to change --> $DIR/issue-83471.rs:11:1 | LL | #[lang = "fn"] @@ -32,7 +32,7 @@ LL | fn call(export_name); = note: for more information, see issue #41686 = note: `#[warn(anonymous_parameters)]` on by default -error[E0718]: `fn` language item must be applied to a trait with 1 generic argument +error[E0718]: `fn` lang item must be applied to a trait with 1 generic argument --> $DIR/issue-83471.rs:11:1 | LL | #[lang = "fn"] diff --git a/tests/ui/lang-items/issue-87573.rs b/tests/ui/lang-items/issue-87573.rs index aeb0c245a72ec..7b805e8b0cdb6 100644 --- a/tests/ui/lang-items/issue-87573.rs +++ b/tests/ui/lang-items/issue-87573.rs @@ -18,11 +18,11 @@ trait Sync {} impl Sync for bool {} #[lang = "drop_in_place"] -//~^ ERROR: `drop_in_place` language item must be applied to a function with at least 1 generic argument +//~^ ERROR: `drop_in_place` lang item must be applied to a function with at least 1 generic argument fn drop_fn() { while false {} } #[lang = "start"] -//~^ ERROR: `start` language item must be applied to a function with 1 generic argument +//~^ ERROR: `start` lang item must be applied to a function with 1 generic argument fn start(){} diff --git a/tests/ui/lang-items/issue-87573.stderr b/tests/ui/lang-items/issue-87573.stderr index 25560cfa0e6c7..7085bb8c339d4 100644 --- a/tests/ui/lang-items/issue-87573.stderr +++ b/tests/ui/lang-items/issue-87573.stderr @@ -1,4 +1,4 @@ -error[E0718]: `drop_in_place` language item must be applied to a function with at least 1 generic argument +error[E0718]: `drop_in_place` lang item must be applied to a function with at least 1 generic argument --> $DIR/issue-87573.rs:20:1 | LL | #[lang = "drop_in_place"] @@ -7,7 +7,7 @@ LL | LL | fn drop_fn() { | - this function has 0 generic arguments -error[E0718]: `start` language item must be applied to a function with 1 generic argument +error[E0718]: `start` lang item must be applied to a function with 1 generic argument --> $DIR/issue-87573.rs:26:1 | LL | #[lang = "start"] diff --git a/tests/ui/lang-items/lang-item-generic-requirements.rs b/tests/ui/lang-items/lang-item-generic-requirements.rs index 697790023d6c9..0f982df61e8e7 100644 --- a/tests/ui/lang-items/lang-item-generic-requirements.rs +++ b/tests/ui/lang-items/lang-item-generic-requirements.rs @@ -9,30 +9,30 @@ trait MySized {} #[lang = "add"] trait MyAdd<'a, T> {} -//~^^ ERROR: `add` language item must be applied to a trait with 1 generic argument [E0718] +//~^^ ERROR: `add` lang item must be applied to a trait with 1 generic argument [E0718] #[lang = "drop_in_place"] -//~^ ERROR `drop_in_place` language item must be applied to a function with at least 1 generic +//~^ ERROR `drop_in_place` lang item must be applied to a function with at least 1 generic fn my_ptr_drop() {} #[lang = "index"] trait MyIndex<'a, T> {} -//~^^ ERROR: `index` language item must be applied to a trait with 1 generic argument [E0718] +//~^^ ERROR: `index` lang item must be applied to a trait with 1 generic argument [E0718] #[lang = "phantom_data"] -//~^ ERROR `phantom_data` language item must be applied to a struct with 1 generic argument +//~^ ERROR `phantom_data` lang item must be applied to a struct with 1 generic argument struct MyPhantomData; //~^ ERROR `T` is never used //~| ERROR `U` is never used #[lang = "owned_box"] -//~^ ERROR `owned_box` language item must be applied to a struct with at least 1 generic argument +//~^ ERROR `owned_box` lang item must be applied to a struct with at least 1 generic argument struct Foo; // When the `start` lang item is missing generics very odd things can happen, especially when // it comes to cross-crate monomorphization #[lang = "start"] -//~^ ERROR `start` language item must be applied to a function with 1 generic argument [E0718] +//~^ ERROR `start` lang item must be applied to a function with 1 generic argument [E0718] fn start(_: *const u8, _: isize, _: *const *const u8) -> isize { 0 } diff --git a/tests/ui/lang-items/lang-item-generic-requirements.stderr b/tests/ui/lang-items/lang-item-generic-requirements.stderr index 30abdf84046ea..3de67d6594035 100644 --- a/tests/ui/lang-items/lang-item-generic-requirements.stderr +++ b/tests/ui/lang-items/lang-item-generic-requirements.stderr @@ -1,4 +1,4 @@ -error[E0718]: `add` language item must be applied to a trait with 1 generic argument +error[E0718]: `add` lang item must be applied to a trait with 1 generic argument --> $DIR/lang-item-generic-requirements.rs:10:1 | LL | #[lang = "add"] @@ -6,7 +6,7 @@ LL | #[lang = "add"] LL | trait MyAdd<'a, T> {} | ------- this trait has 2 generic arguments -error[E0718]: `drop_in_place` language item must be applied to a function with at least 1 generic argument +error[E0718]: `drop_in_place` lang item must be applied to a function with at least 1 generic argument --> $DIR/lang-item-generic-requirements.rs:14:1 | LL | #[lang = "drop_in_place"] @@ -15,7 +15,7 @@ LL | LL | fn my_ptr_drop() {} | - this function has 0 generic arguments -error[E0718]: `index` language item must be applied to a trait with 1 generic argument +error[E0718]: `index` lang item must be applied to a trait with 1 generic argument --> $DIR/lang-item-generic-requirements.rs:18:1 | LL | #[lang = "index"] @@ -23,7 +23,7 @@ LL | #[lang = "index"] LL | trait MyIndex<'a, T> {} | ------- this trait has 2 generic arguments -error[E0718]: `phantom_data` language item must be applied to a struct with 1 generic argument +error[E0718]: `phantom_data` lang item must be applied to a struct with 1 generic argument --> $DIR/lang-item-generic-requirements.rs:22:1 | LL | #[lang = "phantom_data"] @@ -32,7 +32,7 @@ LL | LL | struct MyPhantomData; | ------ this struct has 2 generic arguments -error[E0718]: `owned_box` language item must be applied to a struct with at least 1 generic argument +error[E0718]: `owned_box` lang item must be applied to a struct with at least 1 generic argument --> $DIR/lang-item-generic-requirements.rs:28:1 | LL | #[lang = "owned_box"] @@ -41,7 +41,7 @@ LL | LL | struct Foo; | - this struct has 0 generic arguments -error[E0718]: `start` language item must be applied to a function with 1 generic argument +error[E0718]: `start` lang item must be applied to a function with 1 generic argument --> $DIR/lang-item-generic-requirements.rs:34:1 | LL | #[lang = "start"] diff --git a/tests/ui/lang-items/start_lang_item_with_target_feature.rs b/tests/ui/lang-items/start_lang_item_with_target_feature.rs index 4717304c5c6b8..eb712ba409259 100644 --- a/tests/ui/lang-items/start_lang_item_with_target_feature.rs +++ b/tests/ui/lang-items/start_lang_item_with_target_feature.rs @@ -11,7 +11,7 @@ pub trait Sized {} #[lang = "start"] #[target_feature(enable = "avx2")] -//~^ ERROR `start` language item function is not allowed to have `#[target_feature]` +//~^ ERROR `start` lang item function is not allowed to have `#[target_feature]` fn start(_main: fn() -> T, _argc: isize, _argv: *const *const u8, _sigpipe: u8) -> isize { 0 } diff --git a/tests/ui/lang-items/start_lang_item_with_target_feature.stderr b/tests/ui/lang-items/start_lang_item_with_target_feature.stderr index bb0583dfde056..6214e3f8bc79a 100644 --- a/tests/ui/lang-items/start_lang_item_with_target_feature.stderr +++ b/tests/ui/lang-items/start_lang_item_with_target_feature.stderr @@ -1,11 +1,11 @@ -error: `start` language item function is not allowed to have `#[target_feature]` +error: `start` lang item function is not allowed to have `#[target_feature]` --> $DIR/start_lang_item_with_target_feature.rs:13:1 | LL | #[target_feature(enable = "avx2")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn start(_main: fn() -> T, _argc: isize, _argv: *const *const u8, _sigpipe: u8) -> isize { - | ------------------------------------------------------------------------------------------- `start` language item function is not allowed to have `#[target_feature]` + | ------------------------------------------------------------------------------------------- `start` lang item function is not allowed to have `#[target_feature]` error: aborting due to 1 previous error diff --git a/tests/ui/panic-handler/panic-handler-wrong-location.rs b/tests/ui/panic-handler/panic-handler-wrong-location.rs index fc3ef401e3db2..49685ee45926e 100644 --- a/tests/ui/panic-handler/panic-handler-wrong-location.rs +++ b/tests/ui/panic-handler/panic-handler-wrong-location.rs @@ -3,6 +3,6 @@ #![no_std] #![no_main] -#[panic_handler] //~ ERROR `panic_impl` language item must be applied to a function +#[panic_handler] //~ ERROR `panic_impl` lang item must be applied to a function #[no_mangle] static X: u32 = 42; diff --git a/tests/ui/panic-handler/panic-handler-wrong-location.stderr b/tests/ui/panic-handler/panic-handler-wrong-location.stderr index ae3ed5ab12b4c..66ee91aa4c199 100644 --- a/tests/ui/panic-handler/panic-handler-wrong-location.stderr +++ b/tests/ui/panic-handler/panic-handler-wrong-location.stderr @@ -1,4 +1,4 @@ -error[E0718]: `panic_impl` language item must be applied to a function +error[E0718]: `panic_impl` lang item must be applied to a function --> $DIR/panic-handler-wrong-location.rs:6:1 | LL | #[panic_handler] diff --git a/tests/ui/unknown-language-item.rs b/tests/ui/unknown-language-item.rs index 20ffef71497c0..ce206d20358a1 100644 --- a/tests/ui/unknown-language-item.rs +++ b/tests/ui/unknown-language-item.rs @@ -3,7 +3,7 @@ #[lang = "foo"] fn bar() -> ! { -//~^^ ERROR definition of an unknown language item: `foo` +//~^^ ERROR definition of an unknown lang item: `foo` loop {} } diff --git a/tests/ui/unknown-language-item.stderr b/tests/ui/unknown-language-item.stderr index 1e0256867c562..832f134241844 100644 --- a/tests/ui/unknown-language-item.stderr +++ b/tests/ui/unknown-language-item.stderr @@ -1,8 +1,8 @@ -error[E0522]: definition of an unknown language item: `foo` +error[E0522]: definition of an unknown lang item: `foo` --> $DIR/unknown-language-item.rs:4:1 | LL | #[lang = "foo"] - | ^^^^^^^^^^^^^^^ definition of unknown language item `foo` + | ^^^^^^^^^^^^^^^ definition of unknown lang item `foo` error: aborting due to 1 previous error From 5af861cf7b4a87857ca6aa9dab2ad277ea8c059b Mon Sep 17 00:00:00 2001 From: Dominik Stolz Date: Wed, 17 Apr 2024 23:40:03 +0200 Subject: [PATCH 4/7] Disallow ambiguous attributes on expressions --- compiler/rustc_parse/messages.ftl | 2 + compiler/rustc_parse/src/errors.rs | 9 ++++ compiler/rustc_parse/src/parser/expr.rs | 32 +++++++------ .../clippy/tests/ui/cfg_attr_rustfmt.fixed | 6 +-- src/tools/clippy/tests/ui/cfg_attr_rustfmt.rs | 6 +-- src/tools/rustfmt/tests/source/attrib.rs | 4 +- src/tools/rustfmt/tests/target/attrib.rs | 4 +- tests/pretty/ast-stmt-expr-attr.rs | 18 ++++---- tests/pretty/stmt_expr_attributes.rs | 12 ++--- .../unused/unused-doc-comments-edge-cases.rs | 4 +- .../unused-doc-comments-edge-cases.stderr | 14 +++--- .../attribute/attr-binary-expr-ambigous.fixed | 15 ++++++ .../attribute/attr-binary-expr-ambigous.rs | 15 ++++++ .../attr-binary-expr-ambigous.stderr | 46 +++++++++++++++++++ tests/ui/proc-macro/issue-81555.rs | 3 +- 15 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 tests/ui/parser/attribute/attr-binary-expr-ambigous.fixed create mode 100644 tests/ui/parser/attribute/attr-binary-expr-ambigous.rs create mode 100644 tests/ui/parser/attribute/attr-binary-expr-ambigous.stderr diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index e2436759c22c5..9dcf5a5761023 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -620,6 +620,8 @@ parse_or_pattern_not_allowed_in_let_binding = top-level or-patterns are not allo parse_out_of_range_hex_escape = out of range hex escape .label = must be a character in the range [\x00-\x7f] +parse_outer_attr_ambiguous = ambiguous outer attributes + parse_outer_attr_explanation = outer attributes, like `#[test]`, annotate the item following them parse_outer_attribute_not_allowed_on_if_else = outer attributes are not allowed on `if` and `else` branches diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index eae2d904c35e1..d8cac23f2f12b 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -495,6 +495,15 @@ pub(crate) struct OuterAttributeNotAllowedOnIfElse { pub attributes: Span, } +#[derive(Diagnostic)] +#[diag(parse_outer_attr_ambiguous)] +pub(crate) struct AmbiguousOuterAttributes { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub sugg: WrapInParentheses, +} + #[derive(Diagnostic)] #[diag(parse_missing_in_in_for_loop)] pub(crate) struct MissingInInForLoop { diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 00947a4c5853f..1625173970a46 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -327,7 +327,9 @@ impl<'a> Parser<'a> { this.parse_expr_assoc_with(prec + prec_adjustment, LhsExpr::NotYetParsed) })?; - let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span); + self.error_ambiguous_outer_attrs(&lhs, lhs_span, rhs.span); + let span = lhs_span.to(rhs.span); + lhs = match op { AssocOp::Add | AssocOp::Subtract @@ -426,6 +428,18 @@ impl<'a> Parser<'a> { }); } + fn error_ambiguous_outer_attrs(&self, lhs: &P, lhs_span: Span, rhs_span: Span) { + if let Some(attr) = lhs.attrs.iter().find(|a| a.style == AttrStyle::Outer) { + self.dcx().emit_err(errors::AmbiguousOuterAttributes { + span: attr.span.to(rhs_span), + sugg: errors::WrapInParentheses::Expression { + left: attr.span.shrink_to_lo(), + right: lhs_span.shrink_to_hi(), + }, + }); + } + } + /// Possibly translate the current token to an associative operator. /// The method does not advance the current token. /// @@ -506,7 +520,8 @@ impl<'a> Parser<'a> { None }; let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span); - let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span); + self.error_ambiguous_outer_attrs(&lhs, lhs.span, rhs_span); + let span = lhs.span.to(rhs_span); let limits = if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed }; let range = self.mk_range(Some(lhs), rhs, limits); @@ -722,7 +737,8 @@ impl<'a> Parser<'a> { expr_kind: fn(P, P) -> ExprKind, ) -> PResult<'a, P> { let mk_expr = |this: &mut Self, lhs: P, rhs: P| { - this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, rhs.span), expr_kind(lhs, rhs)) + this.error_ambiguous_outer_attrs(&lhs, lhs_span, rhs.span); + this.mk_expr(lhs_span.to(rhs.span), expr_kind(lhs, rhs)) }; // Save the state of the parser before parsing type normally, in case there is a @@ -3807,16 +3823,6 @@ impl<'a> Parser<'a> { self.mk_expr(span, ExprKind::Err(guar)) } - /// Create expression span ensuring the span of the parent node - /// is larger than the span of lhs and rhs, including the attributes. - fn mk_expr_sp(&self, lhs: &P, lhs_span: Span, rhs_span: Span) -> Span { - lhs.attrs - .iter() - .find(|a| a.style == AttrStyle::Outer) - .map_or(lhs_span, |a| a.span) - .to(rhs_span) - } - fn collect_tokens_for_expr( &mut self, attrs: AttrWrapper, diff --git a/src/tools/clippy/tests/ui/cfg_attr_rustfmt.fixed b/src/tools/clippy/tests/ui/cfg_attr_rustfmt.fixed index 05d5b3d10eaf8..84dac431169ab 100644 --- a/src/tools/clippy/tests/ui/cfg_attr_rustfmt.fixed +++ b/src/tools/clippy/tests/ui/cfg_attr_rustfmt.fixed @@ -16,7 +16,7 @@ fn foo( fn skip_on_statements() { #[rustfmt::skip] - 5+3; + { 5+3; } } #[rustfmt::skip] @@ -33,11 +33,11 @@ mod foo { #[clippy::msrv = "1.29"] fn msrv_1_29() { #[cfg_attr(rustfmt, rustfmt::skip)] - 1+29; + { 1+29; } } #[clippy::msrv = "1.30"] fn msrv_1_30() { #[rustfmt::skip] - 1+30; + { 1+30; } } diff --git a/src/tools/clippy/tests/ui/cfg_attr_rustfmt.rs b/src/tools/clippy/tests/ui/cfg_attr_rustfmt.rs index bc29e20210e8a..4ab5c70e13b5b 100644 --- a/src/tools/clippy/tests/ui/cfg_attr_rustfmt.rs +++ b/src/tools/clippy/tests/ui/cfg_attr_rustfmt.rs @@ -16,7 +16,7 @@ fn foo( fn skip_on_statements() { #[cfg_attr(rustfmt, rustfmt::skip)] - 5+3; + { 5+3; } } #[cfg_attr(rustfmt, rustfmt_skip)] @@ -33,11 +33,11 @@ mod foo { #[clippy::msrv = "1.29"] fn msrv_1_29() { #[cfg_attr(rustfmt, rustfmt::skip)] - 1+29; + { 1+29; } } #[clippy::msrv = "1.30"] fn msrv_1_30() { #[cfg_attr(rustfmt, rustfmt::skip)] - 1+30; + { 1+30; } } diff --git a/src/tools/rustfmt/tests/source/attrib.rs b/src/tools/rustfmt/tests/source/attrib.rs index d45fba5522436..fc13cd02b03ef 100644 --- a/src/tools/rustfmt/tests/source/attrib.rs +++ b/src/tools/rustfmt/tests/source/attrib.rs @@ -214,8 +214,8 @@ type Os = NoSource; // #3313 fn stmt_expr_attributes() { let foo ; - #[must_use] - foo = false ; + (#[must_use] + foo) = false ; } // #3509 diff --git a/src/tools/rustfmt/tests/target/attrib.rs b/src/tools/rustfmt/tests/target/attrib.rs index 7e61f68d76a14..7b3309676de2f 100644 --- a/src/tools/rustfmt/tests/target/attrib.rs +++ b/src/tools/rustfmt/tests/target/attrib.rs @@ -248,8 +248,8 @@ type Os = NoSource; // #3313 fn stmt_expr_attributes() { let foo; - #[must_use] - foo = false; + (#[must_use] + foo) = false; } // #3509 diff --git a/tests/pretty/ast-stmt-expr-attr.rs b/tests/pretty/ast-stmt-expr-attr.rs index fd7272a1b1fd3..899f7173f704e 100644 --- a/tests/pretty/ast-stmt-expr-attr.rs +++ b/tests/pretty/ast-stmt-expr-attr.rs @@ -13,17 +13,17 @@ fn syntax() { let _ = #[attr] (); let _ = #[attr] (#[attr] 0,); let _ = #[attr] (#[attr] 0, 0); - let _ = #[attr] 0 + #[attr] 0; - let _ = #[attr] 0 / #[attr] 0; - let _ = #[attr] 0 & #[attr] 0; - let _ = #[attr] 0 % #[attr] 0; + let _ = (#[attr] 0) + #[attr] 0; + let _ = (#[attr] 0) / #[attr] 0; + let _ = (#[attr] 0) & #[attr] 0; + let _ = (#[attr] 0) % #[attr] 0; let _ = #[attr] (0 + 0); let _ = #[attr] !0; let _ = #[attr] -0; let _ = #[attr] false; let _ = #[attr] 0; let _ = #[attr] 'c'; - let _ = #[attr] x as Y; + let _ = (#[attr] x) as Y; let _ = #[attr] (x as Y); let _ = #[attr] while true { @@ -88,9 +88,9 @@ fn syntax() { let _ = (); foo }; - let _ = #[attr] x = y; + let _ = (#[attr] x) = y; let _ = #[attr] (x = y); - let _ = #[attr] x += y; + let _ = (#[attr] x) += y; let _ = #[attr] (x += y); let _ = #[attr] foo.bar; let _ = (#[attr] foo).bar; @@ -98,8 +98,8 @@ fn syntax() { let _ = (#[attr] foo).0; let _ = #[attr] foo[bar]; let _ = (#[attr] foo)[bar]; - let _ = #[attr] 0..#[attr] 0; - let _ = #[attr] 0..; + let _ = (#[attr] 0)..#[attr] 0; + let _ = (#[attr] 0)..; let _ = #[attr] (0..0); let _ = #[attr] (0..); let _ = #[attr] (..0); diff --git a/tests/pretty/stmt_expr_attributes.rs b/tests/pretty/stmt_expr_attributes.rs index 98ad98b863ac6..5076adf5aa47b 100644 --- a/tests/pretty/stmt_expr_attributes.rs +++ b/tests/pretty/stmt_expr_attributes.rs @@ -148,13 +148,13 @@ fn _11() { let _ = #[rustc_dummy] (0); let _ = #[rustc_dummy] (0,); let _ = #[rustc_dummy] (0, 0); - let _ = #[rustc_dummy] 0 + #[rustc_dummy] 0; + let _ = (#[rustc_dummy] 0) + #[rustc_dummy] 0; let _ = #[rustc_dummy] !0; let _ = #[rustc_dummy] -0i32; let _ = #[rustc_dummy] false; let _ = #[rustc_dummy] 'c'; let _ = #[rustc_dummy] 0; - let _ = #[rustc_dummy] 0 as usize; + let _ = (#[rustc_dummy] 0) as usize; let _ = #[rustc_dummy] while false { #![rustc_dummy] @@ -214,8 +214,8 @@ fn _11() { #![rustc_dummy] }; let mut x = 0; - let _ = #[rustc_dummy] x = 15; - let _ = #[rustc_dummy] x += 15; + let _ = (#[rustc_dummy] x) = 15; + let _ = (#[rustc_dummy] x) += 15; let s = Foo { data: () }; let _ = #[rustc_dummy] s.data; let _ = (#[rustc_dummy] s).data; @@ -225,8 +225,8 @@ fn _11() { let v = vec!(0); let _ = #[rustc_dummy] v[0]; let _ = (#[rustc_dummy] v)[0]; - let _ = #[rustc_dummy] 0..#[rustc_dummy] 0; - let _ = #[rustc_dummy] 0..; + let _ = (#[rustc_dummy] 0)..#[rustc_dummy] 0; + let _ = (#[rustc_dummy] 0)..; let _ = #[rustc_dummy] (0..0); let _ = #[rustc_dummy] (0..); let _ = #[rustc_dummy] (..0); diff --git a/tests/ui/lint/unused/unused-doc-comments-edge-cases.rs b/tests/ui/lint/unused/unused-doc-comments-edge-cases.rs index ba32fb566e83b..0f9eac93930bc 100644 --- a/tests/ui/lint/unused/unused-doc-comments-edge-cases.rs +++ b/tests/ui/lint/unused/unused-doc-comments-edge-cases.rs @@ -20,10 +20,10 @@ fn doc_comment_between_if_else(num: u8) -> bool { } fn doc_comment_on_expr(num: u8) -> bool { - /// useless doc comment + (/// useless doc comment //~^ ERROR: attributes on expressions are experimental //~| ERROR: unused doc comment - num == 3 + num) == 3 } fn doc_comment_on_expr_field() -> bool { diff --git a/tests/ui/lint/unused/unused-doc-comments-edge-cases.stderr b/tests/ui/lint/unused/unused-doc-comments-edge-cases.stderr index 55e4834e6707a..add85b2f5e043 100644 --- a/tests/ui/lint/unused/unused-doc-comments-edge-cases.stderr +++ b/tests/ui/lint/unused/unused-doc-comments-edge-cases.stderr @@ -5,10 +5,10 @@ LL | else { | ^^^^ expected expression error[E0658]: attributes on expressions are experimental - --> $DIR/unused-doc-comments-edge-cases.rs:23:5 + --> $DIR/unused-doc-comments-edge-cases.rs:23:6 | -LL | /// useless doc comment - | ^^^^^^^^^^^^^^^^^^^^^^^ +LL | (/// useless doc comment + | ^^^^^^^^^^^^^^^^^^^^^^^ | = note: see issue #15701 for more information = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable @@ -32,12 +32,12 @@ LL | #![deny(unused_doc_comments)] | ^^^^^^^^^^^^^^^^^^^ error: unused doc comment - --> $DIR/unused-doc-comments-edge-cases.rs:23:5 + --> $DIR/unused-doc-comments-edge-cases.rs:23:6 | -LL | /// useless doc comment - | ^^^^^^^^^^^^^^^^^^^^^^^ +LL | (/// useless doc comment + | ^^^^^^^^^^^^^^^^^^^^^^^ ... -LL | num == 3 +LL | num) == 3 | --- rustdoc does not generate documentation for expressions | = help: use `//` for a plain comment diff --git a/tests/ui/parser/attribute/attr-binary-expr-ambigous.fixed b/tests/ui/parser/attribute/attr-binary-expr-ambigous.fixed new file mode 100644 index 0000000000000..aae71ede77132 --- /dev/null +++ b/tests/ui/parser/attribute/attr-binary-expr-ambigous.fixed @@ -0,0 +1,15 @@ +//@ run-rustfix +#![feature(stmt_expr_attributes)] +#![allow(unused_assignments, unused_attributes)] + +fn main() { + let mut x = (#[deprecated] 1) + 2; //~ ERROR ambiguous + + (#[deprecated] x) = 4; //~ ERROR ambiguous + + x = (#[deprecated] 5) as i32; //~ ERROR ambiguous + + let _r = (#[deprecated] 1)..6; //~ ERROR ambiguous + + println!("{}", x); +} diff --git a/tests/ui/parser/attribute/attr-binary-expr-ambigous.rs b/tests/ui/parser/attribute/attr-binary-expr-ambigous.rs new file mode 100644 index 0000000000000..613e01d743bdd --- /dev/null +++ b/tests/ui/parser/attribute/attr-binary-expr-ambigous.rs @@ -0,0 +1,15 @@ +//@ run-rustfix +#![feature(stmt_expr_attributes)] +#![allow(unused_assignments, unused_attributes)] + +fn main() { + let mut x = #[deprecated] 1 + 2; //~ ERROR ambiguous + + #[deprecated] x = 4; //~ ERROR ambiguous + + x = #[deprecated] 5 as i32; //~ ERROR ambiguous + + let _r = #[deprecated] 1..6; //~ ERROR ambiguous + + println!("{}", x); +} diff --git a/tests/ui/parser/attribute/attr-binary-expr-ambigous.stderr b/tests/ui/parser/attribute/attr-binary-expr-ambigous.stderr new file mode 100644 index 0000000000000..0430570fd49d9 --- /dev/null +++ b/tests/ui/parser/attribute/attr-binary-expr-ambigous.stderr @@ -0,0 +1,46 @@ +error: ambiguous outer attributes + --> $DIR/attr-binary-expr-ambigous.rs:6:17 + | +LL | let mut x = #[deprecated] 1 + 2; + | ^^^^^^^^^^^^^^^^^^^ + | +help: wrap the expression in parentheses + | +LL | let mut x = (#[deprecated] 1) + 2; + | + + + +error: ambiguous outer attributes + --> $DIR/attr-binary-expr-ambigous.rs:8:5 + | +LL | #[deprecated] x = 4; + | ^^^^^^^^^^^^^^^^^^^ + | +help: wrap the expression in parentheses + | +LL | (#[deprecated] x) = 4; + | + + + +error: ambiguous outer attributes + --> $DIR/attr-binary-expr-ambigous.rs:10:9 + | +LL | x = #[deprecated] 5 as i32; + | ^^^^^^^^^^^^^^^^^^^^^^ + | +help: wrap the expression in parentheses + | +LL | x = (#[deprecated] 5) as i32; + | + + + +error: ambiguous outer attributes + --> $DIR/attr-binary-expr-ambigous.rs:12:14 + | +LL | let _r = #[deprecated] 1..6; + | ^^^^^^^^^^^^^^^^^^ + | +help: wrap the expression in parentheses + | +LL | let _r = (#[deprecated] 1)..6; + | + + + +error: aborting due to 4 previous errors + diff --git a/tests/ui/proc-macro/issue-81555.rs b/tests/ui/proc-macro/issue-81555.rs index 7a61a31952f41..b337ab7ce37db 100644 --- a/tests/ui/proc-macro/issue-81555.rs +++ b/tests/ui/proc-macro/issue-81555.rs @@ -10,6 +10,5 @@ use test_macros::identity_attr; fn main() { let _x; let y = (); - #[identity_attr] - _x = y; + (#[identity_attr] _x) = y; } From 9bd175c8a206cabbca2818d9cf509939b95301a8 Mon Sep 17 00:00:00 2001 From: klensy Date: Tue, 23 Apr 2024 11:23:20 +0300 Subject: [PATCH 5/7] parser: remove ununsed(no reads) max_angle_bracket_count field --- compiler/rustc_parse/src/parser/mod.rs | 3 --- compiler/rustc_parse/src/parser/path.rs | 1 - 2 files changed, 4 deletions(-) diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 4a996f89a9a3d..cd8a5600c73d7 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -162,7 +162,6 @@ pub struct Parser<'a> { /// /// See the comments in the `parse_path_segment` function for more details. unmatched_angle_bracket_count: u16, - max_angle_bracket_count: u16, angle_bracket_nesting: u16, last_unexpected_token_span: Option, @@ -430,7 +429,6 @@ impl<'a> Parser<'a> { num_bump_calls: 0, break_last_token: false, unmatched_angle_bracket_count: 0, - max_angle_bracket_count: 0, angle_bracket_nesting: 0, last_unexpected_token_span: None, subparser_name, @@ -778,7 +776,6 @@ impl<'a> Parser<'a> { if ate { // See doc comment for `unmatched_angle_bracket_count`. self.unmatched_angle_bracket_count += 1; - self.max_angle_bracket_count += 1; debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count); } ate diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 0f410772dd980..b97ec8c613d91 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -299,7 +299,6 @@ impl<'a> Parser<'a> { // parsing a new path. if style == PathStyle::Expr { self.unmatched_angle_bracket_count = 0; - self.max_angle_bracket_count = 0; } // Generic arguments are found - `<`, `(`, `::<` or `::(`. From 132921e18798634eba88a8cd79c81d8aa9c956cd Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 26 Mar 2024 08:47:51 +0100 Subject: [PATCH 6/7] try to fix unnecessarily fragile smir test --- tests/ui-fulldeps/stable-mir/check_transform.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ui-fulldeps/stable-mir/check_transform.rs b/tests/ui-fulldeps/stable-mir/check_transform.rs index e7d852a27df0e..6345ee24f7893 100644 --- a/tests/ui-fulldeps/stable-mir/check_transform.rs +++ b/tests/ui-fulldeps/stable-mir/check_transform.rs @@ -137,9 +137,9 @@ fn generate_input(path: &str) -> std::io::Result<()> { write!( file, r#" - #![feature(panic_internals)] + fn panic_str(msg: &str) {{ panic!("{{}}", msg); }} pub fn dummy() {{ - core::panicking::panic_str("oops"); + panic_str("oops"); }} "# )?; From 7f3c83126a5bbb6d7d440b97a7c970b05c3ff5b8 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Tue, 23 Apr 2024 17:54:58 +0800 Subject: [PATCH 7/7] remove `push_trait_bound_inner` --- compiler/rustc_hir_analysis/src/bounds.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/bounds.rs b/compiler/rustc_hir_analysis/src/bounds.rs index d3f51195dfb29..aafb5c1c0b4bc 100644 --- a/compiler/rustc_hir_analysis/src/bounds.rs +++ b/compiler/rustc_hir_analysis/src/bounds.rs @@ -43,16 +43,6 @@ impl<'tcx> Bounds<'tcx> { trait_ref: ty::PolyTraitRef<'tcx>, span: Span, polarity: ty::PredicatePolarity, - ) { - self.push_trait_bound_inner(tcx, trait_ref, span, polarity); - } - - fn push_trait_bound_inner( - &mut self, - tcx: TyCtxt<'tcx>, - trait_ref: ty::PolyTraitRef<'tcx>, - span: Span, - polarity: ty::PredicatePolarity, ) { let clause = ( trait_ref