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

Commit 96a7742

Browse files
committed
Option begone part 1
1 parent 0f4ffaa commit 96a7742

File tree

8 files changed

+94
-105
lines changed

8 files changed

+94
-105
lines changed

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -151,47 +151,45 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
151151
if let Some(err) = exp.err {
152152
format_to!(expn_text, "/* error: {} */", err);
153153
}
154-
if let Some((parse, token_map)) = exp.value {
155-
if expect_errors {
156-
assert!(!parse.errors().is_empty(), "no parse errors in expansion");
157-
for e in parse.errors() {
158-
format_to!(expn_text, "/* parse error: {} */\n", e);
159-
}
160-
} else {
161-
assert!(
162-
parse.errors().is_empty(),
163-
"parse errors in expansion: \n{:#?}",
164-
parse.errors()
165-
);
154+
let (parse, token_map) = exp.value;
155+
if expect_errors {
156+
assert!(!parse.errors().is_empty(), "no parse errors in expansion");
157+
for e in parse.errors() {
158+
format_to!(expn_text, "/* parse error: {} */\n", e);
166159
}
167-
let pp = pretty_print_macro_expansion(
168-
parse.syntax_node(),
169-
show_token_ids.then_some(&*token_map),
160+
} else {
161+
assert!(
162+
parse.errors().is_empty(),
163+
"parse errors in expansion: \n{:#?}",
164+
parse.errors()
170165
);
171-
let indent = IndentLevel::from_node(call.syntax());
172-
let pp = reindent(indent, pp);
173-
format_to!(expn_text, "{}", pp);
166+
}
167+
let pp = pretty_print_macro_expansion(
168+
parse.syntax_node(),
169+
show_token_ids.then_some(&*token_map),
170+
);
171+
let indent = IndentLevel::from_node(call.syntax());
172+
let pp = reindent(indent, pp);
173+
format_to!(expn_text, "{}", pp);
174174

175-
if tree {
176-
let tree = format!("{:#?}", parse.syntax_node())
177-
.split_inclusive('\n')
178-
.map(|line| format!("// {line}"))
179-
.collect::<String>();
180-
format_to!(expn_text, "\n{}", tree)
181-
}
175+
if tree {
176+
let tree = format!("{:#?}", parse.syntax_node())
177+
.split_inclusive('\n')
178+
.map(|line| format!("// {line}"))
179+
.collect::<String>();
180+
format_to!(expn_text, "\n{}", tree)
182181
}
183182
let range = call.syntax().text_range();
184183
let range: Range<usize> = range.into();
185184

186185
if show_token_ids {
187-
if let Some((tree, map, _)) = arg.as_deref() {
188-
let tt_range = call.token_tree().unwrap().syntax().text_range();
189-
let mut ranges = Vec::new();
190-
extract_id_ranges(&mut ranges, map, tree);
191-
for (range, id) in ranges {
192-
let idx = (tt_range.start() + range.end()).into();
193-
text_edits.push((idx..idx, format!("#{}", id.0)));
194-
}
186+
let (tree, map, _) = &*arg;
187+
let tt_range = call.token_tree().unwrap().syntax().text_range();
188+
let mut ranges = Vec::new();
189+
extract_id_ranges(&mut ranges, map, tree);
190+
for (range, id) in ranges {
191+
let idx = (tt_range.start() + range.end()).into();
192+
text_edits.push((idx..idx, format!("#{}", id.0)));
195193
}
196194
text_edits.push((range.start..range.start, "// ".into()));
197195
call.to_string().match_indices('\n').for_each(|(offset, _)| {

crates/hir-def/src/nameres/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ impl DefCollector<'_> {
13711371

13721372
self.def_map.diagnostics.push(diag);
13731373
}
1374-
if let Some(errors) = value {
1374+
if let errors @ [_, ..] = &*value {
13751375
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(macro_call_id);
13761376
let diag = DefDiagnostic::macro_expansion_parse_error(module_id, loc.kind, &errors);
13771377
self.def_map.diagnostics.push(diag);

crates/hir-expand/src/db.rs

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub trait ExpandDatabase: SourceDatabase {
108108
fn parse_macro_expansion(
109109
&self,
110110
macro_file: MacroFile,
111-
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>>;
111+
) -> ExpandResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>;
112112

113113
/// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the
114114
/// reason why we use salsa at all.
@@ -123,7 +123,7 @@ pub trait ExpandDatabase: SourceDatabase {
123123
fn macro_arg(
124124
&self,
125125
id: MacroCallId,
126-
) -> Option<Arc<(tt::Subtree, mbe::TokenMap, fixup::SyntaxFixupUndoInfo)>>;
126+
) -> Arc<(tt::Subtree, mbe::TokenMap, fixup::SyntaxFixupUndoInfo)>;
127127
/// Extracts syntax node, corresponding to a macro call. That's a firewall
128128
/// query, only typing in the macro call itself changes the returned
129129
/// subtree.
@@ -133,7 +133,7 @@ pub trait ExpandDatabase: SourceDatabase {
133133
fn macro_def(&self, id: MacroDefId) -> Result<Arc<TokenExpander>, mbe::ParseError>;
134134

135135
/// Expand macro call to a token tree.
136-
fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>>;
136+
fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult<Arc<tt::Subtree>>;
137137
/// Special case of the previous query for procedural macros. We can't LRU
138138
/// proc macros, since they are not deterministic in general, and
139139
/// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
@@ -143,7 +143,7 @@ pub trait ExpandDatabase: SourceDatabase {
143143
fn parse_macro_expansion_error(
144144
&self,
145145
macro_call: MacroCallId,
146-
) -> ExpandResult<Option<Box<[SyntaxError]>>>;
146+
) -> ExpandResult<Box<[SyntaxError]>>;
147147

148148
fn hygiene_frame(&self, file_id: HirFileId) -> Arc<HygieneFrame>;
149149
}
@@ -257,12 +257,12 @@ fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
257257
}
258258

259259
fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> Option<SyntaxNode> {
260-
match file_id.repr() {
261-
HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()),
260+
Some(match file_id.repr() {
261+
HirFileIdRepr::FileId(file_id) => db.parse(file_id).tree().syntax().clone(),
262262
HirFileIdRepr::MacroFile(macro_file) => {
263-
db.parse_macro_expansion(macro_file).value.map(|(it, _)| it.syntax_node())
263+
db.parse_macro_expansion(macro_file).value.0.syntax_node()
264264
}
265-
}
265+
})
266266
}
267267

268268
fn parse_or_expand_with_err(
@@ -272,17 +272,17 @@ fn parse_or_expand_with_err(
272272
match file_id.repr() {
273273
HirFileIdRepr::FileId(file_id) => ExpandResult::ok(Some(db.parse(file_id).to_syntax())),
274274
HirFileIdRepr::MacroFile(macro_file) => {
275-
db.parse_macro_expansion(macro_file).map(|it| it.map(|(parse, _)| parse))
275+
db.parse_macro_expansion(macro_file).map(|it| Some(it.0))
276276
}
277277
}
278278
}
279279

280280
fn parse_macro_expansion(
281281
db: &dyn ExpandDatabase,
282282
macro_file: MacroFile,
283-
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
283+
) -> ExpandResult<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
284284
let _p = profile::span("parse_macro_expansion");
285-
let mbe::ValueResult { value, err } = db.macro_expand(macro_file.macro_call_id);
285+
let mbe::ValueResult { value: tt, err } = db.macro_expand(macro_file.macro_call_id);
286286

287287
if let Some(err) = &err {
288288
if tracing::enabled!(tracing::Level::DEBUG) {
@@ -308,10 +308,6 @@ fn parse_macro_expansion(
308308
);
309309
}
310310
}
311-
let tt = match value {
312-
Some(tt) => tt,
313-
None => return ExpandResult { value: None, err },
314-
};
315311

316312
let expand_to = macro_expand_to(db, macro_file.macro_call_id);
317313

@@ -320,14 +316,23 @@ fn parse_macro_expansion(
320316

321317
let (parse, rev_token_map) = token_tree_to_syntax_node(&tt, expand_to);
322318

323-
ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err }
319+
ExpandResult { value: (parse, Arc::new(rev_token_map)), err }
324320
}
325321

326322
fn macro_arg(
327323
db: &dyn ExpandDatabase,
328324
id: MacroCallId,
329-
) -> Option<Arc<(tt::Subtree, mbe::TokenMap, fixup::SyntaxFixupUndoInfo)>> {
330-
let arg = db.macro_arg_text(id)?;
325+
) -> Arc<(tt::Subtree, mbe::TokenMap, fixup::SyntaxFixupUndoInfo)> {
326+
let Some(arg) = db.macro_arg_text(id) else {
327+
return Arc::new((
328+
tt::Subtree {
329+
delimiter: tt::Delimiter::UNSPECIFIED,
330+
token_trees: Vec::new(),
331+
},
332+
Default::default(),
333+
Default::default())
334+
);
335+
};
331336
let loc = db.lookup_intern_macro_call(id);
332337

333338
let node = SyntaxNode::new_root(arg);
@@ -346,7 +351,7 @@ fn macro_arg(
346351
// proc macros expect their inputs without parentheses, MBEs expect it with them included
347352
tt.delimiter = tt::Delimiter::unspecified();
348353
}
349-
Some(Arc::new((tt, tmap, fixups.undo_info)))
354+
Arc::new((tt, tmap, fixups.undo_info))
350355
}
351356

352357
fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<SyntaxNode> {
@@ -448,79 +453,66 @@ fn macro_def(
448453
}
449454
}
450455

451-
fn macro_expand(
452-
db: &dyn ExpandDatabase,
453-
id: MacroCallId,
454-
// FIXME: Remove the OPtion if possible
455-
) -> ExpandResult<Option<Arc<tt::Subtree>>> {
456+
fn macro_expand(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<Arc<tt::Subtree>> {
456457
let _p = profile::span("macro_expand");
457458
let loc: MacroCallLoc = db.lookup_intern_macro_call(id);
458459
if let Some(eager) = &loc.eager {
459-
return ExpandResult {
460-
value: Some(eager.arg_or_expansion.clone()),
461-
err: eager.error.clone(),
462-
};
460+
return ExpandResult { value: eager.arg_or_expansion.clone(), err: eager.error.clone() };
463461
}
464462

465-
let macro_arg = match db.macro_arg(id) {
466-
Some(it) => it,
467-
None => {
468-
return ExpandResult::only_err(ExpandError::Other(
469-
"Failed to lower macro args to token tree".into(),
470-
))
471-
}
472-
};
473-
474463
let expander = match db.macro_def(loc.def) {
475464
Ok(it) => it,
476465
// FIXME: This is weird -- we effectively report macro *definition*
477466
// errors lazily, when we try to expand the macro. Instead, they should
478467
// be reported at the definition site when we construct a def map.
479468
// (Note we do report them also at the definition site in the late diagnostic pass)
480469
Err(err) => {
481-
return ExpandResult::only_err(ExpandError::Other(
482-
format!("invalid macro definition: {err}").into(),
483-
))
470+
return ExpandResult {
471+
value: Arc::new(tt::Subtree {
472+
delimiter: tt::Delimiter::UNSPECIFIED,
473+
token_trees: vec![],
474+
}),
475+
err: Some(ExpandError::Other(format!("invalid macro definition: {err}").into())),
476+
}
484477
}
485478
};
479+
let macro_arg = db.macro_arg(id);
486480
let ExpandResult { value: mut tt, err } = expander.expand(db, id, &macro_arg.0);
487481
// Set a hard limit for the expanded tt
488482
let count = tt.count();
489483
if TOKEN_LIMIT.check(count).is_err() {
490-
return ExpandResult::only_err(ExpandError::Other(
491-
format!(
492-
"macro invocation exceeds token limit: produced {} tokens, limit is {}",
493-
count,
494-
TOKEN_LIMIT.inner(),
495-
)
496-
.into(),
497-
));
484+
return ExpandResult {
485+
value: Arc::new(tt::Subtree {
486+
delimiter: tt::Delimiter::UNSPECIFIED,
487+
token_trees: vec![],
488+
}),
489+
err: Some(ExpandError::Other(
490+
format!(
491+
"macro invocation exceeds token limit: produced {} tokens, limit is {}",
492+
count,
493+
TOKEN_LIMIT.inner(),
494+
)
495+
.into(),
496+
)),
497+
};
498498
}
499499

500500
fixup::reverse_fixups(&mut tt, &macro_arg.1, &macro_arg.2);
501501

502-
ExpandResult { value: Some(Arc::new(tt)), err }
502+
ExpandResult { value: Arc::new(tt), err }
503503
}
504504

505505
fn parse_macro_expansion_error(
506506
db: &dyn ExpandDatabase,
507507
macro_call_id: MacroCallId,
508-
) -> ExpandResult<Option<Box<[SyntaxError]>>> {
508+
) -> ExpandResult<Box<[SyntaxError]>> {
509509
db.parse_macro_expansion(MacroFile { macro_call_id })
510-
.map(|it| it.map(|(it, _)| it.errors().to_vec().into_boxed_slice()))
510+
.map(|it| it.0.errors().to_vec().into_boxed_slice())
511511
}
512512

513513
fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<tt::Subtree> {
514514
let loc: MacroCallLoc = db.lookup_intern_macro_call(id);
515-
let macro_arg = match db.macro_arg(id) {
516-
Some(it) => it,
517-
None => {
518-
return ExpandResult::with_err(
519-
tt::Subtree::empty(),
520-
ExpandError::Other("No arguments for proc-macro".into()),
521-
)
522-
}
523-
};
515+
let macro_arg = db.macro_arg(id);
524516

525517
let expander = match loc.def.kind {
526518
MacroDefKind::ProcMacro(expander, ..) => expander,

crates/hir-expand/src/fixup.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tt::token_id::Subtree;
1414
/// The result of calculating fixes for a syntax node -- a bunch of changes
1515
/// (appending to and replacing nodes), the information that is needed to
1616
/// reverse those changes afterwards, and a token map.
17-
#[derive(Debug)]
17+
#[derive(Debug, Default)]
1818
pub(crate) struct SyntaxFixups {
1919
pub(crate) append: FxHashMap<SyntaxElement, Vec<SyntheticToken>>,
2020
pub(crate) replace: FxHashMap<SyntaxElement, Vec<SyntheticToken>>,
@@ -24,7 +24,7 @@ pub(crate) struct SyntaxFixups {
2424
}
2525

2626
/// This is the information needed to reverse the fixups.
27-
#[derive(Debug, PartialEq, Eq)]
27+
#[derive(Debug, Default, PartialEq, Eq)]
2828
pub struct SyntaxFixupUndoInfo {
2929
original: Vec<Subtree>,
3030
}

crates/hir-expand/src/hygiene.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ fn make_hygiene_info(
200200
});
201201

202202
let macro_def = db.macro_def(loc.def).ok()?;
203-
let (_, exp_map) = db.parse_macro_expansion(macro_file).value?;
204-
let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
203+
let (_, exp_map) = db.parse_macro_expansion(macro_file).value;
204+
let macro_arg = db.macro_arg(macro_file.macro_call_id);
205205

206206
Some(HygieneInfo {
207207
file: macro_file,

crates/hir-expand/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ impl HirFileId {
257257
let arg_tt = loc.kind.arg(db)?;
258258

259259
let macro_def = db.macro_def(loc.def).ok()?;
260-
let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?;
261-
let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
260+
let (parse, exp_map) = db.parse_macro_expansion(macro_file).value;
261+
let macro_arg = db.macro_arg(macro_file.macro_call_id);
262262

263263
let def = loc.def.ast_id().left().and_then(|id| {
264264
let def_tt = match id.to_node(db) {

crates/ide/src/status.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStat
120120
}
121121
}
122122

123-
impl<M> FromIterator<TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>
123+
impl<M> FromIterator<TableEntry<MacroFile, ExpandResult<(Parse<SyntaxNode>, M)>>>
124124
for SyntaxTreeStats
125125
{
126126
fn from_iter<T>(iter: T) -> SyntaxTreeStats
127127
where
128-
T: IntoIterator<Item = TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>,
128+
T: IntoIterator<Item = TableEntry<MacroFile, ExpandResult<(Parse<SyntaxNode>, M)>>>,
129129
{
130130
let mut res = SyntaxTreeStats::default();
131131
for entry in iter {

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ impl flags::AnalysisStats {
180180

181181
let mut total_macro_file_size = Bytes::default();
182182
for e in hir::db::ParseMacroExpansionQuery.in_db(db).entries::<Vec<_>>() {
183-
if let Some((val, _)) = db.parse_macro_expansion(e.key).value {
184-
total_macro_file_size += syntax_len(val.syntax_node())
185-
}
183+
let val = db.parse_macro_expansion(e.key).value.0;
184+
total_macro_file_size += syntax_len(val.syntax_node())
186185
}
187186
eprintln!("source files: {total_file_size}, macro files: {total_macro_file_size}");
188187
}

0 commit comments

Comments
 (0)