Skip to content

Commit 443df57

Browse files
committed
woes with const stability
1 parent 53e56f9 commit 443df57

File tree

8 files changed

+88
-16
lines changed

8 files changed

+88
-16
lines changed

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ fn build_error_for_const_call<'tcx>(
280280
} else {
281281
let mut sugg = None;
282282

283+
// TODO: update this
283284
if ccx.tcx.is_lang_item(trait_id, LangItem::PartialEq) {
284285
match (args[0].unpack(), args[1].unpack()) {
285286
(GenericArgKind::Type(self_ty), GenericArgKind::Type(rhs_ty))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
152152
) -> MatchPairTree<'pat, 'tcx> {
153153
let tcx = self.tcx;
154154

155+
// TODO: should these be arrays?
155156
let (const_ty, pat_ty) = match src_pat_ty.kind() {
156157
ty::Slice(_) => (
157158
Ty::new_imm_ref(

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,22 @@ 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-
_ => (),
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);
453461
}
454462

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

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

474482
let bool_ty = self.tcx.types.bool;
475483
let eq_result = self.temp(bool_ty, source_info.span);

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,3 @@ pub(crate) fn ref_pat_borrow_kind(ref_mutability: Mutability) -> BorrowKind {
229229
Mutability::Not => BorrowKind::Shared,
230230
}
231231
}
232-
233-
#[lang = "MatchLoweredCmp"]
234-
pub trait MatchLoweredCmp {
235-
fn same_as(&self, other: &Self) -> bool;
236-
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ symbols! {
783783
div,
784784
div_assign,
785785
diverging_block_default,
786+
do_match,
786787
do_not_recommend,
787788
doc,
788789
doc_alias,

library/core/src/cmp.rs

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

28+
mod pattern;
2829
mod bytewise;
2930
pub(crate) use bytewise::BytewiseEq;
3031

library/core/src/cmp/pattern.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#[lang = "MatchLoweredCmp"]
2+
#[const_trait]
3+
trait MatchLoweredCmp<Rhs: ?Sized = Self> {
4+
#[allow(dead_code)]
5+
fn do_match(&self, other: &Rhs) -> bool;
6+
}
7+
8+
impl const MatchLoweredCmp for u8 {
9+
fn do_match(&self, other: &Self) -> bool {
10+
*self == *other
11+
}
12+
}
13+
14+
// TODO: should this be here considering the possible optimized eq target outputs?
15+
impl const MatchLoweredCmp for [u8] {
16+
fn do_match(&self, other: &Self) -> bool {
17+
if self.len() != other.len() {
18+
return false;
19+
}
20+
21+
let mut i = 0;
22+
23+
while i < self.len() {
24+
if self[i] != other[i] {
25+
return false;
26+
}
27+
28+
i += 1;
29+
}
30+
31+
true
32+
}
33+
}
34+
35+
//impl<const N:usize> const MatchLoweredCmp for [u8; N] {
36+
// fn do_match(&self, other: &Self) -> bool {
37+
// let mut i = 0;
38+
//
39+
// while i < N {
40+
// if self[i] != other[i] {
41+
// return false;
42+
// }
43+
// i += 1;
44+
// }
45+
//
46+
// true
47+
// }
48+
//}
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

tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error: const function that might be (indirectly) exposed to stable cannot use `#
2222
LL | const fn bar2() -> u32 { foo2() }
2323
| ^^^^^^
2424
|
25-
= help: mark the callee as `#[rustc_const_stable_indirect]` if it does not itself require any unstable features
25+
= help: mark the callee as `#` if it does not itself require any unstable features
2626
help: if the caller is not (yet) meant to be exposed to stable, add `#[rustc_const_unstable]` (this is what you probably want to do)
2727
|
2828
LL + #[rustc_const_unstable(feature = "...", issue = "...")]

0 commit comments

Comments
 (0)