Skip to content

Commit a7b4d66

Browse files
committed
9 - Make more use of let_chains
Continuation of rust-lang#94376. cc rust-lang#53667
1 parent f0c4da4 commit a7b4d66

File tree

8 files changed

+176
-218
lines changed

8 files changed

+176
-218
lines changed

compiler/rustc_trait_selection/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010
//!
1111
//! This API is completely unstable and subject to change.
1212
13+
#![allow(rustc::potential_query_instability)]
1314
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1415
#![feature(bool_to_option)]
1516
#![feature(box_patterns)]
16-
#![feature(drain_filter)]
17+
#![feature(control_flow_enum)]
18+
#![feature(crate_visibility_modifier)]
1719
#![feature(derive_default_enum)]
20+
#![feature(drain_filter)]
1821
#![feature(hash_drain_filter)]
1922
#![feature(label_break_value)]
23+
#![feature(let_chains)]
2024
#![feature(let_else)]
2125
#![feature(never_type)]
22-
#![feature(crate_visibility_modifier)]
23-
#![feature(control_flow_enum)]
2426
#![recursion_limit = "512"] // For rustdoc
25-
#![allow(rustc::potential_query_instability)]
2627

2728
#[macro_use]
2829
extern crate rustc_macros;

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+44-46
Original file line numberDiff line numberDiff line change
@@ -1098,42 +1098,43 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10981098
);
10991099
}
11001100
}
1101-
if let &[ArgKind::Tuple(_, ref fields)] = &expected_args[..] {
1102-
if fields.len() == found_args.len() && is_closure {
1103-
let sugg = format!(
1104-
"|({}){}|",
1105-
found_args
1106-
.iter()
1107-
.map(|arg| match arg {
1108-
ArgKind::Arg(name, _) => name.to_owned(),
1109-
_ => "_".to_owned(),
1110-
})
1111-
.collect::<Vec<String>>()
1112-
.join(", "),
1113-
// add type annotations if available
1114-
if found_args.iter().any(|arg| match arg {
1115-
ArgKind::Arg(_, ty) => ty != "_",
1116-
_ => false,
1117-
}) {
1118-
format!(
1119-
": ({})",
1120-
fields
1121-
.iter()
1122-
.map(|(_, ty)| ty.to_owned())
1123-
.collect::<Vec<String>>()
1124-
.join(", ")
1125-
)
1126-
} else {
1127-
String::new()
1128-
},
1129-
);
1130-
err.span_suggestion_verbose(
1131-
found_span,
1132-
"change the closure to accept a tuple instead of individual arguments",
1133-
sugg,
1134-
Applicability::MachineApplicable,
1135-
);
1136-
}
1101+
if let &[ArgKind::Tuple(_, ref fields)] = &expected_args[..]
1102+
&& fields.len() == found_args.len()
1103+
&& is_closure
1104+
{
1105+
let sugg = format!(
1106+
"|({}){}|",
1107+
found_args
1108+
.iter()
1109+
.map(|arg| match arg {
1110+
ArgKind::Arg(name, _) => name.to_owned(),
1111+
_ => "_".to_owned(),
1112+
})
1113+
.collect::<Vec<String>>()
1114+
.join(", "),
1115+
// add type annotations if available
1116+
if found_args.iter().any(|arg| match arg {
1117+
ArgKind::Arg(_, ty) => ty != "_",
1118+
_ => false,
1119+
}) {
1120+
format!(
1121+
": ({})",
1122+
fields
1123+
.iter()
1124+
.map(|(_, ty)| ty.to_owned())
1125+
.collect::<Vec<String>>()
1126+
.join(", ")
1127+
)
1128+
} else {
1129+
String::new()
1130+
},
1131+
);
1132+
err.span_suggestion_verbose(
1133+
found_span,
1134+
"change the closure to accept a tuple instead of individual arguments",
1135+
sugg,
1136+
Applicability::MachineApplicable,
1137+
);
11371138
}
11381139
}
11391140

@@ -2231,16 +2232,13 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
22312232
if obligated_types.iter().any(|ot| ot == &self_ty) {
22322233
return true;
22332234
}
2234-
if let ty::Adt(def, substs) = self_ty.kind() {
2235-
if let [arg] = &substs[..] {
2236-
if let ty::subst::GenericArgKind::Type(ty) = arg.unpack() {
2237-
if let ty::Adt(inner_def, _) = ty.kind() {
2238-
if inner_def == def {
2239-
return true;
2240-
}
2241-
}
2242-
}
2243-
}
2235+
if let ty::Adt(def, substs) = self_ty.kind()
2236+
&& let [arg] = &substs[..]
2237+
&& let ty::subst::GenericArgKind::Type(ty) = arg.unpack()
2238+
&& let ty::Adt(inner_def, _) = ty.kind()
2239+
&& inner_def == def
2240+
{
2241+
return true;
22442242
}
22452243
}
22462244
false

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+35-50
Original file line numberDiff line numberDiff line change
@@ -891,23 +891,20 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
891891
}
892892
if let Some(typeck_results) =
893893
self.in_progress_typeck_results.map(|t| t.borrow())
894+
&& let ty = typeck_results.expr_ty_adjusted(base)
895+
&& let ty::FnDef(def_id, _substs) = ty.kind()
896+
&& let Some(hir::Node::Item(hir::Item { span, ident, .. })) =
897+
hir.get_if_local(*def_id)
894898
{
895-
let ty = typeck_results.expr_ty_adjusted(base);
896-
if let ty::FnDef(def_id, _substs) = ty.kind() {
897-
if let Some(hir::Node::Item(hir::Item { span, ident, .. })) =
898-
hir.get_if_local(*def_id)
899-
{
900-
err.span_suggestion_verbose(
901-
span.shrink_to_lo(),
902-
&format!(
903-
"alternatively, consider making `fn {}` asynchronous",
904-
ident
905-
),
906-
"async ".to_string(),
907-
Applicability::MaybeIncorrect,
908-
);
909-
}
910-
}
899+
err.span_suggestion_verbose(
900+
span.shrink_to_lo(),
901+
&format!(
902+
"alternatively, consider making `fn {}` asynchronous",
903+
ident
904+
),
905+
"async ".to_string(),
906+
Applicability::MaybeIncorrect,
907+
);
911908
}
912909
}
913910
}
@@ -1000,34 +997,24 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1000997
span: Span,
1001998
trait_pred: ty::PolyTraitPredicate<'tcx>,
1002999
) {
1003-
let is_empty_tuple =
1004-
|ty: ty::Binder<'tcx, Ty<'_>>| *ty.skip_binder().kind() == ty::Tuple(ty::List::empty());
1005-
10061000
let hir = self.tcx.hir();
10071001
let parent_node = hir.get_parent_node(obligation.cause.body_id);
10081002
let node = hir.find(parent_node);
1009-
if let Some(hir::Node::Item(hir::Item {
1010-
kind: hir::ItemKind::Fn(sig, _, body_id), ..
1011-
})) = node
1003+
if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })) = node
1004+
&& let body = hir.body(*body_id)
1005+
&& let hir::ExprKind::Block(blk, _) = &body.value.kind
1006+
&& sig.decl.output.span().overlaps(span)
1007+
&& blk.expr.is_none()
1008+
&& *trait_pred.self_ty().skip_binder().kind() == ty::Tuple(ty::List::empty())
1009+
// FIXME(estebank): When encountering a method with a trait
1010+
// bound not satisfied in the return type with a body that has
1011+
// no return, suggest removal of semicolon on last statement.
1012+
// Once that is added, close #54771.
1013+
&& let Some(stmt) = blk.stmts.last()
1014+
&& let hir::StmtKind::Semi(_) = stmt.kind
10121015
{
1013-
let body = hir.body(*body_id);
1014-
if let hir::ExprKind::Block(blk, _) = &body.value.kind {
1015-
if sig.decl.output.span().overlaps(span)
1016-
&& blk.expr.is_none()
1017-
&& is_empty_tuple(trait_pred.self_ty())
1018-
{
1019-
// FIXME(estebank): When encountering a method with a trait
1020-
// bound not satisfied in the return type with a body that has
1021-
// no return, suggest removal of semicolon on last statement.
1022-
// Once that is added, close #54771.
1023-
if let Some(ref stmt) = blk.stmts.last() {
1024-
if let hir::StmtKind::Semi(_) = stmt.kind {
1025-
let sp = self.tcx.sess.source_map().end_point(stmt.span);
1026-
err.span_label(sp, "consider removing this semicolon");
1027-
}
1028-
}
1029-
}
1030-
}
1016+
let sp = self.tcx.sess.source_map().end_point(stmt.span);
1017+
err.span_label(sp, "consider removing this semicolon");
10311018
}
10321019
}
10331020

