Get rid of static predefined symbols

Make them all `const`.
This commit is contained in:
Chayim Refael Friedman 2025-04-21 03:07:35 +03:00
parent 2f2cff19f8
commit 9477e46bec
24 changed files with 120 additions and 112 deletions

View file

@ -117,17 +117,20 @@ impl Attrs {
}
impl Attrs {
pub fn by_key<'attrs>(&'attrs self, key: &'attrs Symbol) -> AttrQuery<'attrs> {
#[inline]
pub fn by_key(&self, key: Symbol) -> AttrQuery<'_> {
AttrQuery { attrs: self, key }
}
#[inline]
pub fn rust_analyzer_tool(&self) -> impl Iterator<Item = &Attr> {
self.iter()
.filter(|&attr| attr.path.segments().first().is_some_and(|s| *s == sym::rust_analyzer))
}
#[inline]
pub fn cfg(&self) -> Option<CfgExpr> {
let mut cfgs = self.by_key(&sym::cfg).tt_values().map(CfgExpr::parse);
let mut cfgs = self.by_key(sym::cfg).tt_values().map(CfgExpr::parse);
let first = cfgs.next()?;
match cfgs.next() {
Some(second) => {
@ -138,10 +141,12 @@ impl Attrs {
}
}
#[inline]
pub fn cfgs(&self) -> impl Iterator<Item = CfgExpr> + '_ {
self.by_key(&sym::cfg).tt_values().map(CfgExpr::parse)
self.by_key(sym::cfg).tt_values().map(CfgExpr::parse)
}
#[inline]
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
match self.cfg() {
None => true,
@ -149,52 +154,63 @@ impl Attrs {
}
}
#[inline]
pub fn lang(&self) -> Option<&Symbol> {
self.by_key(&sym::lang).string_value()
self.by_key(sym::lang).string_value()
}
#[inline]
pub fn lang_item(&self) -> Option<LangItem> {
self.by_key(&sym::lang).string_value().and_then(LangItem::from_symbol)
self.by_key(sym::lang).string_value().and_then(LangItem::from_symbol)
}
#[inline]
pub fn has_doc_hidden(&self) -> bool {
self.by_key(&sym::doc).tt_values().any(|tt| {
self.by_key(sym::doc).tt_values().any(|tt| {
tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::hidden)
})
}
#[inline]
pub fn has_doc_notable_trait(&self) -> bool {
self.by_key(&sym::doc).tt_values().any(|tt| {
self.by_key(sym::doc).tt_values().any(|tt| {
tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::notable_trait)
})
}
#[inline]
pub fn doc_exprs(&self) -> impl Iterator<Item = DocExpr> + '_ {
self.by_key(&sym::doc).tt_values().map(DocExpr::parse)
self.by_key(sym::doc).tt_values().map(DocExpr::parse)
}
#[inline]
pub fn doc_aliases(&self) -> impl Iterator<Item = Symbol> + '_ {
self.doc_exprs().flat_map(|doc_expr| doc_expr.aliases().to_vec())
}
#[inline]
pub fn export_name(&self) -> Option<&Symbol> {
self.by_key(&sym::export_name).string_value()
self.by_key(sym::export_name).string_value()
}
#[inline]
pub fn is_proc_macro(&self) -> bool {
self.by_key(&sym::proc_macro).exists()
self.by_key(sym::proc_macro).exists()
}
#[inline]
pub fn is_proc_macro_attribute(&self) -> bool {
self.by_key(&sym::proc_macro_attribute).exists()
self.by_key(sym::proc_macro_attribute).exists()
}
#[inline]
pub fn is_proc_macro_derive(&self) -> bool {
self.by_key(&sym::proc_macro_derive).exists()
self.by_key(sym::proc_macro_derive).exists()
}
#[inline]
pub fn is_test(&self) -> bool {
self.iter().any(|it| {
it.path()
@ -210,20 +226,24 @@ impl Attrs {
})
}
#[inline]
pub fn is_ignore(&self) -> bool {
self.by_key(&sym::ignore).exists()
self.by_key(sym::ignore).exists()
}
#[inline]
pub fn is_bench(&self) -> bool {
self.by_key(&sym::bench).exists()
self.by_key(sym::bench).exists()
}
#[inline]
pub fn is_unstable(&self) -> bool {
self.by_key(&sym::unstable).exists()
self.by_key(sym::unstable).exists()
}
#[inline]
pub fn rustc_legacy_const_generics(&self) -> Option<Box<Box<[u32]>>> {
self.by_key(&sym::rustc_legacy_const_generics)
self.by_key(sym::rustc_legacy_const_generics)
.tt_values()
.next()
.map(parse_rustc_legacy_const_generics)
@ -231,8 +251,9 @@ impl Attrs {
.map(Box::new)
}
#[inline]
pub fn repr(&self) -> Option<ReprOptions> {
self.by_key(&sym::repr).tt_values().filter_map(parse_repr_tt).fold(None, |acc, repr| {
self.by_key(sym::repr).tt_values().filter_map(parse_repr_tt).fold(None, |acc, repr| {
acc.map_or(Some(repr), |mut acc| {
merge_repr(&mut acc, repr);
Some(acc)
@ -681,36 +702,42 @@ impl AttrSourceMap {
}
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct AttrQuery<'attr> {
attrs: &'attr Attrs,
key: &'attr Symbol,
key: Symbol,
}
impl<'attr> AttrQuery<'attr> {
#[inline]
pub fn tt_values(self) -> impl Iterator<Item = &'attr crate::tt::TopSubtree> {
self.attrs().filter_map(|attr| attr.token_tree_value())
}
#[inline]
pub fn string_value(self) -> Option<&'attr Symbol> {
self.attrs().find_map(|attr| attr.string_value())
}
#[inline]
pub fn string_value_with_span(self) -> Option<(&'attr Symbol, span::Span)> {
self.attrs().find_map(|attr| attr.string_value_with_span())
}
#[inline]
pub fn string_value_unescape(self) -> Option<Cow<'attr, str>> {
self.attrs().find_map(|attr| attr.string_value_unescape())
}
#[inline]
pub fn exists(self) -> bool {
self.attrs().next().is_some()
}
#[inline]
pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
let key = self.key;
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == *key))
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == key))
}
/// Find string value for a specific key inside token tree
@ -719,10 +746,11 @@ impl<'attr> AttrQuery<'attr> {
/// #[doc(html_root_url = "url")]
/// ^^^^^^^^^^^^^ key
/// ```
pub fn find_string_value_in_tt(self, key: &'attr Symbol) -> Option<&'attr str> {
#[inline]
pub fn find_string_value_in_tt(self, key: Symbol) -> Option<&'attr str> {
self.tt_values().find_map(|tt| {
let name = tt.iter()
.skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == *key))
.skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == key))
.nth(2);
match name {

View file

@ -1712,7 +1712,7 @@ impl ModCollector<'_, '_> {
id: ItemTreeId::new(self.tree_id, item_tree_id),
}
.intern(db);
let is_prelude = attrs.by_key(&sym::prelude_import).exists();
let is_prelude = attrs.by_key(sym::prelude_import).exists();
Import::from_use(
self.item_tree,
ItemTreeId::new(self.tree_id, item_tree_id),
@ -1770,7 +1770,7 @@ impl ModCollector<'_, '_> {
if !is_self {
self.process_macro_use_extern_crate(
id,
attrs.by_key(&sym::macro_use).attrs(),
attrs.by_key(sym::macro_use).attrs(),
resolved.krate,
);
}
@ -2015,8 +2015,8 @@ impl ModCollector<'_, '_> {
}
fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
let path_attr = attrs.by_key(&sym::path).string_value_unescape();
let is_macro_use = attrs.by_key(&sym::macro_use).exists();
let path_attr = attrs.by_key(sym::path).string_value_unescape();
let is_macro_use = attrs.by_key(sym::macro_use).exists();
let module = &self.item_tree[module_id];
match &module.kind {
// inline module, just recurse
@ -2093,7 +2093,7 @@ impl ModCollector<'_, '_> {
let is_macro_use = is_macro_use
|| item_tree
.top_level_attrs(db, krate)
.by_key(&sym::macro_use)
.by_key(sym::macro_use)
.exists();
if is_macro_use {
self.import_all_legacy_macros(module_id);
@ -2252,11 +2252,11 @@ impl ModCollector<'_, '_> {
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
let export_attr = attrs.by_key(&sym::macro_export);
let export_attr = || attrs.by_key(sym::macro_export);
let is_export = export_attr.exists();
let is_export = export_attr().exists();
let local_inner = if is_export {
export_attr.tt_values().flat_map(|it| it.iter()).any(|it| match it {
export_attr().tt_values().flat_map(|it| it.iter()).any(|it| match it {
tt::TtElement::Leaf(tt::Leaf::Ident(ident)) => ident.sym == sym::local_inner_macros,
_ => false,
})
@ -2265,17 +2265,17 @@ impl ModCollector<'_, '_> {
};
// Case 1: builtin macros
let expander = if attrs.by_key(&sym::rustc_builtin_macro).exists() {
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
// `#[rustc_builtin_macro = "builtin_name"]` overrides the `macro_rules!` name.
let name;
let name = match attrs.by_key(&sym::rustc_builtin_macro).string_value_with_span() {
let name = match attrs.by_key(sym::rustc_builtin_macro).string_value_with_span() {
Some((it, span)) => {
name = Name::new_symbol(it.clone(), span.ctx);
&name
}
None => {
let explicit_name =
attrs.by_key(&sym::rustc_builtin_macro).tt_values().next().and_then(|tt| {
attrs.by_key(sym::rustc_builtin_macro).tt_values().next().and_then(|tt| {
match tt.token_trees().flat_tokens().first() {
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
_ => None,
@ -2305,7 +2305,7 @@ impl ModCollector<'_, '_> {
// Case 2: normal `macro_rules!` macro
MacroExpander::Declarative
};
let allow_internal_unsafe = attrs.by_key(&sym::allow_internal_unsafe).exists();
let allow_internal_unsafe = attrs.by_key(sym::allow_internal_unsafe).exists();
let mut flags = MacroRulesLocFlags::empty();
flags.set(MacroRulesLocFlags::LOCAL_INNER, local_inner);
@ -2339,14 +2339,14 @@ impl ModCollector<'_, '_> {
// Case 1: builtin macros
let mut helpers_opt = None;
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
let expander = if attrs.by_key(&sym::rustc_builtin_macro).exists() {
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
if let Some(expander) = find_builtin_macro(&mac.name) {
match expander {
Either::Left(it) => MacroExpander::BuiltIn(it),
Either::Right(it) => MacroExpander::BuiltInEager(it),
}
} else if let Some(expander) = find_builtin_derive(&mac.name) {
if let Some(attr) = attrs.by_key(&sym::rustc_builtin_macro).tt_values().next() {
if let Some(attr) = attrs.by_key(sym::rustc_builtin_macro).tt_values().next() {
// NOTE: The item *may* have both `#[rustc_builtin_macro]` and `#[proc_macro_derive]`,
// in which case rustc ignores the helper attributes from the latter, but it
// "doesn't make sense in practice" (see rust-lang/rust#87027).
@ -2377,7 +2377,7 @@ impl ModCollector<'_, '_> {
// Case 2: normal `macro`
MacroExpander::Declarative
};
let allow_internal_unsafe = attrs.by_key(&sym::allow_internal_unsafe).exists();
let allow_internal_unsafe = attrs.by_key(sym::allow_internal_unsafe).exists();
let macro_id = Macro2Loc {
container: module,

View file

@ -35,7 +35,7 @@ impl Attrs {
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Bang })
} else if self.is_proc_macro_attribute() {
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Attr })
} else if self.by_key(&sym::proc_macro_derive).exists() {
} else if self.by_key(sym::proc_macro_derive).exists() {
let derive = self.parse_proc_macro_derive();
Some(match derive {
Some((name, helpers)) => {
@ -52,12 +52,12 @@ impl Attrs {
}
pub fn parse_proc_macro_derive(&self) -> Option<(Name, Box<[Name]>)> {
let derive = self.by_key(&sym::proc_macro_derive).tt_values().next()?;
let derive = self.by_key(sym::proc_macro_derive).tt_values().next()?;
parse_macro_name_and_helper_attrs(derive)
}
pub fn parse_rustc_builtin_macro(&self) -> Option<(Name, Box<[Name]>)> {
let derive = self.by_key(&sym::rustc_builtin_macro).tt_values().next()?;
let derive = self.by_key(sym::rustc_builtin_macro).tt_values().next()?;
parse_macro_name_and_helper_attrs(derive)
}
}

View file

@ -72,10 +72,10 @@ impl StructSignature {
let attrs = item_tree.attrs(db, loc.container.krate, ModItem::from(loc.id.value).into());
let mut flags = StructFlags::empty();
if attrs.by_key(&sym::rustc_has_incoherent_inherent_impls).exists() {
if attrs.by_key(sym::rustc_has_incoherent_inherent_impls).exists() {
flags |= StructFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS;
}
if attrs.by_key(&sym::fundamental).exists() {
if attrs.by_key(sym::fundamental).exists() {
flags |= StructFlags::FUNDAMENTAL;
}
if let Some(lang) = attrs.lang_item() {
@ -128,10 +128,10 @@ impl UnionSignature {
let item_tree = loc.id.item_tree(db);
let attrs = item_tree.attrs(db, krate, ModItem::from(loc.id.value).into());
let mut flags = StructFlags::empty();
if attrs.by_key(&sym::rustc_has_incoherent_inherent_impls).exists() {
if attrs.by_key(sym::rustc_has_incoherent_inherent_impls).exists() {
flags |= StructFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS;
}
if attrs.by_key(&sym::fundamental).exists() {
if attrs.by_key(sym::fundamental).exists() {
flags |= StructFlags::FUNDAMENTAL;
}
@ -181,7 +181,7 @@ impl EnumSignature {
let item_tree = loc.id.item_tree(db);
let attrs = item_tree.attrs(db, loc.container.krate, ModItem::from(loc.id.value).into());
let mut flags = EnumFlags::empty();
if attrs.by_key(&sym::rustc_has_incoherent_inherent_impls).exists() {
if attrs.by_key(sym::rustc_has_incoherent_inherent_impls).exists() {
flags |= EnumFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS;
}
@ -241,7 +241,7 @@ impl ConstSignature {
let module = loc.container.module(db);
let attrs = item_tree.attrs(db, module.krate, ModItem::from(loc.id.value).into());
let mut flags = ConstFlags::empty();
if attrs.by_key(&sym::rustc_allow_incoherent_impl).exists() {
if attrs.by_key(sym::rustc_allow_incoherent_impl).exists() {
flags |= ConstFlags::RUSTC_ALLOW_INCOHERENT_IMPL;
}
let source = loc.source(db);
@ -297,7 +297,7 @@ impl StaticSignature {
let module = loc.container.module(db);
let attrs = item_tree.attrs(db, module.krate, ModItem::from(loc.id.value).into());
let mut flags = StaticFlags::empty();
if attrs.by_key(&sym::rustc_allow_incoherent_impl).exists() {
if attrs.by_key(sym::rustc_allow_incoherent_impl).exists() {
flags |= StaticFlags::RUSTC_ALLOW_INCOHERENT_IMPL;
}
@ -415,19 +415,19 @@ impl TraitSignature {
if source.value.unsafe_token().is_some() {
flags.insert(TraitFlags::UNSAFE);
}
if attrs.by_key(&sym::fundamental).exists() {
if attrs.by_key(sym::fundamental).exists() {
flags |= TraitFlags::FUNDAMENTAL;
}
if attrs.by_key(&sym::rustc_has_incoherent_inherent_impls).exists() {
if attrs.by_key(sym::rustc_has_incoherent_inherent_impls).exists() {
flags |= TraitFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS;
}
if attrs.by_key(&sym::rustc_paren_sugar).exists() {
if attrs.by_key(sym::rustc_paren_sugar).exists() {
flags |= TraitFlags::RUSTC_PAREN_SUGAR;
}
let mut skip_array_during_method_dispatch =
attrs.by_key(&sym::rustc_skip_array_during_method_dispatch).exists();
attrs.by_key(sym::rustc_skip_array_during_method_dispatch).exists();
let mut skip_boxed_slice_during_method_dispatch = false;
for tt in attrs.by_key(&sym::rustc_skip_during_method_dispatch).tt_values() {
for tt in attrs.by_key(sym::rustc_skip_during_method_dispatch).tt_values() {
for tt in tt.iter() {
if let tt::iter::TtElement::Leaf(tt::Leaf::Ident(ident)) = tt {
skip_array_during_method_dispatch |= ident.sym == sym::array;
@ -531,11 +531,11 @@ impl FunctionSignature {
let mut flags = FnFlags::empty();
let attrs = item_tree.attrs(db, module.krate, ModItem::from(loc.id.value).into());
if attrs.by_key(&sym::rustc_allow_incoherent_impl).exists() {
if attrs.by_key(sym::rustc_allow_incoherent_impl).exists() {
flags.insert(FnFlags::RUSTC_ALLOW_INCOHERENT_IMPL);
}
if attrs.by_key(&sym::target_feature).exists() {
if attrs.by_key(sym::target_feature).exists() {
flags.insert(FnFlags::HAS_TARGET_FEATURE);
}
let legacy_const_generics_indices = attrs.rustc_legacy_const_generics();
@ -543,7 +543,7 @@ impl FunctionSignature {
let source = loc.source(db);
if source.value.unsafe_token().is_some() {
if attrs.by_key(&sym::rustc_deprecated_safe_2024).exists() {
if attrs.by_key(sym::rustc_deprecated_safe_2024).exists() {
flags.insert(FnFlags::DEPRECATED_SAFE_2024);
} else {
flags.insert(FnFlags::UNSAFE);
@ -668,10 +668,10 @@ impl TypeAliasSignature {
loc.container.module(db).krate(),
ModItem::from(loc.id.value).into(),
);
if attrs.by_key(&sym::rustc_has_incoherent_inherent_impls).exists() {
if attrs.by_key(sym::rustc_has_incoherent_inherent_impls).exists() {
flags.insert(TypeAliasFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPL);
}
if attrs.by_key(&sym::rustc_allow_incoherent_impl).exists() {
if attrs.by_key(sym::rustc_allow_incoherent_impl).exists() {
flags.insert(TypeAliasFlags::RUSTC_ALLOW_INCOHERENT_IMPL);
}
if matches!(loc.container, ItemContainerId::ExternBlockId(_)) {

View file

@ -142,7 +142,7 @@ impl Name {
/// name is equal only to itself. It's not clear how to implement this in
/// salsa though, so we punt on that bit for a moment.
pub const fn missing() -> Name {
Name { symbol: sym::consts::MISSING_NAME, ctx: () }
Name { symbol: sym::MISSING_NAME, ctx: () }
}
/// Returns true if this is a fake name for things missing in the source code. See

View file

@ -201,7 +201,7 @@ impl<'a> DeclValidator<'a> {
// Don't run the lint on extern "[not Rust]" fn items with the
// #[no_mangle] attribute.
let no_mangle = self.db.attrs(func.into()).by_key(&sym::no_mangle).exists();
let no_mangle = self.db.attrs(func.into()).by_key(sym::no_mangle).exists();
if no_mangle && data.abi.as_ref().is_some_and(|abi| *abi != sym::Rust) {
cov_mark::hit!(extern_func_no_mangle_ignored);
} else {

View file

@ -106,7 +106,7 @@ impl<'db> MatchCheckCtx<'db> {
/// Returns whether the given ADT is from another crate declared `#[non_exhaustive]`.
fn is_foreign_non_exhaustive(&self, adt: hir_def::AdtId) -> bool {
let is_local = adt.krate(self.db) == self.module.krate();
!is_local && self.db.attrs(adt.into()).by_key(&sym::non_exhaustive).exists()
!is_local && self.db.attrs(adt.into()).by_key(sym::non_exhaustive).exists()
}
fn variant_id_for_adt(

View file

@ -2314,8 +2314,8 @@ impl HirDisplayWithExpressionStore for Path {
let name = crate_data
.display_name
.as_ref()
.map(|name| name.canonical_name())
.unwrap_or(&sym::dollar_crate);
.map(|name| (*name.canonical_name()).clone())
.unwrap_or(sym::dollar_crate);
write!(f, "{name}")?
}
}

View file

@ -125,10 +125,7 @@ fn layout_scalar_valid_range(db: &dyn HirDatabase, def: AdtId) -> (Bound<u128>,
}
Bound::Unbounded
};
(
get(&sym::rustc_layout_scalar_valid_range_start),
get(&sym::rustc_layout_scalar_valid_range_end),
)
(get(sym::rustc_layout_scalar_valid_range_start), get(sym::rustc_layout_scalar_valid_range_end))
}
pub(crate) fn layout_of_adt_recover(

View file

@ -197,7 +197,7 @@ impl TraitImpls {
// FIXME: Reservation impls should be considered during coherence checks. If we are
// (ever) to implement coherence checks, this filtering should be done by the trait
// solver.
if db.attrs(impl_id.into()).by_key(&sym::rustc_reservation_impl).exists() {
if db.attrs(impl_id.into()).by_key(sym::rustc_reservation_impl).exists() {
continue;
}
let target_trait = match db.impl_trait(impl_id) {

View file

@ -59,7 +59,7 @@ impl Evaluator<'_> {
let function_data = self.db.function_signature(def);
let attrs = self.db.attrs(def.into());
let is_intrinsic = attrs.by_key(&sym::rustc_intrinsic).exists()
let is_intrinsic = attrs.by_key(sym::rustc_intrinsic).exists()
// Keep this around for a bit until extern "rustc-intrinsic" abis are no longer used
|| (match &function_data.abi {
Some(abi) => *abi == sym::rust_dash_intrinsic,
@ -82,7 +82,7 @@ impl Evaluator<'_> {
locals,
span,
!function_data.has_body()
|| attrs.by_key(&sym::rustc_intrinsic_must_be_overridden).exists(),
|| attrs.by_key(sym::rustc_intrinsic_must_be_overridden).exists(),
);
}
let is_extern_c = match def.lookup(self.db).container {
@ -299,7 +299,7 @@ impl Evaluator<'_> {
use LangItem::*;
let attrs = self.db.attrs(def.into());
if attrs.by_key(&sym::rustc_const_panic_str).exists() {
if attrs.by_key(sym::rustc_const_panic_str).exists() {
// `#[rustc_const_panic_str]` is treated like `lang = "begin_panic"` by rustc CTFE.
return Some(LangItem::BeginPanic);
}

View file

@ -36,7 +36,7 @@ impl TargetFeatures {
/// Retrieves the target features from the attributes, and does not expand the target features implied by them.
pub(crate) fn from_attrs_no_implications(attrs: &Attrs) -> Self {
let enabled = attrs
.by_key(&sym::target_feature)
.by_key(sym::target_feature)
.tt_values()
.filter_map(|tt| match tt.token_trees().flat_tokens() {
[

View file

@ -305,7 +305,7 @@ pub fn is_fn_unsafe_to_call(
if is_intrinsic_block {
// legacy intrinsics
// extern "rust-intrinsic" intrinsics are unsafe unless they have the rustc_safe_intrinsic attribute
if db.attrs(func.into()).by_key(&sym::rustc_safe_intrinsic).exists() {
if db.attrs(func.into()).by_key(sym::rustc_safe_intrinsic).exists() {
Unsafety::Safe
} else {
Unsafety::Unsafe

View file

@ -274,7 +274,7 @@ impl Crate {
pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> {
// Look for #![doc(html_root_url = "...")]
let attrs = db.attrs(AttrDefId::ModuleId(self.root_module().into()));
let doc_url = attrs.by_key(&sym::doc).find_string_value_in_tt(&sym::html_root_url);
let doc_url = attrs.by_key(sym::doc).find_string_value_in_tt(sym::html_root_url);
doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/")
}
@ -799,7 +799,7 @@ impl Module {
))
});
let res = type_params.chain(lifetime_params).any(|p| {
db.attrs(AttrDefId::GenericParamId(p)).by_key(&sym::may_dangle).exists()
db.attrs(AttrDefId::GenericParamId(p)).by_key(sym::may_dangle).exists()
});
Some(res)
})()
@ -3160,7 +3160,7 @@ impl Macro {
}
pub fn is_macro_export(self, db: &dyn HirDatabase) -> bool {
matches!(self.id, MacroId::MacroRulesId(_) if db.attrs(self.id.into()).by_key(&sym::macro_export).exists())
matches!(self.id, MacroId::MacroRulesId(_) if db.attrs(self.id.into()).by_key(sym::macro_export).exists())
}
pub fn is_proc_macro(self) -> bool {

View file

@ -386,7 +386,7 @@ impl ExtendedEnum {
fn is_non_exhaustive(self, db: &RootDatabase, krate: Crate) -> bool {
match self {
ExtendedEnum::Enum(e) => {
e.attrs(db).by_key(&sym::non_exhaustive).exists() && e.module(db).krate() != krate
e.attrs(db).by_key(sym::non_exhaustive).exists() && e.module(db).krate() != krate
}
_ => false,
}

View file

@ -93,7 +93,7 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
let kind = struct_type.kind(ctx.db());
let struct_def_path = module.find_path(ctx.db(), struct_def, cfg)?;
let is_non_exhaustive = struct_def.attrs(ctx.db())?.by_key(&sym::non_exhaustive).exists();
let is_non_exhaustive = struct_def.attrs(ctx.db())?.by_key(sym::non_exhaustive).exists();
let is_foreign_crate = struct_def.module(ctx.db()).is_some_and(|m| m.krate() != module.krate());
let fields = struct_type.fields(ctx.db());

View file

@ -57,7 +57,7 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext<'_>) ->
if !parent_module.is_mod_rs(db)
&& parent_module
.attrs(db)
.by_key(&sym::path)
.by_key(sym::path)
.string_value_unescape()
.is_none() =>
{

View file

@ -92,7 +92,7 @@ impl<'a> RenderContext<'a> {
fn is_deprecated(&self, def: impl HasAttrs) -> bool {
let attrs = def.attrs(self.db());
attrs.by_key(&sym::deprecated).exists()
attrs.by_key(sym::deprecated).exists()
}
fn is_deprecated_assoc_item(&self, as_assoc_item: impl AsAssocItem) -> bool {

View file

@ -96,7 +96,7 @@ pub(crate) fn visible_fields(
.copied()
.collect::<Vec<_>>();
let has_invisible_field = n_fields - fields.len() > 0;
let is_foreign_non_exhaustive = item.attrs(ctx.db).by_key(&sym::non_exhaustive).exists()
let is_foreign_non_exhaustive = item.attrs(ctx.db).by_key(sym::non_exhaustive).exists()
&& item.krate(ctx.db) != module.krate();
let fields_omitted = has_invisible_field || is_foreign_non_exhaustive;
Some((fields, fields_omitted))

View file

@ -93,7 +93,7 @@ pub fn docs_with_rangemap(
attrs: &AttrsWithOwner,
) -> Option<(Documentation, DocsRangeMap)> {
let docs = attrs
.by_key(&sym::doc)
.by_key(sym::doc)
.attrs()
.filter_map(|attr| attr.string_value_unescape().map(|s| (s, attr.id)));
let indent = doc_indent(attrs);
@ -135,7 +135,7 @@ pub fn docs_with_rangemap(
}
pub fn docs_from_attrs(attrs: &hir::Attrs) -> Option<String> {
let docs = attrs.by_key(&sym::doc).attrs().filter_map(|attr| attr.string_value_unescape());
let docs = attrs.by_key(sym::doc).attrs().filter_map(|attr| attr.string_value_unescape());
let indent = doc_indent(attrs);
let mut buf = String::new();
for doc in docs {
@ -266,7 +266,7 @@ fn get_doc_string_in_attr(it: &ast::Attr) -> Option<ast::String> {
fn doc_indent(attrs: &hir::Attrs) -> usize {
let mut min = !0;
for val in attrs.by_key(&sym::doc).attrs().filter_map(|attr| attr.string_value_unescape()) {
for val in attrs.by_key(sym::doc).attrs().filter_map(|attr| attr.string_value_unescape()) {
if let Some(m) =
val.lines().filter_map(|line| line.chars().position(|c| !c.is_whitespace())).min()
{

View file

@ -369,7 +369,7 @@ impl Definition {
if let Definition::Macro(macro_def) = self {
return match macro_def.kind(db) {
hir::MacroKind::Declarative => {
if macro_def.attrs(db).by_key(&sym::macro_export).exists() {
if macro_def.attrs(db).by_key(sym::macro_export).exists() {
SearchScope::reverse_dependencies(db, module.krate())
} else {
SearchScope::krate(db, module.krate())

View file

@ -597,7 +597,7 @@ fn filename_and_frag_for_def(
Definition::Module(m) => match m.name(db) {
// `#[doc(keyword = "...")]` is internal used only by rust compiler
Some(name) => {
match m.attrs(db).by_key(&sym::doc).find_string_value_in_tt(&sym::keyword) {
match m.attrs(db).by_key(sym::doc).find_string_value_in_tt(sym::keyword) {
Some(kw) => {
format!("keyword.{}.html", kw)
}

View file

@ -160,7 +160,7 @@ pub(super) fn doc_comment(
let mut new_comments = Vec::new();
let mut string;
for attr in attributes.by_key(&sym::doc).attrs() {
for attr in attributes.by_key(sym::doc).attrs() {
let InFile { file_id, value: src } = attrs_source_map.source_of(attr);
if file_id != src_file_id {
continue;

View file

@ -13,35 +13,18 @@ use crate::{
macro_rules! define_symbols {
(@WITH_NAME: $($alias:ident = $value:literal,)* @PLAIN: $($name:ident,)*) => {
// We define symbols as both `const`s and `static`s because some const code requires const symbols,
// but code from before the transition relies on the lifetime of the predefined symbols and making them
// `const`s make it error (because now they're temporaries). In the future we probably should only
// use consts.
/// Predefined symbols as `const`s (instead of the default `static`s).
pub mod consts {
use super::{Symbol, TaggedArcPtr};
// The strings should be in `static`s so that symbol equality holds.
$(
pub const $name: Symbol = {
static SYMBOL_STR: &str = stringify!($name);
Symbol { repr: TaggedArcPtr::non_arc(&SYMBOL_STR) }
};
)*
$(
pub const $alias: Symbol = {
static SYMBOL_STR: &str = $value;
Symbol { repr: TaggedArcPtr::non_arc(&SYMBOL_STR) }
};
)*
}
// The strings should be in `static`s so that symbol equality holds.
$(
pub static $name: Symbol = consts::$name;
pub const $name: Symbol = {
static SYMBOL_STR: &str = stringify!($name);
Symbol { repr: TaggedArcPtr::non_arc(&SYMBOL_STR) }
};
)*
$(
pub static $alias: Symbol = consts::$alias;
pub const $alias: Symbol = {
static SYMBOL_STR: &str = $value;
Symbol { repr: TaggedArcPtr::non_arc(&SYMBOL_STR) }
};
)*