Skip to content

Commit 360e978

Browse files
committed
Don't call closures immediately, use try{} blocks
1 parent bddbf38 commit 360e978

File tree

10 files changed

+88
-115
lines changed

10 files changed

+88
-115
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,22 @@ pub(super) fn compare_impl_method<'tcx>(
4747

4848
let impl_m_span = tcx.def_span(impl_m.def_id);
4949

50-
if let Err(_) = compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref) {
51-
return;
52-
}
53-
54-
if let Err(_) = compare_number_of_generics(tcx, impl_m, trait_m, trait_item_span, false) {
55-
return;
56-
}
57-
58-
if let Err(_) = compare_generic_param_kinds(tcx, impl_m, trait_m, false) {
59-
return;
60-
}
61-
62-
if let Err(_) =
63-
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)
64-
{
65-
return;
66-
}
67-
68-
if let Err(_) = compare_synthetic_generics(tcx, impl_m, trait_m) {
69-
return;
70-
}
71-
72-
if let Err(_) = compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span) {
73-
return;
74-
}
75-
76-
if let Err(_) = compare_method_predicate_entailment(
77-
tcx,
78-
impl_m,
79-
impl_m_span,
80-
trait_m,
81-
impl_trait_ref,
82-
CheckImpliedWfMode::Check,
83-
) {
84-
return;
85-
}
50+
let _: Result<_, ErrorGuaranteed> = try {
51+
compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref)?;
52+
compare_number_of_generics(tcx, impl_m, trait_m, trait_item_span, false)?;
53+
compare_generic_param_kinds(tcx, impl_m, trait_m, false)?;
54+
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;
55+
compare_synthetic_generics(tcx, impl_m, trait_m)?;
56+
compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;
57+
compare_method_predicate_entailment(
58+
tcx,
59+
impl_m,
60+
impl_m_span,
61+
trait_m,
62+
impl_trait_ref,
63+
CheckImpliedWfMode::Check,
64+
)?;
65+
};
8666
}
8767

8868
/// This function is best explained by example. Consider a trait:
@@ -1493,7 +1473,7 @@ fn compare_synthetic_generics<'tcx>(
14931473
// explicit generics
14941474
(true, false) => {
14951475
err.span_label(impl_span, "expected generic parameter, found `impl Trait`");
1496-
(|| {
1476+
let _: Option<_> = try {
14971477
// try taking the name from the trait impl
14981478
// FIXME: this is obviously suboptimal since the name can already be used
14991479
// as another generic argument
@@ -1526,14 +1506,13 @@ fn compare_synthetic_generics<'tcx>(
15261506
],
15271507
Applicability::MaybeIncorrect,
15281508
);
1529-
Some(())
1530-
})();
1509+
};
15311510
}
15321511
// The case where the trait method uses `impl Trait`, but the impl method uses
15331512
// explicit generics.
15341513
(false, true) => {
15351514
err.span_label(impl_span, "expected `impl Trait`, found generic parameter");
1536-
(|| {
1515+
let _: Option<_> = try {
15371516
let impl_m = impl_m.def_id.as_local()?;
15381517
let impl_m = tcx.hir().expect_impl_item(impl_m);
15391518
let input_tys = match impl_m.kind {
@@ -1573,8 +1552,7 @@ fn compare_synthetic_generics<'tcx>(
15731552
],
15741553
Applicability::MaybeIncorrect,
15751554
);
1576-
Some(())
1577-
})();
1555+
};
15781556
}
15791557
_ => unreachable!(),
15801558
}
@@ -1799,16 +1777,16 @@ pub(super) fn compare_impl_ty<'tcx>(
17991777
) {
18001778
debug!("compare_impl_type(impl_trait_ref={:?})", impl_trait_ref);
18011779

1802-
let _: Result<(), ErrorGuaranteed> = (|| {
1780+
let _: Result<(), ErrorGuaranteed> = try {
18031781
compare_number_of_generics(tcx, impl_ty, trait_ty, trait_item_span, false)?;
18041782

18051783
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
18061784

18071785
let sp = tcx.def_span(impl_ty.def_id);
18081786
compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?;
18091787

1810-
check_type_bounds(tcx, trait_ty, impl_ty, impl_ty_span, impl_trait_ref)
1811-
})();
1788+
check_type_bounds(tcx, trait_ty, impl_ty, impl_ty_span, impl_trait_ref)?;
1789+
};
18121790
}
18131791

