Skip to content

Commit 0877a89

Browse files
committed
rename and disallow matching str in const
1 parent 47e8946 commit 0877a89

File tree

6 files changed

+34
-29
lines changed

6 files changed

+34
-29
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ 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) {
705+
if tcx.is_lang_item(trait_did, LangItem::PatternConstEq) {
706706
return;
707707
}
708708

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ language_item_table! {
419419
CStr, sym::CStr, c_str, Target::Struct, GenericRequirement::None;
420420

421421
// FIXME(xacrimon): Used for lowering of match/if let statements. will made obsolete by const PartialEq.
422-
MatchLoweredCmp, sym::MatchLoweredCmp, match_lowered_cmp, Target::Trait, GenericRequirement::None;
422+
PatternConstEq, sym::PatternConstEq, pattern_const_eq, Target::Trait, GenericRequirement::None;
423423
}
424424

425425
pub enum GenericRequirement {

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ macro_rules! arena_types {
114114
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
115115
[] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls,
116116
[] hir_owner_nodes: rustc_hir::OwnerNodes<'tcx>,
117-
[] pats2: rustc_middle::thir::Pat<'tcx>,
117+
[] thir_pat: rustc_middle::thir::Pat<'tcx>,
118118
]);
119119
)
120120
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_middle::{bug, span_bug};
1717
use rustc_span::def_id::DefId;
1818
use rustc_span::source_map::Spanned;
1919
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
20+
use rustc_trait_selection::infer::InferCtxtExt;
2021
use tracing::{debug, instrument};
2122

2223
use crate::builder::Builder;
@@ -362,7 +363,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
362363
);
363364
}
364365

365-
/// Compare two values using `<T as std::compare::pattern::MatchLoweredCmp>::do_match`.
366+
/// Compare two values using `<T as std::compare::pattern::PatternConstEq>::eq`.
366367
/// If the values are already references, just call it directly, otherwise
367368
/// take a reference to the values first and then call it.
368369
fn non_scalar_compare(
@@ -456,19 +457,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
456457
// The only types that can end up here are str and ScalarInt slices,
457458
// which have their comparison defined in `core`.
458459
// (Interestingly this means that exhaustiveness analysis relies, for soundness,
459-
// on the `MatchLoweredCmp` impls for `str` and `[T]` to be correct!)
460+
// on the `PatternConstEq` and `PartialEq` impls for `str` and `[T]` to be correct!)
460461
let compare_ty = match *ty.kind() {
461462
ty::Ref(_, deref_ty, _) if deref_ty == self.tcx.types.str_ || deref_ty.is_slice() => {
462463
deref_ty
463464
}
464465
_ => span_bug!(source_info.span, "invalid type for non-scalar compare: {}", ty),
465466
};
466467

467-
let cmp_trait_def_id =
468-
self.tcx.require_lang_item(LangItem::MatchLoweredCmp, Some(source_info.span));
469-
let method =
470-
trait_method(self.tcx, cmp_trait_def_id, sym::do_match, [compare_ty, compare_ty]);
468+
let const_cmp_trait_def =
469+
self.tcx.require_lang_item(LangItem::PatternConstEq, Some(source_info.span));
470+
let fallback_cmp_trait_def =
471+
self.tcx.require_lang_item(LangItem::PartialEq, Some(source_info.span));
472+
473+
let cmp_trait_def = if self
474+
.infcx
475+
.type_implements_trait(const_cmp_trait_def, [compare_ty, compare_ty], self.param_env)
476+
.must_apply_modulo_regions()
477+
{
478+
const_cmp_trait_def
479+
} else {
480+
fallback_cmp_trait_def
481+
};
471482

483+
let method = trait_method(self.tcx, cmp_trait_def, sym::eq, [compare_ty, compare_ty]);
472484
let bool_ty = self.tcx.types.bool;
473485
let eq_result = self.temp(bool_ty, source_info.span);
474486
let eq_block = self.cfg.start_new_block();

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ symbols! {
267267
LintDiagnostic,
268268
LintPass,
269269
LocalKey,
270-
MatchLoweredCmp,
271270
Mutex,
272271
MutexGuard,
273272
N,
@@ -287,6 +286,7 @@ symbols! {
287286
PartialOrd,
288287
Path,
289288
PathBuf,
289+
PatternConstEq,
290290
Pending,
291291
PinCoerceUnsized,
292292
Pointer,
@@ -783,7 +783,6 @@ symbols! {
783783
div,
784784
div_assign,
785785
diverging_block_default,
786-
do_match,
787786
do_not_recommend,
788787
doc,
789788
doc_alias,

library/core/src/cmp/pattern.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ use crate::cmp::BytewiseEq;
22
use crate::intrinsics::compare_bytes;
33
use crate::mem;
44

5-
#[lang = "MatchLoweredCmp"]
5+
#[lang = "PatternConstEq"]
66
#[const_trait]
7-
trait MatchLoweredCmp<Rhs = Self>
7+
trait PatternConstEq<Rhs = Self>
88
where
99
Rhs: ?Sized,
1010
{
1111
#[allow(dead_code)]
12-
fn do_match(&self, other: &Rhs) -> bool;
12+
fn eq(&self, other: &Rhs) -> bool;
1313
}
1414

1515
macro_rules ! impl_for_primitive {
1616
($($t:ty),*) => {
1717
$(
18-
impl const MatchLoweredCmp for $t {
19-
fn do_match(&self, other: &Self) -> bool {
18+
impl const PatternConstEq for $t {
19+
fn eq(&self, other: &Self) -> bool {
2020
*self == *other
2121
}
2222
}
@@ -31,25 +31,19 @@ impl_for_primitive! {
3131
f32, f64
3232
}
3333

34-
impl const MatchLoweredCmp for str {
35-
fn do_match(&self, other: &Self) -> bool {
36-
<[u8] as MatchLoweredCmp>::do_match((*self).as_bytes(), (*other).as_bytes())
37-
}
38-
}
39-
40-
impl<T> const MatchLoweredCmp for [T]
34+
impl<T> const PatternConstEq for [T]
4135
where
42-
T: ~const MatchLoweredCmp,
36+
T: ~const PatternConstEq,
4337
{
44-
default fn do_match(&self, other: &Self) -> bool {
38+
default fn eq(&self, other: &Self) -> bool {
4539
if self.len() != other.len() {
4640
return false;
4741
}
4842

4943
let mut i = 0;
5044

5145
while i < self.len() {
52-
if <T as MatchLoweredCmp>::do_match(&self[i], &other[i]) == false {
46+
if <T as PatternConstEq>::eq(&self[i], &other[i]) == false {
5347
return false;
5448
}
5549

@@ -61,11 +55,11 @@ where
6155
}
6256

6357
#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
64-
impl<T> const MatchLoweredCmp for [T]
58+
impl<T> const PatternConstEq for [T]
6559
where
66-
T: ~const MatchLoweredCmp + BytewiseEq<T>,
60+
T: ~const PatternConstEq + BytewiseEq<T>,
6761
{
68-
fn do_match(&self, other: &Self) -> bool {
62+
fn eq(&self, other: &Self) -> bool {
6963
if self.len() != other.len() {
7064
return false;
7165
}

0 commit comments

Comments
 (0)