Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit daa0138

Browse files
committed
Auto merge of rust-lang#14001 - Veykril:lang-item, r=Veykril
Replace SmolStr usage with lang item enum for lang items Fixes rust-lang/rust-analyzer#13395
2 parents 1afa032 + f8ed4d7 commit daa0138

File tree

24 files changed

+496
-371
lines changed

24 files changed

+496
-371
lines changed

crates/hir-def/src/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use either::Either;
66
use hir_expand::{db::AstDatabase, HirFileId};
77
use intern::Interned;
88
use la_arena::ArenaMap;
9-
use syntax::{ast, AstPtr, SmolStr};
9+
use syntax::{ast, AstPtr};
1010

1111
use crate::{
1212
adt::{EnumData, StructData},
@@ -19,7 +19,7 @@ use crate::{
1919
generics::GenericParams,
2020
import_map::ImportMap,
2121
item_tree::{AttrOwner, ItemTree},
22-
lang_item::{LangItemTarget, LangItems},
22+
lang_item::{LangItem, LangItemTarget, LangItems},
2323
nameres::{diagnostics::DefDiagnostic, DefMap},
2424
visibility::{self, Visibility},
2525
AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, ExternBlockId,
@@ -183,7 +183,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
183183
fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
184184

185185
#[salsa::invoke(LangItems::lang_item_query)]
186-
fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option<LangItemTarget>;
186+
fn lang_item(&self, start_crate: CrateId, item: LangItem) -> Option<LangItemTarget>;
187187

188188
#[salsa::invoke(ImportMap::import_map_query)]
189189
fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;

crates/hir-def/src/lang_item.rs

Lines changed: 269 additions & 29 deletions
Large diffs are not rendered by default.

crates/hir-expand/src/name.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ impl Name {
133133
}
134134
}
135135

136+
/// Returns the text this name represents if it isn't a tuple field.
137+
pub fn as_str(&self) -> Option<&str> {
138+
match &self.0 {
139+
Repr::Text(it) => Some(it),
140+
_ => None,
141+
}
142+
}
143+
136144
/// Returns the textual representation of this name as a [`SmolStr`].
137145
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
138146
/// the general case.

crates/hir-ty/src/autoderef.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use std::sync::Arc;
77

88
use chalk_ir::cast::Cast;
9+
use hir_def::lang_item::LangItem;
910
use hir_expand::name::name;
1011
use limit::Limit;
11-
use syntax::SmolStr;
1212

1313
use crate::{
1414
db::HirDatabase, infer::unify::InferenceTable, Canonical, Goal, Interner, ProjectionTyExt,
@@ -117,9 +117,8 @@ fn deref_by_trait(table: &mut InferenceTable<'_>, ty: Ty) -> Option<Ty> {
117117
}
118118

119119
let db = table.db;
120-
let deref_trait = db
121-
.lang_item(table.trait_env.krate, SmolStr::new_inline("deref"))
122-
.and_then(|l| l.as_trait())?;
120+
let deref_trait =
121+
db.lang_item(table.trait_env.krate, LangItem::Deref).and_then(|l| l.as_trait())?;
123122
let target = db.trait_data(deref_trait).associated_type_by_name(&name![Target])?;
124123

125124
let projection = {

crates/hir-ty/src/chalk_db.rs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::sync::Arc;
44

55
use cov_mark::hit;
6-
use syntax::SmolStr;
76
use tracing::debug;
87

98
use chalk_ir::{cast::Cast, fold::shift::Shift, CanonicalVarKinds};
@@ -12,7 +11,7 @@ use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait};
1211
use base_db::CrateId;
1312
use hir_def::{
1413
expr::Movability,
15-
lang_item::{lang_attr, LangItemTarget},
14+
lang_item::{lang_attr, LangItem, LangItemTarget},
1615
AssocItemId, GenericDefId, HasModule, ItemContainerId, Lookup, ModuleId, TypeAliasId,
1716
};
1817
use hir_expand::name::name;
@@ -182,9 +181,9 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
182181
&self,
183182
well_known_trait: rust_ir::WellKnownTrait,
184183
) -> Option<chalk_ir::TraitId<Interner>> {
185-
let lang_attr = lang_attr_from_well_known_trait(well_known_trait);
184+
let lang_attr = lang_item_from_well_known_trait(well_known_trait);
186185
let trait_ = match self.db.lang_item(self.krate, lang_attr.into()) {
187-
Some(LangItemTarget::TraitId(trait_)) => trait_,
186+
Some(LangItemTarget::Trait(trait_)) => trait_,
188187
_ => return None,
189188
};
190189
Some(to_chalk_trait_id(trait_))
@@ -216,7 +215,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
216215
crate::ImplTraitId::AsyncBlockTypeImplTrait(..) => {
217216
if let Some((future_trait, future_output)) = self
218217
.db
219-
.lang_item(self.krate, SmolStr::new_inline("future_trait"))
218+
.lang_item(self.krate, LangItem::Future)
220219
.and_then(|item| item.as_trait())
221220
.and_then(|trait_| {
222221
let alias =
@@ -246,7 +245,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
246245
binder.push(crate::wrap_empty_binders(impl_bound));
247246
let sized_trait = self
248247
.db
249-
.lang_item(self.krate, SmolStr::new_inline("sized"))
248+
.lang_item(self.krate, LangItem::Sized)
250249
.and_then(|item| item.as_trait());
251250
if let Some(sized_trait_) = sized_trait {
252251
let sized_bound = WhereClause::Implemented(TraitRef {
@@ -493,7 +492,7 @@ pub(crate) fn associated_ty_data_query(
493492

494493
if !ctx.unsized_types.borrow().contains(&self_ty) {
495494
let sized_trait = db
496-
.lang_item(resolver.krate(), SmolStr::new_inline("sized"))
495+
.lang_item(resolver.krate(), LangItem::Sized)
497496
.and_then(|lang_item| lang_item.as_trait().map(to_chalk_trait_id));
498497
let sized_bound = sized_trait.into_iter().map(|sized_trait| {
499498
let trait_bound =
@@ -541,8 +540,8 @@ pub(crate) fn trait_datum_query(
541540
let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
542541
let associated_ty_ids = trait_data.associated_types().map(to_assoc_type_id).collect();
543542
let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses };
544-
let well_known =
545-
lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name));
543+
let well_known = lang_attr(db.upcast(), trait_)
544+
.and_then(|name| well_known_trait_from_lang_item(LangItem::from_str(&name)?));
546545
let trait_datum = TraitDatum {
547546
id: trait_id,
548547
binders: make_binders(db, &generic_params, trait_datum_bound),
@@ -553,42 +552,42 @@ pub(crate) fn trait_datum_query(
553552
Arc::new(trait_datum)
554553
}
555554

556-
fn well_known_trait_from_lang_attr(name: &str) -> Option<WellKnownTrait> {
557-
Some(match name {
558-
"clone" => WellKnownTrait::Clone,
559-
"coerce_unsized" => WellKnownTrait::CoerceUnsized,
560-
"copy" => WellKnownTrait::Copy,
561-
"discriminant_kind" => WellKnownTrait::DiscriminantKind,
562-
"dispatch_from_dyn" => WellKnownTrait::DispatchFromDyn,
563-
"drop" => WellKnownTrait::Drop,
564-
"fn" => WellKnownTrait::Fn,
565-
"fn_mut" => WellKnownTrait::FnMut,
566-
"fn_once" => WellKnownTrait::FnOnce,
567-
"generator" => WellKnownTrait::Generator,
568-
"sized" => WellKnownTrait::Sized,
569-
"unpin" => WellKnownTrait::Unpin,
570-
"unsize" => WellKnownTrait::Unsize,
571-
"tuple_trait" => WellKnownTrait::Tuple,
555+
fn well_known_trait_from_lang_item(item: LangItem) -> Option<WellKnownTrait> {
556+
Some(match item {
557+
LangItem::Clone => WellKnownTrait::Clone,
558+
LangItem::CoerceUnsized => WellKnownTrait::CoerceUnsized,
559+
LangItem::Copy => WellKnownTrait::Copy,
560+
LangItem::DiscriminantKind => WellKnownTrait::DiscriminantKind,
561+
LangItem::DispatchFromDyn => WellKnownTrait::DispatchFromDyn,
562+
LangItem::Drop => WellKnownTrait::Drop,
563+
LangItem::Fn => WellKnownTrait::Fn,
564+
LangItem::FnMut => WellKnownTrait::FnMut,
565+
LangItem::FnOnce => WellKnownTrait::FnOnce,
566+
LangItem::Generator => WellKnownTrait::Generator,
567+
LangItem::Sized => WellKnownTrait::Sized,
568+
LangItem::Unpin => WellKnownTrait::Unpin,
569+
LangItem::Unsize => WellKnownTrait::Unsize,
570+
LangItem::Tuple => WellKnownTrait::Tuple,
572571
_ => return None,
573572
})
574573
}
575574

576-
fn lang_attr_from_well_known_trait(attr: WellKnownTrait) -> &'static str {
577-
match attr {
578-
WellKnownTrait::Clone => "clone",
579-
WellKnownTrait::CoerceUnsized => "coerce_unsized",
580-
WellKnownTrait::Copy => "copy",
581-
WellKnownTrait::DiscriminantKind => "discriminant_kind",
582-
WellKnownTrait::DispatchFromDyn => "dispatch_from_dyn",
583-
WellKnownTrait::Drop => "drop",
584-
WellKnownTrait::Fn => "fn",
585-
WellKnownTrait::FnMut => "fn_mut",
586-
WellKnownTrait::FnOnce => "fn_once",
587-
WellKnownTrait::Generator => "generator",
588-
WellKnownTrait::Sized => "sized",
589-
WellKnownTrait::Tuple => "tuple_trait",
590-
WellKnownTrait::Unpin => "unpin",
591-
WellKnownTrait::Unsize => "unsize",
575+
fn lang_item_from_well_known_trait(trait_: WellKnownTrait) -> LangItem {
576+
match trait_ {
577+
WellKnownTrait::Clone => LangItem::Clone,
578+
WellKnownTrait::CoerceUnsized => LangItem::CoerceUnsized,
579+
WellKnownTrait::Copy => LangItem::Copy,
580+
WellKnownTrait::DiscriminantKind => LangItem::DiscriminantKind,
581+
WellKnownTrait::DispatchFromDyn => LangItem::DispatchFromDyn,
582+
WellKnownTrait::Drop => LangItem::Drop,
583+
WellKnownTrait::Fn => LangItem::Fn,
584+
WellKnownTrait::FnMut => LangItem::FnMut,
585+
WellKnownTrait::FnOnce => LangItem::FnOnce,
586+
WellKnownTrait::Generator => LangItem::Generator,
587+
WellKnownTrait::Sized => LangItem::Sized,
588+
WellKnownTrait::Tuple => LangItem::Tuple,
589+
WellKnownTrait::Unpin => LangItem::Unpin,
590+
WellKnownTrait::Unsize => LangItem::Unsize,
592591
}
593592
}
594593

crates/hir-ty/src/chalk_ext.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use chalk_ir::{FloatTy, IntTy, Mutability, Scalar, TyVariableKind, UintTy};
44
use hir_def::{
55
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
66
generics::TypeOrConstParamData,
7+
lang_item::LangItem,
78
type_ref::Rawness,
89
FunctionId, GenericDefId, HasModule, ItemContainerId, Lookup, TraitId,
910
};
10-
use syntax::SmolStr;
1111

1212
use crate::{
1313
db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
@@ -214,9 +214,8 @@ impl TyExt for Ty {
214214
match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) {
215215
ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => {
216216
let krate = def.module(db.upcast()).krate();
217-
if let Some(future_trait) = db
218-
.lang_item(krate, SmolStr::new_inline("future_trait"))
219-
.and_then(|item| item.as_trait())
217+
if let Some(future_trait) =
218+
db.lang_item(krate, LangItem::Future).and_then(|item| item.as_trait())
220219
{
221220
// This is only used by type walking.
222221
// Parameters will be walked outside, and projection predicate is not used.

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use std::fmt;
66
use std::sync::Arc;
77

8-
use hir_def::{path::path, resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId, HasModule};
8+
use hir_def::lang_item::LangItem;
9+
use hir_def::{resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId, HasModule};
10+
use hir_def::{ItemContainerId, Lookup};
911
use hir_expand::name;
1012
use itertools::Either;
1113
use itertools::Itertools;
@@ -245,26 +247,25 @@ struct FilterMapNextChecker {
245247
impl FilterMapNextChecker {
246248
fn new(resolver: &hir_def::resolver::Resolver, db: &dyn HirDatabase) -> Self {
247249
// Find and store the FunctionIds for Iterator::filter_map and Iterator::next
248-
let iterator_path = path![core::iter::Iterator];
249-
let mut filter_map_function_id = None;
250-
let mut next_function_id = None;
251-
252-
if let Some(iterator_trait_id) = resolver.resolve_known_trait(db.upcast(), &iterator_path) {
253-
let iterator_trait_items = &db.trait_data(iterator_trait_id).items;
254-
for item in iterator_trait_items.iter() {
255-
if let (name, AssocItemId::FunctionId(id)) = item {
256-
if *name == name![filter_map] {
257-
filter_map_function_id = Some(*id);
250+
let (next_function_id, filter_map_function_id) = match db
251+
.lang_item(resolver.krate(), LangItem::IteratorNext)
252+
.and_then(|it| it.as_function())
253+
{
254+
Some(next_function_id) => (
255+
Some(next_function_id),
256+
match next_function_id.lookup(db.upcast()).container {
257+
ItemContainerId::TraitId(iterator_trait_id) => {
258+
let iterator_trait_items = &db.trait_data(iterator_trait_id).items;
259+
iterator_trait_items.iter().find_map(|(name, it)| match it {
260+
&AssocItemId::FunctionId(id) if *name == name![filter_map] => Some(id),
261+
_ => None,
262+
})
258263
}
259-
if *name == name![next] {
260-
next_function_id = Some(*id);
261-
}
262-
}
263-
if filter_map_function_id.is_some() && next_function_id.is_some() {
264-
break;
265-
}
266-
}
267-
}
264+
_ => None,
265+
},
266+
),
267+
None => (None, None),
268+
};
268269
Self { filter_map_function_id, next_function_id, prev_filter_map_expr_id: None }
269270
}
270271

crates/hir-ty/src/display.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use hir_def::{
1212
find_path,
1313
generics::{TypeOrConstParamData, TypeParamProvenance},
1414
item_scope::ItemInNs,
15+
lang_item::LangItem,
1516
path::{Path, PathKind},
1617
type_ref::{ConstScalar, TraitBoundModifier, TypeBound, TypeRef},
1718
visibility::Visibility,
@@ -21,7 +22,6 @@ use hir_expand::{hygiene::Hygiene, name::Name};
2122
use intern::{Internable, Interned};
2223
use itertools::Itertools;
2324
use smallvec::SmallVec;
24-
use syntax::SmolStr;
2525

2626
use crate::{
2727
db::HirDatabase,
@@ -925,7 +925,7 @@ impl SizedByDefault {
925925
Self::NotSized => false,
926926
Self::Sized { anchor } => {
927927
let sized_trait = db
928-
.lang_item(anchor, SmolStr::new_inline("sized"))
928+
.lang_item(anchor, LangItem::Sized)
929929
.and_then(|lang_item| lang_item.as_trait());
930930
Some(trait_) == sized_trait
931931
}
@@ -1057,8 +1057,7 @@ fn write_bounds_like_dyn_trait(
10571057
}
10581058
if let SizedByDefault::Sized { anchor } = default_sized {
10591059
let sized_trait =
1060-
f.db.lang_item(anchor, SmolStr::new_inline("sized"))
1061-
.and_then(|lang_item| lang_item.as_trait());
1060+
f.db.lang_item(anchor, LangItem::Sized).and_then(|lang_item| lang_item.as_trait());
10621061
if !is_sized {
10631062
if !first {
10641063
write!(f, " + ")?;

0 commit comments

Comments
 (0)