Only generate OpaqueCast
for opaque types
This commit is contained in:
parent
40e2de8c41
commit
dab1074b91
6 changed files with 47 additions and 23 deletions
|
@ -293,6 +293,10 @@ impl<'tcx> PlaceBuilder<'tcx> {
|
||||||
self.base
|
self.base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] {
|
||||||
|
&self.projection
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
|
pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
|
||||||
self.project(PlaceElem::Field(f, ty))
|
self.project(PlaceElem::Field(f, ty))
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,7 +244,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
.map(|arm| {
|
.map(|arm| {
|
||||||
let arm = &self.thir[arm];
|
let arm = &self.thir[arm];
|
||||||
let arm_has_guard = arm.guard.is_some();
|
let arm_has_guard = arm.guard.is_some();
|
||||||
let arm_candidate = Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard);
|
let arm_candidate =
|
||||||
|
Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard, self);
|
||||||
(arm, arm_candidate)
|
(arm, arm_candidate)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
@ -578,7 +579,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
initializer: PlaceBuilder<'tcx>,
|
initializer: PlaceBuilder<'tcx>,
|
||||||
set_match_place: bool,
|
set_match_place: bool,
|
||||||
) -> BlockAnd<()> {
|
) -> BlockAnd<()> {
|
||||||
let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false);
|
let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false, self);
|
||||||
let fake_borrow_temps = self.lower_match_tree(
|
let fake_borrow_temps = self.lower_match_tree(
|
||||||
block,
|
block,
|
||||||
irrefutable_pat.span,
|
irrefutable_pat.span,
|
||||||
|
@ -859,11 +860,16 @@ struct Candidate<'pat, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
|
impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
|
||||||
fn new(place: PlaceBuilder<'tcx>, pattern: &'pat Pat<'tcx>, has_guard: bool) -> Self {
|
fn new(
|
||||||
|
place: PlaceBuilder<'tcx>,
|
||||||
|
pattern: &'pat Pat<'tcx>,
|
||||||
|
has_guard: bool,
|
||||||
|
cx: &Builder<'_, 'tcx>,
|
||||||
|
) -> Self {
|
||||||
Candidate {
|
Candidate {
|
||||||
span: pattern.span,
|
span: pattern.span,
|
||||||
has_guard,
|
has_guard,
|
||||||
match_pairs: smallvec![MatchPair::new(place, pattern)],
|
match_pairs: smallvec![MatchPair::new(place, pattern, cx)],
|
||||||
bindings: Vec::new(),
|
bindings: Vec::new(),
|
||||||
ascriptions: Vec::new(),
|
ascriptions: Vec::new(),
|
||||||
subcandidates: Vec::new(),
|
subcandidates: Vec::new(),
|
||||||
|
@ -1383,7 +1389,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
debug!("candidate={:#?}\npats={:#?}", candidate, pats);
|
debug!("candidate={:#?}\npats={:#?}", candidate, pats);
|
||||||
let mut or_candidates: Vec<_> = pats
|
let mut or_candidates: Vec<_> = pats
|
||||||
.iter()
|
.iter()
|
||||||
.map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard))
|
.map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard, self))
|
||||||
.collect();
|
.collect();
|
||||||
let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect();
|
let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect();
|
||||||
let otherwise = if candidate.otherwise_block.is_some() {
|
let otherwise = if candidate.otherwise_block.is_some() {
|
||||||
|
@ -1779,8 +1785,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
let expr_span = expr.span;
|
let expr_span = expr.span;
|
||||||
let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr, expr_span));
|
let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr, expr_span));
|
||||||
let wildcard = Pat::wildcard_from_ty(pat.ty);
|
let wildcard = Pat::wildcard_from_ty(pat.ty);
|
||||||
let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false);
|
let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false, self);
|
||||||
let mut otherwise_candidate = Candidate::new(expr_place_builder.clone(), &wildcard, false);
|
let mut otherwise_candidate =
|
||||||
|
Candidate::new(expr_place_builder.clone(), &wildcard, false, self);
|
||||||
let fake_borrow_temps = self.lower_match_tree(
|
let fake_borrow_temps = self.lower_match_tree(
|
||||||
block,
|
block,
|
||||||
pat.span,
|
pat.span,
|
||||||
|
@ -2276,8 +2283,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
let (matching, failure) = self.in_if_then_scope(*let_else_scope, |this| {
|
let (matching, failure) = self.in_if_then_scope(*let_else_scope, |this| {
|
||||||
let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
|
let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
|
||||||
let pat = Pat { ty: init.ty, span: else_block_span, kind: PatKind::Wild };
|
let pat = Pat { ty: init.ty, span: else_block_span, kind: PatKind::Wild };
|
||||||
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false);
|
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false, this);
|
||||||
let mut candidate = Candidate::new(scrutinee.clone(), pattern, false);
|
let mut candidate = Candidate::new(scrutinee.clone(), pattern, false, this);
|
||||||
let fake_borrow_temps = this.lower_match_tree(
|
let fake_borrow_temps = this.lower_match_tree(
|
||||||
block,
|
block,
|
||||||
initializer_span,
|
initializer_span,
|
||||||
|
|
|
@ -132,7 +132,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
) -> Vec<Candidate<'pat, 'tcx>> {
|
) -> Vec<Candidate<'pat, 'tcx>> {
|
||||||
pats.iter()
|
pats.iter()
|
||||||
.map(|box pat| {
|
.map(|box pat| {
|
||||||
let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard);
|
let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard, self);
|
||||||
self.simplify_candidate(&mut candidate);
|
self.simplify_candidate(&mut candidate);
|
||||||
candidate
|
candidate
|
||||||
})
|
})
|
||||||
|
@ -164,7 +164,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
|
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern, self));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -194,7 +194,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
|
|
||||||
if let Some(subpattern) = subpattern.as_ref() {
|
if let Some(subpattern) = subpattern.as_ref() {
|
||||||
// this is the `x @ P` case; have to keep matching against `P` now
|
// this is the `x @ P` case; have to keep matching against `P` now
|
||||||
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
|
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern, self));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -305,7 +305,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
|
|
||||||
PatKind::Deref { ref subpattern } => {
|
PatKind::Deref { ref subpattern } => {
|
||||||
let place_builder = match_pair.place.deref();
|
let place_builder = match_pair.place.deref();
|
||||||
candidate.match_pairs.push(MatchPair::new(place_builder, subpattern));
|
candidate.match_pairs.push(MatchPair::new(place_builder, subpattern, self));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -729,7 +729,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
// e.g., `(x as Variant).0`
|
// e.g., `(x as Variant).0`
|
||||||
let place = downcast_place.clone().field(subpattern.field, subpattern.pattern.ty);
|
let place = downcast_place.clone().field(subpattern.field, subpattern.pattern.ty);
|
||||||
// e.g., `(x as Variant).0 @ P1`
|
// e.g., `(x as Variant).0 @ P1`
|
||||||
MatchPair::new(place, &subpattern.pattern)
|
MatchPair::new(place, &subpattern.pattern, self)
|
||||||
});
|
});
|
||||||
|
|
||||||
candidate.match_pairs.extend(consequent_match_pairs);
|
candidate.match_pairs.extend(consequent_match_pairs);
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
|
use crate::build::expr::as_place::PlaceBase;
|
||||||
use crate::build::expr::as_place::PlaceBuilder;
|
use crate::build::expr::as_place::PlaceBuilder;
|
||||||
use crate::build::matches::MatchPair;
|
use crate::build::matches::MatchPair;
|
||||||
use crate::build::Builder;
|
use crate::build::Builder;
|
||||||
use rustc_middle::mir::*;
|
use rustc_middle::mir::*;
|
||||||
use rustc_middle::thir::*;
|
use rustc_middle::thir::*;
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
|
use rustc_middle::ty::TypeVisitable;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
@ -17,7 +19,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|fieldpat| {
|
.map(|fieldpat| {
|
||||||
let place = place.clone().field(fieldpat.field, fieldpat.pattern.ty);
|
let place = place.clone().field(fieldpat.field, fieldpat.pattern.ty);
|
||||||
MatchPair::new(place, &fieldpat.pattern)
|
MatchPair::new(place, &fieldpat.pattern, self)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
@ -45,7 +47,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
let elem =
|
let elem =
|
||||||
ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
|
ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
|
||||||
let place = place.clone().project(elem);
|
let place = place.clone().project(elem);
|
||||||
MatchPair::new(place, subpattern)
|
MatchPair::new(place, subpattern, self)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if let Some(subslice_pat) = opt_slice {
|
if let Some(subslice_pat) = opt_slice {
|
||||||
|
@ -55,7 +57,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
to: if exact_size { min_length - suffix_len } else { suffix_len },
|
to: if exact_size { min_length - suffix_len } else { suffix_len },
|
||||||
from_end: !exact_size,
|
from_end: !exact_size,
|
||||||
});
|
});
|
||||||
match_pairs.push(MatchPair::new(subslice, subslice_pat));
|
match_pairs.push(MatchPair::new(subslice, subslice_pat, self));
|
||||||
}
|
}
|
||||||
|
|
||||||
match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
|
match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
|
||||||
|
@ -66,7 +68,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
from_end: !exact_size,
|
from_end: !exact_size,
|
||||||
};
|
};
|
||||||
let place = place.clone().project(elem);
|
let place = place.clone().project(elem);
|
||||||
MatchPair::new(place, subpattern)
|
MatchPair::new(place, subpattern, self)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,14 +97,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
|
|
||||||
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
|
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
|
||||||
pub(in crate::build) fn new(
|
pub(in crate::build) fn new(
|
||||||
mut place: PlaceBuilder<'tcx>,
|
place: PlaceBuilder<'tcx>,
|
||||||
pattern: &'pat Pat<'tcx>,
|
pattern: &'pat Pat<'tcx>,
|
||||||
|
cx: &Builder<'_, 'tcx>,
|
||||||
) -> MatchPair<'pat, 'tcx> {
|
) -> MatchPair<'pat, 'tcx> {
|
||||||
// Force the place type to the pattern's type.
|
// Force the place type to the pattern's type.
|
||||||
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
|
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
|
||||||
// FIXME(oli-obk): only add this projection if `place` actually had an opaque
|
let mut place = match place.try_upvars_resolved(cx) {
|
||||||
// type before the projection.
|
Ok(val) | Err(val) => val,
|
||||||
|
};
|
||||||
|
let may_need_cast = match place.base() {
|
||||||
|
PlaceBase::Local(local) => {
|
||||||
|
let ty = Place::ty_from(local, place.projection(), &cx.local_decls, cx.tcx).ty;
|
||||||
|
ty != pattern.ty && ty.has_opaque_types()
|
||||||
|
}
|
||||||
|
_ => true,
|
||||||
|
};
|
||||||
|
if may_need_cast {
|
||||||
place = place.project(ProjectionElem::OpaqueCast(pattern.ty));
|
place = place.project(ProjectionElem::OpaqueCast(pattern.ty));
|
||||||
|
}
|
||||||
MatchPair { place, pattern }
|
MatchPair { place, pattern }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -893,7 +893,7 @@ where
|
||||||
}
|
}
|
||||||
ty::Slice(ety) => self.open_drop_for_array(*ety, None),
|
ty::Slice(ety) => self.open_drop_for_array(*ety, None),
|
||||||
|
|
||||||
_ => bug!("open drop from non-ADT `{:?}`", ty),
|
_ => span_bug!(self.source_info.span, "open drop from non-ADT `{:?}`", ty),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue