Skip to content

byte slice pattern "unsizing" #3

@compiler-errors

Description

@compiler-errors
fn test() {
    let x: [u8; 4] = [0, 0, 0, 0];

    match &x[..] {
        b"abcd" => {}
        _ => {}
    }
}

We have a structural_resolve call in check_pat_slice, but we actually need to structurally resolve the pointed-to type too. This is roughly the fix:

diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs
index c36c75e4443..27ce743beb9 100644
--- a/compiler/rustc_hir_typeck/src/pat.rs
+++ b/compiler/rustc_hir_typeck/src/pat.rs
@@ -396,8 +396,8 @@ fn check_pat_lit(
         let mut pat_ty = ty;
         if let hir::ExprKind::Lit(Spanned { node: ast::LitKind::ByteStr(..), .. }) = lt.kind {
             let expected = self.structurally_resolved_type(span, expected);
-            if let ty::Ref(_, inner_ty, _) = expected.kind()
-                && matches!(inner_ty.kind(), ty::Slice(_))
+            if let ty::Ref(_, inner_ty, _) = *expected.kind()
+                && self.structurally_resolved_type(span, inner_ty).is_slice()
             {
                 let tcx = self.tcx;
                 trace!(?lt.hir_id.local_id, "polymorphic byte string lit");

This relies on #5, since the shallow_resolve call needs to actually be able to do something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    hacked-aroundA hack exists to work around this, but it may not be the optimal solutionneeds-cleaner-fixA medium-sized fixed needs to be put up and applied

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions