mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 21:35:20 +00:00
remove Cancelable from adt API
This commit is contained in:
parent
05149d3532
commit
9446be2999
8 changed files with 41 additions and 58 deletions
|
@ -19,7 +19,7 @@ impl Struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
|
pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
|
||||||
Ok(db.struct_data(self.def_id)?.variant_data.clone())
|
Ok(db.struct_data(self.def_id).variant_data.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,16 +37,13 @@ impl StructData {
|
||||||
StructData { name, variant_data }
|
StructData { name, variant_data }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn struct_data_query(
|
pub(crate) fn struct_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<StructData> {
|
||||||
db: &impl HirDatabase,
|
|
||||||
def_id: DefId,
|
|
||||||
) -> Cancelable<Arc<StructData>> {
|
|
||||||
let def_loc = def_id.loc(db);
|
let def_loc = def_id.loc(db);
|
||||||
assert!(def_loc.kind == DefKind::Struct);
|
assert!(def_loc.kind == DefKind::Struct);
|
||||||
let syntax = db.file_item(def_loc.source_item_id);
|
let syntax = db.file_item(def_loc.source_item_id);
|
||||||
let struct_def =
|
let struct_def =
|
||||||
ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node");
|
ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node");
|
||||||
Ok(Arc::new(StructData::new(struct_def)))
|
Arc::new(StructData::new(struct_def))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,10 +81,7 @@ impl EnumData {
|
||||||
EnumData { name, variants }
|
EnumData { name, variants }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn enum_data_query(
|
pub(crate) fn enum_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<EnumData> {
|
||||||
db: &impl HirDatabase,
|
|
||||||
def_id: DefId,
|
|
||||||
) -> Cancelable<Arc<EnumData>> {
|
|
||||||
let def_loc = def_id.loc(db);
|
let def_loc = def_id.loc(db);
|
||||||
assert!(def_loc.kind == DefKind::Enum);
|
assert!(def_loc.kind == DefKind::Enum);
|
||||||
let syntax = db.file_item(def_loc.source_item_id);
|
let syntax = db.file_item(def_loc.source_item_id);
|
||||||
|
@ -107,7 +101,7 @@ impl EnumData {
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
};
|
};
|
||||||
Ok(Arc::new(EnumData::new(enum_def, variants)))
|
Arc::new(EnumData::new(enum_def, variants))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +127,7 @@ impl EnumVariantData {
|
||||||
pub(crate) fn enum_variant_data_query(
|
pub(crate) fn enum_variant_data_query(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
def_id: DefId,
|
def_id: DefId,
|
||||||
) -> Cancelable<Arc<EnumVariantData>> {
|
) -> Arc<EnumVariantData> {
|
||||||
let def_loc = def_id.loc(db);
|
let def_loc = def_id.loc(db);
|
||||||
assert!(def_loc.kind == DefKind::EnumVariant);
|
assert!(def_loc.kind == DefKind::EnumVariant);
|
||||||
let syntax = db.file_item(def_loc.source_item_id);
|
let syntax = db.file_item(def_loc.source_item_id);
|
||||||
|
@ -146,10 +140,7 @@ impl EnumVariantData {
|
||||||
.expect("enum variant list should have enum ancestor");
|
.expect("enum variant list should have enum ancestor");
|
||||||
let enum_def_id = get_def_id(db, &def_loc, enum_node, DefKind::Enum);
|
let enum_def_id = get_def_id(db, &def_loc, enum_node, DefKind::Enum);
|
||||||
|
|
||||||
Ok(Arc::new(EnumVariantData::new(
|
Arc::new(EnumVariantData::new(variant_def, Enum::new(enum_def_id)))
|
||||||
variant_def,
|
|
||||||
Enum::new(enum_def_id),
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -175,13 +175,12 @@ impl Struct {
|
||||||
self.def_id
|
self.def_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
|
||||||
Ok(db.struct_data(self.def_id)?.name.clone())
|
db.struct_data(self.def_id).name.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fields(&self, db: &impl HirDatabase) -> Cancelable<Vec<StructField>> {
|
pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
|
||||||
let res = db
|
db.struct_data(self.def_id)
|
||||||
.struct_data(self.def_id)?
|
|
||||||
.variant_data
|
.variant_data
|
||||||
.fields()
|
.fields()
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -189,15 +188,11 @@ impl Struct {
|
||||||
struct_: self.clone(),
|
struct_: self.clone(),
|
||||||
name: it.name.clone(),
|
name: it.name.clone(),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect()
|
||||||
Ok(res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source(
|
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
|
||||||
&self,
|
def_id_to_ast(db, self.def_id)
|
||||||
db: &impl HirDatabase,
|
|
||||||
) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
|
|
||||||
Ok(def_id_to_ast(db, self.def_id))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,16 +210,16 @@ impl Enum {
|
||||||
self.def_id
|
self.def_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
|
||||||
Ok(db.enum_data(self.def_id)?.name.clone())
|
db.enum_data(self.def_id).name.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, EnumVariant)>> {
|
pub fn variants(&self, db: &impl HirDatabase) -> Vec<(Name, EnumVariant)> {
|
||||||
Ok(db.enum_data(self.def_id)?.variants.clone())
|
db.enum_data(self.def_id).variants.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
|
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
|
||||||
Ok(def_id_to_ast(db, self.def_id))
|
def_id_to_ast(db, self.def_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,23 +237,20 @@ impl EnumVariant {
|
||||||
self.def_id
|
self.def_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parent_enum(&self, db: &impl HirDatabase) -> Cancelable<Enum> {
|
pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum {
|
||||||
Ok(db.enum_variant_data(self.def_id)?.parent_enum.clone())
|
db.enum_variant_data(self.def_id).parent_enum.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
|
||||||
Ok(db.enum_variant_data(self.def_id)?.name.clone())
|
db.enum_variant_data(self.def_id).name.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
|
pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
|
||||||
Ok(db.enum_variant_data(self.def_id)?.variant_data.clone())
|
db.enum_variant_data(self.def_id).variant_data.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source(
|
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
|
||||||
&self,
|
def_id_to_ast(db, self.def_id)
|
||||||
db: &impl HirDatabase,
|
|
||||||
) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
|
|
||||||
Ok(def_id_to_ast(db, self.def_id))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,7 +154,7 @@ impl Module {
|
||||||
if segments.len() == idx + 1 {
|
if segments.len() == idx + 1 {
|
||||||
// enum variant
|
// enum variant
|
||||||
let matching_variant =
|
let matching_variant =
|
||||||
e.variants(db)?.into_iter().find(|(n, _variant)| n == name);
|
e.variants(db).into_iter().find(|(n, _variant)| n == name);
|
||||||
|
|
||||||
if let Some((_n, variant)) = matching_variant {
|
if let Some((_n, variant)) = matching_variant {
|
||||||
return Ok(PerNs::both(variant.def_id(), e.def_id()));
|
return Ok(PerNs::both(variant.def_id(), e.def_id()));
|
||||||
|
|
|
@ -37,17 +37,17 @@ pub trait HirDatabase: SyntaxDatabase
|
||||||
use fn query_definitions::fn_scopes;
|
use fn query_definitions::fn_scopes;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_data(def_id: DefId) -> Cancelable<Arc<StructData>> {
|
fn struct_data(def_id: DefId) -> Arc<StructData> {
|
||||||
type StructDataQuery;
|
type StructDataQuery;
|
||||||
use fn crate::adt::StructData::struct_data_query;
|
use fn crate::adt::StructData::struct_data_query;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enum_data(def_id: DefId) -> Cancelable<Arc<EnumData>> {
|
fn enum_data(def_id: DefId) -> Arc<EnumData> {
|
||||||
type EnumDataQuery;
|
type EnumDataQuery;
|
||||||
use fn crate::adt::EnumData::enum_data_query;
|
use fn crate::adt::EnumData::enum_data_query;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enum_variant_data(def_id: DefId) -> Cancelable<Arc<EnumVariantData>> {
|
fn enum_variant_data(def_id: DefId) -> Arc<EnumVariantData> {
|
||||||
type EnumVariantDataQuery;
|
type EnumVariantDataQuery;
|
||||||
use fn crate::adt::EnumVariantData::enum_variant_data_query;
|
use fn crate::adt::EnumVariantData::enum_variant_data_query;
|
||||||
}
|
}
|
||||||
|
|
|
@ -463,19 +463,19 @@ fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> {
|
||||||
fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> {
|
fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable<Ty> {
|
||||||
Ok(Ty::Adt {
|
Ok(Ty::Adt {
|
||||||
def_id: s.def_id(),
|
def_id: s.def_id(),
|
||||||
name: s.name(db)?.unwrap_or_else(Name::missing),
|
name: s.name(db).unwrap_or_else(Name::missing),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> {
|
pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable<Ty> {
|
||||||
Ok(Ty::Adt {
|
Ok(Ty::Adt {
|
||||||
def_id: s.def_id(),
|
def_id: s.def_id(),
|
||||||
name: s.name(db)?.unwrap_or_else(Name::missing),
|
name: s.name(db).unwrap_or_else(Name::missing),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable<Ty> {
|
pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable<Ty> {
|
||||||
let enum_parent = ev.parent_enum(db)?;
|
let enum_parent = ev.parent_enum(db);
|
||||||
|
|
||||||
type_for_enum(db, enum_parent)
|
type_for_enum(db, enum_parent)
|
||||||
}
|
}
|
||||||
|
@ -510,7 +510,7 @@ pub(super) fn type_for_field(
|
||||||
let def = def_id.resolve(db)?;
|
let def = def_id.resolve(db)?;
|
||||||
let variant_data = match def {
|
let variant_data = match def {
|
||||||
Def::Struct(s) => s.variant_data(db)?,
|
Def::Struct(s) => s.variant_data(db)?,
|
||||||
Def::EnumVariant(ev) => ev.variant_data(db)?,
|
Def::EnumVariant(ev) => ev.variant_data(db),
|
||||||
// TODO: unions
|
// TODO: unions
|
||||||
_ => panic!(
|
_ => panic!(
|
||||||
"trying to get type for field in non-struct/variant {:?}",
|
"trying to get type for field in non-struct/variant {:?}",
|
||||||
|
|
|
@ -29,7 +29,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
|
||||||
Ty::Adt { def_id, .. } => {
|
Ty::Adt { def_id, .. } => {
|
||||||
match def_id.resolve(ctx.db)? {
|
match def_id.resolve(ctx.db)? {
|
||||||
Def::Struct(s) => {
|
Def::Struct(s) => {
|
||||||
for field in s.fields(ctx.db)? {
|
for field in s.fields(ctx.db) {
|
||||||
CompletionItem::new(
|
CompletionItem::new(
|
||||||
CompletionKind::Reference,
|
CompletionKind::Reference,
|
||||||
field.name().to_string(),
|
field.name().to_string(),
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hir::Def::Enum(e) => {
|
hir::Def::Enum(e) => {
|
||||||
e.variants(ctx.db)?
|
e.variants(ctx.db)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|(variant_name, _variant)| {
|
.for_each(|(variant_name, _variant)| {
|
||||||
CompletionItem::new(CompletionKind::Reference, variant_name.to_string())
|
CompletionItem::new(CompletionKind::Reference, variant_name.to_string())
|
||||||
|
|
|
@ -112,15 +112,15 @@ impl NavigationTarget {
|
||||||
pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
|
pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
|
||||||
let res = match def {
|
let res = match def {
|
||||||
Def::Struct(s) => {
|
Def::Struct(s) => {
|
||||||
let (file_id, node) = s.source(db)?;
|
let (file_id, node) = s.source(db);
|
||||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||||
}
|
}
|
||||||
Def::Enum(e) => {
|
Def::Enum(e) => {
|
||||||
let (file_id, node) = e.source(db)?;
|
let (file_id, node) = e.source(db);
|
||||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||||
}
|
}
|
||||||
Def::EnumVariant(ev) => {
|
Def::EnumVariant(ev) => {
|
||||||
let (file_id, node) = ev.source(db)?;
|
let (file_id, node) = ev.source(db);
|
||||||
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
||||||
}
|
}
|
||||||
Def::Function(f) => {
|
Def::Function(f) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue