Skip to content

Commit db061fb

Browse files
Merge #6639
6639: Use `ExpandResult` instead of `MacroResult` r=jonas-schievink a=jonas-schievink `MacroResult` is redundant. bors r+ 🤖 Co-authored-by: Jonas Schievink <[email protected]>
2 parents 1542797 + 6a9338e commit db061fb

File tree

5 files changed

+42
-56
lines changed

5 files changed

+42
-56
lines changed

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub use hir_def::{
5757
visibility::Visibility,
5858
};
5959
pub use hir_expand::{
60-
db::MacroResult, name::known, name::AsName, name::Name, HirFileId, InFile, MacroCallId,
60+
name::known, name::AsName, name::Name, ExpandResult, HirFileId, InFile, MacroCallId,
6161
MacroCallLoc, /* FIXME */ MacroDefId, MacroFile, Origin,
6262
};
6363
pub use hir_ty::display::HirDisplay;

crates/hir_expand/src/db.rs

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,6 @@ use crate::{
1313
MacroFile, ProcMacroExpander,
1414
};
1515

16-
/// A result of some macro expansion.
17-
#[derive(Debug, Clone, Eq, PartialEq)]
18-
pub struct MacroResult<T> {
19-
/// The result of the expansion. Might be `None` when error recovery was impossible and no
20-
/// usable result was produced.
21-
pub value: Option<T>,
22-
23-
/// The error that occurred during expansion or processing.
24-
///
25-
/// Since we do error recovery, getting an error here does not mean that `value` will be absent.
26-
pub error: Option<String>,
27-
}
28-
2916
#[derive(Debug, Clone, Eq, PartialEq)]
3017
pub enum TokenExpander {
3118
MacroRules(mbe::MacroRules),
@@ -91,29 +78,15 @@ pub trait AstDatabase: SourceDatabase {
9178
fn parse_macro_expansion(
9279
&self,
9380
macro_file: MacroFile,
94-
) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>;
95-
fn macro_expand(&self, macro_call: MacroCallId) -> MacroResult<Arc<tt::Subtree>>;
81+
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>>;
82+
fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>>;
9683

9784
#[salsa::interned]
9885
fn intern_eager_expansion(&self, eager: EagerCallLoc) -> EagerMacroId;
9986

10087
fn expand_proc_macro(&self, call: MacroCallId) -> Result<tt::Subtree, mbe::ExpandError>;
10188
}
10289

103-
impl<T> MacroResult<T> {
104-
fn error(message: String) -> Self {
105-
Self { value: None, error: Some(message) }
106-
}
107-
108-
fn map<U>(self, f: impl FnOnce(T) -> U) -> MacroResult<U> {
109-
MacroResult { value: self.value.map(f), error: self.error }
110-
}
111-
112-
fn drop_value<U>(self) -> MacroResult<U> {
113-
MacroResult { value: None, error: self.error }
114-
}
115-
}
116-
11790
/// This expands the given macro call, but with different arguments. This is
11891
/// used for completion, where we want to see what 'would happen' if we insert a
11992
/// token. The `token_to_map` mapped down into the expansion, with the mapped
@@ -194,7 +167,7 @@ fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree,
194167
Some(Arc::new((tt, tmap)))
195168
}
196169

197-
fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> MacroResult<Arc<tt::Subtree>> {
170+
fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>> {
198171
macro_expand_with_arg(db, id, None)
199172
}
200173

@@ -215,18 +188,18 @@ fn macro_expand_with_arg(
215188
db: &dyn AstDatabase,
216189
id: MacroCallId,
217190
arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
218-
) -> MacroResult<Arc<tt::Subtree>> {
191+
) -> ExpandResult<Option<Arc<tt::Subtree>>> {
219192
let lazy_id = match id {
220193
MacroCallId::LazyMacro(id) => id,
221194
MacroCallId::EagerMacro(id) => {
222195
if arg.is_some() {
223-
return MacroResult::error(
196+
return ExpandResult::str_err(
224197
"hypothetical macro expansion not implemented for eager macro".to_owned(),
225198
);
226199
} else {
227-
return MacroResult {
200+
return ExpandResult {
228201
value: Some(db.lookup_intern_eager_expansion(id).subtree),
229-
error: None,
202+
err: None,
230203
};
231204
}
232205
}
@@ -235,21 +208,24 @@ fn macro_expand_with_arg(
235208
let loc = db.lookup_intern_macro(lazy_id);
236209
let macro_arg = match arg.or_else(|| db.macro_arg(id)) {
237210
Some(it) => it,
238-
None => return MacroResult::error("Fail to args in to tt::TokenTree".into()),
211+
None => return ExpandResult::str_err("Fail to args in to tt::TokenTree".into()),
239212
};
240213

241214
let macro_rules = match db.macro_def(loc.def) {
242215
Some(it) => it,
243-
None => return MacroResult::error("Fail to find macro definition".into()),
216+
None => return ExpandResult::str_err("Fail to find macro definition".into()),
244217
};
245218
let ExpandResult { value: tt, err } = macro_rules.0.expand(db, lazy_id, &macro_arg.0);
246219
// Set a hard limit for the expanded tt
247220
let count = tt.count();
248221
if count > 262144 {
249-
return MacroResult::error(format!("Total tokens count exceed limit : count = {}", count));
222+
return ExpandResult::str_err(format!(
223+
"Total tokens count exceed limit : count = {}",
224+
count
225+
));
250226
}
251227

252-
MacroResult { value: Some(Arc::new(tt)), error: err.map(|e| format!("{:?}", e)) }
228+
ExpandResult { value: Some(Arc::new(tt)), err }
253229
}
254230

255231
fn expand_proc_macro(
@@ -283,23 +259,23 @@ fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNod
283259
match file_id.0 {
284260
HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
285261
HirFileIdRepr::MacroFile(macro_file) => {
286-
db.parse_macro_expansion(macro_file).map(|(it, _)| it.syntax_node()).value
262+
db.parse_macro_expansion(macro_file).value.map(|(it, _)| it.syntax_node())
287263
}
288264
}
289265
}
290266

291267
fn parse_macro_expansion(
292268
db: &dyn AstDatabase,
293269
macro_file: MacroFile,
294-
) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
270+
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
295271
parse_macro_with_arg(db, macro_file, None)
296272
}
297273

298274
fn parse_macro_with_arg(
299275
db: &dyn AstDatabase,
300276
macro_file: MacroFile,
301277
arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
302-
) -> MacroResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
278+
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
303279
let _p = profile::span("parse_macro_query");
304280

305281
let macro_call_id = macro_file.macro_call_id;
@@ -308,7 +284,7 @@ fn parse_macro_with_arg(
308284
} else {
309285
db.macro_expand(macro_call_id)
310286
};
311-
if let Some(err) = &result.error {
287+
if let Some(err) = &result.err {
312288
// Note:
313289
// The final goal we would like to make all parse_macro success,
314290
// such that the following log will not call anyway.
@@ -326,50 +302,50 @@ fn parse_macro_with_arg(
326302
.join("\n");
327303

328304
log::warn!(
329-
"fail on macro_parse: (reason: {} macro_call: {:#}) parents: {}",
305+
"fail on macro_parse: (reason: {:?} macro_call: {:#}) parents: {}",
330306
err,
331307
node.value,
332308
parents
333309
);
334310
}
335311
_ => {
336-
log::warn!("fail on macro_parse: (reason: {})", err);
312+
log::warn!("fail on macro_parse: (reason: {:?})", err);
337313
}
338314
}
339315
}
340316
let tt = match result.value {
341317
Some(tt) => tt,
342-
None => return result.drop_value(),
318+
None => return ExpandResult { value: None, err: result.err },
343319
};
344320

345321
let fragment_kind = to_fragment_kind(db, macro_call_id);
346322

347323
let (parse, rev_token_map) = match mbe::token_tree_to_syntax_node(&tt, fragment_kind) {
348324
Ok(it) => it,
349325
Err(err) => {
350-
return MacroResult::error(format!("{:?}", err));
326+
return ExpandResult::only_err(err);
351327
}
352328
};
353329

354-
match result.error {
355-
Some(error) => {
330+
match result.err {
331+
Some(err) => {
356332
// Safety check for recursive identity macro.
357333
let node = parse.syntax_node();
358334
let file: HirFileId = macro_file.into();
359335
let call_node = match file.call_node(db) {
360336
Some(it) => it,
361337
None => {
362-
return MacroResult::error(error);
338+
return ExpandResult::only_err(err);
363339
}
364340
};
365341

366342
if !diff(&node, &call_node.value).is_empty() {
367-
MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: Some(error) }
343+
ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: Some(err) }
368344
} else {
369-
return MacroResult::error(error);
345+
return ExpandResult::only_err(err);
370346
}
371347
}
372-
None => MacroResult { value: Some((parse, Arc::new(rev_token_map))), error: None },
348+
None => ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: None },
373349
}
374350
}
375351

crates/hir_expand/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub mod proc_macro;
1515
pub mod quote;
1616
pub mod eager;
1717

18+
pub use mbe::{ExpandError, ExpandResult};
19+
1820
use std::hash::Hash;
1921
use std::sync::Arc;
2022

crates/ide/src/status.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{fmt, iter::FromIterator, sync::Arc};
22

3-
use hir::{MacroFile, MacroResult};
3+
use hir::{ExpandResult, MacroFile};
44
use ide_db::base_db::{
55
salsa::debug::{DebugQueryTable, TableEntry},
66
CrateId, FileId, FileTextQuery, SourceDatabase, SourceRootId,
@@ -115,12 +115,12 @@ impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStat
115115
}
116116
}
117117

118-
impl<M> FromIterator<TableEntry<MacroFile, MacroResult<(Parse<SyntaxNode>, M)>>>
118+
impl<M> FromIterator<TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>
119119
for SyntaxTreeStats
120120
{
121121
fn from_iter<T>(iter: T) -> SyntaxTreeStats
122122
where
123-
T: IntoIterator<Item = TableEntry<MacroFile, MacroResult<(Parse<SyntaxNode>, M)>>>,
123+
T: IntoIterator<Item = TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>,
124124
{
125125
let mut res = SyntaxTreeStats::default();
126126
for entry in iter {

crates/mbe/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum ExpandError {
3333
ConversionError,
3434
InvalidRepeat,
3535
ProcMacroError(tt::ExpansionError),
36+
Other(String),
3637
}
3738

3839
impl From<tt::ExpansionError> for ExpandError {
@@ -264,6 +265,13 @@ impl<T> ExpandResult<T> {
264265
Self { value: Default::default(), err: Some(err) }
265266
}
266267

268+
pub fn str_err(err: String) -> Self
269+
where
270+
T: Default,
271+
{
272+
Self::only_err(ExpandError::Other(err))
273+
}
274+
267275
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
268276
ExpandResult { value: f(self.value), err: self.err }
269277
}

0 commit comments

Comments
 (0)