about summary refs log tree commit diff
path: root/src/librustc_mir
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2019-11-05 16:30:04 +0000
committerNadrieril <nadrieril+git@gmail.com>2019-11-05 18:32:26 +0000
commitf774eb69fd19cabaa17dfe91dd0a1b23a2dce842 (patch)
treec0e53808a54d03c9ba000c349522eed878075ee2 /src/librustc_mir
parentd582ed5684de1951774b637ccfe8b4cf8bd2008b (diff)
downloadrust-f774eb69fd19cabaa17dfe91dd0a1b23a2dce842.tar.gz
rust-f774eb69fd19cabaa17dfe91dd0a1b23a2dce842.zip
Use VarLenSlice consistently when splitting constructors
The previous behaviour ignored slice lengths above a certain length
because it could not do otherwise. We now have VarLenSlice however, that
can represent the ignored lengths to make the algorithm more consistent.
This does not change the correctness of the algorithm, but makes it
easier to reason about.
As a nice side-effect, exhaustiveness errors have improved: they now
capture all missing lengths instead of only the shortest.
Diffstat (limited to 'src/librustc_mir')
-rw-r--r--src/librustc_mir/hair/pattern/_match.rs23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs
index 15f88334e8d..8d7e605f7c9 100644
--- a/src/librustc_mir/hair/pattern/_match.rs
+++ b/src/librustc_mir/hair/pattern/_match.rs
@@ -2042,7 +2042,7 @@ fn split_grouped_constructors<'p, 'tcx>(
                 //
                 // Of course, if fixed-length patterns exist, we must be sure
                 // that our length is large enough to miss them all, so
-                // we can pick `L = max(FIXED_LEN+1 ∪ {max(PREFIX_LEN) + max(SUFFIX_LEN)})`
+                // we can pick `L = max(max(FIXED_LEN)+1, max(PREFIX_LEN) + max(SUFFIX_LEN))`
                 //
                 // for example, with the above pair of patterns, all elements
                 // but the first and last can be added/removed, so any
@@ -2080,9 +2080,24 @@ fn split_grouped_constructors<'p, 'tcx>(
                     }
                 }
 
-                let max_slice_length = cmp::max(max_fixed_len + 1, max_prefix_len + max_suffix_len);
-                split_ctors
-                    .extend((self_prefix + self_suffix..=max_slice_length).map(FixedLenSlice))
+                // For diagnostics, we keep the prefix and suffix lengths separate, so in the case
+                // where `max_fixed_len+1` is the largest, we adapt `max_prefix_len` accordingly,
+                // so that `L = max_prefix_len + max_suffix_len`.
+                if max_fixed_len + 1 >= max_prefix_len + max_suffix_len {
+                    // The subtraction can't overflow thanks to the above check.
+                    // The new `max_prefix_len` is also guaranteed to be larger than its previous
+                    // value.
+                    max_prefix_len = max_fixed_len + 1 - max_suffix_len;
+                }
+
+                // `ctor` originally covered the range `(self_prefix + self_suffix..infinity)`. We
+                // now split it into two: lengths smaller than `max_prefix_len + max_suffix_len`
+                // are treated independently as fixed-lengths slices, and lengths above are
+                // captured by a final VarLenSlice constructor.
+                split_ctors.extend(
+                    (self_prefix + self_suffix..max_prefix_len + max_suffix_len).map(FixedLenSlice),
+                );
+                split_ctors.push(VarLenSlice(max_prefix_len, max_suffix_len));
             }
             // Any other constructor can be used unchanged.
             _ => split_ctors.push(ctor),