18141792
/// The equivalent of [compare_method_predicate_entailment], but for associated types

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
251251
VarValue::Empty(a_universe) => {
252252
let b_data = var_values.value_mut(b_vid);
253253

254-
let changed = (|| match *b_data {
254+
let changed = match *b_data {
255255
VarValue::Empty(b_universe) => {
256256
// Empty regions are ordered according to the universe
257257
// they are associated with.
@@ -280,20 +280,20 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
280280
};
281281

282282
if lub == cur_region {
283-
return false;
283+
false
284+
} else {
285+
debug!(
286+
"Expanding value of {:?} from {:?} to {:?}",
287+
b_vid, cur_region, lub
288+
);
289+
290+
*b_data = VarValue::Value(lub);
291+
true
284292
}
285-
286-
debug!(
287-
"Expanding value of {:?} from {:?} to {:?}",
288-
b_vid, cur_region, lub
289-
);
290-
291-
*b_data = VarValue::Value(lub);
292-
true
293293
}
294294

295295
VarValue::ErrorValue => false,
296-
})();
296+
};
297297

298298
if changed {
299299
changes.push(b_vid);

compiler/rustc_interface/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(internal_output_capture)]
44
#![feature(thread_spawn_unchecked)]
55
#![feature(once_cell)]
6+
#![feature(try_blocks)]
67
#![recursion_limit = "256"]
78
#![allow(rustc::potential_query_instability)]
89
#![deny(rustc::untranslatable_diagnostic)]

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ fn write_out_deps(
559559
}
560560
let deps_filename = outputs.path(OutputType::DepInfo);
561561

562-
let result = (|| -> io::Result<()> {
562+
let result: io::Result<()> = try {
563563
// Build a list of files used to compile the output and
564564
// write Makefile-compatible dependency rules
565565
let mut files: Vec<String> = sess
@@ -646,9 +646,7 @@ fn write_out_deps(
646646
writeln!(file)?;
647647
}
648648
}
649-
650-
Ok(())
651-
})();
649+
};
652650

653651
match result {
654652
Ok(_) => {

compiler/rustc_middle/src/traits/chalk.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,20 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> {
159159
}
160160
chalk_ir::TyKind::Array(ty, len) => Some(write!(fmt, "[{:?}; {:?}]", ty, len)),
161161
chalk_ir::TyKind::Slice(ty) => Some(write!(fmt, "[{:?}]", ty)),
162-
chalk_ir::TyKind::Tuple(len, substs) => Some((|| {
163-
write!(fmt, "(")?;
164-
for (idx, substitution) in substs.interned().iter().enumerate() {
165-
if idx == *len && *len != 1 {
166-
// Don't add a trailing comma if the tuple has more than one element
167-
write!(fmt, "{:?}", substitution)?;
168-
} else {
169-
write!(fmt, "{:?},", substitution)?;
162+
chalk_ir::TyKind::Tuple(len, substs) => Some(
163+
try {
164+
write!(fmt, "(")?;
165+
for (idx, substitution) in substs.interned().iter().enumerate() {
166+
if idx == *len && *len != 1 {
167+
// Don't add a trailing comma if the tuple has more than one element
168+
write!(fmt, "{:?}", substitution)?;
169+
} else {
170+
write!(fmt, "{:?},", substitution)?;
171+
}
170172
}
171-
}
172-
write!(fmt, ")")
173-
})()),
173+
write!(fmt, ")")?;
174+
},
175+
),
174176
_ => None,
175177
}
176178
}

compiler/rustc_mir_build/src/build/custom/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ pub(super) fn build_custom_mir<'tcx>(
8686
block_map: FxHashMap::default(),
8787
};
8888

89-
let res = (|| {
89+
let res: PResult<_> = try {
9090
pctxt.parse_args(&params)?;
91-
pctxt.parse_body(expr)
92-
})();
91+
pctxt.parse_body(expr)?;
92+
};
9393
if let Err(err) = res {
9494
tcx.sess.diagnostic().span_fatal(
9595
err.span,

compiler/rustc_mir_build/src/build/expr/stmt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
113113
//
114114
// it is usually better to focus on `the_value` rather
115115
// than the entirety of block(s) surrounding it.
116-
let adjusted_span = (|| {
116+
let adjusted_span =
117117
if let ExprKind::Block { block } = expr.kind
118118
&& let Some(tail_ex) = this.thir[block].expr
119119
{
@@ -135,10 +135,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
135135
tail_result_is_ignored: true,
136136
span: expr.span,
137137
});
138-
return Some(expr.span);
139-
}
140-
None
141-
})();
138+
Some(expr.span)
139+
} else {
140+
None
141+
};
142142

