Skip to content

Commit ab19d4a

Browse files
authored
Rollup merge of #93046 - est31:let_else, r=davidtwco
Use let_else in even more places Followup of #89933, #91018, #91481.
2 parents 1f3a2dd + b2dd137 commit ab19d4a

File tree

9 files changed

+131
-137
lines changed

9 files changed

+131
-137
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,7 @@ fn generator_layout_and_saved_local_names<'tcx>(
14931493

14941494
let state_arg = mir::Local::new(1);
14951495
for var in &body.var_debug_info {
1496-
let place = if let mir::VarDebugInfoContents::Place(p) = var.value { p } else { continue };
1496+
let mir::VarDebugInfoContents::Place(place) = &var.value else { continue };
14971497
if place.local != state_arg {
14981498
continue;
14991499
}

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(bool_to_option)]
99
#![feature(crate_visibility_modifier)]
10+
#![feature(let_else)]
1011
#![feature(extern_types)]
1112
#![feature(nll)]
1213
#![recursion_limit = "256"]

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(bool_to_option)]
22
#![feature(box_patterns)]
3+
#![feature(let_else)]
34
#![feature(internal_output_capture)]
45
#![feature(thread_spawn_unchecked)]
56
#![feature(nll)]

compiler/rustc_interface/src/util.rs

+46-46
Original file line numberDiff line numberDiff line change
@@ -717,57 +717,57 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
717717
}
718718

719719
fn should_ignore_fn(ret_ty: &ast::FnRetTy) -> bool {
720-
if let ast::FnRetTy::Ty(ref ty) = ret_ty {
721-
fn involves_impl_trait(ty: &ast::Ty) -> bool {
722-
match ty.kind {
723-
ast::TyKind::ImplTrait(..) => true,
724-
ast::TyKind::Slice(ref subty)
725-
| ast::TyKind::Array(ref subty, _)
726-
| ast::TyKind::Ptr(ast::MutTy { ty: ref subty, .. })
727-
| ast::TyKind::Rptr(_, ast::MutTy { ty: ref subty, .. })
728-
| ast::TyKind::Paren(ref subty) => involves_impl_trait(subty),
729-
ast::TyKind::Tup(ref tys) => any_involves_impl_trait(tys.iter()),
730-
ast::TyKind::Path(_, ref path) => {
731-
path.segments.iter().any(|seg| match seg.args.as_deref() {
732-
None => false,
733-
Some(&ast::GenericArgs::AngleBracketed(ref data)) => {
734-
data.args.iter().any(|arg| match arg {
735-
ast::AngleBracketedArg::Arg(arg) => match arg {
736-
ast::GenericArg::Type(ty) => involves_impl_trait(ty),
737-
ast::GenericArg::Lifetime(_)
738-
| ast::GenericArg::Const(_) => false,
739-
},
740-
ast::AngleBracketedArg::Constraint(c) => match c.kind {
741-
ast::AssocConstraintKind::Bound { .. } => true,
742-
ast::AssocConstraintKind::Equality { ref term } => {
743-
match term {
744-
Term::Ty(ty) => involves_impl_trait(ty),
745-
// FIXME(...): This should check if the constant
746-
// involves a trait impl, but for now ignore.
747-
Term::Const(_) => false,
748-
}
720+
let ast::FnRetTy::Ty(ref ty) = ret_ty else {
721+
return false;
722+
};
723+
fn involves_impl_trait(ty: &ast::Ty) -> bool {
724+
match ty.kind {
725+
ast::TyKind::ImplTrait(..) => true,
726+
ast::TyKind::Slice(ref subty)
727+
| ast::TyKind::Array(ref subty, _)
728+
| ast::TyKind::Ptr(ast::MutTy { ty: ref subty, .. })
729+
| ast::TyKind::Rptr(_, ast::MutTy { ty: ref subty, .. })
730+
| ast::TyKind::Paren(ref subty) => involves_impl_trait(subty),
731+
ast::TyKind::Tup(ref tys) => any_involves_impl_trait(tys.iter()),
732+
ast::TyKind::Path(_, ref path) => {
733+
path.segments.iter().any(|seg| match seg.args.as_deref() {
734+
None => false,
735+
Some(&ast::GenericArgs::AngleBracketed(ref data)) => {
736+
data.args.iter().any(|arg| match arg {
737+
ast::AngleBracketedArg::Arg(arg) => match arg {
738+
ast::GenericArg::Type(ty) => involves_impl_trait(ty),
739+
ast::GenericArg::Lifetime(_) | ast::GenericArg::Const(_) => {
740+
false
741+
}
742+
},
743+
ast::AngleBracketedArg::Constraint(c) => match c.kind {
744+
ast::AssocConstraintKind::Bound { .. } => true,
745+
ast::AssocConstraintKind::Equality { ref term } => {
746+
match term {
747+
Term::Ty(ty) => involves_impl_trait(ty),
748+
// FIXME(...): This should check if the constant
749+
// involves a trait impl, but for now ignore.
750+
Term::Const(_) => false,
749751
}
750-
},
751-
})
752-
}
753-
Some(&ast::GenericArgs::Parenthesized(ref data)) => {
754-
any_involves_impl_trait(data.inputs.iter())
755-
|| ReplaceBodyWithLoop::should_ignore_fn(&data.output)
756-
}
757-
})
758-
}
759-
_ => false,
752+
}
753+
},
754+
})
755+
}
756+
Some(&ast::GenericArgs::Parenthesized(ref data)) => {
757+
any_involves_impl_trait(data.inputs.iter())
758+
|| ReplaceBodyWithLoop::should_ignore_fn(&data.output)
759+
}
760+
})
760761
}
762+
_ => false,
761763
}
764+
}
762765

763-
fn any_involves_impl_trait<'a, I: Iterator<Item = &'a P<ast::Ty>>>(mut it: I) -> bool {
764-
it.any(|subty| involves_impl_trait(subty))
765-
}
766-
767-
involves_impl_trait(ty)
768-
} else {
769-
false
766+
fn any_involves_impl_trait<'a, I: Iterator<Item = &'a P<ast::Ty>>>(mut it: I) -> bool {
767+
it.any(|subty| involves_impl_trait(subty))
770768
}
769+
770+
involves_impl_trait(ty)
771771
}
772772

773773
fn is_sig_const(sig: &ast::FnSig) -> bool {

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

+15-16
Original file line numberDiff line numberDiff line change
@@ -1347,23 +1347,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13471347

13481348
let mut otherwise = None;
13491349
for match_pair in match_pairs {
1350-
if let PatKind::Or { ref pats } = *match_pair.pattern.kind {
1351-
let or_span = match_pair.pattern.span;
1352-
let place = match_pair.place;
1353-
1354-
first_candidate.visit_leaves(|leaf_candidate| {
1355-
self.test_or_pattern(
1356-
leaf_candidate,
1357-
&mut otherwise,
1358-
pats,
1359-
or_span,
1360-
place.clone(),
1361-
fake_borrows,
1362-
);
1363-
});
1364-
} else {
1350+
let PatKind::Or { ref pats } = &*match_pair.pattern.kind else {
13651351
bug!("Or-patterns should have been sorted to the end");
1366-
}
1352+
};
1353+
let or_span = match_pair.pattern.span;
1354+
let place = match_pair.place;
1355+
1356+
first_candidate.visit_leaves(|leaf_candidate| {
1357+
self.test_or_pattern(
1358+
leaf_candidate,
1359+
&mut otherwise,
1360+
pats,
1361+
or_span,
1362+
place.clone(),
1363+
fake_borrows,
1364+
);
1365+
});
13671366
}
13681367

13691368
let remainder_start = otherwise.unwrap_or_else(|| self.cfg.start_new_block());

compiler/rustc_mir_build/src/build/matches/test.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8888
switch_ty: Ty<'tcx>,
8989
options: &mut FxIndexMap<&'tcx ty::Const<'tcx>, u128>,
9090
) -> bool {
91-
let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) {
92-
Some(match_pair) => match_pair,
93-
_ => {
94-
return false;
95-
}
91+
let Some(match_pair) = candidate.match_pairs.iter().find(|mp| mp.place == *test_place) else {
92+
return false;
9693
};
9794

9895
match *match_pair.pattern.kind {

compiler/rustc_resolve/src/late/diagnostics.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1171,9 +1171,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
11711171
ident: Symbol,
11721172
kind: &AssocItemKind,
11731173
) -> Option<Symbol> {
1174-
let module = if let Some((module, _)) = self.current_trait_ref {
1175-
module
1176-
} else {
1174+
let Some((module, _)) = &self.current_trait_ref else {
11771175
return None;
11781176
};
11791177
if ident == kw::Underscore {

compiler/rustc_resolve/src/late/lifetimes.rs

+37-38
Original file line numberDiff line numberDiff line change
@@ -1000,46 +1000,45 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
10001000
// `fn foo<'a>() -> MyAnonTy<'a> { ... }`
10011001
// ^ ^this gets resolved in the current scope
10021002
for lifetime in lifetimes {
1003-
if let hir::GenericArg::Lifetime(lifetime) = lifetime {
1004-
self.visit_lifetime(lifetime);
1003+
let hir::GenericArg::Lifetime(lifetime) = lifetime else {
1004+
continue
1005+
};
1006+
self.visit_lifetime(lifetime);
1007+
1008+
// Check for predicates like `impl for<'a> Trait<impl OtherTrait<'a>>`
1009+
// and ban them. Type variables instantiated inside binders aren't
1010+
// well-supported at the moment, so this doesn't work.
1011+
// In the future, this should be fixed and this error should be removed.
1012+
let def = self.map.defs.get(&lifetime.hir_id).cloned();
1013+
let Some(Region::LateBound(_, _, def_id, _)) = def else {
1014+
continue
1015+
};
1016+
let Some(def_id) = def_id.as_local() else {
1017+
continue
1018+
};
1019+
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1020+
// Ensure that the parent of the def is an item, not HRTB
1021+
let parent_id = self.tcx.hir().get_parent_node(hir_id);
1022+
// FIXME(cjgillot) Can this check be replaced by
1023+
// `let parent_is_item = parent_id.is_owner();`?
1024+
let parent_is_item = if let Some(parent_def_id) = parent_id.as_owner() {
1025+
matches!(self.tcx.hir().krate().owners.get(parent_def_id), Some(Some(_)),)
1026+
} else {
1027+
false
1028+
};
10051029

1006-
// Check for predicates like `impl for<'a> Trait<impl OtherTrait<'a>>`
1007-
// and ban them. Type variables instantiated inside binders aren't
1008-
// well-supported at the moment, so this doesn't work.
1009-
// In the future, this should be fixed and this error should be removed.
1010-
let def = self.map.defs.get(&lifetime.hir_id).cloned();
1011-
if let Some(Region::LateBound(_, _, def_id, _)) = def {
1012-
if let Some(def_id) = def_id.as_local() {
1013-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1014-
// Ensure that the parent of the def is an item, not HRTB
1015-
let parent_id = self.tcx.hir().get_parent_node(hir_id);
1016-
// FIXME(cjgillot) Can this check be replaced by
1017-
// `let parent_is_item = parent_id.is_owner();`?
1018-
let parent_is_item =
1019-
if let Some(parent_def_id) = parent_id.as_owner() {
1020-
matches!(
1021-
self.tcx.hir().krate().owners.get(parent_def_id),
1022-
Some(Some(_)),
1023-
)
1024-
} else {
1025-
false
1026-
};
1027-
1028-
if !parent_is_item {
1029-
if !self.trait_definition_only {
1030-
struct_span_err!(
1031-
self.tcx.sess,
1032-
lifetime.span,
1033-
E0657,
1034-
"`impl Trait` can only capture lifetimes \
1035-
bound at the fn or impl level"
1036-
)
1037-
.emit();
1038-
}
1039-
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
1040-
}
1041-
}
1030+
if !parent_is_item {
1031+
if !self.trait_definition_only {
1032+
struct_span_err!(
1033+
self.tcx.sess,
1034+
lifetime.span,
1035+
E0657,
1036+
"`impl Trait` can only capture lifetimes \
1037+
bound at the fn or impl level"
1038+
)
1039+
.emit();
10421040
}
1041+
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
10431042
}
10441043
}
10451044

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

+27-28
Original file line numberDiff line numberDiff line change
@@ -840,39 +840,38 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
840840
};
841841

842842
for refs_remaining in 0..refs_number {
843-
if let ty::Ref(_, inner_ty, _) = suggested_ty.kind() {
844-
suggested_ty = inner_ty;
843+
let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else {
844+
break;
845+
};
846+
suggested_ty = inner_ty;
845847

846-
let new_obligation = self.mk_trait_obligation_with_new_self_ty(
847-
obligation.param_env,
848-
trait_ref,
849-
suggested_ty,
850-
);
848+
let new_obligation = self.mk_trait_obligation_with_new_self_ty(
849+
obligation.param_env,
850+
trait_ref,
851+
suggested_ty,
852+
);
851853

852-
if self.predicate_may_hold(&new_obligation) {
853-
let sp = self
854-
.tcx
855-
.sess
856-
.source_map()
857-
.span_take_while(span, |c| c.is_whitespace() || *c == '&');
854+
if self.predicate_may_hold(&new_obligation) {
855+
let sp = self
856+
.tcx
857+
.sess
858+
.source_map()
859+
.span_take_while(span, |c| c.is_whitespace() || *c == '&');
858860

859-
let remove_refs = refs_remaining + 1;
861+
let remove_refs = refs_remaining + 1;
860862

861-
let msg = if remove_refs == 1 {
862-
"consider removing the leading `&`-reference".to_string()
863-
} else {
864-
format!("consider removing {} leading `&`-references", remove_refs)
865-
};
863+
let msg = if remove_refs == 1 {
864+
"consider removing the leading `&`-reference".to_string()
865+
} else {
866+
format!("consider removing {} leading `&`-references", remove_refs)
867+
};
866868

867-
err.span_suggestion_short(
868-
sp,
869-
&msg,
870-
String::new(),
871-
Applicability::MachineApplicable,
872-
);
873-
break;
874-
}
875-
} else {
869+
err.span_suggestion_short(
870+
sp,
871+
&msg,
872+
String::new(),
873+
Applicability::MachineApplicable,
874+
);
876875
break;
877876
}
878877
}

0 commit comments

Comments
 (0)