Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a2a3fec

Browse files
committed
Option begone part 2
1 parent 96a7742 commit a2a3fec

31 files changed

+114
-153
lines changed

crates/hir-def/src/attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl AttrsWithOwner {
498498
AttrDefId::FieldId(id) => {
499499
let map = db.fields_attrs_source_map(id.parent);
500500
let file_id = id.parent.file_id(db);
501-
let root = db.parse_or_expand(file_id).unwrap();
501+
let root = db.parse_or_expand(file_id);
502502
let owner = match &map[id.local_id] {
503503
Either::Left(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
504504
Either::Right(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
@@ -514,7 +514,7 @@ impl AttrsWithOwner {
514514
AttrDefId::EnumVariantId(id) => {
515515
let map = db.variants_attrs_source_map(id.parent);
516516
let file_id = id.parent.lookup(db).id.file_id();
517-
let root = db.parse_or_expand(file_id).unwrap();
517+
let root = db.parse_or_expand(file_id);
518518
InFile::new(file_id, ast::AnyHasAttrs::new(map[id.local_id].to_node(&root)))
519519
}
520520
AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),

crates/hir-def/src/body.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,12 @@ impl Expander {
174174
fn enter_expand_inner(
175175
db: &dyn DefDatabase,
176176
call_id: MacroCallId,
177-
mut error: Option<ExpandError>,
177+
error: Option<ExpandError>,
178178
) -> ExpandResult<Option<InFile<Parse<SyntaxNode>>>> {
179179
let file_id = call_id.as_file();
180180
let ExpandResult { value, err } = db.parse_or_expand_with_err(file_id);
181181

182-
if error.is_none() {
183-
error = err;
184-
}
185-
186-
let parse = match value {
187-
Some(it) => it,
188-
None => {
189-
// Only `None` if the macro expansion produced no usable AST.
190-
if error.is_none() {
191-
tracing::warn!("no error despite `parse_or_expand` failing");
192-
}
193-
194-
return ExpandResult::only_err(error.unwrap_or_else(|| {
195-
ExpandError::Other("failed to parse macro invocation".into())
196-
}));
197-
}
198-
};
199-
200-
ExpandResult { value: Some(InFile::new(file_id, parse)), err: error }
182+
ExpandResult { value: Some(InFile::new(file_id, value)), err: error.or(err) }
201183
}
202184

203185
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {

crates/hir-def/src/data.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -640,22 +640,20 @@ impl<'a> AssocItemCollector<'a> {
640640
AssocItem::MacroCall(call) => {
641641
let file_id = self.expander.current_file_id();
642642
let root = self.db.parse_or_expand(file_id);
643-
if let Some(root) = root {
644-
let call = &item_tree[call];
645-
646-
let ast_id_map = self.db.ast_id_map(file_id);
647-
let macro_call = ast_id_map.get(call.ast_id).to_node(&root);
648-
let _cx = stdx::panic_context::enter(format!(
649-
"collect_items MacroCall: {macro_call}"
650-
));
651-
if let Ok(res) =
652-
self.expander.enter_expand::<ast::MacroItems>(self.db, macro_call)
653-
{
654-
self.collect_macro_items(res, &|| hir_expand::MacroCallKind::FnLike {
655-
ast_id: InFile::new(file_id, call.ast_id),
656-
expand_to: hir_expand::ExpandTo::Items,
657-
});
658-
}
643+
let call = &item_tree[call];
644+
645+
let ast_id_map = self.db.ast_id_map(file_id);
646+
let macro_call = ast_id_map.get(call.ast_id).to_node(&root);
647+
let _cx = stdx::panic_context::enter(format!(
648+
"collect_items MacroCall: {macro_call}"
649+
));
650+
if let Ok(res) =
651+
self.expander.enter_expand::<ast::MacroItems>(self.db, macro_call)
652+
{
653+
self.collect_macro_items(res, &|| hir_expand::MacroCallKind::FnLike {
654+
ast_id: InFile::new(file_id, call.ast_id),
655+
expand_to: hir_expand::ExpandTo::Items,
656+
});
659657
}
660658
}
661659
}

crates/hir-def/src/item_tree.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ pub struct ItemTree {
108108
impl ItemTree {
109109
pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
110110
let _p = profile::span("file_item_tree_query").detail(|| format!("{file_id:?}"));
111-
let syntax = match db.parse_or_expand(file_id) {
112-
Some(node) => node,
113-
None => return Default::default(),
114-
};
111+
let syntax = db.parse_or_expand(file_id);
115112
if never!(syntax.kind() == SyntaxKind::ERROR, "{:?} from {:?} {}", file_id, syntax, syntax)
116113
{
117114
// FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic

crates/hir-def/src/src.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<N: ItemTreeNode> HasSource for AssocItemLoc<N> {
2020
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
2121
let tree = self.id.item_tree(db);
2222
let ast_id_map = db.ast_id_map(self.id.file_id());
23-
let root = db.parse_or_expand(self.id.file_id()).unwrap();
23+
let root = db.parse_or_expand(self.id.file_id());
2424
let node = &tree[self.id.value];
2525

2626
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
@@ -33,7 +33,7 @@ impl<N: ItemTreeNode> HasSource for ItemLoc<N> {
3333
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
3434
let tree = self.id.item_tree(db);
3535
let ast_id_map = db.ast_id_map(self.id.file_id());
36-
let root = db.parse_or_expand(self.id.file_id()).unwrap();
36+
let root = db.parse_or_expand(self.id.file_id());
3737
let node = &tree[self.id.value];
3838

3939
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
@@ -46,7 +46,7 @@ impl HasSource for Macro2Loc {
4646
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
4747
let tree = self.id.item_tree(db);
4848
let ast_id_map = db.ast_id_map(self.id.file_id());
49-
let root = db.parse_or_expand(self.id.file_id()).unwrap();
49+
let root = db.parse_or_expand(self.id.file_id());
5050
let node = &tree[self.id.value];
5151

5252
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
@@ -59,7 +59,7 @@ impl HasSource for MacroRulesLoc {
5959
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
6060
let tree = self.id.item_tree(db);
6161
let ast_id_map = db.ast_id_map(self.id.file_id());
62-
let root = db.parse_or_expand(self.id.file_id()).unwrap();
62+
let root = db.parse_or_expand(self.id.file_id());
6363
let node = &tree[self.id.value];
6464

6565
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
@@ -72,7 +72,7 @@ impl HasSource for ProcMacroLoc {
7272
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
7373
let tree = self.id.item_tree(db);
7474
let ast_id_map = db.ast_id_map(self.id.file_id());
75-
let root = db.parse_or_expand(self.id.file_id()).unwrap();
75+
let root = db.parse_or_expand(self.id.file_id());
7676
let node = &tree[self.id.value];
7777

7878
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))

crates/hir-expand/src/builtin_derive_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
198198
fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
199199
let info = match parse_adt(tt) {
200200
Ok(info) => info,
201-
Err(e) => return ExpandResult::with_err(tt::Subtree::empty(), e),
201+
Err(e) => return ExpandResult::new(tt::Subtree::empty(), e),
202202
};
203203
let mut where_block = vec![];
204204
let (params, args): (Vec<_>, Vec<_>) = info

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,7 @@ fn format_args_expand(
249249
let mut args = parse_exprs_with_sep(tt, ',');
250250

251251
if args.is_empty() {
252-
return ExpandResult::with_err(
253-
tt::Subtree::empty(),
254-
mbe::ExpandError::NoMatchingRule.into(),
255-
);
252+
return ExpandResult::new(tt::Subtree::empty(), mbe::ExpandError::NoMatchingRule.into());
256253
}
257254
for arg in &mut args {
258255
// Remove `key =`.
@@ -575,7 +572,7 @@ fn include_expand(
575572
Ok((subtree, map, file_id)) => {
576573
ExpandResult::ok(ExpandedEager { subtree, included_file: Some((file_id, map)) })
577574
}
578-
Err(e) => ExpandResult::with_err(
575+
Err(e) => ExpandResult::new(
579576
ExpandedEager { subtree: tt::Subtree::empty(), included_file: None },
580577
e,
581578
),
@@ -588,7 +585,7 @@ fn include_bytes_expand(
588585
tt: &tt::Subtree,
589586
) -> ExpandResult<ExpandedEager> {
590587
if let Err(e) = parse_string(tt) {
591-
return ExpandResult::with_err(
588+
return ExpandResult::new(
592589
ExpandedEager { subtree: tt::Subtree::empty(), included_file: None },
593590
e,
594591
);
@@ -613,7 +610,7 @@ fn include_str_expand(
613610
let path = match parse_string(tt) {
614611
Ok(it) => it,
615612
Err(e) => {
616-
return ExpandResult::with_err(
613+
return ExpandResult::new(
617614
ExpandedEager { subtree: tt::Subtree::empty(), included_file: None },
618615
e,
619616
)
@@ -650,7 +647,7 @@ fn env_expand(
650647
let key = match parse_string(tt) {
651648
Ok(it) => it,
652649
Err(e) => {
653-
return ExpandResult::with_err(
650+
return ExpandResult::new(
654651
ExpandedEager { subtree: tt::Subtree::empty(), included_file: None },
655652
e,
656653
)
@@ -686,7 +683,7 @@ fn option_env_expand(
686683
let key = match parse_string(tt) {
687684
Ok(it) => it,
688685
Err(e) => {
689-
return ExpandResult::with_err(
686+
return ExpandResult::new(
690687
ExpandedEager { subtree: tt::Subtree::empty(), included_file: None },
691688
e,
692689
)

crates/hir-expand/src/db.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,9 @@ pub trait ExpandDatabase: SourceDatabase {
9898
/// Main public API -- parses a hir file, not caring whether it's a real
9999
/// file or a macro expansion.
100100
#[salsa::transparent]
101-
fn parse_or_expand(&self, file_id: HirFileId) -> Option<SyntaxNode>;
101+
fn parse_or_expand(&self, file_id: HirFileId) -> SyntaxNode;
102102
#[salsa::transparent]
103-
fn parse_or_expand_with_err(
104-
&self,
105-
file_id: HirFileId,
106-
) -> ExpandResult<Option<Parse<SyntaxNode>>>;
103+
fn parse_or_expand_with_err(&self, file_id: HirFileId) -> ExpandResult<Parse<SyntaxNode>>;
107104
/// Implementation for the macro case.
108105
fn parse_macro_expansion(
109106
&self,
@@ -252,27 +249,26 @@ pub fn expand_speculative(
252249
}
253250

254251
fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
255-
let map = db.parse_or_expand(file_id).map(|it| AstIdMap::from_source(&it)).unwrap_or_default();
256-
Arc::new(map)
252+
Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id)))
257253
}
258254

259-
fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
260-
Some(match file_id.repr() {
255+
fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode {
256+
match file_id.repr() {
261257
HirFileIdRepr::FileId(file_id) => db.parse(file_id).tree().syntax().clone(),
262258
HirFileIdRepr::MacroFile(macro_file) => {
263259
db.parse_macro_expansion(macro_file).value.0.syntax_node()
264260
}
265-
})
261+
}
266262
}
267263

268264
fn parse_or_expand_with_err(
269265
db: &dyn ExpandDatabase,
270266
file_id: HirFileId,
271-
) -> ExpandResult<Option<Parse<SyntaxNode>>> {
267+
) -> ExpandResult<Parse<SyntaxNode>> {
272268
match file_id.repr() {
273-
HirFileIdRepr::FileId(file_id) => ExpandResult::ok(Some(db.parse(file_id).to_syntax())),
269+
HirFileIdRepr::FileId(file_id) => ExpandResult::ok(db.parse(file_id).to_syntax()),
274270
HirFileIdRepr::MacroFile(macro_file) => {
275-
db.parse_macro_expansion(macro_file).map(|it| Some(it.0))
271+
db.parse_macro_expansion(macro_file).map(|(it, _)| it)
276272
}
277273
}
278274
}

crates/hir-expand/src/eager.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn lazy_expand(
111111
def: &MacroDefId,
112112
macro_call: InFile<ast::MacroCall>,
113113
krate: CrateId,
114-
) -> ExpandResult<Option<InFile<Parse<SyntaxNode>>>> {
114+
) -> ExpandResult<InFile<Parse<SyntaxNode>>> {
115115
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
116116

117117
let expand_to = ExpandTo::from_call_site(&macro_call.value);
@@ -121,8 +121,7 @@ fn lazy_expand(
121121
MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id), expand_to },
122122
);
123123

124-
db.parse_or_expand_with_err(id.as_file())
125-
.map(|parse| parse.map(|parse| InFile::new(id.as_file(), parse)))
124+
db.parse_or_expand_with_err(id.as_file()).map(|parse| InFile::new(id.as_file(), parse))
126125
}
127126

128127
fn eager_macro_recur(
@@ -162,8 +161,7 @@ fn eager_macro_recur(
162161
Err(err) => return Err(err),
163162
};
164163
id.map(|call| {
165-
call.and_then(|call| db.parse_or_expand(call.as_file()))
166-
.map(|it| it.clone_for_update())
164+
call.map(|call| db.parse_or_expand(call.as_file()).clone_for_update())
167165
})
168166
}
169167
MacroDefKind::Declarative(_)
@@ -174,23 +172,18 @@ fn eager_macro_recur(
174172
let ExpandResult { value, err } =
175173
lazy_expand(db, &def, curr.with_value(child.clone()), krate);
176174

177-
match value {
178-
Some(val) => {
179-
// replace macro inside
180-
let hygiene = Hygiene::new(db, val.file_id);
181-
let ExpandResult { value, err: error } = eager_macro_recur(
182-
db,
183-
&hygiene,
184-
// FIXME: We discard parse errors here
185-
val.map(|it| it.syntax_node()),
186-
krate,
187-
macro_resolver,
188-
)?;
189-
let err = if err.is_none() { error } else { err };
190-
ExpandResult { value, err }
191-
}
192-
None => ExpandResult { value: None, err },
193-
}
175+
// replace macro inside
176+
let hygiene = Hygiene::new(db, value.file_id);
177+
let ExpandResult { value, err: error } = eager_macro_recur(
178+
db,
179+
&hygiene,
180+
// FIXME: We discard parse errors here
181+
value.map(|it| it.syntax_node()),
182+
krate,
183+
macro_resolver,
184+
)?;
185+
let err = if err.is_none() { error } else { err };
186+
ExpandResult { value, err }
194187
}
195188
};
196189
if err.is_some() {

crates/hir-expand/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ pub type AstId<N> = InFile<FileAstId<N>>;
730730

731731
impl<N: AstNode> AstId<N> {
732732
pub fn to_node(&self, db: &dyn db::ExpandDatabase) -> N {
733-
let root = db.parse_or_expand(self.file_id).unwrap();
733+
let root = db.parse_or_expand(self.file_id);
734734
db.ast_id_map(self.file_id).get(self.value).to_node(&root)
735735
}
736736
}
@@ -766,7 +766,7 @@ impl<T> InFile<T> {
766766
}
767767

768768
pub fn file_syntax(&self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
769-
db.parse_or_expand(self.file_id).expect("source created from invalid file")
769+
db.parse_or_expand(self.file_id)
770770
}
771771
}
772772

crates/hir-expand/src/proc_macro.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl ProcMacroExpander {
3838
Some(Ok(proc_macros)) => proc_macros,
3939
Some(Err(_)) | None => {
4040
never!("Non-dummy expander even though there are no proc macros");
41-
return ExpandResult::with_err(
41+
return ExpandResult::new(
4242
tt::Subtree::empty(),
4343
ExpandError::Other("Internal error".into()),
4444
);
@@ -52,7 +52,7 @@ impl ProcMacroExpander {
5252
proc_macros.len(),
5353
id.0
5454
);
55-
return ExpandResult::with_err(
55+
return ExpandResult::new(
5656
tt::Subtree::empty(),
5757
ExpandError::Other("Internal error".into()),
5858
);
@@ -75,17 +75,15 @@ impl ProcMacroExpander {
7575
}
7676
}
7777
ProcMacroExpansionError::System(text)
78-
| ProcMacroExpansionError::Panic(text) => ExpandResult::with_err(
79-
tt::Subtree::empty(),
80-
ExpandError::Other(text.into()),
81-
),
78+
| ProcMacroExpansionError::Panic(text) => {
79+
ExpandResult::new(tt::Subtree::empty(), ExpandError::Other(text.into()))
80+
}
8281
},
8382
}
8483
}
85-
None => ExpandResult::with_err(
86-
tt::Subtree::empty(),
87-
ExpandError::UnresolvedProcMacro(def_crate),
88-
),
84+
None => {
85+
ExpandResult::new(tt::Subtree::empty(), ExpandError::UnresolvedProcMacro(def_crate))
86+
}
8987
}
9088
}
9189
}

0 commit comments

Comments
 (0)