Skip to content

Commit bc287b8

Browse files
committed
Unconfuse expression and pattern field init shorthands
1 parent fa3c449 commit bc287b8

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

crates/ide/src/syntax_highlighting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ fn highlight_element(
459459
highlight_def(db, def) | HighlightModifier::Definition
460460
}
461461
Some(NameClass::ConstReference(def)) => highlight_def(db, def),
462-
Some(NameClass::FieldShorthand { field, .. }) => {
462+
Some(NameClass::PatFieldShorthand { field, .. }) => {
463463
let mut h = HighlightTag::Field.into();
464464
if let Definition::Field(field) = field {
465465
if let VariantDef::Union(_) = field.parent_def(db) {

crates/ide_db/src/defs.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,31 @@ pub enum NameClass {
8383
Definition(Definition),
8484
/// `None` in `if let None = Some(82) {}`
8585
ConstReference(Definition),
86-
FieldShorthand {
86+
/// `field` in `if let Foo { field } = todo!() {}`
87+
PatFieldShorthand {
8788
local: Local,
8889
field: Definition,
8990
},
9091
}
9192

9293
impl NameClass {
9394
pub fn definition(self, db: &dyn HirDatabase) -> Option<Definition> {
94-
Some(match self {
95+
let res = match self {
9596
NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()),
9697
NameClass::Definition(it) => it,
9798
NameClass::ConstReference(_) => return None,
98-
NameClass::FieldShorthand { local, field: _ } => Definition::Local(local),
99-
})
99+
/// Both `local` and `field` are definitions here, but only `local`
100+
/// is the definition which is introduced by this name.
101+
NameClass::PatFieldShorthand { local, field: _ } => Definition::Local(local),
102+
};
103+
Some(res)
100104
}
101105

102106
pub fn definition_or_reference(self, db: &dyn HirDatabase) -> Definition {
103107
match self {
104108
NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()),
105109
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
106-
NameClass::FieldShorthand { local: _, field } => field,
110+
NameClass::PatFieldShorthand { local: _, field } => field,
107111
}
108112
}
109113
}
@@ -161,7 +165,7 @@ pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option
161165
if record_pat_field.name_ref().is_none() {
162166
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
163167
let field = Definition::Field(field);
164-
return Some(NameClass::FieldShorthand { local, field });
168+
return Some(NameClass::PatFieldShorthand { local, field });
165169
}
166170
}
167171
}

crates/ide_db/src/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<'a> FindUsages<'a> {
314314

315315
fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool {
316316
match classify_name(self.sema, name) {
317-
Some(NameClass::FieldShorthand { local: _, field }) => {
317+
Some(NameClass::PatFieldShorthand { local: _, field }) => {
318318
let reference = match self.def {
319319
Definition::Field(_) if &field == self.def => Reference {
320320
file_range: self.sema.original_range(name.syntax()),

0 commit comments

Comments
 (0)