Skip to content

Commit 49377cc

Browse files
committed
cleanup 2
1 parent 0f4dedf commit 49377cc

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,17 +702,17 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
702702
// We can't determine the actual callee here, so we have to do different checks
703703
// than usual.
704704

705-
if tcx.is_lang_item(trait_did, LangItem::PatternConstEq) {
706-
return;
707-
}
708-
709705
trace!("attempting to call a trait method");
710706
let trait_is_const = tcx.is_const_trait(trait_did);
711707

712708
// Only consider a trait to be const if the const conditions hold.
713709
// Otherwise, it's really misleading to call something "conditionally"
714710
// const when it's very obviously not conditionally const.
715711
if trait_is_const && has_const_conditions == Some(ConstConditionsHold::Yes) {
712+
if tcx.is_lang_item(trait_did, LangItem::PatternConstEq) {
713+
return;
714+
}
715+
716716
// Trait calls are always conditionally-const.
717717
self.check_op(ops::ConditionallyConstCall {
718718
callee,

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6464
prefix_valtree,
6565
place.clone(),
6666
elem_ty,
67+
opt_slice.is_some() || !suffix.is_empty(),
6768
);
6869

6970
match_pairs.push(match_pair);
@@ -101,7 +102,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
101102
}
102103

103104
fn should_optimize_subslice(&self, subslice: &[Box<Pat<'tcx>>]) -> bool {
104-
// Only wasted effort if we're just comparing a single elememt anyway.
105105
subslice.len() > 1 && subslice.iter().all(|p| self.is_constant_pattern(p))
106106
}
107107

@@ -140,11 +140,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
140140

141141
fn valtree_to_match_pair<'pat>(
142142
&mut self,
143-
src_path: &'pat Pat<'tcx>,
143+
source_pattern: &'pat Pat<'tcx>,
144144
subslice_len: u64,
145145
valtree: ty::ValTree<'tcx>,
146146
place: PlaceBuilder<'tcx>,
147147
elem_ty: Ty<'tcx>,
148+
subsliced: bool,
148149
) -> MatchPairTree<'pat, 'tcx> {
149150
let tcx = self.tcx;
150151
let const_ty = Ty::new_imm_ref(
@@ -153,15 +154,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
153154
Ty::new_array(tcx, elem_ty, subslice_len),
154155
);
155156

156-
// note: this is fine since we set range = Some in TestCase::Constant
157-
// which notifies any eventual comparison code that it should subslice `place`
158-
let pat_ty = Ty::new_slice(tcx, elem_ty);
157+
let pat_ty = if subsliced { Ty::new_slice(tcx, elem_ty) } else { source_pattern.ty };
159158
let ty_const = ty::Const::new(tcx, ty::ConstKind::Value(const_ty, valtree));
160159
let value = Const::Ty(const_ty, ty_const);
161-
let test_case = TestCase::Constant { value, range: Some(0..subslice_len) };
160+
let test_case = TestCase::Constant { value, range: subsliced.then(|| 0..subslice_len) };
162161
let pattern = tcx.arena.alloc(Pat {
163162
ty: pat_ty,
164-
span: src_path.span,
163+
span: source_pattern.span,
165164
kind: PatKind::Constant { value },
166165
});
167166

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,37 +181,33 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
181181
ref_str_ty,
182182
);
183183
} else if !ty.is_scalar() {
184-
let new_place;
185-
let cmp_block;
186-
if let Some(range) = range {
184+
let (place, block) = if let Some(range) = range {
187185
assert_eq!(range.start, 0);
188-
let elem_ty = ty.sequence_element_type(tcx);
189-
cmp_block = self.cfg.start_new_block();
190-
let subslice_ptr = self.subslice(
186+
let target_block = self.cfg.start_new_block();
187+
let subslice = self.subslice(
191188
block,
192-
cmp_block,
189+
target_block,
193190
place,
194191
place_ty.ty,
195192
test.span,
196-
elem_ty,
193+
ty.sequence_element_type(tcx),
197194
range.end,
198195
);
199196

200-
new_place = PlaceBuilder::from(subslice_ptr).deref().to_place(self);
197+
(subslice, target_block)
201198
} else {
202-
cmp_block = block;
203-
new_place = place;
204-
}
199+
(place, block)
200+
};
205201

206202
// Use `PartialEq::eq` instead of `BinOp::Eq`
207203
// (the binop can only handle primitives)
208204
self.non_scalar_compare(
209-
cmp_block,
205+
block,
210206
success_block,
211207
fail_block,
212208
source_info,
213209
value,
214-
new_place,
210+
place,
215211
ty,
216212
);
217213
} else {
@@ -371,7 +367,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
371367
fn_span: source_info.span,
372368
});
373369

374-
subslice_ptr
370+
PlaceBuilder::from(subslice_ptr).deref().to_place(self)
375371
}
376372

377373
/// Perform `let temp = <ty as Deref>::deref(&place)`.
@@ -447,7 +443,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
447443
);
448444
}
449445

450-
/// Compare two values using `<T as std::compare::pattern::PatternConstEq>::eq`.
446+
/// Compare two values using `<T as std::cmp::pattern::PatternConstEq>::eq`
447+
/// or `<T as std::cmp::PartialEq>::eq` if they depending on exposed const-capability.
451448
/// If the values are already references, just call it directly, otherwise
452449
/// take a reference to the values first and then call it.
453450
fn non_scalar_compare(

0 commit comments

Comments
 (0)