Skip to content

Commit

Permalink
Rollup merge of #113007 - compiler-errors:dont-structural-resolve-byt…
Browse files Browse the repository at this point in the history
…e-str-pat, r=oli-obk

Revert "Structurally resolve correctly in check_pat_lit"

This reverts commit 54fb5a4. Also adds a couple of tests, and downgrades the existing `-Ztrait-solver=next` test to a known-bug.

Fixes #112993
  • Loading branch information
matthiaskrgr authored Jun 25, 2023
2 parents 0d03812 + e304a1f commit 75f6a7a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// They can denote both statically and dynamically-sized byte arrays.
let mut pat_ty = ty;
if let hir::ExprKind::Lit(Spanned { node: ast::LitKind::ByteStr(..), .. }) = lt.kind {
if let ty::Ref(_, inner_ty, _) = *self.structurally_resolved_type(span, expected).kind()
&& self.structurally_resolved_type(span, inner_ty).is_slice()
let expected = self.structurally_resolved_type(span, expected);
if let ty::Ref(_, inner_ty, _) = expected.kind()
&& matches!(inner_ty.kind(), ty::Slice(_))
{
let tcx = self.tcx;
trace!(?lt.hir_id.local_id, "polymorphic byte string lit");
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/pattern/byte-string-inference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// check-pass

fn load<L>() -> Option<L> {
todo!()
}

fn main() {
while let Some(tag) = load() {
match &tag {
b"NAME" => {}
b"DATA" => {}
_ => {}
}
}
}
2 changes: 1 addition & 1 deletion tests/ui/traits/new-solver/slice-match-byte-lit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// compile-flags: -Ztrait-solver=next
// check-pass
// known-bug: rust-lang/trait-system-refactor-initiative#38

fn test(s: &[u8]) {
match &s[0..3] {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/traits/new-solver/slice-match-byte-lit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0271]: type mismatch resolving `[u8; 3] <: <Range<usize> as SliceIndex<[u8]>>::Output`
--> $DIR/slice-match-byte-lit.rs:6:9
|
LL | match &s[0..3] {
| -------- this expression has type `&<std::ops::Range<usize> as SliceIndex<[u8]>>::Output`
LL | b"uwu" => {}
| ^^^^^^ types differ

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.

0 comments on commit 75f6a7a

Please sign in to comment.