mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Merge commit 'baee6b338b
' into sync-from-ra
This commit is contained in:
parent
0155385b57
commit
aa55ce9567
139 changed files with 4248 additions and 1042 deletions
|
@ -12,9 +12,9 @@ use hir_ty::db::HirDatabase;
|
|||
use syntax::{ast, AstNode};
|
||||
|
||||
use crate::{
|
||||
Adt, AssocItem, Const, ConstParam, Enum, Field, Function, GenericParam, Impl, LifetimeParam,
|
||||
Macro, Module, ModuleDef, Static, Struct, Trait, TraitAlias, TypeAlias, TypeParam, Union,
|
||||
Variant,
|
||||
Adt, AssocItem, Const, ConstParam, Enum, ExternCrateDecl, Field, Function, GenericParam, Impl,
|
||||
LifetimeParam, Macro, Module, ModuleDef, Static, Struct, Trait, TraitAlias, TypeAlias,
|
||||
TypeParam, Union, Variant,
|
||||
};
|
||||
|
||||
pub trait HasAttrs {
|
||||
|
@ -120,6 +120,39 @@ impl HasAttrs for AssocItem {
|
|||
}
|
||||
}
|
||||
|
||||
impl HasAttrs for ExternCrateDecl {
|
||||
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
|
||||
let def = AttrDefId::ExternCrateId(self.into());
|
||||
db.attrs_with_owner(def)
|
||||
}
|
||||
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
||||
let crate_docs = self.resolved_crate(db)?.root_module().attrs(db).docs().map(String::from);
|
||||
let def = AttrDefId::ExternCrateId(self.into());
|
||||
let decl_docs = db.attrs(def).docs().map(String::from);
|
||||
match (decl_docs, crate_docs) {
|
||||
(None, None) => None,
|
||||
(Some(decl_docs), None) => Some(decl_docs),
|
||||
(None, Some(crate_docs)) => Some(crate_docs),
|
||||
(Some(mut decl_docs), Some(crate_docs)) => {
|
||||
decl_docs.push('\n');
|
||||
decl_docs.push('\n');
|
||||
decl_docs += &crate_docs;
|
||||
Some(decl_docs)
|
||||
}
|
||||
}
|
||||
.map(Documentation::new)
|
||||
}
|
||||
fn resolve_doc_path(
|
||||
self,
|
||||
db: &dyn HirDatabase,
|
||||
link: &str,
|
||||
ns: Option<Namespace>,
|
||||
) -> Option<ModuleDef> {
|
||||
let def = AttrDefId::ExternCrateId(self.into());
|
||||
resolve_doc_path(db, def, link, ns).map(ModuleDef::from)
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves the item `link` points to in the scope of `def`.
|
||||
fn resolve_doc_path(
|
||||
db: &dyn HirDatabase,
|
||||
|
@ -140,6 +173,7 @@ fn resolve_doc_path(
|
|||
AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::ImplId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::ExternBlockId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::UseId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::MacroId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::ExternCrateId(it) => it.resolver(db.upcast()),
|
||||
AttrDefId::GenericParamId(it) => match it {
|
||||
|
|
|
@ -10,8 +10,3 @@ pub use hir_expand::db::{
|
|||
MacroExpandQuery, ParseMacroExpansionErrorQuery, ParseMacroExpansionQuery,
|
||||
};
|
||||
pub use hir_ty::db::*;
|
||||
|
||||
#[test]
|
||||
fn hir_database_is_object_safe() {
|
||||
fn _assert_object_safe(_: &dyn HirDatabase) {}
|
||||
}
|
||||
|
|
|
@ -18,9 +18,9 @@ use hir_ty::{
|
|||
};
|
||||
|
||||
use crate::{
|
||||
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Enum, Field, Function, GenericParam,
|
||||
HasCrate, HasVisibility, LifetimeParam, Macro, Module, Static, Struct, Trait, TraitAlias,
|
||||
TyBuilder, Type, TypeAlias, TypeOrConstParam, TypeParam, Union, Variant,
|
||||
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl, Field,
|
||||
Function, GenericParam, HasCrate, HasVisibility, LifetimeParam, Macro, Module, Static, Struct,
|
||||
Trait, TraitAlias, TyBuilder, Type, TypeAlias, TypeOrConstParam, TypeParam, Union, Variant,
|
||||
};
|
||||
|
||||
impl HirDisplay for Function {
|
||||
|
@ -238,6 +238,18 @@ impl HirDisplay for Type {
|
|||
}
|
||||
}
|
||||
|
||||
impl HirDisplay for ExternCrateDecl {
|
||||
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
||||
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
||||
f.write_str("extern crate ")?;
|
||||
write!(f, "{}", self.name(f.db).display(f.db.upcast()))?;
|
||||
if let Some(alias) = self.alias(f.db) {
|
||||
write!(f, " as {alias}",)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl HirDisplay for GenericParam {
|
||||
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
||||
match self {
|
||||
|
|
|
@ -15,7 +15,7 @@ use crate::{
|
|||
};
|
||||
|
||||
macro_rules! from_id {
|
||||
($(($id:path, $ty:path)),*) => {$(
|
||||
($(($id:path, $ty:path)),* $(,)?) => {$(
|
||||
impl From<$id> for $ty {
|
||||
fn from(id: $id) -> $ty {
|
||||
$ty { id }
|
||||
|
@ -47,7 +47,8 @@ from_id![
|
|||
(hir_def::TypeParamId, crate::TypeParam),
|
||||
(hir_def::ConstParamId, crate::ConstParam),
|
||||
(hir_def::LifetimeParamId, crate::LifetimeParam),
|
||||
(hir_def::MacroId, crate::Macro)
|
||||
(hir_def::MacroId, crate::Macro),
|
||||
(hir_def::ExternCrateId, crate::ExternCrateDecl),
|
||||
];
|
||||
|
||||
impl From<AdtId> for Adt {
|
||||
|
|
|
@ -11,9 +11,9 @@ use hir_expand::{HirFileId, InFile};
|
|||
use syntax::ast;
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase, Adt, Const, Enum, Field, FieldSource, Function, Impl, LifetimeParam,
|
||||
LocalSource, Macro, Module, Static, Struct, Trait, TraitAlias, TypeAlias, TypeOrConstParam,
|
||||
Union, Variant,
|
||||
db::HirDatabase, Adt, Const, Enum, ExternCrateDecl, Field, FieldSource, Function, Impl,
|
||||
LifetimeParam, LocalSource, Macro, Module, Static, Struct, Trait, TraitAlias, TypeAlias,
|
||||
TypeOrConstParam, Union, Variant,
|
||||
};
|
||||
|
||||
pub trait HasSource {
|
||||
|
@ -207,3 +207,11 @@ impl HasSource for LocalSource {
|
|||
Some(self.source)
|
||||
}
|
||||
}
|
||||
|
||||
impl HasSource for ExternCrateDecl {
|
||||
type Ast = ast::ExternCrate;
|
||||
|
||||
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
|
||||
Some(self.id.lookup(db.upcast()).source(db.upcast()))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,14 +48,15 @@ use hir_def::{
|
|||
layout::{self, ReprOptions, TargetDataLayout},
|
||||
macro_id_to_def_id,
|
||||
nameres::{self, diagnostics::DefDiagnostic},
|
||||
path::ImportAlias,
|
||||
per_ns::PerNs,
|
||||
resolver::{HasResolver, Resolver},
|
||||
src::HasSource as _,
|
||||
AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
|
||||
EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, InTypeConstId, ItemContainerId,
|
||||
LifetimeParamId, LocalEnumVariantId, LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId,
|
||||
StaticId, StructId, TraitAliasId, TraitId, TypeAliasId, TypeOrConstParamId, TypeParamId,
|
||||
UnionId,
|
||||
AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, CrateRootModuleId, DefWithBodyId,
|
||||
EnumId, EnumVariantId, ExternCrateId, FunctionId, GenericDefId, HasModule, ImplId,
|
||||
InTypeConstId, ItemContainerId, LifetimeParamId, LocalEnumVariantId, LocalFieldId, Lookup,
|
||||
MacroExpander, MacroId, ModuleId, StaticId, StructId, TraitAliasId, TraitId, TypeAliasId,
|
||||
TypeOrConstParamId, TypeParamId, UnionId,
|
||||
};
|
||||
use hir_expand::{name::name, MacroCallKind};
|
||||
use hir_ty::{
|
||||
|
@ -200,9 +201,8 @@ impl Crate {
|
|||
db.crate_graph().transitive_rev_deps(self.id).map(|id| Crate { id })
|
||||
}
|
||||
|
||||
pub fn root_module(self, db: &dyn HirDatabase) -> Module {
|
||||
let def_map = db.crate_def_map(self.id);
|
||||
Module { id: def_map.crate_root().into() }
|
||||
pub fn root_module(self) -> Module {
|
||||
Module { id: CrateRootModuleId::from(self.id).into() }
|
||||
}
|
||||
|
||||
pub fn modules(self, db: &dyn HirDatabase) -> Vec<Module> {
|
||||
|
@ -247,7 +247,7 @@ impl Crate {
|
|||
/// Try to get the root URL of the documentation of a 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(db).into()));
|
||||
let attrs = db.attrs(AttrDefId::ModuleId(self.root_module().into()));
|
||||
let doc_url = attrs.by_key("doc").find_string_value_in_tt("html_root_url");
|
||||
doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/")
|
||||
}
|
||||
|
@ -2128,6 +2128,47 @@ impl HasVisibility for Function {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ExternCrateDecl {
|
||||
pub(crate) id: ExternCrateId,
|
||||
}
|
||||
|
||||
impl ExternCrateDecl {
|
||||
pub fn module(self, db: &dyn HirDatabase) -> Module {
|
||||
self.id.module(db.upcast()).into()
|
||||
}
|
||||
|
||||
pub fn resolved_crate(self, db: &dyn HirDatabase) -> Option<Crate> {
|
||||
db.extern_crate_decl_data(self.id).crate_id.map(Into::into)
|
||||
}
|
||||
|
||||
pub fn name(self, db: &dyn HirDatabase) -> Name {
|
||||
db.extern_crate_decl_data(self.id).name.clone()
|
||||
}
|
||||
|
||||
pub fn alias(self, db: &dyn HirDatabase) -> Option<ImportAlias> {
|
||||
db.extern_crate_decl_data(self.id).alias.clone()
|
||||
}
|
||||
|
||||
/// Returns the name under which this crate is made accessible, taking `_` into account.
|
||||
pub fn alias_or_name(self, db: &dyn HirDatabase) -> Option<Name> {
|
||||
let extern_crate_decl_data = db.extern_crate_decl_data(self.id);
|
||||
match &extern_crate_decl_data.alias {
|
||||
Some(ImportAlias::Underscore) => None,
|
||||
Some(ImportAlias::Alias(alias)) => Some(alias.clone()),
|
||||
None => Some(extern_crate_decl_data.name.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasVisibility for ExternCrateDecl {
|
||||
fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
|
||||
db.extern_crate_decl_data(self.id)
|
||||
.visibility
|
||||
.resolve(db.upcast(), &self.id.resolver(db.upcast()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct InTypeConst {
|
||||
pub(crate) id: InTypeConstId,
|
||||
|
@ -4715,6 +4756,12 @@ pub trait HasContainer {
|
|||
fn container(&self, db: &dyn HirDatabase) -> ItemContainer;
|
||||
}
|
||||
|
||||
impl HasContainer for ExternCrateDecl {
|
||||
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
|
||||
container_id_to_hir(self.id.lookup(db.upcast()).container.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl HasContainer for Module {
|
||||
fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
|
||||
// FIXME: handle block expressions as modules (their parent is in a different DefMap)
|
||||
|
|
|
@ -15,11 +15,7 @@ use hir_def::{
|
|||
type_ref::Mutability,
|
||||
AsMacroCall, DefWithBodyId, FieldId, FunctionId, MacroId, TraitId, VariantId,
|
||||
};
|
||||
use hir_expand::{
|
||||
db::ExpandDatabase,
|
||||
name::{known, AsName},
|
||||
ExpansionInfo, MacroCallId,
|
||||
};
|
||||
use hir_expand::{db::ExpandDatabase, name::AsName, ExpansionInfo, MacroCallId};
|
||||
use itertools::Itertools;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
|
@ -439,10 +435,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
|
|||
self.imp.resolve_path(path)
|
||||
}
|
||||
|
||||
pub fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option<Crate> {
|
||||
self.imp.resolve_extern_crate(extern_crate)
|
||||
}
|
||||
|
||||
pub fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantDef> {
|
||||
self.imp.resolve_variant(record_lit).map(VariantDef::from)
|
||||
}
|
||||
|
@ -1242,18 +1234,6 @@ impl<'db> SemanticsImpl<'db> {
|
|||
self.analyze(path.syntax())?.resolve_path(self.db, path)
|
||||
}
|
||||
|
||||
fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option<Crate> {
|
||||
let krate = self.scope(extern_crate.syntax())?.krate();
|
||||
let name = extern_crate.name_ref()?.as_name();
|
||||
if name == known::SELF_PARAM {
|
||||
return Some(krate);
|
||||
}
|
||||
krate
|
||||
.dependencies(self.db)
|
||||
.into_iter()
|
||||
.find_map(|dep| (dep.name == name).then_some(dep.krate))
|
||||
}
|
||||
|
||||
fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {
|
||||
self.analyze(record_lit.syntax())?.resolve_variant(self.db, record_lit)
|
||||
}
|
||||
|
@ -1603,6 +1583,7 @@ to_def_impls![
|
|||
(crate::Local, ast::SelfParam, self_param_to_def),
|
||||
(crate::Label, ast::Label, label_to_def),
|
||||
(crate::Adt, ast::Adt, adt_to_def),
|
||||
(crate::ExternCrateDecl, ast::ExternCrate, extern_crate_to_def),
|
||||
];
|
||||
|
||||
fn find_root(node: &SyntaxNode) -> SyntaxNode {
|
||||
|
|
|
@ -93,9 +93,9 @@ use hir_def::{
|
|||
DynMap,
|
||||
},
|
||||
hir::{BindingId, LabelId},
|
||||
AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId,
|
||||
GenericDefId, GenericParamId, ImplId, LifetimeParamId, MacroId, ModuleId, StaticId, StructId,
|
||||
TraitAliasId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
|
||||
AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternCrateId, FieldId,
|
||||
FunctionId, GenericDefId, GenericParamId, ImplId, LifetimeParamId, MacroId, ModuleId, StaticId,
|
||||
StructId, TraitAliasId, TraitId, TypeAliasId, TypeParamId, UnionId, UseId, VariantId,
|
||||
};
|
||||
use hir_expand::{attrs::AttrId, name::AsName, HirFileId, MacroCallId};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
@ -203,6 +203,16 @@ impl SourceToDefCtx<'_, '_> {
|
|||
) -> Option<EnumVariantId> {
|
||||
self.to_def(src, keys::VARIANT)
|
||||
}
|
||||
pub(super) fn extern_crate_to_def(
|
||||
&mut self,
|
||||
src: InFile<ast::ExternCrate>,
|
||||
) -> Option<ExternCrateId> {
|
||||
self.to_def(src, keys::EXTERN_CRATE)
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub(super) fn use_to_def(&mut self, src: InFile<ast::Use>) -> Option<UseId> {
|
||||
self.to_def(src, keys::USE)
|
||||
}
|
||||
pub(super) fn adt_to_def(
|
||||
&mut self,
|
||||
InFile { file_id, value }: InFile<ast::Adt>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue