Skip to content

Commit f30e546

Browse files
authored
Rollup merge of rust-lang#108678 - llogiq:use-option-as-slice, r=Nilstrieb
Use `Option::as_slice` where applicable After rust-lang#105871 introduced `Option::as_slice`, this PR uses it within the compiler. I found it interesting that all cases where `as_slice` could be used were done with different code before; so it seems the new API also has the benefit of being "the obvious solution" where before there was a mix of options, none clearly better than the rest.
2 parents 97280be + 0333450 commit f30e546

File tree

4 files changed

+8
-17
lines changed

4 files changed

+8
-17
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use rustc_trait_selection::traits::{self, ObligationCauseCode, SelectionContext}
3636

3737
use std::iter;
3838
use std::mem;
39-
use std::slice;
4039

4140
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4241
pub(in super::super) fn check_casts(&mut self) {
@@ -1507,11 +1506,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15071506
let coerce = if blk.targeted_by_break {
15081507
CoerceMany::new(coerce_to_ty)
15091508
} else {
1510-
let tail_expr: &[&hir::Expr<'_>] = match tail_expr {
1511-
Some(e) => slice::from_ref(e),
1512-
None => &[],
1513-
};
1514-
CoerceMany::with_coercion_sites(coerce_to_ty, tail_expr)
1509+
CoerceMany::with_coercion_sites(coerce_to_ty, blk.expr.as_slice())
15151510
};
15161511

15171512
let prev_diverges = self.diverges.get();

compiler/rustc_hir_typeck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(min_specialization)]
66
#![feature(control_flow_enum)]
77
#![feature(drain_filter)]
8+
#![feature(option_as_slice)]
89
#![allow(rustc::potential_query_instability)]
910
#![recursion_limit = "256"]
1011

compiler/rustc_hir_typeck/src/op.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -749,14 +749,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
749749
}
750750

751751
let opname = Ident::with_dummy_span(opname);
752-
let input_types =
753-
opt_rhs.as_ref().map(|(_, ty)| std::slice::from_ref(ty)).unwrap_or_default();
752+
let (opt_rhs_expr, opt_rhs_ty) = opt_rhs.unzip();
753+
let input_types = opt_rhs_ty.as_slice();
754754
let cause = self.cause(
755755
span,
756756
traits::BinOp {
757-
rhs_span: opt_rhs.map(|(expr, _)| expr.span),
758-
is_lit: opt_rhs
759-
.map_or(false, |(expr, _)| matches!(expr.kind, hir::ExprKind::Lit(_))),
757+
rhs_span: opt_rhs_expr.map(|expr| expr.span),
758+
is_lit: opt_rhs_expr
759+
.map_or(false, |expr| matches!(expr.kind, hir::ExprKind::Lit(_))),
760760
output_ty: expected.only_has_type(self),
761761
},
762762
);

compiler/rustc_hir_typeck/src/place_op.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_middle::ty::adjustment::{AllowTwoPhase, AutoBorrow, AutoBorrowMutabili
1111
use rustc_middle::ty::{self, Ty};
1212
use rustc_span::symbol::{sym, Ident};
1313
use rustc_span::Span;
14-
use std::slice;
1514

1615
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1716
/// Type-check `*oprnd_expr` with `oprnd_expr` type-checked already.
@@ -393,11 +392,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
393392
Some(self.typeck_results.borrow().node_substs(expr.hir_id).type_at(1))
394393
}
395394
};
396-
let arg_tys = match arg_ty {
397-
None => &[],
398-
Some(ref ty) => slice::from_ref(ty),
399-
};
400-
395+
let arg_tys = arg_ty.as_slice();
401396
let method = self.try_mutable_overloaded_place_op(expr.span, base_ty, arg_tys, op);
402397
let method = match method {
403398
Some(ok) => self.register_infer_ok_obligations(ok),

0 commit comments

Comments
 (0)