143143
let temp =
144144
unpack!(block = this.as_temp(block, statement_scope, expr, Mutability::Not));

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,23 @@ impl IntRange {
141141
) -> Option<IntRange> {
142142
let ty = value.ty();
143143
if let Some((target_size, bias)) = Self::integral_size_and_signed_bias(tcx, ty) {
144-
let val = (|| {
145-
match value {
146-
mir::ConstantKind::Val(ConstValue::Scalar(scalar), _) => {
147-
// For this specific pattern we can skip a lot of effort and go
148-
// straight to the result, after doing a bit of checking. (We
149-
// could remove this branch and just fall through, which
150-
// is more general but much slower.)
151-
return scalar.to_bits_or_ptr_internal(target_size).unwrap().left();
144+
let val =
145+
if let mir::ConstantKind::Val(ConstValue::Scalar(scalar), _) = value {
146+
// For this specific pattern we can skip a lot of effort and go
147+
// straight to the result, after doing a bit of checking. (We
148+
// could remove this branch and just fall through, which
149+
// is more general but much slower.)
150+
scalar.to_bits_or_ptr_internal(target_size).unwrap().left()?
151+
} else {
152+
if let mir::ConstantKind::Ty(c) = value
153+
&& let ty::ConstKind::Value(_) = c.kind()
154+
{
155+
bug!("encountered ConstValue in mir::ConstantKind::Ty, whereas this is expected to be in ConstantKind::Val");
152156
}
153-
mir::ConstantKind::Ty(c) => match c.kind() {
154-
ty::ConstKind::Value(_) => bug!(
155-
"encountered ConstValue in mir::ConstantKind::Ty, whereas this is expected to be in ConstantKind::Val"
156-
),
157-
_ => {}
158-
},
159-
_ => {}
160-
}
161157

162-
// This is a more general form of the previous case.
163-
value.try_eval_bits(tcx, param_env, ty)
164-
})()?;
158+
// This is a more general form of the previous case.
159+
value.try_eval_bits(tcx, param_env, ty)?
160+
};
165161
let val = val ^ bias;
166162
Some(IntRange { range: val..=val, bias })
167163
} else {

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,12 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
208208
_ => {
209209
// This `for` loop was once a call to `all()`, but this lower-level
210210
// form was a perf win. See #64545 for details.
211-
(|| {
212-
for &infer_var in &pending_obligation.stalled_on {
213-
if self.selcx.infcx.ty_or_const_infer_var_changed(infer_var) {
214-
return true;
215-
}
211+
for &infer_var in &pending_obligation.stalled_on {
212+
if self.selcx.infcx.ty_or_const_infer_var_changed(infer_var) {
213+
return true;
216214
}
217-
false
218-
})()
215+
}
216+
false
219217
}
220218
}
221219
}

compiler/rustc_trait_selection/src/traits/query/normalize.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
201201
// wait to fold the substs.
202202

203203
// Wrap this in a closure so we don't accidentally return from the outer function
204-
let res = (|| match *ty.kind() {
204+
let res = match *ty.kind() {
205205
// This is really important. While we *can* handle this, this has
206206
// severe performance implications for large opaque types with
207207
// late-bound regions. See `issue-88862` benchmark.
@@ -210,7 +210,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
210210
{
211211
// Only normalize `impl Trait` outside of type inference, usually in codegen.
212212
match self.param_env.reveal() {
213-
Reveal::UserFacing => ty.try_super_fold_with(self),
213+
Reveal::UserFacing => ty.try_super_fold_with(self)?,
214214

215215
Reveal::All => {
216216
let substs = substs.try_fold_with(self)?;
@@ -230,7 +230,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
230230
if concrete_ty == ty {
231231
bug!(
232232
"infinite recursion generic_ty: {:#?}, substs: {:#?}, \
233-
concrete_ty: {:#?}, ty: {:#?}",
233+
concrete_ty: {:#?}, ty: {:#?}",
234234
generic_ty,
235235
substs,
236236
concrete_ty,
@@ -239,7 +239,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
239239
}
240240
let folded_ty = ensure_sufficient_stack(|| self.try_fold_ty(concrete_ty));
241241
self.anon_depth -= 1;
242-
folded_ty
242+
folded_ty?
243243
}
244244
}
245245
}
@@ -287,9 +287,9 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
287287
// `tcx.normalize_projection_ty` may normalize to a type that still has
288288
// unevaluated consts, so keep normalizing here if that's the case.
289289
if res != ty && res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) {
290-
Ok(res.try_super_fold_with(self)?)
290+
res.try_super_fold_with(self)?
291291
} else {
292-
Ok(res)
292+
res
293293
}
294294
}
295295

@@ -344,14 +344,14 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
344344
// `tcx.normalize_projection_ty` may normalize to a type that still has
345345
// unevaluated consts, so keep normalizing here if that's the case.
346346
if res != ty && res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) {
347-
Ok(res.try_super_fold_with(self)?)
347+
res.try_super_fold_with(self)?
348348
} else {
349-
Ok(res)
349+
res
350350
}
351351
}
352352

353-
_ => ty.try_super_fold_with(self),
354-
})()?;
353+
_ => ty.try_super_fold_with(self)?,
354+
};
355355

356356
self.cache.insert(ty, res);
357357
Ok(res)

0 commit comments

Comments
 (0)