Skip to content

Commit 7aee2b3

Browse files
committed
Make configure_annotatable/flat_map_annotatable infallible.
They each have a single callsite, and the result is always unwrapped, so the `Option<Annotatable>` return type is misleading. Also, the comment at the `configure_annotatable` call site is wrong, talking about a result vector, so this commit also removes that.
1 parent b420d92 commit 7aee2b3

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

compiler/rustc_builtin_macros/src/cfg_eval.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,44 @@ pub(crate) fn cfg_eval(
3939
let features = Some(features);
4040
CfgEval(StripUnconfigured { sess, features, config_tokens: true, lint_node_id })
4141
.configure_annotatable(annotatable)
42-
// Since the item itself has already been configured by the `InvocationCollector`,
43-
// we know that fold result vector will contain exactly one element.
44-
.unwrap()
4542
}
4643

4744
struct CfgEval<'a>(StripUnconfigured<'a>);
4845

49-
fn flat_map_annotatable(
50-
vis: &mut impl MutVisitor,
51-
annotatable: Annotatable,
52-
) -> Option<Annotatable> {
46+
fn flat_map_annotatable(vis: &mut impl MutVisitor, annotatable: Annotatable) -> Annotatable {
5347
match annotatable {
54-
Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
48+
Annotatable::Item(item) => Annotatable::Item(vis.flat_map_item(item).pop().unwrap()),
5549
Annotatable::AssocItem(item, ctxt) => {
56-
Some(Annotatable::AssocItem(vis.flat_map_assoc_item(item, ctxt).pop()?, ctxt))
50+
Annotatable::AssocItem(vis.flat_map_assoc_item(item, ctxt).pop().unwrap(), ctxt)
5751
}
5852
Annotatable::ForeignItem(item) => {
59-
vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
53+
Annotatable::ForeignItem(vis.flat_map_foreign_item(item).pop().unwrap())
6054
}
6155
Annotatable::Stmt(stmt) => {
62-
vis.flat_map_stmt(stmt.into_inner()).pop().map(P).map(Annotatable::Stmt)
56+
Annotatable::Stmt(P(vis.flat_map_stmt(stmt.into_inner()).pop().unwrap()))
6357
}
6458
Annotatable::Expr(mut expr) => {
6559
vis.visit_expr(&mut expr);
66-
Some(Annotatable::Expr(expr))
60+
Annotatable::Expr(expr)
6761
}
68-
Annotatable::Arm(arm) => vis.flat_map_arm(arm).pop().map(Annotatable::Arm),
62+
Annotatable::Arm(arm) => Annotatable::Arm(vis.flat_map_arm(arm).pop().unwrap()),
6963
Annotatable::ExprField(field) => {
70-
vis.flat_map_expr_field(field).pop().map(Annotatable::ExprField)
64+
Annotatable::ExprField(vis.flat_map_expr_field(field).pop().unwrap())
65+
}
66+
Annotatable::PatField(fp) => {
67+
Annotatable::PatField(vis.flat_map_pat_field(fp).pop().unwrap())
7168
}
72-
Annotatable::PatField(fp) => vis.flat_map_pat_field(fp).pop().map(Annotatable::PatField),
7369
Annotatable::GenericParam(param) => {
74-
vis.flat_map_generic_param(param).pop().map(Annotatable::GenericParam)
70+
Annotatable::GenericParam(vis.flat_map_generic_param(param).pop().unwrap())
71+
}
72+
Annotatable::Param(param) => Annotatable::Param(vis.flat_map_param(param).pop().unwrap()),
73+
Annotatable::FieldDef(sf) => {
74+
Annotatable::FieldDef(vis.flat_map_field_def(sf).pop().unwrap())
7575
}
76-
Annotatable::Param(param) => vis.flat_map_param(param).pop().map(Annotatable::Param),
77-
Annotatable::FieldDef(sf) => vis.flat_map_field_def(sf).pop().map(Annotatable::FieldDef),
78-
Annotatable::Variant(v) => vis.flat_map_variant(v).pop().map(Annotatable::Variant),
76+
Annotatable::Variant(v) => Annotatable::Variant(vis.flat_map_variant(v).pop().unwrap()),
7977
Annotatable::Crate(mut krate) => {
8078
vis.visit_crate(&mut krate);
81-
Some(Annotatable::Crate(krate))
79+
Annotatable::Crate(krate)
8280
}
8381
}
8482
}
@@ -123,11 +121,11 @@ impl CfgEval<'_> {
123121
self.0.configure(node)
124122
}
125123

126-
fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Annotatable> {
124+
fn configure_annotatable(mut self, mut annotatable: Annotatable) -> Annotatable {
127125
// Tokenizing and re-parsing the `Annotatable` can have a significant
128126
// performance impact, so try to avoid it if possible
129127
if !has_cfg_or_cfg_attr(&annotatable) {
130-
return Some(annotatable);
128+
return annotatable;
131129
}
132130

133131
// The majority of parsed attribute targets will never need to have early cfg-expansion
@@ -197,13 +195,13 @@ impl CfgEval<'_> {
197195
Ok(a) => annotatable = a,
198196
Err(err) => {
199197
err.emit();
200-
return Some(annotatable);
198+
return annotatable;
201199
}
202200
}
203201

204202
// Now that we have our re-parsed `AttrTokenStream`, recursively configuring
205203
// our attribute target will correctly configure the tokens as well.
206-
flat_map_annotatable(self, annotatable)
204+
flat_map_annotatable(&mut self, annotatable)
207205
}
208206
}
209207

0 commit comments

Comments
 (0)