Skip to content

Commit 0feafe8

Browse files
bors[bot]Veykril
andauthored
Merge #11411
11411: minor: Simplify r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 9597e55 + ec677e3 commit 0feafe8

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

crates/hir_expand/src/lib.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ pub mod quote;
1616
pub mod eager;
1717
pub mod mod_path;
1818

19-
use base_db::ProcMacroKind;
20-
use either::Either;
21-
2219
pub use mbe::{ExpandError, ExpandResult, Origin};
23-
use mod_path::ModPath;
2420

2521
use std::{hash::Hash, iter, sync::Arc};
2622

27-
use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
23+
use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange, ProcMacroKind};
24+
use either::Either;
2825
use syntax::{
2926
algo::{self, skip_trivia_token},
3027
ast::{self, AstNode, HasDocComments},
@@ -37,6 +34,7 @@ use crate::{
3734
builtin_derive_macro::BuiltinDeriveExpander,
3835
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
3936
db::TokenExpander,
37+
mod_path::ModPath,
4038
proc_macro::ProcMacroExpander,
4139
};
4240

@@ -61,11 +59,13 @@ enum HirFileIdRepr {
6159
FileId(FileId),
6260
MacroFile(MacroFile),
6361
}
62+
6463
impl From<FileId> for HirFileId {
6564
fn from(id: FileId) -> Self {
6665
HirFileId(HirFileIdRepr::FileId(id))
6766
}
6867
}
68+
6969
impl From<MacroFile> for HirFileId {
7070
fn from(id: MacroFile) -> Self {
7171
HirFileId(HirFileIdRepr::MacroFile(id))
@@ -151,8 +151,8 @@ impl HirFileId {
151151
HirFileIdRepr::FileId(file_id) => file_id,
152152
HirFileIdRepr::MacroFile(macro_file) => {
153153
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
154-
let file_id = match &loc.eager {
155-
Some(EagerCallInfo { included_file: Some(file), .. }) => (*file).into(),
154+
let file_id = match loc.eager {
155+
Some(EagerCallInfo { included_file: Some(file), .. }) => file.into(),
156156
_ => loc.kind.file_id(),
157157
};
158158
file_id.original_file(db)
@@ -249,10 +249,7 @@ impl HirFileId {
249249
HirFileIdRepr::FileId(_) => false,
250250
HirFileIdRepr::MacroFile(macro_file) => {
251251
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
252-
match loc.def.kind {
253-
MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _) => true,
254-
_ => false,
255-
}
252+
matches!(loc.def.kind, MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _))
256253
}
257254
}
258255
}
@@ -302,15 +299,15 @@ impl MacroDefId {
302299
}
303300

304301
pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
305-
let id = match &self.kind {
306-
MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
302+
let id = match self.kind {
303+
MacroDefKind::ProcMacro(.., id) => return Either::Right(id),
307304
MacroDefKind::Declarative(id)
308305
| MacroDefKind::BuiltIn(_, id)
309306
| MacroDefKind::BuiltInAttr(_, id)
310307
| MacroDefKind::BuiltInDerive(_, id)
311308
| MacroDefKind::BuiltInEager(_, id) => id,
312309
};
313-
Either::Left(*id)
310+
Either::Left(id)
314311
}
315312

316313
pub fn is_proc_macro(&self) -> bool {
@@ -359,20 +356,15 @@ impl MacroCallKind {
359356
/// get only the specific derive that is being referred to.
360357
pub fn original_call_range(self, db: &dyn db::AstDatabase) -> FileRange {
361358
let mut kind = self;
362-
loop {
359+
let file_id = loop {
363360
match kind.file_id().0 {
364361
HirFileIdRepr::MacroFile(file) => {
365362
kind = db.lookup_intern_macro_call(file.macro_call_id).kind;
366363
}
367-
_ => break,
364+
HirFileIdRepr::FileId(file_id) => break file_id,
368365
}
369-
}
370-
371-
// `call_id` is now the outermost macro call, so its location is in a real file.
372-
let file_id = match kind.file_id().0 {
373-
HirFileIdRepr::FileId(it) => it,
374-
HirFileIdRepr::MacroFile(_) => unreachable!("encountered unexpected macro file"),
375366
};
367+
376368
let range = match kind {
377369
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
378370
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
@@ -574,7 +566,6 @@ impl ExpansionInfo {
574566
/// `AstId` points to an AST node in any file.
575567
///
576568
/// It is stable across reparses, and can be used as salsa key/value.
577-
// FIXME: isn't this just a `Source<FileAstId<N>>` ?
578569
pub type AstId<N> = InFile<FileAstId<N>>;
579570

580571
impl<N: AstNode> AstId<N> {
@@ -602,7 +593,6 @@ impl<T> InFile<T> {
602593
InFile { file_id, value }
603594
}
604595

605-
// Similarly, naming here is stupid...
606596
pub fn with_value<U>(&self, value: U) -> InFile<U> {
607597
InFile::new(self.file_id, value)
608598
}

0 commit comments

Comments
 (0)