Skip to content

Commit 86ba54c

Browse files
bors[bot]Jonas Schievink
and
Jonas Schievink
authored
Merge #10964
10964: minor: Move synstructure hack out of ItemTree lowering r=jonas-schievink a=jonas-schievink Don't set the name to `None` for the rest of the IDE if it's generated by synstructure. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 77f2d34 + c0a30ff commit 86ba54c

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

crates/hir_def/src/data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl ImplData {
255255

256256
#[derive(Debug, Clone, PartialEq, Eq)]
257257
pub struct ConstData {
258-
/// const _: () = ();
258+
/// `None` for `const _: () = ();`
259259
pub name: Option<Name>,
260260
pub type_ref: Interned<TypeRef>,
261261
pub visibility: RawVisibility,

crates/hir_def/src/item_tree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ pub struct Enum {
648648

649649
#[derive(Debug, Clone, Eq, PartialEq)]
650650
pub struct Const {
651-
/// const _: () = ();
651+
/// `None` for `const _: () = ();`
652652
pub name: Option<Name>,
653653
pub visibility: RawVisibilityId,
654654
pub type_ref: Interned<TypeRef>,

crates/hir_def/src/item_tree/lower.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,7 @@ impl<'a> Ctx<'a> {
375375
}
376376

377377
fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
378-
let mut name = konst.name().map(|it| it.as_name());
379-
if name.as_ref().map_or(false, |n| n.to_smol_str().starts_with("_DERIVE_")) {
380-
// FIXME: this is a hack to treat consts generated by synstructure as unnamed
381-
// remove this some time in the future
382-
name = None;
383-
}
378+
let name = konst.name().map(|it| it.as_name());
384379
let type_ref = self.lower_type_ref_opt(konst.ty());
385380
let visibility = self.lower_visibility(konst);
386381
let ast_id = self.source_ast_id_map.ast_id(konst);

crates/hir_ty/src/method_resolution.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use arrayvec::ArrayVec;
88
use base_db::{CrateId, Edition};
99
use chalk_ir::{cast::Cast, Mutability, UniverseIndex};
1010
use hir_def::{
11-
lang_item::LangItemTarget, nameres::DefMap, AssocItemId, BlockId, FunctionId, GenericDefId,
12-
HasModule, ImplId, ItemContainerId, Lookup, ModuleId, TraitId,
11+
item_scope::ItemScope, lang_item::LangItemTarget, nameres::DefMap, AssocItemId, BlockId,
12+
ConstId, FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, Lookup, ModuleDefId,
13+
ModuleId, TraitId,
1314
};
1415
use hir_expand::name::Name;
1516
use rustc_hash::{FxHashMap, FxHashSet};
@@ -177,7 +178,7 @@ impl TraitImpls {
177178

178179
// To better support custom derives, collect impls in all unnamed const items.
179180
// const _: () = { ... };
180-
for konst in module_data.scope.unnamed_consts() {
181+
for konst in collect_unnamed_consts(db, &module_data.scope) {
181182
let body = db.body(konst.into());
182183
for (_, block_def_map) in body.blocks(db.upcast()) {
183184
self.collect_def_map(db, &block_def_map);
@@ -297,7 +298,7 @@ impl InherentImpls {
297298

298299
// To better support custom derives, collect impls in all unnamed const items.
299300
// const _: () = { ... };
300-
for konst in module_data.scope.unnamed_consts() {
301+
for konst in collect_unnamed_consts(db, &module_data.scope) {
301302
let body = db.body(konst.into());
302303
for (_, block_def_map) in body.blocks(db.upcast()) {
303304
self.collect_def_map(db, &block_def_map);
@@ -318,6 +319,34 @@ impl InherentImpls {
318319
}
319320
}
320321

322+
fn collect_unnamed_consts<'a>(
323+
db: &'a dyn HirDatabase,
324+
scope: &'a ItemScope,
325+
) -> impl Iterator<Item = ConstId> + 'a {
326+
let unnamed_consts = scope.unnamed_consts();
327+
328+
// FIXME: Also treat consts named `_DERIVE_*` as unnamed, since synstructure generates those.
329+
// Should be removed once synstructure stops doing that.
330+
let synstructure_hack_consts = scope.values().filter_map(|(item, _)| match item {
331+
ModuleDefId::ConstId(id) => {
332+
let loc = id.lookup(db.upcast());
333+
let item_tree = loc.id.item_tree(db.upcast());
334+
if item_tree[loc.id.value]
335+
.name
336+
.as_ref()
337+
.map_or(false, |n| n.to_smol_str().starts_with("_DERIVE_"))
338+
{
339+
Some(id)
340+
} else {
341+
None
342+
}
343+
}
344+
_ => None,
345+
});
346+
347+
unnamed_consts.chain(synstructure_hack_consts)
348+
}
349+
321350
pub fn def_crates(
322351
db: &dyn HirDatabase,
323352
ty: &Ty,

crates/hir_ty/src/tests/method_resolution.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,28 @@ fn f() {
12481248
);
12491249
}
12501250

1251+
#[test]
1252+
fn trait_impl_in_synstructure_const() {
1253+
check_types(
1254+
r#"
1255+
struct S;
1256+
1257+
trait Tr {
1258+
fn method(&self) -> u16;
1259+
}
1260+
1261+
const _DERIVE_Tr_: () = {
1262+
impl Tr for S {}
1263+
};
1264+
1265+
fn f() {
1266+
S.method();
1267+
//^^^^^^^^^^ u16
1268+
}
1269+
"#,
1270+
);
1271+
}
1272+
12511273
#[test]
12521274
fn inherent_impl_in_unnamed_const() {
12531275
check_types(

0 commit comments

Comments
 (0)