Skip to content

Commit e1dbf43

Browse files
committed
Replace usage of ast::NameOrNameRef with ast::NameLike
1 parent e52bdc5 commit e1dbf43

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

crates/hir_expand/src/name.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,18 @@ impl AsName for ast::Name {
8787
}
8888
}
8989

90-
impl AsName for ast::NameOrNameRef {
90+
impl AsName for ast::Lifetime {
91+
fn as_name(&self) -> Name {
92+
Name::resolve(self.text())
93+
}
94+
}
95+
96+
impl AsName for ast::NameLike {
9197
fn as_name(&self) -> Name {
9298
match self {
93-
ast::NameOrNameRef::Name(it) => it.as_name(),
94-
ast::NameOrNameRef::NameRef(it) => it.as_name(),
99+
ast::NameLike::Name(it) => it.as_name(),
100+
ast::NameLike::NameRef(it) => it.as_name(),
101+
ast::NameLike::Lifetime(it) => it.as_name(),
95102
}
96103
}
97104
}

crates/syntax/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub use self::{
1919
expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
2020
generated::{nodes::*, tokens::*},
2121
node_ext::{
22-
AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
23-
SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind,
22+
AttrKind, FieldKind, Macro, NameLike, PathSegmentKind, SelfParamKind, SlicePatComponents,
23+
StructKind, TypeBoundKind, VisibilityKind,
2424
},
2525
token_ext::*,
2626
traits::*,

crates/syntax/src/ast/node_ext.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl ast::RecordExprField {
297297
}
298298
}
299299

300-
#[derive(Debug, Clone)]
300+
#[derive(Debug, Clone, PartialEq)]
301301
pub enum NameLike {
302302
NameRef(ast::NameRef),
303303
Name(ast::Name),
@@ -335,6 +335,16 @@ impl ast::AstNode for NameLike {
335335
}
336336
}
337337

338+
impl fmt::Display for NameLike {
339+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
340+
match self {
341+
NameLike::Name(it) => fmt::Display::fmt(it, f),
342+
NameLike::NameRef(it) => fmt::Display::fmt(it, f),
343+
NameLike::Lifetime(it) => fmt::Display::fmt(it, f),
344+
}
345+
}
346+
}
347+
338348
mod __ {
339349
use super::{
340350
ast::{Lifetime, Name, NameRef},
@@ -343,26 +353,11 @@ mod __ {
343353
stdx::impl_from!(NameRef, Name, Lifetime for NameLike);
344354
}
345355

346-
#[derive(Debug, Clone, PartialEq)]
347-
pub enum NameOrNameRef {
348-
Name(ast::Name),
349-
NameRef(ast::NameRef),
350-
}
351-
352-
impl fmt::Display for NameOrNameRef {
353-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
354-
match self {
355-
NameOrNameRef::Name(it) => fmt::Display::fmt(it, f),
356-
NameOrNameRef::NameRef(it) => fmt::Display::fmt(it, f),
357-
}
358-
}
359-
}
360-
361356
impl ast::RecordPatField {
362357
pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> {
363358
let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?;
364359
match candidate.field_name()? {
365-
NameOrNameRef::NameRef(name_ref) if name_ref == *field_name => Some(candidate),
360+
NameLike::NameRef(name_ref) if name_ref == *field_name => Some(candidate),
366361
_ => None,
367362
}
368363
}
@@ -371,19 +366,19 @@ impl ast::RecordPatField {
371366
let candidate =
372367
field_name.syntax().ancestors().nth(2).and_then(ast::RecordPatField::cast)?;
373368
match candidate.field_name()? {
374-
NameOrNameRef::Name(name) if name == *field_name => Some(candidate),
369+
NameLike::Name(name) if name == *field_name => Some(candidate),
375370
_ => None,
376371
}
377372
}
378373

379374
/// Deals with field init shorthand
380-
pub fn field_name(&self) -> Option<NameOrNameRef> {
375+
pub fn field_name(&self) -> Option<NameLike> {
381376
if let Some(name_ref) = self.name_ref() {
382-
return Some(NameOrNameRef::NameRef(name_ref));
377+
return Some(NameLike::NameRef(name_ref));
383378
}
384379
if let Some(ast::Pat::IdentPat(pat)) = self.pat() {
385380
let name = pat.name()?;
386-
return Some(NameOrNameRef::Name(name));
381+
return Some(NameLike::Name(name));
387382
}
388383
None
389384
}

0 commit comments

Comments
 (0)