@@ -2481,17 +2468,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24812468
debug!("suggest_await_before_try: try_trait_obligation {:?}", try_obligation);
24822469
if self.predicate_may_hold(&try_obligation)
24832470
&& impls_future.must_apply_modulo_regions()
2471+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
2472+
&& snippet.ends_with('?')
24842473
{
2485-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
2486-
if snippet.ends_with('?') {
2487-
err.span_suggestion_verbose(
2488-
span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
2489-
"consider `await`ing on the `Future`",
2490-
".await".to_string(),
2491-
Applicability::MaybeIncorrect,
2492-
);
2493-
}
2494-
}
2474+
err.span_suggestion_verbose(
2475+
span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
2476+
"consider `await`ing on the `Future`",
2477+
".await".to_string(),
2478+
Applicability::MaybeIncorrect,
2479+
);
24952480
}
24962481
}
24972482
}

compiler/rustc_trait_selection/src/traits/on_unimplemented.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ impl<'tcx> OnUnimplementedDirective {
9191
)
9292
})?;
9393
attr::eval_condition(cond, &tcx.sess.parse_sess, Some(tcx.features()), &mut |item| {
94-
if let Some(symbol) = item.value_str() {
95-
if parse_value(symbol).is_err() {
96-
errored = true;
97-
}
94+
if let Some(symbol) = item.value_str() && parse_value(symbol).is_err() {
95+
errored = true;
9896
}
9997
true
10098
});
@@ -232,24 +230,22 @@ impl<'tcx> OnUnimplementedDirective {
232230
options.iter().filter_map(|(k, v)| v.as_ref().map(|v| (*k, v.to_owned()))).collect();
233231

234232
for command in self.subcommands.iter().chain(Some(self)).rev() {
235-
if let Some(ref condition) = command.condition {
236-
if !attr::eval_condition(
237-
condition,
238-
&tcx.sess.parse_sess,
239-
Some(tcx.features()),
240-
&mut |c| {
241-
c.ident().map_or(false, |ident| {
242-
let value = c.value_str().map(|s| {
243-
OnUnimplementedFormatString(s).format(tcx, trait_ref, &options_map)
244-
});
233+
if let Some(ref condition) = command.condition && !attr::eval_condition(
234+
condition,
235+
&tcx.sess.parse_sess,
236+
Some(tcx.features()),
237+
&mut |c| {
238+
c.ident().map_or(false, |ident| {
239+
let value = c.value_str().map(|s| {
240+
OnUnimplementedFormatString(s).format(tcx, trait_ref, &options_map)
241+
});
245242

246-
options.contains(&(ident.name, value))
247-
})
248-
},
249-
) {
250-
debug!("evaluate: skipping {:?} due to condition", command);
251-
continue;
252-
}
243+
options.contains(&(ident.name, value))
244+
})
245+
},
246+
) {
247+
debug!("evaluate: skipping {:?} due to condition", command);
248+
continue;
253249
}
254250
debug!("evaluate: {:?} succeeded", command);
255251
if let Some(ref message_) = command.message {

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,19 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> {
4343
let c_ty = self.infcx.canonicalize_query(self.param_env.and(ty), &mut orig_values);
4444
let span = self.cause.span;
4545
debug!("c_ty = {:?}", c_ty);
46-
if let Ok(result) = tcx.dropck_outlives(c_ty) {
47-
if result.is_proven() {
48-
if let Ok(InferOk { value, obligations }) =
49-
self.infcx.instantiate_query_response_and_region_obligations(
50-
self.cause,
51-
self.param_env,
52-
&orig_values,
53-
result,
54-
)
55-
{
56-
let ty = self.infcx.resolve_vars_if_possible(ty);
57-
let kinds = value.into_kinds_reporting_overflows(tcx, span, ty);
58-
return InferOk { value: kinds, obligations };
59-
}
60-
}
46+
if let Ok(result) = tcx.dropck_outlives(c_ty)
47+
&& result.is_proven()
48+
&& let Ok(InferOk { value, obligations }) =
49+
self.infcx.instantiate_query_response_and_region_obligations(
50+
self.cause,
51+
self.param_env,
52+
&orig_values,
53+
result,
54+
)
55+
{
56+
let ty = self.infcx.resolve_vars_if_possible(ty);
57+
let kinds = value.into_kinds_reporting_overflows(tcx, span, ty);
58+
return InferOk { value: kinds, obligations };
6159
}
6260

6361
// Errors and ambiuity in dropck occur in two cases:

0 commit comments

Comments
 (0)