Skip to content

Commit dab1074

Browse files
committed
Only generate OpaqueCast for opaque types
1 parent 40e2de8 commit dab1074

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

compiler/rustc_mir_build/src/build/expr/as_place.rs

+4
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ impl<'tcx> PlaceBuilder<'tcx> {
293293
self.base
294294
}
295295

296+
pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] {
297+
&self.projection
298+
}
299+
296300
pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
297301
self.project(PlaceElem::Field(f, ty))
298302
}

compiler/rustc_mir_build/src/build/matches/mod.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
244244
.map(|arm| {
245245
let arm = &self.thir[arm];
246246
let arm_has_guard = arm.guard.is_some();
247-
let arm_candidate = Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard);
247+
let arm_candidate =
248+
Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard, self);
248249
(arm, arm_candidate)
249250
})
250251
.collect()
@@ -578,7 +579,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
578579
initializer: PlaceBuilder<'tcx>,
579580
set_match_place: bool,
580581
) -> BlockAnd<()> {
581-
let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false);
582+
let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false, self);
582583
let fake_borrow_temps = self.lower_match_tree(
583584
block,
584585
irrefutable_pat.span,
@@ -859,11 +860,16 @@ struct Candidate<'pat, 'tcx> {
859860
}
860861

861862
impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
862-
fn new(place: PlaceBuilder<'tcx>, pattern: &'pat Pat<'tcx>, has_guard: bool) -> Self {
863+
fn new(
864+
place: PlaceBuilder<'tcx>,
865+
pattern: &'pat Pat<'tcx>,
866+
has_guard: bool,
867+
cx: &Builder<'_, 'tcx>,
868+
) -> Self {
863869
Candidate {
864870
span: pattern.span,
865871
has_guard,
866-
match_pairs: smallvec![MatchPair::new(place, pattern)],
872+
match_pairs: smallvec![MatchPair::new(place, pattern, cx)],
867873
bindings: Vec::new(),
868874
ascriptions: Vec::new(),
869875
subcandidates: Vec::new(),
@@ -1383,7 +1389,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13831389
debug!("candidate={:#?}\npats={:#?}", candidate, pats);
13841390
let mut or_candidates: Vec<_> = pats
13851391
.iter()
1386-
.map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard))
1392+
.map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard, self))
13871393
.collect();
13881394
let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect();
13891395
let otherwise = if candidate.otherwise_block.is_some() {
@@ -1779,8 +1785,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17791785
let expr_span = expr.span;
17801786
let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr, expr_span));
17811787
let wildcard = Pat::wildcard_from_ty(pat.ty);
1782-
let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false);
1783-
let mut otherwise_candidate = Candidate::new(expr_place_builder.clone(), &wildcard, false);
1788+
let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false, self);
1789+
let mut otherwise_candidate =
1790+
Candidate::new(expr_place_builder.clone(), &wildcard, false, self);
17841791
let fake_borrow_temps = self.lower_match_tree(
17851792
block,
17861793
pat.span,
@@ -2276,8 +2283,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
22762283
let (matching, failure) = self.in_if_then_scope(*let_else_scope, |this| {
22772284
let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
22782285
let pat = Pat { ty: init.ty, span: else_block_span, kind: PatKind::Wild };
2279-
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false);
2280-
let mut candidate = Candidate::new(scrutinee.clone(), pattern, false);
2286+
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false, this);
2287+
let mut candidate = Candidate::new(scrutinee.clone(), pattern, false, this);
22812288
let fake_borrow_temps = this.lower_match_tree(
22822289
block,
22832290
initializer_span,

compiler/rustc_mir_build/src/build/matches/simplify.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
132132
) -> Vec<Candidate<'pat, 'tcx>> {
133133
pats.iter()
134134
.map(|box pat| {
135-
let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard);
135+
let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard, self);
136136
self.simplify_candidate(&mut candidate);
137137
candidate
138138
})
@@ -164,7 +164,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
164164
});
165165
}
166166

167-
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
167+
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern, self));
168168

169169
Ok(())
170170
}
@@ -194,7 +194,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
194194

195195
if let Some(subpattern) = subpattern.as_ref() {
196196
// this is the `x @ P` case; have to keep matching against `P` now
197-
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
197+
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern, self));
198198
}
199199

