Skip to content

Commit 5a09b72

Browse files
authored
Rollup merge of #102853 - cjgillot:skip-opaque-cast, r=jackh726
Skip chained OpaqueCast when building captures. Fixes #102089
2 parents 302bf31 + e828ce5 commit 5a09b72

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Diff for: compiler/rustc_mir_build/src/build/expr/as_place.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_target::abi::VariantIdx;
1717

1818
use rustc_index::vec::Idx;
1919

20+
use std::assert_matches::assert_matches;
2021
use std::iter;
2122

2223
/// The "outermost" place that holds this value.
@@ -232,22 +233,20 @@ fn strip_prefix<'tcx>(
232233
projections: Vec<PlaceElem<'tcx>>,
233234
prefix_projections: &[HirProjection<'tcx>],
234235
) -> impl Iterator<Item = PlaceElem<'tcx>> {
235-
let mut iter = projections.into_iter();
236-
let mut next = || match iter.next()? {
236+
let mut iter = projections
237+
.into_iter()
237238
// Filter out opaque casts, they are unnecessary in the prefix.
238-
ProjectionElem::OpaqueCast(..) => iter.next(),
239-
other => Some(other),
240-
};
239+
.filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..)));
241240
for projection in prefix_projections {
242241
match projection.kind {
243242
HirProjectionKind::Deref => {
244-
assert!(matches!(next(), Some(ProjectionElem::Deref)));
243+
assert_matches!(iter.next(), Some(ProjectionElem::Deref));
245244
}
246245
HirProjectionKind::Field(..) => {
247246
if base_ty.is_enum() {
248-
assert!(matches!(next(), Some(ProjectionElem::Downcast(..))));
247+
assert_matches!(iter.next(), Some(ProjectionElem::Downcast(..)));
249248
}
250-
assert!(matches!(next(), Some(ProjectionElem::Field(..))));
249+
assert_matches!(iter.next(), Some(ProjectionElem::Field(..)));
251250
}
252251
HirProjectionKind::Index | HirProjectionKind::Subslice => {
253252
bug!("unexpected projection kind: {:?}", projection);

Diff for: compiler/rustc_mir_build/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! This crate also contains the match exhaustiveness and usefulness checking.
44
#![allow(rustc::potential_query_instability)]
5+
#![feature(assert_matches)]
56
#![feature(box_patterns)]
67
#![feature(control_flow_enum)]
78
#![feature(if_let_guard)]
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition:2021
2+
// check-pass
3+
4+
pub struct Example<'a, T> {
5+
a: T,
6+
b: &'a T,
7+
}
8+
9+
impl<'a, T> Example<'a, T> {
10+
pub fn error_trying_to_destructure_self_in_closure(self) {
11+
let closure = || {
12+
let Self { a, b } = self;
13+
};
14+
}
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)