Skip to content

Commit cc14db6

Browse files
committed
Make it clearer that check_pat_lit only handles literal patterns
1 parent 18d13b5 commit cc14db6

File tree

1 file changed

+12
-12
lines changed
  • compiler/rustc_hir_typeck/src

1 file changed

+12
-12
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::assert_matches;
12
use std::cmp;
23
use std::collections::hash_map::Entry::{Occupied, Vacant};
34

@@ -24,7 +25,6 @@ use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
2425
use rustc_session::parse::feature_err;
2526
use rustc_span::edit_distance::find_best_match_for_name;
2627
use rustc_span::edition::Edition;
27-
use rustc_span::source_map::Spanned;
2828
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, kw, sym};
2929
use rustc_trait_selection::infer::InferCtxtExt;
3030
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
@@ -611,7 +611,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
611611
self.write_ty(*hir_id, ty);
612612
ty
613613
}
614-
PatKind::Expr(lt) => self.check_pat_lit(pat.span, lt, expected, &pat_info.top_info),
614+
PatKind::Expr(expr @ PatExpr { kind: PatExprKind::Lit { lit, .. }, .. }) => {
615+
self.check_pat_lit(pat.span, expr, &lit.node, expected, &pat_info.top_info)
616+
}
615617
PatKind::Range(lhs, rhs, _) => {
616618
self.check_pat_range(pat.span, lhs, rhs, expected, &pat_info.top_info)
617619
}
@@ -938,31 +940,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
938940
fn check_pat_lit(
939941
&self,
940942
span: Span,
941-
lt: &hir::PatExpr<'tcx>,
943+
expr: &hir::PatExpr<'tcx>,
944+
lit_kind: &ast::LitKind,
942945
expected: Ty<'tcx>,
943946
ti: &TopInfo<'tcx>,
944947
) -> Ty<'tcx> {
948+
assert_matches!(expr.kind, hir::PatExprKind::Lit { .. });
949+
945950
// We've already computed the type above (when checking for a non-ref pat),
946951
// so avoid computing it again.
947-
let ty = self.node_ty(lt.hir_id);
952+
let ty = self.node_ty(expr.hir_id);
948953

949954
// Byte string patterns behave the same way as array patterns
950955
// They can denote both statically and dynamically-sized byte arrays.
951956
// Additionally, when `deref_patterns` is enabled, byte string literal patterns may have
952957
// types `[u8]` or `[u8; N]`, in order to type, e.g., `deref!(b"..."): Vec<u8>`.
953958
let mut pat_ty = ty;
954-
if let hir::PatExprKind::Lit {
955-
lit: Spanned { node: ast::LitKind::ByteStr(..), .. }, ..
956-
} = lt.kind
957-
{
959+
if matches!(lit_kind, ast::LitKind::ByteStr(..)) {
958960
let tcx = self.tcx;
959961
let expected = self.structurally_resolve_type(span, expected);
960962
match *expected.kind() {
961963
// Allow `b"...": &[u8]`
962964
ty::Ref(_, inner_ty, _)
963965
if self.try_structurally_resolve_type(span, inner_ty).is_slice() =>
964966
{
965-
trace!(?lt.hir_id.local_id, "polymorphic byte string lit");
967+
trace!(?expr.hir_id.local_id, "polymorphic byte string lit");
966968
pat_ty = Ty::new_imm_ref(
967969
tcx,
968970
tcx.lifetimes.re_static,
@@ -988,9 +990,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
988990
// When `deref_patterns` is enabled, in order to allow `deref!("..."): String`, we allow
989991
// string literal patterns to have type `str`. This is accounted for when lowering to MIR.
990992
if self.tcx.features().deref_patterns()
991-
&& let hir::PatExprKind::Lit {
992-
lit: Spanned { node: ast::LitKind::Str(..), .. }, ..
993-
} = lt.kind
993+
&& matches!(lit_kind, ast::LitKind::Str(..))
994994
&& self.try_structurally_resolve_type(span, expected).is_str()
995995
{
996996
pat_ty = self.tcx.types.str_;

0 commit comments

Comments
 (0)