diff --git a/Cargo.lock b/Cargo.lock index 545c776b4805e..1a930b64f4581 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3687,7 +3687,6 @@ dependencies = [ "serde_json", "smallvec", "tempfile", - "thin-vec", "thorin-dwp", "tracing", "wasm-encoder 0.219.2", diff --git a/compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs b/compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs index d825d770fa357..4f1a8cd8b403c 100644 --- a/compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs +++ b/compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs @@ -29,7 +29,7 @@ impl CombineAttributeParser for AllowInternalUnstableParser { pub(crate) struct UnstableFeatureBoundParser; impl CombineAttributeParser for UnstableFeatureBoundParser { - const PATH: &'static [rustc_span::Symbol] = &[sym::unstable_feature_bound]; + const PATH: &[rustc_span::Symbol] = &[sym::unstable_feature_bound]; type Item = (Symbol, Span); const CONVERT: ConvertFn = |items, _| AttributeKind::UnstableFeatureBound(items); const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ diff --git a/compiler/rustc_attr_parsing/src/attributes/inline.rs b/compiler/rustc_attr_parsing/src/attributes/inline.rs index f6aab9ea0ee2a..767200bfa9bf1 100644 --- a/compiler/rustc_attr_parsing/src/attributes/inline.rs +++ b/compiler/rustc_attr_parsing/src/attributes/inline.rs @@ -10,7 +10,7 @@ use super::prelude::*; pub(crate) struct InlineParser; impl SingleAttributeParser for InlineParser { - const PATH: &'static [Symbol] = &[sym::inline]; + const PATH: &[Symbol] = &[sym::inline]; const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost; const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ @@ -67,7 +67,7 @@ impl SingleAttributeParser for InlineParser { pub(crate) struct RustcForceInlineParser; impl SingleAttributeParser for RustcForceInlineParser { - const PATH: &'static [Symbol] = &[sym::rustc_force_inline]; + const PATH: &[Symbol] = &[sym::rustc_force_inline]; const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost; const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ diff --git a/compiler/rustc_attr_parsing/src/attributes/repr.rs b/compiler/rustc_attr_parsing/src/attributes/repr.rs index 34d4957edc70e..fb0b8df652848 100644 --- a/compiler/rustc_attr_parsing/src/attributes/repr.rs +++ b/compiler/rustc_attr_parsing/src/attributes/repr.rs @@ -272,7 +272,7 @@ fn parse_alignment(node: &LitKind) -> Result { pub(crate) struct AlignParser(Option<(Align, Span)>); impl AlignParser { - const PATH: &'static [Symbol] = &[sym::rustc_align]; + const PATH: &[Symbol] = &[sym::rustc_align]; const TEMPLATE: AttributeTemplate = template!(List: &[""]); fn parse(&mut self, cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) { @@ -329,7 +329,7 @@ impl AttributeParser for AlignParser { pub(crate) struct AlignStaticParser(AlignParser); impl AlignStaticParser { - const PATH: &'static [Symbol] = &[sym::rustc_align_static]; + const PATH: &[Symbol] = &[sym::rustc_align_static]; const TEMPLATE: AttributeTemplate = AlignParser::TEMPLATE; fn parse(&mut self, cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) { diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs index 0fd8c8a04a57a..0d19dc25d402c 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs @@ -2,7 +2,8 @@ use std::path::PathBuf; use rustc_ast::{LitIntType, LitKind, MetaItemLit}; use rustc_hir::attrs::{ - BorrowckGraphvizFormatKind, RustcCleanAttribute, RustcCleanQueries, RustcLayoutType, + BorrowckGraphvizFormatKind, CguFields, CguKind, DivergingBlockBehavior, + DivergingFallbackBehavior, RustcCleanAttribute, RustcCleanQueries, RustcLayoutType, RustcMirKind, }; use rustc_session::errors; @@ -10,12 +11,14 @@ use rustc_span::Symbol; use super::prelude::*; use super::util::parse_single_integer; -use crate::session_diagnostics::{AttributeRequiresOpt, RustcScalableVectorCountOutOfRange}; +use crate::session_diagnostics::{ + AttributeRequiresOpt, CguFieldsMissing, RustcScalableVectorCountOutOfRange, +}; pub(crate) struct RustcMainParser; impl NoArgsAttributeParser for RustcMainParser { - const PATH: &'static [Symbol] = &[sym::rustc_main]; + const PATH: &[Symbol] = &[sym::rustc_main]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]); const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcMain; @@ -100,7 +103,7 @@ impl NoArgsAttributeParser for RustcNoImplicitAutorefsParser { pub(crate) struct RustcLayoutScalarValidRangeStartParser; impl SingleAttributeParser for RustcLayoutScalarValidRangeStartParser { - const PATH: &'static [Symbol] = &[sym::rustc_layout_scalar_valid_range_start]; + const PATH: &[Symbol] = &[sym::rustc_layout_scalar_valid_range_start]; const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]); @@ -115,7 +118,7 @@ impl SingleAttributeParser for RustcLayoutScalarValidRangeStartPars pub(crate) struct RustcLayoutScalarValidRangeEndParser; impl SingleAttributeParser for RustcLayoutScalarValidRangeEndParser { - const PATH: &'static [Symbol] = &[sym::rustc_layout_scalar_valid_range_end]; + const PATH: &[Symbol] = &[sym::rustc_layout_scalar_valid_range_end]; const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]); @@ -204,6 +207,325 @@ impl NoArgsAttributeParser for RustcLintOptTyParser { const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintOptTy; } +fn parse_cgu_fields( + cx: &mut AcceptContext<'_, '_, S>, + args: &ArgParser, + accepts_kind: bool, +) -> Option<(Symbol, Symbol, Option)> { + let Some(args) = args.list() else { + cx.expected_list(cx.attr_span, args); + return None; + }; + + let mut cfg = None::<(Symbol, Span)>; + let mut module = None::<(Symbol, Span)>; + let mut kind = None::<(Symbol, Span)>; + + for arg in args.mixed() { + let Some(arg) = arg.meta_item() else { + cx.expected_name_value(args.span, None); + continue; + }; + + let res = match arg.ident().map(|i| i.name) { + Some(sym::cfg) => &mut cfg, + Some(sym::module) => &mut module, + Some(sym::kind) if accepts_kind => &mut kind, + _ => { + cx.expected_specific_argument( + arg.path().span(), + if accepts_kind { + &[sym::cfg, sym::module, sym::kind] + } else { + &[sym::cfg, sym::module] + }, + ); + continue; + } + }; + + let Some(i) = arg.args().name_value() else { + cx.expected_name_value(arg.span(), None); + continue; + }; + + let Some(str) = i.value_as_str() else { + cx.expected_string_literal(i.value_span, Some(i.value_as_lit())); + continue; + }; + + if res.is_some() { + cx.duplicate_key(arg.span(), arg.ident().unwrap().name); + continue; + } + + *res = Some((str, i.value_span)); + } + + let Some((cfg, _)) = cfg else { + cx.emit_err(CguFieldsMissing { span: args.span, name: &cx.attr_path, field: sym::cfg }); + return None; + }; + let Some((module, _)) = module else { + cx.emit_err(CguFieldsMissing { span: args.span, name: &cx.attr_path, field: sym::module }); + return None; + }; + let kind = if let Some((kind, span)) = kind { + Some(match kind { + sym::no => CguKind::No, + sym::pre_dash_lto => CguKind::PreDashLto, + sym::post_dash_lto => CguKind::PostDashLto, + sym::any => CguKind::Any, + _ => { + cx.expected_specific_argument_strings( + span, + &[sym::no, sym::pre_dash_lto, sym::post_dash_lto, sym::any], + ); + return None; + } + }) + } else { + // return None so that an unwrap for the attributes that need it is ok. + if accepts_kind { + cx.emit_err(CguFieldsMissing { + span: args.span, + name: &cx.attr_path, + field: sym::kind, + }); + return None; + }; + + None + }; + + Some((cfg, module, kind)) +} + +#[derive(Default)] +pub(crate) struct RustcCguTestAttributeParser { + items: ThinVec<(Span, CguFields)>, +} + +impl AttributeParser for RustcCguTestAttributeParser { + const ATTRIBUTES: AcceptMapping = &[ + ( + &[sym::rustc_partition_reused], + template!(List: &[r#"cfg = "...", module = "...""#]), + |this, cx, args| { + this.items.extend(parse_cgu_fields(cx, args, false).map(|(cfg, module, _)| { + (cx.attr_span, CguFields::PartitionReused { cfg, module }) + })); + }, + ), + ( + &[sym::rustc_partition_codegened], + template!(List: &[r#"cfg = "...", module = "...""#]), + |this, cx, args| { + this.items.extend(parse_cgu_fields(cx, args, false).map(|(cfg, module, _)| { + (cx.attr_span, CguFields::PartitionCodegened { cfg, module }) + })); + }, + ), + ( + &[sym::rustc_expected_cgu_reuse], + template!(List: &[r#"cfg = "...", module = "...", kind = "...""#]), + |this, cx, args| { + this.items.extend(parse_cgu_fields(cx, args, true).map(|(cfg, module, kind)| { + // unwrap ok because if not given, we return None in `parse_cgu_fields`. + (cx.attr_span, CguFields::ExpectedCguReuse { cfg, module, kind: kind.unwrap() }) + })); + }, + ), + ]; + + const ALLOWED_TARGETS: AllowedTargets = + AllowedTargets::AllowList(&[Allow(Target::Mod), Allow(Target::Crate)]); + + fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option { + Some(AttributeKind::RustcCguTestAttr(self.items)) + } +} + +pub(crate) struct RustcDeprecatedSafe2024Parser; + +impl SingleAttributeParser for RustcDeprecatedSafe2024Parser { + const PATH: &[Symbol] = &[sym::rustc_deprecated_safe_2024]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ + Allow(Target::Fn), + Allow(Target::Method(MethodKind::Inherent)), + Allow(Target::Method(MethodKind::Trait { body: false })), + Allow(Target::Method(MethodKind::Trait { body: true })), + Allow(Target::Method(MethodKind::TraitImpl)), + ]); + const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost; + const TEMPLATE: AttributeTemplate = template!(List: &[r#"audit_that = "...""#]); + + fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option { + let Some(args) = args.list() else { + cx.expected_list(cx.attr_span, args); + return None; + }; + + let Some(single) = args.single() else { + cx.expected_single_argument(args.span); + return None; + }; + + let Some(arg) = single.meta_item() else { + cx.expected_name_value(args.span, None); + return None; + }; + + let Some(args) = arg.word_is(sym::audit_that) else { + cx.expected_specific_argument(arg.span(), &[sym::audit_that]); + return None; + }; + + let Some(nv) = args.name_value() else { + cx.expected_name_value(arg.span(), Some(sym::audit_that)); + return None; + }; + + let Some(suggestion) = nv.value_as_str() else { + cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit())); + return None; + }; + + Some(AttributeKind::RustcDeprecatedSafe2024 { suggestion }) + } +} + +pub(crate) struct RustcConversionSuggestionParser; + +impl NoArgsAttributeParser for RustcConversionSuggestionParser { + const PATH: &[Symbol] = &[sym::rustc_conversion_suggestion]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ + Allow(Target::Fn), + Allow(Target::Method(MethodKind::Inherent)), + Allow(Target::Method(MethodKind::Trait { body: false })), + Allow(Target::Method(MethodKind::Trait { body: true })), + Allow(Target::Method(MethodKind::TraitImpl)), + ]); + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcConversionSuggestion; +} + +pub(crate) struct RustcCaptureAnalysisParser; + +impl NoArgsAttributeParser for RustcCaptureAnalysisParser { + const PATH: &[Symbol] = &[sym::rustc_capture_analysis]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Closure)]); + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcCaptureAnalysis; +} + +pub(crate) struct RustcNeverTypeOptionsParser; + +impl SingleAttributeParser for RustcNeverTypeOptionsParser { + const PATH: &[Symbol] = &[sym::rustc_never_type_options]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]); + const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost; + const TEMPLATE: AttributeTemplate = template!(List: &[ + r#"fallback = "unit", "never", "no""#, + r#"diverging_block_default = "unit", "never""#, + ]); + + fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option { + let Some(list) = args.list() else { + cx.expected_list(cx.attr_span, args); + return None; + }; + + let mut fallback = None::; + let mut diverging_block_default = None::; + + for arg in list.mixed() { + let Some(meta) = arg.meta_item() else { + cx.expected_name_value(arg.span(), None); + continue; + }; + + let res = match meta.ident().map(|i| i.name) { + Some(sym::fallback) => &mut fallback, + Some(sym::diverging_block_default) => &mut diverging_block_default, + _ => { + cx.expected_specific_argument( + meta.path().span(), + &[sym::fallback, sym::diverging_block_default], + ); + continue; + } + }; + + let Some(nv) = meta.args().name_value() else { + cx.expected_name_value(meta.span(), None); + continue; + }; + + let Some(field) = nv.value_as_str() else { + cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit())); + continue; + }; + + if res.is_some() { + cx.duplicate_key(meta.span(), meta.ident().unwrap().name); + continue; + } + + *res = Some(Ident { name: field, span: nv.value_span }); + } + + let fallback = match fallback { + None => None, + Some(Ident { name: sym::unit, .. }) => Some(DivergingFallbackBehavior::ToUnit), + Some(Ident { name: sym::never, .. }) => Some(DivergingFallbackBehavior::ToNever), + Some(Ident { name: sym::no, .. }) => Some(DivergingFallbackBehavior::NoFallback), + Some(Ident { span, .. }) => { + cx.expected_specific_argument_strings(span, &[sym::unit, sym::never, sym::no]); + return None; + } + }; + + let diverging_block_default = match diverging_block_default { + None => None, + Some(Ident { name: sym::unit, .. }) => Some(DivergingBlockBehavior::Unit), + Some(Ident { name: sym::never, .. }) => Some(DivergingBlockBehavior::Never), + Some(Ident { span, .. }) => { + cx.expected_specific_argument_strings(span, &[sym::unit, sym::no]); + return None; + } + }; + + Some(AttributeKind::RustcNeverTypeOptions { fallback, diverging_block_default }) + } +} + +pub(crate) struct RustcTrivialFieldReadsParser; + +impl NoArgsAttributeParser for RustcTrivialFieldReadsParser { + const PATH: &[Symbol] = &[sym::rustc_trivial_field_reads]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Trait)]); + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcTrivialFieldReads; +} + +pub(crate) struct RustcNoMirInlineParser; + +impl NoArgsAttributeParser for RustcNoMirInlineParser { + const PATH: &[Symbol] = &[sym::rustc_no_mir_inline]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ + Allow(Target::Fn), + Allow(Target::Method(MethodKind::Inherent)), + Allow(Target::Method(MethodKind::Trait { body: false })), + Allow(Target::Method(MethodKind::Trait { body: true })), + Allow(Target::Method(MethodKind::TraitImpl)), + ]); + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoMirInline; +} + pub(crate) struct RustcLintQueryInstabilityParser; impl NoArgsAttributeParser for RustcLintQueryInstabilityParser { @@ -253,21 +575,11 @@ impl NoArgsAttributeParser for RustcLintUntrackedQueryInformationPa pub(crate) struct RustcObjectLifetimeDefaultParser; -impl SingleAttributeParser for RustcObjectLifetimeDefaultParser { +impl NoArgsAttributeParser for RustcObjectLifetimeDefaultParser { const PATH: &[Symbol] = &[sym::rustc_object_lifetime_default]; - const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]); - const TEMPLATE: AttributeTemplate = template!(Word); - - fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option { - if let Err(span) = args.no_args() { - cx.expected_no_args(span); - return None; - } - - Some(AttributeKind::RustcObjectLifetimeDefault) - } + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcObjectLifetimeDefault; } pub(crate) struct RustcSimdMonomorphizeLaneLimitParser; @@ -510,7 +822,7 @@ impl CombineAttributeParser for RustcMirParser { pub(crate) struct RustcNonConstTraitMethodParser; impl NoArgsAttributeParser for RustcNonConstTraitMethodParser { - const PATH: &'static [Symbol] = &[sym::rustc_non_const_trait_method]; + const PATH: &[Symbol] = &[sym::rustc_non_const_trait_method]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ Allow(Target::Method(MethodKind::Trait { body: true })), @@ -755,7 +1067,7 @@ impl NoArgsAttributeParser for RustcInsignificantDtorParser { pub(crate) struct RustcEffectiveVisibilityParser; impl NoArgsAttributeParser for RustcEffectiveVisibilityParser { - const PATH: &'static [Symbol] = &[sym::rustc_effective_visibility]; + const PATH: &[Symbol] = &[sym::rustc_effective_visibility]; const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error; const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ Allow(Target::Use), diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 412f960fb7cfb..47b988bcf338b 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -147,6 +147,7 @@ attribute_parsers!( DocParser, MacroUseParser, NakedParser, + RustcCguTestAttributeParser, StabilityParser, UsedParser, // tidy-alphabetical-end @@ -201,6 +202,7 @@ attribute_parsers!( Single, Single, Single, + Single, Single, Single, Single, @@ -208,7 +210,7 @@ attribute_parsers!( Single, Single, Single, - Single, + Single, Single, Single, Single, @@ -261,7 +263,9 @@ attribute_parsers!( Single>, Single>, Single>, + Single>, Single>, + Single>, Single>, Single>, Single>, @@ -283,8 +287,10 @@ attribute_parsers!( Single>, Single>, Single>, + Single>, Single>, Single>, + Single>, Single>, Single>, Single>, @@ -293,6 +299,7 @@ attribute_parsers!( Single>, Single>, Single>, + Single>, Single>, Single>, Single>, diff --git a/compiler/rustc_attr_parsing/src/session_diagnostics.rs b/compiler/rustc_attr_parsing/src/session_diagnostics.rs index 6ca47e8d698b1..2eb585671fff9 100644 --- a/compiler/rustc_attr_parsing/src/session_diagnostics.rs +++ b/compiler/rustc_attr_parsing/src/session_diagnostics.rs @@ -45,6 +45,15 @@ pub(crate) struct DocAliasStartEnd<'a> { pub attr_str: &'a str, } +#[derive(Diagnostic)] +#[diag("`#[{$name})]` is missing a `{$field}` argument")] +pub(crate) struct CguFieldsMissing<'a> { + #[primary_span] + pub span: Span, + pub name: &'a AttrPath, + pub field: Symbol, +} + #[derive(Diagnostic)] #[diag("`#![doc({$attr_name} = \"...\")]` isn't allowed as a crate-level attribute")] pub(crate) struct DocAttrNotCrateLevel { diff --git a/compiler/rustc_codegen_ssa/Cargo.toml b/compiler/rustc_codegen_ssa/Cargo.toml index 8642206351201..c7d7b7429f34a 100644 --- a/compiler/rustc_codegen_ssa/Cargo.toml +++ b/compiler/rustc_codegen_ssa/Cargo.toml @@ -36,7 +36,6 @@ rustc_trait_selection = { path = "../rustc_trait_selection" } serde_json = "1.0.59" smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } tempfile = "3.2" -thin-vec = "0.2.12" thorin-dwp = "0.9" tracing = "0.1" wasm-encoder = "0.219" diff --git a/compiler/rustc_codegen_ssa/src/assert_module_sources.rs b/compiler/rustc_codegen_ssa/src/assert_module_sources.rs index 43e1e135a666b..081fe0aa91aa4 100644 --- a/compiler/rustc_codegen_ssa/src/assert_module_sources.rs +++ b/compiler/rustc_codegen_ssa/src/assert_module_sources.rs @@ -28,13 +28,13 @@ use std::fmt; use rustc_data_structures::unord::{UnordMap, UnordSet}; use rustc_errors::{DiagArgValue, IntoDiagArg}; -use rustc_hir as hir; +use rustc_hir::attrs::{AttributeKind, CguFields, CguKind}; use rustc_hir::def_id::LOCAL_CRATE; +use rustc_hir::{self as hir, find_attr}; use rustc_middle::mir::mono::CodegenUnitNameBuilder; use rustc_middle::ty::TyCtxt; use rustc_session::Session; -use rustc_span::{Span, Symbol, sym}; -use thin_vec::ThinVec; +use rustc_span::{Span, Symbol}; use tracing::debug; use crate::errors; @@ -63,9 +63,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>, set_reuse: &dyn Fn(&mut CguReuseTr }, }; - for attr in tcx.hir_attrs(rustc_hir::CRATE_HIR_ID) { - ams.check_attr(attr); - } + ams.check_attrs(tcx.hir_attrs(rustc_hir::CRATE_HIR_ID)); set_reuse(&mut ams.cgu_reuse_tracker); @@ -89,109 +87,91 @@ struct AssertModuleSource<'tcx> { } impl<'tcx> AssertModuleSource<'tcx> { - fn check_attr(&mut self, attr: &hir::Attribute) { - let (expected_reuse, comp_kind) = if attr.has_name(sym::rustc_partition_reused) { - (CguReuse::PreLto, ComparisonKind::AtLeast) - } else if attr.has_name(sym::rustc_partition_codegened) { - (CguReuse::No, ComparisonKind::Exact) - } else if attr.has_name(sym::rustc_expected_cgu_reuse) { - match self.field(attr, sym::kind) { - sym::no => (CguReuse::No, ComparisonKind::Exact), - sym::pre_dash_lto => (CguReuse::PreLto, ComparisonKind::Exact), - sym::post_dash_lto => (CguReuse::PostLto, ComparisonKind::Exact), - sym::any => (CguReuse::PreLto, ComparisonKind::AtLeast), - other => { - self.tcx - .dcx() - .emit_fatal(errors::UnknownReuseKind { span: attr.span(), kind: other }); - } + fn check_attrs(&mut self, attrs: &[hir::Attribute]) { + for &(span, cgu_fields) in find_attr!(attrs, + AttributeKind::RustcCguTestAttr(e) => e) + .into_iter() + .flatten() + { + let (expected_reuse, comp_kind) = match cgu_fields { + CguFields::PartitionReused { .. } => (CguReuse::PreLto, ComparisonKind::AtLeast), + CguFields::PartitionCodegened { .. } => (CguReuse::No, ComparisonKind::Exact), + CguFields::ExpectedCguReuse { kind, .. } => match kind { + CguKind::No => (CguReuse::No, ComparisonKind::Exact), + CguKind::PreDashLto => (CguReuse::PreLto, ComparisonKind::Exact), + CguKind::PostDashLto => (CguReuse::PostLto, ComparisonKind::Exact), + CguKind::Any => (CguReuse::PreLto, ComparisonKind::AtLeast), + }, + }; + let (CguFields::ExpectedCguReuse { cfg, module, .. } + | CguFields::PartitionCodegened { cfg, module } + | CguFields::PartitionReused { cfg, module }) = cgu_fields; + + if !self.tcx.sess.opts.unstable_opts.query_dep_graph { + self.tcx.dcx().emit_fatal(errors::MissingQueryDepGraph { span }); } - } else { - return; - }; - - if !self.tcx.sess.opts.unstable_opts.query_dep_graph { - self.tcx.dcx().emit_fatal(errors::MissingQueryDepGraph { span: attr.span() }); - } - if !self.check_config(attr) { - debug!("check_attr: config does not match, ignoring attr"); - return; - } + if !self.check_config(cfg) { + debug!("check_attr: config does not match, ignoring attr"); + return; + } - let user_path = self.field(attr, sym::module).to_string(); - let crate_name = self.tcx.crate_name(LOCAL_CRATE).to_string(); + let user_path = module.as_str(); + let crate_name = self.tcx.crate_name(LOCAL_CRATE); + let crate_name = crate_name.as_str(); - if !user_path.starts_with(&crate_name) { - self.tcx.dcx().emit_fatal(errors::MalformedCguName { - span: attr.span(), - user_path, - crate_name, - }); - } + if !user_path.starts_with(&crate_name) { + self.tcx.dcx().emit_fatal(errors::MalformedCguName { span, user_path, crate_name }); + } - // Split of the "special suffix" if there is one. - let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind('.') { - (&user_path[..index], Some(&user_path[index + 1..])) - } else { - (&user_path[..], None) - }; + // Split of the "special suffix" if there is one. + let (user_path, cgu_special_suffix) = if let Some(index) = user_path.rfind('.') { + (&user_path[..index], Some(&user_path[index + 1..])) + } else { + (&user_path[..], None) + }; - let mut iter = user_path.split('-'); + let mut iter = user_path.split('-'); - // Remove the crate name - assert_eq!(iter.next().unwrap(), crate_name); + // Remove the crate name + assert_eq!(iter.next().unwrap(), crate_name); - let cgu_path_components = iter.collect::>(); + let cgu_path_components = iter.collect::>(); - let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx); - let cgu_name = - cgu_name_builder.build_cgu_name(LOCAL_CRATE, cgu_path_components, cgu_special_suffix); + let cgu_name_builder = &mut CodegenUnitNameBuilder::new(self.tcx); + let cgu_name = cgu_name_builder.build_cgu_name( + LOCAL_CRATE, + cgu_path_components, + cgu_special_suffix, + ); - debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name); + debug!("mapping '{user_path}' to cgu name '{cgu_name}'"); + + if !self.available_cgus.contains(&cgu_name) { + let cgu_names: Vec<&str> = + self.available_cgus.items().map(|cgu| cgu.as_str()).into_sorted_stable_ord(); + self.tcx.dcx().emit_err(errors::NoModuleNamed { + span, + user_path, + cgu_name, + cgu_names: cgu_names.join(", "), + }); + } - if !self.available_cgus.contains(&cgu_name) { - let cgu_names: Vec<&str> = - self.available_cgus.items().map(|cgu| cgu.as_str()).into_sorted_stable_ord(); - self.tcx.dcx().emit_err(errors::NoModuleNamed { - span: attr.span(), - user_path, + self.cgu_reuse_tracker.set_expectation( cgu_name, - cgu_names: cgu_names.join(", "), - }); - } - - self.cgu_reuse_tracker.set_expectation( - cgu_name, - user_path, - attr.span(), - expected_reuse, - comp_kind, - ); - } - - fn field(&self, attr: &hir::Attribute, name: Symbol) -> Symbol { - for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) { - if item.has_name(name) { - if let Some(value) = item.value_str() { - return value; - } else { - self.tcx.dcx().emit_fatal(errors::FieldAssociatedValueExpected { - span: item.span(), - name, - }); - } - } + user_path, + span, + expected_reuse, + comp_kind, + ); } - - self.tcx.dcx().emit_fatal(errors::NoField { span: attr.span(), name }); } /// Scan for a `cfg="foo"` attribute and check whether we have a /// cfg flag called `foo`. - fn check_config(&self, attr: &hir::Attribute) -> bool { + fn check_config(&self, value: Symbol) -> bool { let config = &self.tcx.sess.psess.config; - let value = self.field(attr, sym::cfg); debug!("check_config(config={:?}, value={:?})", config, value); if config.iter().any(|&(name, _)| name == value) { debug!("check_config: matched"); diff --git a/compiler/rustc_codegen_ssa/src/errors.rs b/compiler/rustc_codegen_ssa/src/errors.rs index 82a6525adcc99..897b5f957e154 100644 --- a/compiler/rustc_codegen_ssa/src/errors.rs +++ b/compiler/rustc_codegen_ssa/src/errors.rs @@ -42,14 +42,6 @@ pub(crate) struct CguNotRecorded<'a> { pub cgu_name: &'a str, } -#[derive(Diagnostic)] -#[diag("unknown cgu-reuse-kind `{$kind}` specified")] -pub(crate) struct UnknownReuseKind { - #[primary_span] - pub span: Span, - pub kind: Symbol, -} - #[derive(Diagnostic)] #[diag("found CGU-reuse attribute but `-Zquery-dep-graph` was not specified")] pub(crate) struct MissingQueryDepGraph { @@ -61,11 +53,11 @@ pub(crate) struct MissingQueryDepGraph { #[diag( "found malformed codegen unit name `{$user_path}`. codegen units names must always start with the name of the crate (`{$crate_name}` in this case)" )] -pub(crate) struct MalformedCguName { +pub(crate) struct MalformedCguName<'a> { #[primary_span] pub span: Span, - pub user_path: String, - pub crate_name: String, + pub user_path: &'a str, + pub crate_name: &'a str, } #[derive(Diagnostic)] @@ -78,22 +70,6 @@ pub(crate) struct NoModuleNamed<'a> { pub cgu_names: String, } -#[derive(Diagnostic)] -#[diag("associated value expected for `{$name}`")] -pub(crate) struct FieldAssociatedValueExpected { - #[primary_span] - pub span: Span, - pub name: Symbol, -} - -#[derive(Diagnostic)] -#[diag("no field `{$name}`")] -pub(crate) struct NoField { - #[primary_span] - pub span: Span, - pub name: Symbol, -} - #[derive(Diagnostic)] #[diag("failed to write lib.def file: {$error}")] pub(crate) struct LibDefWriteFailure { diff --git a/compiler/rustc_hir/src/attrs/data_structures.rs b/compiler/rustc_hir/src/attrs/data_structures.rs index 2d3825ccd0246..7050bb6ea70d8 100644 --- a/compiler/rustc_hir/src/attrs/data_structures.rs +++ b/compiler/rustc_hir/src/attrs/data_structures.rs @@ -49,6 +49,57 @@ pub struct EiiDecl { pub name: Ident, } +#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic, PrintAttribute)] +pub enum CguKind { + No, + PreDashLto, + PostDashLto, + Any, +} + +#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic, PrintAttribute)] +pub enum CguFields { + PartitionReused { cfg: Symbol, module: Symbol }, + PartitionCodegened { cfg: Symbol, module: Symbol }, + ExpectedCguReuse { cfg: Symbol, module: Symbol, kind: CguKind }, +} + +#[derive(Copy, Clone, PartialEq, Debug, PrintAttribute)] +#[derive(HashStable_Generic, Encodable, Decodable)] +pub enum DivergingFallbackBehavior { + /// Always fallback to `()` (aka "always spontaneous decay") + ToUnit, + /// Always fallback to `!` (which should be equivalent to never falling back + not making + /// never-to-any coercions unless necessary) + ToNever, + /// Don't fallback at all + NoFallback, +} + +#[derive(Copy, Clone, PartialEq, Debug, PrintAttribute, Default)] +#[derive(HashStable_Generic, Encodable, Decodable)] +pub enum DivergingBlockBehavior { + /// This is the current stable behavior: + /// + /// ```rust + /// { + /// return; + /// } // block has type = !, even though we are supposedly dropping it with `;` + /// ``` + #[default] + Never, + + /// Alternative behavior: + /// + /// ```ignore (very-unstable-new-attribute) + /// #![rustc_never_type_options(diverging_block_default = "unit")] + /// { + /// return; + /// } // block has type = (), since we are dropping `!` from `return` with `;` + /// ``` + Unit, +} + #[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic, PrintAttribute)] pub enum InlineAttr { None, @@ -1053,6 +1104,12 @@ pub enum AttributeKind { /// Represents `#[rustc_builtin_macro]`. RustcBuiltinMacro { builtin_name: Option, helper_attrs: ThinVec, span: Span }, + /// Represents `#[rustc_capture_analysis]` + RustcCaptureAnalysis, + + /// Represents `#[rustc_expected_cgu_reuse]`, `#[rustc_partition_codegened]` and `#[rustc_partition_reused]`. + RustcCguTestAttr(ThinVec<(Span, CguFields)>), + /// Represents `#[rustc_clean]` RustcClean(ThinVec), @@ -1078,6 +1135,9 @@ pub enum AttributeKind { /// Represents `#[rustc_const_stable_indirect]`. RustcConstStabilityIndirect, + /// Represents `#[rustc_conversion_suggestion]` + RustcConversionSuggestion, + /// Represents `#[rustc_deallocator]` RustcDeallocator, @@ -1090,6 +1150,9 @@ pub enum AttributeKind { /// Represents `#[rustc_deny_explicit_impl]`. RustcDenyExplicitImpl(Span), + /// Represents `#[rustc_deprecated_safe_2024]` + RustcDeprecatedSafe2024 { suggestion: Symbol }, + /// Represents `#[rustc_dummy]`. RustcDummy, @@ -1174,12 +1237,21 @@ pub enum AttributeKind { /// Represents `#[rustc_never_returns_null_ptr]` RustcNeverReturnsNullPointer, + /// Represents `#[rustc_never_type_options]`. + RustcNeverTypeOptions { + fallback: Option, + diverging_block_default: Option, + }, + /// Represents `#[rustc_no_implicit_autorefs]` RustcNoImplicitAutorefs, /// Represents `#[rustc_no_implicit_bounds]` RustcNoImplicitBounds, + /// Represents `#[rustc_no_mir_inline]` + RustcNoMirInline, + /// Represents `#[rustc_non_const_trait_method]`. RustcNonConstTraitMethod, @@ -1257,6 +1329,9 @@ pub enum AttributeKind { /// Represents `#[rustc_then_this_would_need]` RustcThenThisWouldNeed(Span, ThinVec), + /// Represents `#[rustc_trivial_field_reads]` + RustcTrivialFieldReads, + /// Represents `#[rustc_unsafe_specialization_marker]`. RustcUnsafeSpecializationMarker(Span), diff --git a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs index ac9ef79e9ab92..73ac1533cef56 100644 --- a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs +++ b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs @@ -98,16 +98,20 @@ impl AttributeKind { RustcAsPtr(..) => Yes, RustcBodyStability { .. } => No, RustcBuiltinMacro { .. } => Yes, + RustcCaptureAnalysis => No, + RustcCguTestAttr { .. } => No, RustcClean { .. } => No, RustcCoherenceIsCore(..) => No, RustcCoinductive(..) => No, RustcConfusables { .. } => Yes, RustcConstStability { .. } => Yes, RustcConstStabilityIndirect => No, + RustcConversionSuggestion => Yes, RustcDeallocator => No, RustcDefPath(..) => No, RustcDelayedBugFromInsideQuery => No, RustcDenyExplicitImpl(..) => No, + RustcDeprecatedSafe2024 { .. } => Yes, RustcDummy => No, RustcDumpDefParents => No, RustcDumpItemBounds => No, @@ -136,8 +140,10 @@ impl AttributeKind { RustcMir(..) => Yes, RustcMustImplementOneOf { .. } => No, RustcNeverReturnsNullPointer => Yes, + RustcNeverTypeOptions { .. } => No, RustcNoImplicitAutorefs => Yes, RustcNoImplicitBounds => No, + RustcNoMirInline => Yes, RustcNonConstTraitMethod => No, // should be reported via other queries like `constness` RustcNounwind => No, RustcObjcClass { .. } => No, @@ -162,6 +168,7 @@ impl AttributeKind { RustcStrictCoherence(..) => Yes, RustcSymbolName(..) => Yes, RustcThenThisWouldNeed(..) => No, + RustcTrivialFieldReads => Yes, RustcUnsafeSpecializationMarker(..) => No, RustcVariance => No, RustcVarianceOfOpaques => No, diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 84663ff884b4a..68cbfa7280589 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -1,7 +1,8 @@ use rustc_errors::{Applicability, Diag, MultiSpan, listify}; -use rustc_hir as hir; +use rustc_hir::attrs::AttributeKind; use rustc_hir::def::Res; use rustc_hir::intravisit::Visitor; +use rustc_hir::{self as hir, find_attr}; use rustc_infer::infer::DefineOpaqueTypes; use rustc_middle::bug; use rustc_middle::ty::adjustment::AllowTwoPhase; @@ -1081,19 +1082,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { hir_id, |m| { self.has_only_self_parameter(m) - && self - .tcx - // This special internal attribute is used to permit - // "identity-like" conversion methods to be suggested here. - // - // FIXME (#46459 and #46460): ideally - // `std::convert::Into::into` and `std::borrow:ToOwned` would - // also be `#[rustc_conversion_suggestion]`, if not for - // method-probing false-positives and -negatives (respectively). - // - // FIXME? Other potential candidate methods: `as_ref` and - // `as_mut`? - .has_attr(m.def_id, sym::rustc_conversion_suggestion) + // This special internal attribute is used to permit + // "identity-like" conversion methods to be suggested here. + // + // FIXME (#46459 and #46460): ideally + // `std::convert::Into::into` and `std::borrow:ToOwned` would + // also be `#[rustc_conversion_suggestion]`, if not for + // method-probing false-positives and -negatives (respectively). + // + // FIXME? Other potential candidate methods: `as_ref` and + // `as_mut`? + && find_attr!(self.tcx.get_all_attrs(m.def_id), AttributeKind::RustcConversionSuggestion) }, ); diff --git a/compiler/rustc_hir_typeck/src/fallback.rs b/compiler/rustc_hir_typeck/src/fallback.rs index 727666de3c479..2e421c610e7ab 100644 --- a/compiler/rustc_hir_typeck/src/fallback.rs +++ b/compiler/rustc_hir_typeck/src/fallback.rs @@ -7,6 +7,7 @@ use rustc_data_structures::graph::{self}; use rustc_data_structures::unord::{UnordMap, UnordSet}; use rustc_hir as hir; use rustc_hir::HirId; +use rustc_hir::attrs::DivergingFallbackBehavior; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{InferKind, Visitor}; @@ -19,17 +20,6 @@ use tracing::debug; use crate::{FnCtxt, errors}; -#[derive(Copy, Clone)] -pub(crate) enum DivergingFallbackBehavior { - /// Always fallback to `()` (aka "always spontaneous decay") - ToUnit, - /// Always fallback to `!` (which should be equivalent to never falling back + not making - /// never-to-any coercions unless necessary) - ToNever, - /// Don't fallback at all - NoFallback, -} - impl<'tcx> FnCtxt<'_, 'tcx> { /// Performs type inference fallback, setting [`FnCtxt::diverging_fallback_has_occurred`] /// if the never type fallback has occurred. diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 2e85410c89604..54d8306936dd6 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -5,6 +5,7 @@ use itertools::Itertools; use rustc_data_structures::fx::FxIndexSet; use rustc_errors::codes::*; use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, a_or_an, listify, pluralize}; +use rustc_hir::attrs::DivergingBlockBehavior; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::Visitor; @@ -47,29 +48,6 @@ rustc_index::newtype_index! { pub(crate) struct GenericIdx {} } -#[derive(Clone, Copy, Default)] -pub(crate) enum DivergingBlockBehavior { - /// This is the current stable behavior: - /// - /// ```rust - /// { - /// return; - /// } // block has type = !, even though we are supposedly dropping it with `;` - /// ``` - #[default] - Never, - - /// Alternative behavior: - /// - /// ```ignore (very-unstable-new-attribute) - /// #![rustc_never_type_options(diverging_block_default = "unit")] - /// { - /// return; - /// } // block has type = (), since we are dropping `!` from `return` with `;` - /// ``` - Unit, -} - impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub(in super::super) fn check_casts(&mut self) { let mut deferred_cast_checks = self.root_ctxt.deferred_cast_checks.borrow_mut(); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs index c875e2e50d70c..412df9162e9f2 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs @@ -10,8 +10,9 @@ use std::ops::Deref; use hir::def_id::CRATE_DEF_ID; use rustc_errors::DiagCtxtHandle; +use rustc_hir::attrs::{AttributeKind, DivergingBlockBehavior, DivergingFallbackBehavior}; use rustc_hir::def_id::{DefId, LocalDefId}; -use rustc_hir::{self as hir, HirId, ItemLocalMap}; +use rustc_hir::{self as hir, HirId, ItemLocalMap, find_attr}; use rustc_hir_analysis::hir_ty_lowering::{ HirTyLowerer, InherentAssocCandidate, RegionInferReason, }; @@ -19,15 +20,13 @@ use rustc_infer::infer::{self, RegionVariableOrigin}; use rustc_infer::traits::{DynCompatibilityViolation, Obligation}; use rustc_middle::ty::{self, Const, Ty, TyCtxt, TypeVisitableExt}; use rustc_session::Session; -use rustc_span::{self, DUMMY_SP, ErrorGuaranteed, Ident, Span, sym}; +use rustc_span::{self, DUMMY_SP, ErrorGuaranteed, Ident, Span}; use rustc_trait_selection::error_reporting::TypeErrCtxt; use rustc_trait_selection::traits::{ self, FulfillmentError, ObligationCause, ObligationCauseCode, ObligationCtxt, }; use crate::coercion::CoerceMany; -use crate::fallback::DivergingFallbackBehavior; -use crate::fn_ctxt::checks::DivergingBlockBehavior; use crate::{CoroutineTypes, Diverges, EnclosingBreakables, TypeckRootCtxt}; /// The `FnCtxt` stores type-checking context needed to type-check bodies of @@ -517,51 +516,5 @@ fn parse_never_type_options_attr( // Error handling is dubious here (unwraps), but that's probably fine for an internal attribute. // Just don't write incorrect attributes <3 - let mut fallback = None; - let mut block = None; - - let items = if tcx.features().rustc_attrs() { - tcx.get_attr(CRATE_DEF_ID, sym::rustc_never_type_options) - .map(|attr| attr.meta_item_list().unwrap()) - } else { - None - }; - let items = items.unwrap_or_default(); - - for item in items { - if item.has_name(sym::fallback) && fallback.is_none() { - let mode = item.value_str().unwrap(); - match mode { - sym::unit => fallback = Some(DivergingFallbackBehavior::ToUnit), - sym::never => fallback = Some(DivergingFallbackBehavior::ToNever), - sym::no => fallback = Some(DivergingFallbackBehavior::NoFallback), - _ => { - tcx.dcx().span_err(item.span(), format!("unknown never type fallback mode: `{mode}` (supported: `unit`, `niko`, `never` and `no`)")); - } - }; - continue; - } - - if item.has_name(sym::diverging_block_default) && block.is_none() { - let default = item.value_str().unwrap(); - match default { - sym::unit => block = Some(DivergingBlockBehavior::Unit), - sym::never => block = Some(DivergingBlockBehavior::Never), - _ => { - tcx.dcx().span_err(item.span(), format!("unknown diverging block default: `{default}` (supported: `unit` and `never`)")); - } - }; - continue; - } - - tcx.dcx().span_err( - item.span(), - format!( - "unknown or duplicate never type option: `{}` (supported: `fallback`, `diverging_block_default`)", - item.name().unwrap() - ), - ); - } - - (fallback, block) + find_attr!(tcx.get_all_attrs(CRATE_DEF_ID), AttributeKind::RustcNeverTypeOptions {fallback, diverging_block_default} => (*fallback, *diverging_block_default)).unwrap_or_default() } diff --git a/compiler/rustc_hir_typeck/src/upvar.rs b/compiler/rustc_hir_typeck/src/upvar.rs index 1a2b76485f359..767913ba5261a 100644 --- a/compiler/rustc_hir_typeck/src/upvar.rs +++ b/compiler/rustc_hir_typeck/src/upvar.rs @@ -36,10 +36,10 @@ use rustc_abi::FIRST_VARIANT; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_data_structures::unord::{ExtendUnord, UnordSet}; use rustc_errors::{Applicability, MultiSpan}; -use rustc_hir as hir; -use rustc_hir::HirId; +use rustc_hir::attrs::AttributeKind; use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::{self, Visitor}; +use rustc_hir::{self as hir, HirId, find_attr}; use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection, ProjectionKind}; use rustc_middle::mir::FakeReadCause; use rustc_middle::traits::ObligationCauseCode; @@ -1743,7 +1743,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } fn should_log_capture_analysis(&self, closure_def_id: LocalDefId) -> bool { - self.has_rustc_attrs && self.tcx.has_attr(closure_def_id, sym::rustc_capture_analysis) + self.has_rustc_attrs + && find_attr!( + self.tcx.get_all_attrs(closure_def_id), + AttributeKind::RustcCaptureAnalysis + ) } fn log_capture_analysis_first_pass( diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 4a20b05d1fc2d..7bced8168bd1d 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -18,7 +18,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_session::lint::Level; use rustc_session::lint::builtin::{DEPRECATED_SAFE_2024, UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_span::def_id::{DefId, LocalDefId}; -use rustc_span::{Span, Symbol, sym}; +use rustc_span::{Span, Symbol}; use crate::builder::ExprCategory; use crate::errors::*; @@ -98,29 +98,14 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { // from an edition before 2024. &UnsafeOpKind::CallToUnsafeFunction(Some(id)) if !span.at_least_rust_2024() - && let Some(attr) = self.tcx.get_attr(id, sym::rustc_deprecated_safe_2024) => + && let Some(suggestion) = find_attr!(self.tcx.get_all_attrs(id), AttributeKind::RustcDeprecatedSafe2024{suggestion} => suggestion) => { - let suggestion = attr - .meta_item_list() - .unwrap_or_default() - .into_iter() - .find(|item| item.has_name(sym::audit_that)) - .map(|item| { - item.value_str().expect( - "`#[rustc_deprecated_safe_2024(audit_that)]` must have a string value", - ) - }); - let sm = self.tcx.sess.source_map(); - let guarantee = suggestion - .as_ref() - .map(|suggestion| format!("that {}", suggestion)) - .unwrap_or_else(|| String::from("its unsafe preconditions")); - let suggestion = suggestion - .and_then(|suggestion| { - sm.indentation_before(span).map(|indent| { - format!("{}// TODO: Audit that {}.\n", indent, suggestion) // ignore-tidy-todo - }) + let guarantee = format!("that {}", suggestion); + let suggestion = sm + .indentation_before(span) + .map(|indent| { + format!("{}// TODO: Audit that {}.\n", indent, suggestion) // ignore-tidy-todo }) .unwrap_or_default(); diff --git a/compiler/rustc_mir_transform/src/check_inline.rs b/compiler/rustc_mir_transform/src/check_inline.rs index 2d18764267276..1f65bd1ba69bc 100644 --- a/compiler/rustc_mir_transform/src/check_inline.rs +++ b/compiler/rustc_mir_transform/src/check_inline.rs @@ -8,7 +8,6 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::mir::{Body, TerminatorKind}; use rustc_middle::ty; use rustc_middle::ty::TyCtxt; -use rustc_span::sym; use crate::pass_manager::MirLint; @@ -42,7 +41,8 @@ pub(super) fn is_inline_valid_on_fn<'tcx>( def_id: DefId, ) -> Result<(), &'static str> { let codegen_attrs = tcx.codegen_fn_attrs(def_id); - if tcx.has_attr(def_id, sym::rustc_no_mir_inline) { + + if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcNoMirInline) { return Err("#[rustc_no_mir_inline]"); } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 0ba4450b17229..2287b8bf483f6 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -294,15 +294,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | AttributeKind::RustcAsPtr(..) | AttributeKind::RustcBodyStability { .. } | AttributeKind::RustcBuiltinMacro { .. } + | AttributeKind::RustcCaptureAnalysis + | AttributeKind::RustcCguTestAttr(..) | AttributeKind::RustcClean(..) | AttributeKind::RustcCoherenceIsCore(..) | AttributeKind::RustcCoinductive(..) | AttributeKind::RustcConfusables { .. } | AttributeKind::RustcConstStabilityIndirect + | AttributeKind::RustcConversionSuggestion | AttributeKind::RustcDeallocator | AttributeKind::RustcDefPath(..) | AttributeKind::RustcDelayedBugFromInsideQuery | AttributeKind::RustcDenyExplicitImpl(..) + | AttributeKind::RustcDeprecatedSafe2024 {..} | AttributeKind::RustcDummy | AttributeKind::RustcDumpDefParents | AttributeKind::RustcDumpItemBounds @@ -329,8 +333,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | AttributeKind::RustcMain | AttributeKind::RustcMir(_) | AttributeKind::RustcNeverReturnsNullPointer + | AttributeKind::RustcNeverTypeOptions {..} | AttributeKind::RustcNoImplicitAutorefs | AttributeKind::RustcNoImplicitBounds + | AttributeKind::RustcNoMirInline | AttributeKind::RustcNonConstTraitMethod | AttributeKind::RustcNounwind | AttributeKind::RustcObjcClass { .. } @@ -353,6 +359,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | AttributeKind::RustcStrictCoherence(..) | AttributeKind::RustcSymbolName(..) | AttributeKind::RustcThenThisWouldNeed(..) + | AttributeKind::RustcTrivialFieldReads | AttributeKind::RustcUnsafeSpecializationMarker(..) | AttributeKind::RustcVariance | AttributeKind::RustcVarianceOfOpaques @@ -391,25 +398,17 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | sym::lang | sym::default_lib_allocator | sym::rustc_diagnostic_item - | sym::rustc_no_mir_inline | sym::rustc_nonnull_optimization_guaranteed | sym::rustc_inherit_overflow_checks - | sym::rustc_trivial_field_reads | sym::rustc_on_unimplemented | sym::rustc_do_not_const_check | sym::rustc_doc_primitive - | sym::rustc_conversion_suggestion - | sym::rustc_deprecated_safe_2024 | sym::rustc_test_marker | sym::rustc_layout | sym::rustc_proc_macro_decls - | sym::rustc_never_type_options | sym::rustc_autodiff | sym::rustc_capture_analysis | sym::rustc_mir - | sym::rustc_partition_reused - | sym::rustc_partition_codegened - | sym::rustc_expected_cgu_reuse // crate-level attrs, are checked below | sym::feature | sym::register_tool diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 3294b6802a719..75bac9eff2c71 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -10,10 +10,11 @@ use hir::def_id::{LocalDefIdMap, LocalDefIdSet}; use rustc_abi::FieldIdx; use rustc_data_structures::fx::FxIndexSet; use rustc_errors::{ErrorGuaranteed, MultiSpan}; +use rustc_hir::attrs::AttributeKind; use rustc_hir::def::{CtorOf, DefKind, Res}; use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{self as hir, Node, PatKind, QPath}; +use rustc_hir::{self as hir, Node, PatKind, QPath, find_attr}; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::middle::privacy::Level; use rustc_middle::query::Providers; @@ -380,7 +381,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { && let impl_of = self.tcx.parent(impl_item.owner_id.to_def_id()) && self.tcx.is_automatically_derived(impl_of) && let trait_ref = self.tcx.impl_trait_ref(impl_of).instantiate_identity() - && self.tcx.has_attr(trait_ref.def_id, sym::rustc_trivial_field_reads) + && find_attr!( + self.tcx.get_all_attrs(trait_ref.def_id), + AttributeKind::RustcTrivialFieldReads + ) { if let ty::Adt(adt_def, _) = trait_ref.self_ty().kind() && let Some(adt_def_id) = adt_def.did().as_local() diff --git a/tests/ui/attributes/malformed-never-type-options.rs b/tests/ui/attributes/malformed-never-type-options.rs index 0c384be0e2266..7dd7a854ed2e3 100644 --- a/tests/ui/attributes/malformed-never-type-options.rs +++ b/tests/ui/attributes/malformed-never-type-options.rs @@ -2,7 +2,7 @@ //! The `rustc_*` attribute is malformed, but ICEing without a `feature(rustc_attrs)` is still bad. #![rustc_never_type_options(: Unsize = "hi")] -//~^ ERROR expected unsuffixed literal, found `:` +//~^ ERROR expected a literal //~| ERROR use of an internal attribute fn main() {} diff --git a/tests/ui/attributes/malformed-never-type-options.stderr b/tests/ui/attributes/malformed-never-type-options.stderr index 0d2ff4881f2ae..98870c8bddedc 100644 --- a/tests/ui/attributes/malformed-never-type-options.stderr +++ b/tests/ui/attributes/malformed-never-type-options.stderr @@ -1,9 +1,3 @@ -error: expected unsuffixed literal, found `:` - --> $DIR/malformed-never-type-options.rs:4:29 - | -LL | #![rustc_never_type_options(: Unsize = "hi")] - | ^ - error[E0658]: use of an internal attribute --> $DIR/malformed-never-type-options.rs:4:1 | @@ -14,6 +8,12 @@ LL | #![rustc_never_type_options(: Unsize = "hi")] = note: the `#[rustc_never_type_options]` attribute is an internal implementation detail that will never be stable = note: `rustc_never_type_options` is used to experiment with never type fallback and work on never type stabilization +error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `:` + --> $DIR/malformed-never-type-options.rs:4:29 + | +LL | #![rustc_never_type_options(: Unsize = "hi")] + | ^ + error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0658`.