From fe8f66e4bcbe36e7e4b21943661ff23b5369c64b Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Mon, 13 May 2024 13:32:31 -0400 Subject: [PATCH] `rustc_hir_typeck`: Account for `skipped_ref_pats` in `expr_use_visitor` Fixes #125058 --- .../rustc_hir_typeck/src/expr_use_visitor.rs | 12 +++++++++- .../pattern/skipped-ref-pats-issue-125058.rs | 18 ++++++++++++++ .../skipped-ref-pats-issue-125058.stderr | 24 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/ui/pattern/skipped-ref-pats-issue-125058.rs create mode 100644 tests/ui/pattern/skipped-ref-pats-issue-125058.stderr diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 1864c7e6ef8..89f62577506 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -18,12 +18,12 @@ use rustc_hir::def::{CtorOf, Res}; use rustc_hir::def_id::LocalDefId; use rustc_hir::{HirId, PatKind}; -use rustc_middle::{bug, span_bug}; use rustc_middle::hir::place::ProjectionKind; use rustc_middle::mir::FakeReadCause; use rustc_middle::ty::{ self, adjustment, AdtKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt as _, }; +use rustc_middle::{bug, span_bug}; use rustc_span::{ErrorGuaranteed, Span}; use rustc_target::abi::{FieldIdx, VariantIdx, FIRST_VARIANT}; use rustc_trait_selection::infer::InferCtxtExt; @@ -1181,6 +1181,10 @@ fn pat_ty_adjusted(&self, pat: &hir::Pat<'_>) -> Result, Cx::Error> { debug!("pat_ty(pat={:?}) found adjusted ty `{:?}`", pat, first_ty); return Ok(*first_ty); } + } else if let PatKind::Ref(subpat, _) = pat.kind + && self.cx.typeck_results().skipped_ref_pats().contains(pat.hir_id) + { + return self.pat_ty_adjusted(subpat); } self.pat_ty_unadjusted(pat) @@ -1712,6 +1716,12 @@ fn cat_pattern( self.cat_pattern(place_with_id, subpat, op)?; } + PatKind::Ref(subpat, _) + if self.cx.typeck_results().skipped_ref_pats().contains(pat.hir_id) => + { + self.cat_pattern(place_with_id, subpat, op)?; + } + PatKind::Box(subpat) | PatKind::Ref(subpat, _) => { // box p1, &p1, &mut p1. we can ignore the mutability of // PatKind::Ref since that information is already contained diff --git a/tests/ui/pattern/skipped-ref-pats-issue-125058.rs b/tests/ui/pattern/skipped-ref-pats-issue-125058.rs new file mode 100644 index 00000000000..b733e5fda0a --- /dev/null +++ b/tests/ui/pattern/skipped-ref-pats-issue-125058.rs @@ -0,0 +1,18 @@ +//@ run-pass +//@ edition: 2024 +//@ compile-flags: -Zunstable-options + +#![allow(incomplete_features)] +#![feature(ref_pat_eat_one_layer_2024)] + +struct Foo; +//~^ WARN struct `Foo` is never constructed + +fn main() { + || { + //~^ WARN unused closure that must be used + if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { + let _: u32 = x; + } + }; +} diff --git a/tests/ui/pattern/skipped-ref-pats-issue-125058.stderr b/tests/ui/pattern/skipped-ref-pats-issue-125058.stderr new file mode 100644 index 00000000000..cee1cc673c7 --- /dev/null +++ b/tests/ui/pattern/skipped-ref-pats-issue-125058.stderr @@ -0,0 +1,24 @@ +warning: struct `Foo` is never constructed + --> $DIR/skipped-ref-pats-issue-125058.rs:8:8 + | +LL | struct Foo; + | ^^^ + | + = note: `#[warn(dead_code)]` on by default + +warning: unused closure that must be used + --> $DIR/skipped-ref-pats-issue-125058.rs:12:5 + | +LL | / || { +LL | | +LL | | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { +LL | | let _: u32 = x; +LL | | } +LL | | }; + | |_____^ + | + = note: closures are lazy and do nothing unless called + = note: `#[warn(unused_must_use)]` on by default + +warning: 2 warnings emitted +