200200
Ok(())
@@ -305,7 +305,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
305305

306306
PatKind::Deref { ref subpattern } => {
307307
let place_builder = match_pair.place.deref();
308-
candidate.match_pairs.push(MatchPair::new(place_builder, subpattern));
308+
candidate.match_pairs.push(MatchPair::new(place_builder, subpattern, self));
309309
Ok(())
310310
}
311311

compiler/rustc_mir_build/src/build/matches/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
729729
// e.g., `(x as Variant).0`
730730
let place = downcast_place.clone().field(subpattern.field, subpattern.pattern.ty);
731731
// e.g., `(x as Variant).0 @ P1`
732-
MatchPair::new(place, &subpattern.pattern)
732+
MatchPair::new(place, &subpattern.pattern, self)
733733
});
734734

735735
candidate.match_pairs.extend(consequent_match_pairs);

compiler/rustc_mir_build/src/build/matches/util.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use crate::build::expr::as_place::PlaceBase;
12
use crate::build::expr::as_place::PlaceBuilder;
23
use crate::build::matches::MatchPair;
34
use crate::build::Builder;
45
use rustc_middle::mir::*;
56
use rustc_middle::thir::*;
67
use rustc_middle::ty;
8+
use rustc_middle::ty::TypeVisitable;
79
use smallvec::SmallVec;
810
use std::convert::TryInto;
911

@@ -17,7 +19,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1719
.iter()
1820
.map(|fieldpat| {
1921
let place = place.clone().field(fieldpat.field, fieldpat.pattern.ty);
20-
MatchPair::new(place, &fieldpat.pattern)
22+
MatchPair::new(place, &fieldpat.pattern, self)
2123
})
2224
.collect()
2325
}
@@ -45,7 +47,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4547
let elem =
4648
ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
4749
let place = place.clone().project(elem);
48-
MatchPair::new(place, subpattern)
50+
MatchPair::new(place, subpattern, self)
4951
}));
5052

5153
if let Some(subslice_pat) = opt_slice {
@@ -55,7 +57,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5557
to: if exact_size { min_length - suffix_len } else { suffix_len },
5658
from_end: !exact_size,
5759
});
58-
match_pairs.push(MatchPair::new(subslice, subslice_pat));
60+
match_pairs.push(MatchPair::new(subslice, subslice_pat, self));
5961
}
6062

6163
match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
@@ -66,7 +68,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6668
from_end: !exact_size,
6769
};
6870
let place = place.clone().project(elem);
69-
MatchPair::new(place, subpattern)
71+
MatchPair::new(place, subpattern, self)
7072
}));
7173
}
7274

@@ -95,14 +97,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9597

9698
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
9799
pub(in crate::build) fn new(
98-
mut place: PlaceBuilder<'tcx>,
100+
place: PlaceBuilder<'tcx>,
99101
pattern: &'pat Pat<'tcx>,
102+
cx: &Builder<'_, 'tcx>,
100103
) -> MatchPair<'pat, 'tcx> {
101104
// Force the place type to the pattern's type.
102105
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
103-
// FIXME(oli-obk): only add this projection if `place` actually had an opaque
104-
// type before the projection.
105-
place = place.project(ProjectionElem::OpaqueCast(pattern.ty));
106+
let mut place = match place.try_upvars_resolved(cx) {
107+
Ok(val) | Err(val) => val,
108+
};
109+
let may_need_cast = match place.base() {
110+
PlaceBase::Local(local) => {
111+
let ty = Place::ty_from(local, place.projection(), &cx.local_decls, cx.tcx).ty;
112+
ty != pattern.ty && ty.has_opaque_types()
113+
}
114+
_ => true,
115+
};
116+
if may_need_cast {
117+
place = place.project(ProjectionElem::OpaqueCast(pattern.ty));
118+
}
106119
MatchPair { place, pattern }
107120
}
108121
}

compiler/rustc_mir_dataflow/src/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ where
893893
}
894894
ty::Slice(ety) => self.open_drop_for_array(*ety, None),
895895

896-
_ => bug!("open drop from non-ADT `{:?}`", ty),
896+
_ => span_bug!(self.source_info.span, "open drop from non-ADT `{:?}`", ty),
897897
}
898898
}
899899

0 commit comments

Comments
 (0)