Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::borrow::Cow;

use rustc_hir::attrs::lang_items::LangItem;
use thin_vec::thin_vec;
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
22 changes: 11 additions & 11 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -742,15 +742,15 @@ 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,
)
} else {
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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(<expr>)`
Expand Down Expand Up @@ -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(
Expand Down
45 changes: 20 additions & 25 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -325,25 +326,19 @@ struct LoweringContext<'a, 'hir> {
attribute_parser: AttributeParser<'hir>,
}

macro_rules! allow {
($($name:ident: $list:expr;)*) => {
$( static $name: LazyLock<Arc<[Symbol]>> = 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 {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -1053,7 +1048,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&self,
reason: DesugaringKind,
span: Span,
allow_internal_unstable: Option<Arc<[Symbol]>>,
allow_internal_unstable: Option<Cow<'static, [Symbol]>>,
) -> Span {
self.tcx.with_stable_hashing_context(|hcx| {
span.mark_with_reason(allow_internal_unstable, reason, span.edition(), hcx)
Expand Down Expand Up @@ -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)))
}
};

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::borrow::Cow;

use rustc_ast::*;
use rustc_hir::attrs::lang_items::LangItem;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::borrow::Cow;

use rustc_ast::{self as ast, *};
use rustc_errors::StashKey;
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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<Arc<[Symbol]>>,
bound_modifier_allowed_features: Option<Cow<'static, [Symbol]>>,
) -> 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() {
Expand Down Expand Up @@ -507,7 +507,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&mut self,
data: &ParenthesizedArgs,
itctx: ImplTraitContext,
bound_modifier_allowed_features: Option<Arc<[Symbol]>>,
bound_modifier_allowed_features: Option<Cow<'static, [Symbol]>>,
) -> (GenericArgsCtor<'hir>, bool) {
// Switch to `PassThrough` mode for anonymous lifetimes; this
// means that we permit things like `&Ref<T>`, where `Ref` has
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_data_structures/src/stable_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,13 @@ impl<T: ?Sized + StableHash> StableHash for ::std::sync::Arc<T> {
}
}

impl<B: ?Sized + StableHash + ToOwned> StableHash for ::std::borrow::Cow<'_, B> {
#[inline]
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
(**self).stable_hash(hcx, hasher);
}
}

impl StableHash for str {
#[inline]
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::any::Any;
use std::borrow::Cow;
use std::default::Default;
use std::iter;
use std::path::PathBuf;
Expand Down Expand Up @@ -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<Arc<[Symbol]>>,
pub allow_internal_unstable: Option<Cow<'static, [Symbol]>>,
/// The macro's stability info.
pub stability: Option<Stability>,
/// The macro's deprecation info.
Expand Down Expand Up @@ -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::<Vec<_>>().into()),
.then(|| {
Cow::Owned(allow_internal_unstable.iter().map(|i| i.0).collect::<Vec<_>>())
}),
stability,
deprecation: find_attr!(
attrs,
Expand Down Expand Up @@ -1045,7 +1048,7 @@ pub trait ResolverExpand {
&mut self,
call_site: Span,
pass: AstPass,
features: &[Symbol],
features: &'static [Symbol],
parent_module_id: Option<NodeId>,
) -> LocalExpnId;

Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<NodeId>,
) -> LocalExpnId {
let parent_module = parent_module_id
Expand All @@ -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,
),
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -963,7 +963,7 @@ impl Span {
/// allowed inside this span.
pub fn mark_with_reason(
self,
allow_internal_unstable: Option<Arc<[Symbol]>>,
allow_internal_unstable: Option<Cow<'static, [Symbol]>>,
reason: DesugaringKind,
edition: Edition,
hcx: impl StableHashCtxt,
Expand Down Expand Up @@ -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<Arc<[Symbol]>>,
pub allow_internal_unstable: Option<Cow<'static, [Symbol]>>,
/// Edition of the crate in which the macro is defined.
pub edition: Edition,
/// The `DefId` of the macro being invoked,
Expand Down Expand Up @@ -1048,7 +1048,7 @@ impl ExpnData {
parent: ExpnId,
call_site: Span,
def_site: Span,
allow_internal_unstable: Option<Arc<[Symbol]>>,
allow_internal_unstable: Option<Cow<'static, [Symbol]>>,
edition: Edition,
macro_def_id: Option<DefId>,
parent_module: Option<ModId>,
Expand Down Expand Up @@ -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<DefId>,
parent_module: Option<ModId>,
) -> ExpnData {
Expand Down
Loading