Skip to content

Commit 3f6980e

Browse files
committed
simplify macro expansion code
Using `Option` arguments such that you always pass `None` or `Some` at the call site is a code smell.
1 parent 95dc8ef commit 3f6980e

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

crates/hir_expand/src/db.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,27 @@ pub fn expand_hypothetical(
122122
hypothetical_args: &ast::TokenTree,
123123
token_to_map: SyntaxToken,
124124
) -> Option<(SyntaxNode, SyntaxToken)> {
125-
let macro_file = MacroFile { macro_call_id: actual_macro_call };
126125
let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax());
127126
let range =
128127
token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?;
129128
let token_id = tmap_1.token_by_range(range)?;
130-
let macro_def = expander(db, actual_macro_call)?;
131129

132-
let hypothetical_expansion =
133-
macro_expand_with_arg(db, macro_file.macro_call_id, Some(Arc::new((tt, tmap_1))));
134-
let (node, tmap_2) = expansion_to_syntax(db, macro_file, hypothetical_expansion).value?;
130+
let lazy_id = match actual_macro_call {
131+
MacroCallId::LazyMacro(id) => id,
132+
MacroCallId::EagerMacro(_) => return None,
133+
};
134+
135+
let macro_def = {
136+
let loc = db.lookup_intern_macro(lazy_id);
137+
db.macro_def(loc.def)?
138+
};
139+
140+
let hypothetical_expansion = macro_def.expand(db, lazy_id, &tt);
141+
142+
let fragment_kind = to_fragment_kind(db, actual_macro_call);
143+
144+
let (node, tmap_2) =
145+
mbe::token_tree_to_syntax_node(&hypothetical_expansion.value, fragment_kind).ok()?;
135146

136147
let token_id = macro_def.map_id_down(token_id);
137148
let range = tmap_2.range_by_token(token_id)?.by_kind(token_to_map.kind())?;
@@ -156,17 +167,9 @@ fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNod
156167
fn parse_macro_expansion(
157168
db: &dyn AstDatabase,
158169
macro_file: MacroFile,
159-
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
160-
let result = db.macro_expand(macro_file.macro_call_id);
161-
expansion_to_syntax(db, macro_file, result)
162-
}
163-
164-
fn expansion_to_syntax(
165-
db: &dyn AstDatabase,
166-
macro_file: MacroFile,
167-
result: ExpandResult<Option<Arc<tt::Subtree>>>,
168170
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
169171
let _p = profile::span("parse_macro_expansion");
172+
let result = db.macro_expand(macro_file.macro_call_id);
170173

171174
if let Some(err) = &result.err {
172175
// Note:
@@ -309,19 +312,6 @@ fn macro_expand_error(db: &dyn AstDatabase, macro_call: MacroCallId) -> Option<E
309312
db.macro_expand(macro_call).err
310313
}
311314

312-
fn expander(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<TokenExpander>> {
313-
let lazy_id = match id {
314-
MacroCallId::LazyMacro(id) => id,
315-
MacroCallId::EagerMacro(_id) => {
316-
return None;
317-
}
318-
};
319-
320-
let loc = db.lookup_intern_macro(lazy_id);
321-
let macro_rules = db.macro_def(loc.def)?;
322-
Some(macro_rules)
323-
}
324-
325315
fn macro_expand_with_arg(
326316
db: &dyn AstDatabase,
327317
id: MacroCallId,

docs/dev/style.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,39 @@ fn query_all(name: String, case_sensitive: bool) -> Vec<Item> { ... }
449449
fn query_first(name: String, case_sensitive: bool) -> Option<Item> { ... }
450450
```
451451

452+
## Prefer Separate Functions Over Parameters
453+
454+
If a function has a `bool` or an `Option` parameter, and it is always called with `true`, `false`, `Some` and `None` literals, split the function in two.
455+
456+
```rust
457+
// GOOD
458+
fn caller_a() {
459+
foo()
460+
}
461+
462+
fn caller_b() {
463+
foo_with_bar(Bar::new())
464+
}
465+
466+
fn foo() { ... }
467+
fn foo_with_bar(bar: Bar) { ... }
468+
469+
// BAD
470+
fn caller_a() {
471+
foo(None)
472+
}
473+
474+
fn caller_b() {
475+
foo(Some(Bar::new()))
476+
}
477+
478+
fn foo(bar: Option<Bar>) { ... }
479+
```
480+
481+
**Rationale:** more often than not, such functions display "`false sharing`" -- they have additional `if` branching inside for two different cases.
482+
Splitting the two different control flows into two functions simplifies each path, and remove cross-dependencies between the two paths.
483+
If there's common code between `foo` and `foo_with_bar`, extract *that* into a common helper.
484+
452485
## Avoid Monomorphization
453486

454487
Avoid making a lot of code type parametric, *especially* on the boundaries between crates.

0 commit comments

Comments
 (0)