Skip to content

Commit aeb9811

Browse files
committed
lang item escape hatch
1 parent 443df57 commit aeb9811

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,10 @@ 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::MatchLoweredCmp) {
706+
return;
707+
}
708+
705709
trace!("attempting to call a trait method");
706710
let trait_is_const = tcx.is_const_trait(trait_did);
707711

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -442,22 +442,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
442442
(ty, place)
443443
};
444444

445-
//match (ty.peel_refs().kind(), expect_ty.peel_refs().kind()) {
446-
// (ty::Slice(_), ty::Array(elem_ty, _)) => {
447-
// (_, expect) = coerce(expect_ty, expect, *elem_ty);
448-
// }
449-
// (ty::Array(elem_ty, _), ty::Slice(_)) => {
450-
// (ty, val) = coerce(ty, val, *elem_ty);
451-
// }
452-
// _ => (),
453-
//}
454-
455-
if let ty::Array(elem_ty, _) = ty.peel_refs().kind() {
456-
(ty, val) = coerce(ty, val, *elem_ty);
457-
}
458-
459-
if let ty::Array(elem_ty, _) = expect_ty.peel_refs().kind() {
460-
(_, expect) = coerce(expect_ty, expect, *elem_ty);
445+
match (ty.peel_refs().kind(), expect_ty.peel_refs().kind()) {
446+
(ty::Slice(_), ty::Array(elem_ty, _)) => {
447+
(_, expect) = coerce(expect_ty, expect, *elem_ty);
448+
}
449+
(ty::Array(elem_ty, _), ty::Slice(_)) => {
450+
(ty, val) = coerce(ty, val, *elem_ty);
451+
}
452+
_ => (),
461453
}
462454

463455
// Figure out the type on which we are calling `PartialEq`. This involves an extra wrapping
@@ -476,7 +468,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
476468
_ => span_bug!(source_info.span, "invalid type for non-scalar compare: {}", ty),
477469
};
478470

479-
let eq_def_id = self.tcx.require_lang_item(LangItem::MatchLoweredCmp, Some(source_info.span));
471+
let eq_def_id =
472+
self.tcx.require_lang_item(LangItem::MatchLoweredCmp, Some(source_info.span));
480473
let method = trait_method(self.tcx, eq_def_id, sym::do_match, [compare_ty, compare_ty]);
481474

482475
let bool_ty = self.tcx.types.bool;

library/core/src/cmp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
2626
#![stable(feature = "rust1", since = "1.0.0")]
2727

28-
mod pattern;
2928
mod bytewise;
29+
mod pattern;
3030
pub(crate) use bytewise::BytewiseEq;
3131

3232
use self::Ordering::*;

library/core/src/cmp/pattern.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#[lang = "MatchLoweredCmp"]
22
#[const_trait]
3-
trait MatchLoweredCmp<Rhs: ?Sized = Self> {
3+
trait MatchLoweredCmp<Rhs = Self>
4+
where
5+
Rhs: ?Sized,
6+
{
47
#[allow(dead_code)]
58
fn do_match(&self, other: &Rhs) -> bool;
69
}
@@ -11,8 +14,18 @@ impl const MatchLoweredCmp for u8 {
1114
}
1215
}
1316

14-
// TODO: should this be here considering the possible optimized eq target outputs?
15-
impl const MatchLoweredCmp for [u8] {
17+
impl const MatchLoweredCmp for str {
18+
#[rustc_allow_const_fn_unstable(const_trait_impl)]
19+
fn do_match(&self, other: &Self) -> bool {
20+
<[u8] as MatchLoweredCmp>::do_match((*self).as_bytes(), (*other).as_bytes())
21+
}
22+
}
23+
24+
impl<T> const MatchLoweredCmp for [T]
25+
where
26+
T: ~const MatchLoweredCmp,
27+
{
28+
#[rustc_allow_const_fn_unstable(const_trait_impl)]
1629
fn do_match(&self, other: &Self) -> bool {
1730
if self.len() != other.len() {
1831
return false;
@@ -21,7 +34,7 @@ impl const MatchLoweredCmp for [u8] {
2134
let mut i = 0;
2235

2336
while i < self.len() {
24-
if self[i] != other[i] {
37+
if <T as MatchLoweredCmp>::do_match(&self[i], &other[i]) == false {
2538
return false;
2639
}
2740

@@ -32,34 +45,19 @@ impl const MatchLoweredCmp for [u8] {
3245
}
3346
}
3447

35-
//impl<const N:usize> const MatchLoweredCmp for [u8; N] {
48+
//impl<const N:usize, T> const MatchLoweredCmp for [T; N] where T: ~const MatchLoweredCmp {
49+
// #[rustc_allow_const_fn_unstable(const_trait_impl)]
3650
// fn do_match(&self, other: &Self) -> bool {
3751
// let mut i = 0;
3852
//
3953
// while i < N {
40-
// if self[i] != other[i] {
54+
// if <T as MatchLoweredCmp>::do_match(&self[i], &other[i]) == false {
4155
// return false;
4256
// }
57+
//
4358
// i += 1;
4459
// }
4560
//
4661
// true
4762
// }
4863
//}
49-
50-
impl<T, Rhs> MatchLoweredCmp<Rhs> for T where T: PartialEq<Rhs>,
51-
{
52-
default fn do_match(&self, other: &Rhs) -> bool {
53-
<Self as PartialEq<Rhs>>::eq(self, other)
54-
}
55-
}
56-
57-
// #[rustc_const_stable_indirect]
58-
// #[rustc_allow_const_fn_unstable]
59-
// #[allow_internal_unstable]
60-
61-
//#[rustc_const_stable_indirect]
62-
63-
// wrap with fn?
64-
// macro
65-
// scary attrib

0 commit comments

Comments
 (0)