Skip to content

Commit 0a2681c

Browse files
committed
Auto merge of rust-lang#113470 - compiler-errors:new-solver-structurally-resolve-pat, r=lcnr
Structurally resolve in pattern matching when peeling refs in new solver Let me know if you want me to commit the minimized test: ```rust fn test() {} fn test2() {} fn main() { let tests: &[(_, fn())] = &[ ("test", test), ("test2", test2), ]; for (a, b) in tests { todo!(); } } ``` In that test above, the match scrutinee is `<std::vec::Iter<(&'static str, fn())> as Iterator>::Item`, which we cannot peel the refs from. We also need to structurally resolve in the loop, since structural resolve is inherently shallow. I haven't come up with a test where this matters, but I can if you care. Also, I removed two other calls to `resolve_vars_with_obligations` in diagnostics code that I'm pretty convinced are not useful. r? `@lcnr`
2 parents b3ab80c + 846d54f commit 0a2681c

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
335335
expected: Ty<'tcx>,
336336
mut def_bm: BindingMode,
337337
) -> (Ty<'tcx>, BindingMode) {
338-
let mut expected = self.resolve_vars_with_obligations(expected);
339-
338+
let mut expected = self.try_structurally_resolve_type(pat.span, expected);
340339
// Peel off as many `&` or `&mut` from the scrutinee type as possible. For example,
341340
// for `match &&&mut Some(5)` the loop runs three times, aborting when it reaches
342341
// the `Some(5)` which is not of type Ref.
@@ -353,7 +352,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
353352
// Preserve the reference type. We'll need it later during THIR lowering.
354353
pat_adjustments.push(expected);
355354

356-
expected = inner_ty;
355+
expected = self.try_structurally_resolve_type(pat.span, inner_ty);
357356
def_bm = ty::BindByReference(match def_bm {
358357
// If default binding mode is by value, make it `ref` or `ref mut`
359358
// (depending on whether we observe `&` or `&mut`).
@@ -627,6 +626,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
627626
local_ty
628627
}
629628

629+
/// When a variable is bound several times in a `PatKind::Or`, it'll resolve all of the
630+
/// subsequent bindings of the same name to the first usage. Verify that all of these
631+
/// bindings have the same type by comparing them all against the type of that first pat.
630632
fn check_binding_alt_eq_ty(
631633
&self,
632634
ba: hir::BindingAnnotation,
@@ -638,7 +640,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
638640
let var_ty = self.local_ty(span, var_id);
639641
if let Some(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
640642
let hir = self.tcx.hir();
641-
let var_ty = self.resolve_vars_with_obligations(var_ty);
643+
let var_ty = self.resolve_vars_if_possible(var_ty);
642644
let msg = format!("first introduced with type `{var_ty}` here");
643645
err.span_label(hir.span(var_id), msg);
644646
let in_match = hir.parent_iter(var_id).any(|(_, n)| {
@@ -656,7 +658,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
656658
&mut err,
657659
span,
658660
var_ty,
659-
self.resolve_vars_with_obligations(ty),
661+
self.resolve_vars_if_possible(ty),
660662
ba,
661663
);
662664
err.emit();

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

+2
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,8 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16401640
)
16411641
.into(),
16421642
};
1643+
// FIXME(-Ztrait-solver=next): For diagnostic purposes, it would be nice
1644+
// to deeply normalize this type.
16431645
let normalized_term =
16441646
ocx.normalize(&obligation.cause, obligation.param_env, unnormalized_term);
16451647

tests/ui/panics/abort-on-panic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// run-pass
2+
// revisions: current next
3+
//[next] compile-flags: -Ztrait-solver=next
24

35
#![allow(unused_must_use)]
46
#![feature(c_unwind)]

0 commit comments

Comments
 (0)