Skip to content

Commit

Permalink
enable slice patterns and enable building rustdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Sep 6, 2015
1 parent 99f3bfc commit c8a6618
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 108 deletions.
2 changes: 1 addition & 1 deletion mk/main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ RUSTFLAGS2_$(1) += -Z always-build-mir
endef
$(foreach crate,$(TARGET_CRATES),$(eval $(call ADD_MIR_FLAG,$(crate))))
$(foreach crate,$(RUSTC_CRATES),$(eval $(call ADD_MIR_FLAG,$(crate))))
$(foreach crate,syntax,$(eval $(call ADD_MIR_FLAG,$(crate))))
$(foreach crate,$(HOST_CRATES),$(eval $(call ADD_MIR_FLAG,$(crate))))

# platform-specific auto-configuration
include $(CFG_SRC_DIR)mk/platform.mk
Expand Down
26 changes: 10 additions & 16 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<H:Hair> Builder<H> {
}

pub fn lvalue_into_pattern(&mut self,
block: BasicBlock,
mut block: BasicBlock,
var_extent: H::CodeExtent,
irrefutable_pat: PatternRef<H>,
initializer: &Lvalue<H>)
Expand All @@ -132,7 +132,7 @@ impl<H:Hair> Builder<H> {

// Simplify the candidate. Since the pattern is irrefutable, this should
// always convert all match-pairs into bindings.
self.simplify_candidate(&mut candidate);
unpack!(block = self.simplify_candidate(block, &mut candidate));

if !candidate.match_pairs.is_empty() {
self.hir.span_bug(
Expand Down Expand Up @@ -233,15 +233,7 @@ enum TestKind<H:Hair> {
#[derive(Debug)]
struct Test<H:Hair> {
span: H::Span,

// the kind of test to be performed,
kind: TestKind<H>,

// the outcome we expect,
outcome: usize,

// and the match pairs that will result
match_pairs: Vec<MatchPair<H>>
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -261,7 +253,7 @@ impl<H:Hair> Builder<H> {
// complete, all the match pairs which remain require some
// form of test, whether it be a switch or pattern comparison.
for candidate in &mut candidates {
self.simplify_candidate(candidate);
unpack!(block = self.simplify_candidate(block, candidate));
}

// The candidates are inversely sorted by priority. Check to
Expand Down Expand Up @@ -293,14 +285,16 @@ impl<H:Hair> Builder<H> {
debug!("match_candidates: test={:?} match_pair={:?}", test, match_pair);
let target_blocks = self.perform_test(block, &match_pair.lvalue, &test);

for (outcome, target_block) in target_blocks.into_iter().enumerate() {
for (outcome, mut target_block) in target_blocks.into_iter().enumerate() {
let applicable_candidates: Vec<Candidate<H>> =
candidates.iter()
.filter_map(|candidate| {
self.candidate_under_assumption(&match_pair.lvalue,
&test.kind,
outcome,
candidate)
unpack!(target_block =
self.candidate_under_assumption(target_block,
&match_pair.lvalue,
&test.kind,
outcome,
candidate))
})
.collect();
self.match_candidates(span, var_extent, applicable_candidates, target_block);
Expand Down
44 changes: 24 additions & 20 deletions src/librustc_mir/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! sort of test: for example, testing which variant an enum is, or
//! testing a value against a constant.

use build::Builder;
use build::{BlockAnd, Builder};
use build::matches::{Binding, MatchPair, Candidate};
use hair::*;
use repr::*;
Expand All @@ -31,20 +31,25 @@ use std::mem;

impl<H:Hair> Builder<H> {
pub fn simplify_candidate(&mut self,
mut block: BasicBlock,
candidate: &mut Candidate<H>)
-> BlockAnd<()>
{
// repeatedly simplify match pairs until fixed point is reached
loop {
let match_pairs = mem::replace(&mut candidate.match_pairs, vec![]);
let mut progress = match_pairs.len(); // count how many were simplified
for match_pair in match_pairs {
if let Err(match_pair) = self.simplify_match_pair(match_pair, candidate) {
candidate.match_pairs.push(match_pair);
progress -= 1; // this one was not simplified
match self.simplify_match_pair(block, match_pair, candidate) {
Ok(b) => { block = b; }
Err(match_pair) => {
candidate.match_pairs.push(match_pair);
progress -= 1; // this one was not simplified
}
}
}
if progress == 0 {
return; // if we were not able to simplify any, done.
return block.unit(); // if we were not able to simplify any, done.
}
}
}
Expand All @@ -54,14 +59,15 @@ impl<H:Hair> Builder<H> {
/// have been pushed into the candidate. On failure (if false is
/// returned), no changes are made to candidate.
fn simplify_match_pair(&mut self,
mut block: BasicBlock,
match_pair: MatchPair<H>,
candidate: &mut Candidate<H>)
-> Result<(), MatchPair<H>> // returns Err() if cannot simplify
-> Result<BasicBlock, MatchPair<H>> // returns Err() if cannot simplify
{
match match_pair.pattern.kind {
PatternKind::Wild(..) => {
// nothing left to do
Ok(())
Ok(block)
}

PatternKind::Binding { name, mutability, mode, var, ty, subpattern } => {
Expand All @@ -81,24 +87,22 @@ impl<H:Hair> Builder<H> {
candidate.match_pairs.push(MatchPair::new(match_pair.lvalue, subpattern));
}

Ok(())
Ok(block)
}

PatternKind::Constant { .. } => {
// FIXME normalize patterns when possible
Err(match_pair)
}

PatternKind::Array { prefix, slice: None, suffix } => {
self.append_prefix_suffix_pairs(
&mut candidate.match_pairs, match_pair.lvalue.clone(), prefix, suffix);
Ok(())
}

PatternKind::Array { prefix: _, slice: Some(_), suffix: _ } => {
self.hir.span_bug(
match_pair.pattern.span,
&format!("slice patterns not implemented in MIR"));
PatternKind::Array { prefix, slice, suffix } => {
unpack!(block = self.prefix_suffix_slice(&mut candidate.match_pairs,
block,
match_pair.lvalue.clone(),
prefix,
slice,
suffix));
Ok(block)
}

PatternKind::Slice { .. } |
Expand All @@ -112,14 +116,14 @@ impl<H:Hair> Builder<H> {
// tuple struct, match subpats (if any)
candidate.match_pairs.extend(
self.field_match_pairs(match_pair.lvalue, subpatterns));
Ok(())
Ok(block)
}

PatternKind::Deref { subpattern } => {
let lvalue = match_pair.lvalue.deref();
let subpattern = self.hir.mirror(subpattern);
candidate.match_pairs.push(MatchPair::new(lvalue, subpattern));
Ok(())
Ok(block)
}
}
}
Expand Down
Loading

0 comments on commit c8a6618

Please sign in to comment.