diff --git a/compiler/rustc_ast_lowering/src/contract.rs b/compiler/rustc_ast_lowering/src/contract.rs index eaebff521cb68..2969db2d1d5a9 100644 --- a/compiler/rustc_ast_lowering/src/contract.rs +++ b/compiler/rustc_ast_lowering/src/contract.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::borrow::Cow; use rustc_hir::attrs::lang_items::LangItem; use thin_vec::thin_vec; @@ -143,7 +143,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let req_span = self.mark_span_with_reason( rustc_span::DesugaringKind::Contract, lowered_req.span, - Some(Arc::clone(&crate::ALLOW_CONTRACTS)), + Some(Cow::Borrowed(crate::ALLOW_CONTRACTS)), ); let precond = self.expr_call_lang_item_fn_mut( req_span, @@ -161,7 +161,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let ens_span = self.mark_span_with_reason( rustc_span::DesugaringKind::Contract, ens_span, - Some(Arc::clone(&crate::ALLOW_CONTRACTS)), + Some(Cow::Borrowed(crate::ALLOW_CONTRACTS)), ); let lowered_ens = self.lower_expr_mut(&ens); self.expr_call_lang_item_fn( diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 1c1b9a247f7a2..8de21291a63e7 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1,6 +1,6 @@ +use std::borrow::Cow; use std::mem; use std::ops::ControlFlow; -use std::sync::Arc; use rustc_ast::node_id::NodeMap; use rustc_ast::visit::{Visitor, walk_expr}; @@ -742,7 +742,7 @@ impl<'hir> LoweringContext<'_, 'hir> { this.mark_span_with_reason( DesugaringKind::TryBlock, expr.span, - Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)), + Some(Cow::Borrowed(crate::ALLOW_TRY_TRAIT)), ), expr, ) @@ -750,7 +750,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let try_span = this.mark_span_with_reason( DesugaringKind::TryBlock, this.tcx.sess.source_map().end_point(body.span), - Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)), + Some(Cow::Borrowed(crate::ALLOW_TRY_TRAIT)), ); (try_span, this.expr_unit(try_span)) @@ -878,7 +878,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let unstable_span = self.mark_span_with_reason( DesugaringKind::Async, self.lower_span(span), - Some(Arc::clone(self.allow_gen_future())), + Some(self.allow_gen_future()), ); let resume_ty = self.make_lang_item_qpath(LangItem::ResumeTy, unstable_span, None); let input_ty = hir::Ty { @@ -1037,15 +1037,15 @@ impl<'hir> LoweringContext<'_, 'hir> { }; let features = match await_kind { - FutureKind::Future if is_async_gen => Some(Arc::clone(&crate::ALLOW_ASYNC_GEN)), + FutureKind::Future if is_async_gen => Some(Cow::Borrowed(crate::ALLOW_ASYNC_GEN)), FutureKind::Future => None, - FutureKind::AsyncIterator => Some(Arc::clone(&crate::ALLOW_FOR_AWAIT)), + FutureKind::AsyncIterator => Some(Cow::Borrowed(crate::ALLOW_FOR_AWAIT)), }; let span = self.mark_span_with_reason(DesugaringKind::Await, await_kw_span, features); let gen_future_span = self.mark_span_with_reason( DesugaringKind::Await, full_span, - Some(Arc::clone(self.allow_gen_future())), + Some(self.allow_gen_future()), ); let expr_hir_id = expr.hir_id; @@ -1727,7 +1727,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let desugar_span = self.mark_span_with_reason( DesugaringKind::Async, span, - Some(Arc::clone(&crate::ALLOW_ASYNC_GEN)), + Some(Cow::Borrowed(crate::ALLOW_ASYNC_GEN)), ); let wrapped_yielded = self.expr_call_lang_item_fn( desugar_span, @@ -1946,13 +1946,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let unstable_span = self.mark_span_with_reason( DesugaringKind::QuestionMark, span, - Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)), + Some(Cow::Borrowed(crate::ALLOW_TRY_TRAIT)), ); let try_span = self.tcx.sess.source_map().end_point(span); let try_span = self.mark_span_with_reason( DesugaringKind::QuestionMark, try_span, - Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)), + Some(Cow::Borrowed(crate::ALLOW_TRY_TRAIT)), ); // `Try::branch()` @@ -2049,7 +2049,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let unstable_span = self.mark_span_with_reason( DesugaringKind::YeetExpr, span, - Some(Arc::clone(&crate::ALLOW_TRY_TRAIT)), + Some(Cow::Borrowed(crate::ALLOW_TRY_TRAIT)), ); let from_yeet_expr = self.wrap_in_try_constructor( diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 19c37f4a76065..561a979171cc7 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -38,8 +38,9 @@ #![recursion_limit = "256"] // tidy-alphabetical-end +use std::borrow::Cow; use std::mem; -use std::sync::{Arc, LazyLock}; +use std::sync::Arc; use rustc_ast::mut_visit::{self, MutVisitor}; use rustc_ast::node_id::NodeMap; @@ -325,25 +326,19 @@ struct LoweringContext<'a, 'hir> { attribute_parser: AttributeParser<'hir>, } -macro_rules! allow { - ($($name:ident: $list:expr;)*) => { - $( static $name: LazyLock> = LazyLock::new(|| $list.into()); )* - } -} - -allow! { - ALLOW_CONTRACTS: [sym::contracts_internals]; - ALLOW_TRY_TRAIT: [sym::try_trait_v2, sym::try_trait_v2_residual, sym::yeet_desugar_details]; - ALLOW_PATTERN_TYPE: [sym::pattern_types, sym::pattern_type_range_trait]; - ALLOW_GEN_FUTURE: [sym::gen_future]; - ALLOW_GEN_FUTURE_WITH_ASYNC_FN_TRACK_CALLER: [sym::gen_future, sym::closure_track_caller]; - ALLOW_FOR_AWAIT: [sym::async_gen_internals, sym::async_iterator]; - ALLOW_ASYNC_FN_TRAITS: [sym::async_fn_traits]; - ALLOW_ASYNC_GEN: [sym::async_gen_internals]; - // FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller` - // interact with `gen`/`async gen` blocks - ALLOW_ASYNC_ITERATOR: [sym::gen_future, sym::async_iterator]; -} +const ALLOW_CONTRACTS: &[Symbol] = &[sym::contracts_internals]; +const ALLOW_TRY_TRAIT: &[Symbol] = + &[sym::try_trait_v2, sym::try_trait_v2_residual, sym::yeet_desugar_details]; +const ALLOW_PATTERN_TYPE: &[Symbol] = &[sym::pattern_types, sym::pattern_type_range_trait]; +const ALLOW_GEN_FUTURE: &[Symbol] = &[sym::gen_future]; +const ALLOW_GEN_FUTURE_WITH_ASYNC_FN_TRACK_CALLER: &[Symbol] = + &[sym::gen_future, sym::closure_track_caller]; +const ALLOW_FOR_AWAIT: &[Symbol] = &[sym::async_gen_internals, sym::async_iterator]; +const ALLOW_ASYNC_FN_TRAITS: &[Symbol] = &[sym::async_fn_traits]; +const ALLOW_ASYNC_GEN: &[Symbol] = &[sym::async_gen_internals]; +// FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller` +// interact with `gen`/`async gen` blocks +const ALLOW_ASYNC_ITERATOR: &[Symbol] = &[sym::gen_future, sym::async_iterator]; impl<'a, 'hir> LoweringContext<'a, 'hir> { fn new(tcx: TyCtxt<'hir>, resolver: &'a ResolverAstLowering<'hir>, owner: NodeId) -> Self { @@ -383,11 +378,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.tcx.dcx() } - fn allow_gen_future(&self) -> &Arc<[Symbol]> { + fn allow_gen_future(&self) -> Cow<'static, [Symbol]> { if self.tcx.features().async_fn_track_caller() { - &ALLOW_GEN_FUTURE_WITH_ASYNC_FN_TRACK_CALLER + Cow::Borrowed(ALLOW_GEN_FUTURE_WITH_ASYNC_FN_TRACK_CALLER) } else { - &ALLOW_GEN_FUTURE + Cow::Borrowed(ALLOW_GEN_FUTURE) } } } @@ -1053,7 +1048,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &self, reason: DesugaringKind, span: Span, - allow_internal_unstable: Option>, + allow_internal_unstable: Option>, ) -> Span { self.tcx.with_stable_hashing_context(|hcx| { span.mark_with_reason(allow_internal_unstable, reason, span.edition(), hcx) @@ -2095,7 +2090,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let (opaque_ty_node_id, allowed_features) = match coro.kind { CoroutineKind::Async | CoroutineKind::Gen => (coro.return_impl_trait_id, None), CoroutineKind::AsyncGen => { - (coro.return_impl_trait_id, Some(Arc::clone(&ALLOW_ASYNC_ITERATOR))) + (coro.return_impl_trait_id, Some(Cow::Borrowed(ALLOW_ASYNC_ITERATOR))) } }; diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index 0edba3fe0cd14..d2b0b2e994ab5 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::borrow::Cow; use rustc_ast::*; use rustc_hir::attrs::lang_items::LangItem; @@ -482,7 +482,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let unstable_span = self.mark_span_with_reason( DesugaringKind::PatTyRange, span, - Some(Arc::clone(&crate::ALLOW_PATTERN_TYPE)), + Some(Cow::Borrowed(crate::ALLOW_PATTERN_TYPE)), ); let anon_const = self.with_new_scopes(span, |this| { let def_id = this.local_def_id(e.id); @@ -535,7 +535,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let unstable_span = self.mark_span_with_reason( DesugaringKind::PatTyRange, self.lower_span(span), - Some(Arc::clone(&crate::ALLOW_PATTERN_TYPE)), + Some(Cow::Borrowed(crate::ALLOW_PATTERN_TYPE)), ); let span = self.lower_span(base_type); diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index 14f10e2b1c416..01e42437521cf 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::borrow::Cow; use rustc_ast::{self as ast, *}; use rustc_errors::StashKey; @@ -75,7 +75,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let bound_modifier_allowed_features = if let Res::Def(DefKind::Trait, async_def_id) = res && self.tcx.async_fn_trait_kind_from_def_id(async_def_id).is_some() { - Some(Arc::clone(&crate::ALLOW_ASYNC_FN_TRAITS)) + Some(Cow::Borrowed(crate::ALLOW_ASYNC_FN_TRAITS)) } else { None }; @@ -260,7 +260,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // Additional features ungated with a bound modifier like `async`. // This is passed down to the implicit associated type binding in // parenthesized bounds. - bound_modifier_allowed_features: Option>, + bound_modifier_allowed_features: Option>, ) -> hir::PathSegment<'hir> { debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment); let (mut generic_args, infer_args) = if let Some(generic_args) = segment.args.as_deref() { @@ -507,7 +507,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &mut self, data: &ParenthesizedArgs, itctx: ImplTraitContext, - bound_modifier_allowed_features: Option>, + bound_modifier_allowed_features: Option>, ) -> (GenericArgsCtor<'hir>, bool) { // Switch to `PassThrough` mode for anonymous lifetimes; this // means that we permit things like `&Ref`, where `Ref` has diff --git a/compiler/rustc_data_structures/src/stable_hash.rs b/compiler/rustc_data_structures/src/stable_hash.rs index 0513c90831c8c..00e4de492a028 100644 --- a/compiler/rustc_data_structures/src/stable_hash.rs +++ b/compiler/rustc_data_structures/src/stable_hash.rs @@ -428,6 +428,13 @@ impl StableHash for ::std::sync::Arc { } } +impl StableHash for ::std::borrow::Cow<'_, B> { + #[inline] + fn stable_hash(&self, hcx: &mut Hcx, hasher: &mut StableHasher) { + (**self).stable_hash(hcx, hasher); + } +} + impl StableHash for str { #[inline] fn stable_hash(&self, hcx: &mut Hcx, hasher: &mut StableHasher) { diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index fda75319b087b..f49340d6e0c24 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -1,4 +1,5 @@ use std::any::Any; +use std::borrow::Cow; use std::default::Default; use std::iter; use std::path::PathBuf; @@ -777,7 +778,7 @@ pub struct SyntaxExtension { /// Span of the macro definition. pub span: Span, /// List of unstable features that are treated as stable inside this macro. - pub allow_internal_unstable: Option>, + pub allow_internal_unstable: Option>, /// The macro's stability info. pub stability: Option, /// The macro's deprecation info. @@ -913,7 +914,9 @@ impl SyntaxExtension { span, allow_internal_unstable: (!allow_internal_unstable.is_empty()) // FIXME(jdonszelmann): avoid the into_iter/collect? - .then(|| allow_internal_unstable.iter().map(|i| i.0).collect::>().into()), + .then(|| { + Cow::Owned(allow_internal_unstable.iter().map(|i| i.0).collect::>()) + }), stability, deprecation: find_attr!( attrs, @@ -1045,7 +1048,7 @@ pub trait ResolverExpand { &mut self, call_site: Span, pass: AstPass, - features: &[Symbol], + features: &'static [Symbol], parent_module_id: Option, ) -> LocalExpnId; diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 24a52d4143493..137491dfafe7c 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -1,6 +1,7 @@ //! A bunch of methods and structures more or less related to resolving macros and //! interface provided by `Resolver` to macro expander. +use std::borrow::Cow; use std::mem; use std::sync::Arc; @@ -226,7 +227,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { &mut self, call_site: Span, pass: AstPass, - features: &[Symbol], + features: &'static [Symbol], parent_module_id: Option, ) -> LocalExpnId { let parent_module = parent_module_id @@ -237,7 +238,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { ExpnKind::AstPass(pass), call_site, self.tcx.sess.edition(), - features.into(), + Cow::Borrowed(features), None, parent_module, ), diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index a081bd1b65e92..768464d8527e9 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -24,9 +24,9 @@ // because getting it wrong can lead to nested `HygieneData::with` calls that // trigger runtime aborts. (Fortunately these are obvious and easy to fix.) +use std::borrow::Cow; use std::cell::RefCell; use std::hash::Hash; -use std::sync::Arc; use std::{fmt, iter, mem}; use rustc_data_structures::fingerprint::Fingerprint; @@ -963,7 +963,7 @@ impl Span { /// allowed inside this span. pub fn mark_with_reason( self, - allow_internal_unstable: Option>, + allow_internal_unstable: Option>, reason: DesugaringKind, edition: Edition, hcx: impl StableHashCtxt, @@ -1019,7 +1019,7 @@ pub struct ExpnData { /// List of `#[unstable]`/feature-gated features that the macro is allowed to use /// internally without forcing the whole crate to opt-in /// to them. - pub allow_internal_unstable: Option>, + pub allow_internal_unstable: Option>, /// Edition of the crate in which the macro is defined. pub edition: Edition, /// The `DefId` of the macro being invoked, @@ -1048,7 +1048,7 @@ impl ExpnData { parent: ExpnId, call_site: Span, def_site: Span, - allow_internal_unstable: Option>, + allow_internal_unstable: Option>, edition: Edition, macro_def_id: Option, parent_module: Option, @@ -1103,7 +1103,7 @@ impl ExpnData { kind: ExpnKind, call_site: Span, edition: Edition, - allow_internal_unstable: Arc<[Symbol]>, + allow_internal_unstable: Cow<'static, [Symbol]>, macro_def_id: Option, parent_module: Option, ) -> ExpnData {