Skip to content

Commit 1069f57

Browse files
committed
Auto merge of rust-lang#16784 - Veykril:body-invalid, r=Veykril
internal: Remove synstructure const hack support The latest version of it no longer emits these
2 parents 0931361 + 558feea commit 1069f57

File tree

8 files changed

+48
-68
lines changed

8 files changed

+48
-68
lines changed

crates/hir-def/src/child_by_source.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl ChildBySource for ItemScope {
7676
self.extern_crate_decls()
7777
.for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::EXTERN_CRATE));
7878
self.use_decls().for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::USE));
79-
self.unnamed_consts(db)
79+
self.unnamed_consts()
8080
.for_each(|konst| insert_item_loc(db, res, file_id, konst, keys::CONST));
8181
self.attr_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
8282
|(ast_id, call_id)| {

crates/hir-def/src/item_scope.rs

+2-24
Original file line numberDiff line numberDiff line change
@@ -241,30 +241,8 @@ impl ItemScope {
241241
})
242242
}
243243

244-
pub fn unnamed_consts<'a>(
245-
&'a self,
246-
db: &'a dyn DefDatabase,
247-
) -> impl Iterator<Item = ConstId> + 'a {
248-
// FIXME: Also treat consts named `_DERIVE_*` as unnamed, since synstructure generates those.
249-
// Should be removed once synstructure stops doing that.
250-
let synstructure_hack_consts = self.values.values().filter_map(|(item, _, _)| match item {
251-
&ModuleDefId::ConstId(id) => {
252-
let loc = id.lookup(db);
253-
let item_tree = loc.id.item_tree(db);
254-
if item_tree[loc.id.value]
255-
.name
256-
.as_ref()
257-
.map_or(false, |n| n.to_smol_str().starts_with("_DERIVE_"))
258-
{
259-
Some(id)
260-
} else {
261-
None
262-
}
263-
}
264-
_ => None,
265-
});
266-
267-
self.unnamed_consts.iter().copied().chain(synstructure_hack_consts)
244+
pub fn unnamed_consts(&self) -> impl Iterator<Item = ConstId> + '_ {
245+
self.unnamed_consts.iter().copied()
268246
}
269247

270248
/// Iterate over all module scoped macros

crates/hir-def/src/src.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use either::Either;
44
use hir_expand::InFile;
55
use la_arena::ArenaMap;
6-
use syntax::ast;
6+
use syntax::{ast, AstNode, AstPtr};
77

88
use crate::{
99
data::adt::lower_struct, db::DefDatabase, item_tree::ItemTreeNode, trace::Trace, GenericDefId,
@@ -12,8 +12,12 @@ use crate::{
1212
};
1313

1414
pub trait HasSource {
15-
type Value;
16-
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
15+
type Value: AstNode;
16+
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
17+
let InFile { file_id, value } = self.ast_ptr(db);
18+
InFile::new(file_id, value.to_node(&db.parse_or_expand(file_id)))
19+
}
20+
fn ast_ptr(&self, db: &dyn DefDatabase) -> InFile<AstPtr<Self::Value>>;
1721
}
1822

1923
impl<T> HasSource for T
@@ -22,16 +26,14 @@ where
2226
T::Id: ItemTreeNode,
2327
{
2428
type Value = <T::Id as ItemTreeNode>::Source;
25-
26-
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
29+
fn ast_ptr(&self, db: &dyn DefDatabase) -> InFile<AstPtr<Self::Value>> {
2730
let id = self.item_tree_id();
2831
let file_id = id.file_id();
2932
let tree = id.item_tree(db);
3033
let ast_id_map = db.ast_id_map(file_id);
31-
let root = db.parse_or_expand(file_id);
3234
let node = &tree[id.value];
3335

34-
InFile::new(file_id, ast_id_map.get(node.ast_id()).to_node(&root))
36+
InFile::new(file_id, ast_id_map.get(node.ast_id()))
3537
}
3638
}
3739

crates/hir-ty/src/method_resolution.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl TraitImpls {
213213

214214
// To better support custom derives, collect impls in all unnamed const items.
215215
// const _: () = { ... };
216-
for konst in module_data.scope.unnamed_consts(db.upcast()) {
216+
for konst in module_data.scope.unnamed_consts() {
217217
let body = db.body(konst.into());
218218
for (_, block_def_map) in body.blocks(db.upcast()) {
219219
Self::collect_def_map(db, map, &block_def_map);
@@ -337,7 +337,7 @@ impl InherentImpls {
337337

338338
// To better support custom derives, collect impls in all unnamed const items.
339339
// const _: () = { ... };
340-
for konst in module_data.scope.unnamed_consts(db.upcast()) {
340+
for konst in module_data.scope.unnamed_consts() {
341341
let body = db.body(konst.into());
342342
for (_, block_def_map) in body.blocks(db.upcast()) {
343343
self.collect_def_map(db, &block_def_map);

crates/hir-ty/src/tests/method_resolution.rs

-22
Original file line numberDiff line numberDiff line change
@@ -1461,28 +1461,6 @@ fn f() {
14611461
);
14621462
}
14631463

1464-
#[test]
1465-
fn trait_impl_in_synstructure_const() {
1466-
check_types(
1467-
r#"
1468-
struct S;
1469-
1470-
trait Tr {
1471-
fn method(&self) -> u16;
1472-
}
1473-
1474-
const _DERIVE_Tr_: () = {
1475-
impl Tr for S {}
1476-
};
1477-
1478-
fn f() {
1479-
S.method();
1480-
//^^^^^^^^^^ u16
1481-
}
1482-
"#,
1483-
);
1484-
}
1485-
14861464
#[test]
14871465
fn inherent_impl_in_unnamed_const() {
14881466
check_types(

crates/hir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl Module {
754754
scope
755755
.declarations()
756756
.map(ModuleDef::from)
757-
.chain(scope.unnamed_consts(db.upcast()).map(|id| ModuleDef::Const(Const::from(id))))
757+
.chain(scope.unnamed_consts().map(|id| ModuleDef::Const(Const::from(id))))
758758
.collect()
759759
}
760760

crates/hir/src/symbols.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ impl<'a> SymbolCollector<'a> {
165165
// Record renamed imports.
166166
// FIXME: In case it imports multiple items under different namespaces we just pick one arbitrarily
167167
// for now.
168-
// FIXME: This parses!
169168
for id in scope.imports() {
170169
let source = id.import.child_source(self.db.upcast());
171170
let Some(use_tree_src) = source.value.get(id.idx) else { continue };
@@ -196,7 +195,7 @@ impl<'a> SymbolCollector<'a> {
196195
});
197196
}
198197

199-
for const_id in scope.unnamed_consts(self.db.upcast()) {
198+
for const_id in scope.unnamed_consts() {
200199
self.collect_from_body(const_id);
201200
}
202201

crates/ide/src/navigation_target.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,12 @@ impl NavigationTarget {
176176

177177
impl TryToNav for FileSymbol {
178178
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
179-
let root = db.parse_or_expand(self.loc.hir_file_id);
180-
self.loc.ptr.to_node(&root);
181179
Some(
182-
orig_range_with_focus(
180+
orig_range_with_focus_r(
183181
db,
184182
self.loc.hir_file_id,
185-
&self.loc.ptr.to_node(&root),
186-
Some(self.loc.name_ptr.to_node(&root)),
183+
self.loc.ptr.text_range(),
184+
Some(self.loc.name_ptr.text_range()),
187185
)
188186
.map(|(FileRange { file_id, range: full_range }, focus_range)| {
189187
NavigationTarget {
@@ -722,7 +720,21 @@ fn orig_range_with_focus(
722720
value: &SyntaxNode,
723721
name: Option<impl AstNode>,
724722
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
725-
let Some(name) = name else { return orig_range(db, hir_file, value) };
723+
orig_range_with_focus_r(
724+
db,
725+
hir_file,
726+
value.text_range(),
727+
name.map(|it| it.syntax().text_range()),
728+
)
729+
}
730+
731+
fn orig_range_with_focus_r(
732+
db: &RootDatabase,
733+
hir_file: HirFileId,
734+
value: TextRange,
735+
name: Option<TextRange>,
736+
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
737+
let Some(name) = name else { return orig_range_r(db, hir_file, value) };
726738

727739
let call_kind =
728740
|| db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id).kind;
@@ -733,9 +745,9 @@ fn orig_range_with_focus(
733745
.definition_range(db)
734746
};
735747

736-
let value_range = InFile::new(hir_file, value).original_file_range_opt(db);
748+
let value_range = InFile::new(hir_file, value).original_node_file_range_opt(db);
737749
let ((call_site_range, call_site_focus), def_site) =
738-
match InFile::new(hir_file, name.syntax()).original_file_range_opt(db) {
750+
match InFile::new(hir_file, name).original_node_file_range_opt(db) {
739751
// call site name
740752
Some((focus_range, ctxt)) if ctxt.is_root() => {
741753
// Try to upmap the node as well, if it ends up in the def site, go back to the call site
@@ -802,7 +814,7 @@ fn orig_range_with_focus(
802814
}
803815
}
804816
// lost name? can't happen for single tokens
805-
None => return orig_range(db, hir_file, value),
817+
None => return orig_range_r(db, hir_file, value),
806818
};
807819

808820
UpmappingResult {
@@ -845,6 +857,17 @@ fn orig_range(
845857
}
846858
}
847859

860+
fn orig_range_r(
861+
db: &RootDatabase,
862+
hir_file: HirFileId,
863+
value: TextRange,
864+
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
865+
UpmappingResult {
866+
call_site: (InFile::new(hir_file, value).original_node_file_range(db).0, None),
867+
def_site: None,
868+
}
869+
}
870+
848871
#[cfg(test)]
849872
mod tests {
850873
use expect_test::expect;

0 commit comments

Comments
 (0)