Skip to content

Commit e26d0a7

Browse files
committed
auto merge of #15078 : jakub-/rust/pattern-matching-refactor-vol-2, r=pnkfelix
I believe there's more commonality to be found there but maybe small steps are better. I'm also in the process of documenting what I can, I will see if I can add it to this PR. It also seems to me that ideally some of this stuff (especially the pattern sanity check) could live as a separate compiler-agnostic module but I understand this may not be the right time (if not the worst) to start the process of modularising rustc.
2 parents 00f9ff2 + 6b6edf4 commit e26d0a7

File tree

4 files changed

+111
-507
lines changed

4 files changed

+111
-507
lines changed

src/doc/rust.md

-2
Original file line numberDiff line numberDiff line change
@@ -2155,8 +2155,6 @@ These are functions:
21552155

21562156
* `str_eq`
21572157
: Compare two strings (`&str`) for equality.
2158-
* `uniq_str_eq`
2159-
: Compare two owned strings (`String`) for equality.
21602158
* `strdup_uniq`
21612159
: Return a new unique string
21622160
containing a copy of the contents of a unique string.

src/librustc/middle/check_match.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ impl fmt::Show for Matrix {
7474
}
7575
}
7676

77-
struct MatchCheckCtxt<'a> {
78-
tcx: &'a ty::ctxt
77+
pub struct MatchCheckCtxt<'a> {
78+
pub tcx: &'a ty::ctxt
7979
}
8080

8181
#[deriving(Clone, PartialEq)]
82-
enum Constructor {
82+
pub enum Constructor {
8383
/// The constructor of all patterns that don't vary by constructor,
8484
/// e.g. struct patterns and fixed-length arrays.
8585
Single,
@@ -492,9 +492,9 @@ fn is_useful_specialized(cx: &MatchCheckCtxt, &Matrix(ref m): &Matrix, v: &[Gc<P
492492
ctor: Constructor, lty: ty::t, witness: WitnessPreference) -> Usefulness {
493493
let arity = constructor_arity(cx, &ctor, lty);
494494
let matrix = Matrix(m.iter().filter_map(|r| {
495-
specialize(cx, r.as_slice(), &ctor, arity)
495+
specialize(cx, r.as_slice(), &ctor, 0u, arity)
496496
}).collect());
497-
match specialize(cx, v, &ctor, arity) {
497+
match specialize(cx, v, &ctor, 0u, arity) {
498498
Some(v) => is_useful(cx, &matrix, v.as_slice(), witness),
499499
None => NotUseful
500500
}
@@ -580,7 +580,7 @@ fn is_wild(cx: &MatchCheckCtxt, p: Gc<Pat>) -> bool {
580580
///
581581
/// For instance, a tuple pattern (_, 42u, Some([])) has the arity of 3.
582582
/// A struct pattern's arity is the number of fields it contains, etc.
583-
fn constructor_arity(cx: &MatchCheckCtxt, ctor: &Constructor, ty: ty::t) -> uint {
583+
pub fn constructor_arity(cx: &MatchCheckCtxt, ctor: &Constructor, ty: ty::t) -> uint {
584584
match ty::get(ty).sty {
585585
ty::ty_tup(ref fs) => fs.len(),
586586
ty::ty_box(_) | ty::ty_uniq(_) => 1u,
@@ -628,11 +628,11 @@ fn range_covered_by_constructor(ctor: &Constructor,
628628
/// different patterns.
629629
/// Structure patterns with a partial wild pattern (Foo { a: 42, .. }) have their missing
630630
/// fields filled with wild patterns.
631-
fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
632-
constructor: &Constructor, arity: uint) -> Option<Vec<Gc<Pat>>> {
631+
pub fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
632+
constructor: &Constructor, col: uint, arity: uint) -> Option<Vec<Gc<Pat>>> {
633633
let &Pat {
634634
id: pat_id, node: ref node, span: pat_span
635-
} = &(*raw_pat(r[0]));
635+
} = &(*raw_pat(r[col]));
636636
let head: Option<Vec<Gc<Pat>>> = match node {
637637
&PatWild =>
638638
Some(Vec::from_elem(arity, wild())),
@@ -776,7 +776,7 @@ fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
776776
None
777777
}
778778
};
779-
head.map(|head| head.append(r.tail()))
779+
head.map(|head| head.append(r.slice_to(col)).append(r.slice_from(col + 1)))
780780
}
781781

782782
fn default(cx: &MatchCheckCtxt, r: &[Gc<Pat>]) -> Option<Vec<Gc<Pat>>> {

src/librustc/middle/lang_items.rs

-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ lets_do_this! {
248248
OrdTraitLangItem, "ord", ord_trait;
249249

250250
StrEqFnLangItem, "str_eq", str_eq_fn;
251-
UniqStrEqFnLangItem, "uniq_str_eq", uniq_str_eq_fn;
252251

253252
// A number of failure-related lang items. The `fail_` item corresponds to
254253
// divide-by-zero and various failure cases with `match`. The

0 commit comments

Comments
 (0)