Auto merge of #16784 - Veykril:body-invalid, r=Veykril

internal: Remove synstructure const hack support

The latest version of it no longer emits these
This commit is contained in:
bors 2024-03-11 09:05:26 +00:00
commit 1069f57d8b
8 changed files with 48 additions and 68 deletions

View file

@ -76,7 +76,7 @@ impl ChildBySource for ItemScope {
self.extern_crate_decls()
.for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::EXTERN_CRATE));
self.use_decls().for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::USE));
self.unnamed_consts(db)
self.unnamed_consts()
.for_each(|konst| insert_item_loc(db, res, file_id, konst, keys::CONST));
self.attr_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
|(ast_id, call_id)| {

View file

@ -241,30 +241,8 @@ impl ItemScope {
})
}
pub fn unnamed_consts<'a>(
&'a self,
db: &'a dyn DefDatabase,
) -> impl Iterator<Item = ConstId> + 'a {
// FIXME: Also treat consts named `_DERIVE_*` as unnamed, since synstructure generates those.
// Should be removed once synstructure stops doing that.
let synstructure_hack_consts = self.values.values().filter_map(|(item, _, _)| match item {
&ModuleDefId::ConstId(id) => {
let loc = id.lookup(db);
let item_tree = loc.id.item_tree(db);
if item_tree[loc.id.value]
.name
.as_ref()
.map_or(false, |n| n.to_smol_str().starts_with("_DERIVE_"))
{
Some(id)
} else {
None
}
}
_ => None,
});
self.unnamed_consts.iter().copied().chain(synstructure_hack_consts)
pub fn unnamed_consts(&self) -> impl Iterator<Item = ConstId> + '_ {
self.unnamed_consts.iter().copied()
}
/// Iterate over all module scoped macros

View file

@ -3,7 +3,7 @@
use either::Either;
use hir_expand::InFile;
use la_arena::ArenaMap;
use syntax::ast;
use syntax::{ast, AstNode, AstPtr};
use crate::{
data::adt::lower_struct, db::DefDatabase, item_tree::ItemTreeNode, trace::Trace, GenericDefId,
@ -12,8 +12,12 @@ use crate::{
};
pub trait HasSource {
type Value;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
type Value: AstNode;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
let InFile { file_id, value } = self.ast_ptr(db);
InFile::new(file_id, value.to_node(&db.parse_or_expand(file_id)))
}
fn ast_ptr(&self, db: &dyn DefDatabase) -> InFile<AstPtr<Self::Value>>;
}
impl<T> HasSource for T
@ -22,16 +26,14 @@ where
T::Id: ItemTreeNode,
{
type Value = <T::Id as ItemTreeNode>::Source;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
fn ast_ptr(&self, db: &dyn DefDatabase) -> InFile<AstPtr<Self::Value>> {
let id = self.item_tree_id();
let file_id = id.file_id();
let tree = id.item_tree(db);
let ast_id_map = db.ast_id_map(file_id);
let root = db.parse_or_expand(file_id);
let node = &tree[id.value];
InFile::new(file_id, ast_id_map.get(node.ast_id()).to_node(&root))
InFile::new(file_id, ast_id_map.get(node.ast_id()))
}
}

View file

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

View file

@ -1461,28 +1461,6 @@ fn f() {
);
}
#[test]
fn trait_impl_in_synstructure_const() {
check_types(
r#"
struct S;
trait Tr {
fn method(&self) -> u16;
}
const _DERIVE_Tr_: () = {
impl Tr for S {}
};
fn f() {
S.method();
//^^^^^^^^^^ u16
}
"#,
);
}
#[test]
fn inherent_impl_in_unnamed_const() {
check_types(

View file

@ -754,7 +754,7 @@ impl Module {
scope
.declarations()
.map(ModuleDef::from)
.chain(scope.unnamed_consts(db.upcast()).map(|id| ModuleDef::Const(Const::from(id))))
.chain(scope.unnamed_consts().map(|id| ModuleDef::Const(Const::from(id))))
.collect()
}

View file

@ -165,7 +165,6 @@ impl<'a> SymbolCollector<'a> {
// Record renamed imports.
// FIXME: In case it imports multiple items under different namespaces we just pick one arbitrarily
// for now.
// FIXME: This parses!
for id in scope.imports() {
let source = id.import.child_source(self.db.upcast());
let Some(use_tree_src) = source.value.get(id.idx) else { continue };
@ -196,7 +195,7 @@ impl<'a> SymbolCollector<'a> {
});
}
for const_id in scope.unnamed_consts(self.db.upcast()) {
for const_id in scope.unnamed_consts() {
self.collect_from_body(const_id);
}

View file

@ -176,14 +176,12 @@ impl NavigationTarget {
impl TryToNav for FileSymbol {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let root = db.parse_or_expand(self.loc.hir_file_id);
self.loc.ptr.to_node(&root);
Some(
orig_range_with_focus(
orig_range_with_focus_r(
db,
self.loc.hir_file_id,
&self.loc.ptr.to_node(&root),
Some(self.loc.name_ptr.to_node(&root)),
self.loc.ptr.text_range(),
Some(self.loc.name_ptr.text_range()),
)
.map(|(FileRange { file_id, range: full_range }, focus_range)| {
NavigationTarget {
@ -722,7 +720,21 @@ fn orig_range_with_focus(
value: &SyntaxNode,
name: Option<impl AstNode>,
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
let Some(name) = name else { return orig_range(db, hir_file, value) };
orig_range_with_focus_r(
db,
hir_file,
value.text_range(),
name.map(|it| it.syntax().text_range()),
)
}
fn orig_range_with_focus_r(
db: &RootDatabase,
hir_file: HirFileId,
value: TextRange,
name: Option<TextRange>,
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
let Some(name) = name else { return orig_range_r(db, hir_file, value) };
let call_kind =
|| db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id).kind;
@ -733,9 +745,9 @@ fn orig_range_with_focus(
.definition_range(db)
};
let value_range = InFile::new(hir_file, value).original_file_range_opt(db);
let value_range = InFile::new(hir_file, value).original_node_file_range_opt(db);
let ((call_site_range, call_site_focus), def_site) =
match InFile::new(hir_file, name.syntax()).original_file_range_opt(db) {
match InFile::new(hir_file, name).original_node_file_range_opt(db) {
// call site name
Some((focus_range, ctxt)) if ctxt.is_root() => {
// 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(
}
}
// lost name? can't happen for single tokens
None => return orig_range(db, hir_file, value),
None => return orig_range_r(db, hir_file, value),
};
UpmappingResult {
@ -845,6 +857,17 @@ fn orig_range(
}
}
fn orig_range_r(
db: &RootDatabase,
hir_file: HirFileId,
value: TextRange,
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
UpmappingResult {
call_site: (InFile::new(hir_file, value).original_node_file_range(db).0, None),
def_site: None,
}
}
#[cfg(test)]
mod tests {
use expect_test::expect;