From bc287b8f9b7711a38d97be3b619758bb05d54c45 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Oct 2020 16:23:55 +0200 Subject: Unconfuse expression and pattern field init shorthands --- crates/ide/src/syntax_highlighting.rs | 2 +- crates/ide_db/src/defs.rs | 16 ++++++++++------ crates/ide_db/src/search.rs | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 527888306..4d8cd98c7 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -459,7 +459,7 @@ fn highlight_element( highlight_def(db, def) | HighlightModifier::Definition } Some(NameClass::ConstReference(def)) => highlight_def(db, def), - Some(NameClass::FieldShorthand { field, .. }) => { + Some(NameClass::PatFieldShorthand { field, .. }) => { let mut h = HighlightTag::Field.into(); if let Definition::Field(field) = field { if let VariantDef::Union(_) = field.parent_def(db) { diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index a243dcd15..a2682d73c 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -83,7 +83,8 @@ pub enum NameClass { Definition(Definition), /// `None` in `if let None = Some(82) {}` ConstReference(Definition), - FieldShorthand { + /// `field` in `if let Foo { field } = todo!() {}` + PatFieldShorthand { local: Local, field: Definition, }, @@ -91,19 +92,22 @@ pub enum NameClass { impl NameClass { pub fn definition(self, db: &dyn HirDatabase) -> Option { - Some(match self { + let res = match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) => it, NameClass::ConstReference(_) => return None, - NameClass::FieldShorthand { local, field: _ } => Definition::Local(local), - }) + /// Both `local` and `field` are definitions here, but only `local` + /// is the definition which is introduced by this name. + NameClass::PatFieldShorthand { local, field: _ } => Definition::Local(local), + }; + Some(res) } pub fn definition_or_reference(self, db: &dyn HirDatabase) -> Definition { match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) | NameClass::ConstReference(it) => it, - NameClass::FieldShorthand { local: _, field } => field, + NameClass::PatFieldShorthand { local: _, field } => field, } } } @@ -161,7 +165,7 @@ pub fn classify_name(sema: &Semantics, name: &ast::Name) -> Option if record_pat_field.name_ref().is_none() { if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) { let field = Definition::Field(field); - return Some(NameClass::FieldShorthand { local, field }); + return Some(NameClass::PatFieldShorthand { local, field }); } } } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 8e3dcd99c..43b8560ca 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -314,7 +314,7 @@ impl<'a> FindUsages<'a> { fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool { match classify_name(self.sema, name) { - Some(NameClass::FieldShorthand { local: _, field }) => { + Some(NameClass::PatFieldShorthand { local: _, field }) => { let reference = match self.def { Definition::Field(_) if &field == self.def => Reference { file_range: self.sema.original_range(name.syntax()), -- cgit v1.2.3