Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RFC495 semantics for slice patterns #32202

Merged
merged 15 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix damage in librustc
  • Loading branch information
arielb1 committed Jun 8, 2016
commit b2100cc7b5b64b058d712a380ebf9630e4579928
20 changes: 11 additions & 9 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use session::Session;
use traits;
use ty::{self, Ty, TyCtxt, TypeFoldable};

use util::common::slice_pat;

use syntax::ast::{FloatTy, IntTy, UintTy};
use syntax::attr;
use syntax::codemap::DUMMY_SP;
Expand Down Expand Up @@ -98,17 +100,17 @@ impl TargetDataLayout {

let mut dl = TargetDataLayout::default();
for spec in sess.target.target.data_layout.split("-") {
match &spec.split(":").collect::<Vec<_>>()[..] {
["e"] => dl.endian = Endian::Little,
["E"] => dl.endian = Endian::Big,
["a", a..] => dl.aggregate_align = align(a, "a"),
["f32", a..] => dl.f32_align = align(a, "f32"),
["f64", a..] => dl.f64_align = align(a, "f64"),
[p @ "p", s, a..] | [p @ "p0", s, a..] => {
match slice_pat(&&spec.split(":").collect::<Vec<_>>()[..]) {
&["e"] => dl.endian = Endian::Little,
&["E"] => dl.endian = Endian::Big,
&["a", ref a..] => dl.aggregate_align = align(a, "a"),
&["f32", ref a..] => dl.f32_align = align(a, "f32"),
&["f64", ref a..] => dl.f64_align = align(a, "f64"),
&[p @ "p", s, ref a..] | &[p @ "p0", s, ref a..] => {
dl.pointer_size = size(s, p);
dl.pointer_align = align(a, p);
}
[s, a..] if s.starts_with("i") => {
&[s, ref a..] if s.starts_with("i") => {
let ty_align = match s[1..].parse::<u64>() {
Ok(1) => &mut dl.i8_align,
Ok(8) => &mut dl.i8_align,
Expand All @@ -123,7 +125,7 @@ impl TargetDataLayout {
};
*ty_align = align(a, s);
}
[s, a..] if s.starts_with("v") => {
&[s, ref a..] if s.starts_with("v") => {
let v_size = size(&s[1..], "v");
let a = align(a, s);
if let Some(v) = dl.vector_align.iter_mut().find(|v| v.0 == v_size) {
Expand Down
12 changes: 12 additions & 0 deletions src/librustc/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,15 @@ pub fn path2cstr(p: &Path) -> CString {
pub fn path2cstr(p: &Path) -> CString {
CString::new(p.to_str().unwrap()).unwrap()
}

// FIXME(stage0): remove this
// HACK: this is needed because the interpretation of slice
// patterns changed between stage0 and now.
#[cfg(stage0)]
pub fn slice_pat<'a, 'b, T>(t: &'a &'b [T]) -> &'a &'b [T] {
t
}
#[cfg(not(stage0))]
pub fn slice_pat<'a, 'b, T>(t: &'a &'b [T]) -> &'b [T] {
*t
}
5 changes: 3 additions & 2 deletions src/librustc_const_eval/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use rustc::hir::fold::{Folder, noop_fold_pat};
use rustc::hir::print::pat_to_string;
use syntax::ptr::P;
use rustc::util::nodemap::FnvHashMap;
use rustc::util::common::slice_pat;

pub const DUMMY_WILD_PAT: &'static Pat = &Pat {
id: DUMMY_NODE_ID,
Expand Down Expand Up @@ -401,8 +402,8 @@ fn check_exhaustive<'a, 'tcx>(cx: &MatchCheckCtxt<'a, 'tcx>,
hir::MatchSource::ForLoopDesugar => {
// `witnesses[0]` has the form `Some(<head>)`, peel off the `Some`
let witness = match witnesses[0].node {
PatKind::TupleStruct(_, ref pats, _) => match &pats[..] {
[ref pat] => &**pat,
PatKind::TupleStruct(_, ref pats, _) => match slice_pat(&&pats[..]) {
&[ref pat] => &**pat,
_ => bug!(),
},
_ => bug!(),
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc::ty::{self, Ty, TyCtxt};
use middle::const_val::ConstVal;
use rustc_const_eval::eval_const_expr_partial;
use rustc_const_eval::EvalHint::ExprTypeChecked;
use util::common::slice_pat;
use util::nodemap::{FnvHashSet};
use lint::{LateContext, LintContext, LintArray};
use lint::{LintPass, LateLintPass};
Expand Down Expand Up @@ -459,8 +460,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
// Check for a repr() attribute to specify the size of the
// discriminant.
let repr_hints = cx.lookup_repr_hints(def.did);
match &**repr_hints {
[] => {
match slice_pat(&&**repr_hints) {
&[] => {
// Special-case types like `Option<extern fn()>`.
if !is_repr_nullable_ptr(cx, def, substs) {
return FfiUnsafe(
Expand All @@ -470,7 +471,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
the type")
}
}
[ref hint] => {
&[ref hint] => {
if !hint.is_ffi_safe() {
// FIXME: This shouldn't be reachable: we should check
// this earlier.
Expand Down
24 changes: 14 additions & 10 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,17 +432,18 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// apply to. The classical example involves wildcards:
///
/// ```rust,ignore
/// match (x, y) {
/// (true, _) => true, // (0)
/// (_, true) => true, // (1)
/// (false, false) => false // (2)
/// match (x, y, z) {
/// (true, _, true) => true, // (0)
/// (_, true, _) => true, // (1)
/// (false, false, _) => false, // (2)
/// (true, _, false) => false, // (3)
/// }
/// ```
///
/// In that case, after we test on `x`, there are 2 overlapping candidate
/// sets:
///
/// - If the outcome is that `x` is true, candidates 0 and 2
/// - If the outcome is that `x` is true, candidates 0, 1, and 3
/// - If the outcome is that `x` is false, candidates 1 and 2
///
/// Here, the traditional "decision tree" method would generate 2
Expand Down Expand Up @@ -481,11 +482,14 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// ```
///
/// Here we first test the match-pair `x @ "foo"`, which is an `Eq` test.
/// It might seem that we would end up with 2 disjoint candidate sets,
/// consisting of the first candidate or the other 3, but our algorithm
/// doesn't reason about "foo" being distinct from the other constants,
/// it considers to latter arms to potentially match after both outcomes,
/// which obviously leads to an exponential amount of tests.
///
/// It might seem that we would end up with 2 disjoint candidate
/// sets, consisting of the first candidate or the other 3, but our
/// algorithm doesn't reason about "foo" being distinct from the other
/// constants; it considers the latter arms to potentially match after
/// both outcomes, which obviously leads to an exponential amount
/// of tests.
///
/// To avoid these kinds of problems, our algorithm tries to ensure
/// the amount of generated tests is linear. When we do a k-way test,
/// we return an additional "unmatched" set alongside the obvious `k`
Expand Down
9 changes: 5 additions & 4 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use rustc::hir::def_id::DefId;
use rustc::hir::print as pprust;
use rustc::ty::{self, TyCtxt};
use rustc::ty::subst;
use rustc::util::common::slice_pat;

use rustc_const_eval::lookup_const_by_id;

Expand Down Expand Up @@ -197,10 +198,10 @@ fn build_struct<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
let variant = tcx.lookup_adt_def(did).struct_variant();

clean::Struct {
struct_type: match &*variant.fields {
[] => doctree::Unit,
[_] if variant.kind == ty::VariantKind::Tuple => doctree::Newtype,
[..] if variant.kind == ty::VariantKind::Tuple => doctree::Tuple,
struct_type: match slice_pat(&&*variant.fields) {
&[] => doctree::Unit,
&[_] if variant.kind == ty::VariantKind::Tuple => doctree::Newtype,
&[..] if variant.kind == ty::VariantKind::Tuple => doctree::Tuple,
_ => doctree::Plain,
},
generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),
Expand Down
7 changes: 4 additions & 3 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::iter::repeat;

use rustc::middle::cstore::LOCAL_CRATE;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
use rustc::util::common::slice_pat;
use syntax::abi::Abi;
use rustc::hir;

Expand Down Expand Up @@ -474,9 +475,9 @@ impl fmt::Display for clean::Type {
decl.decl)
}
clean::Tuple(ref typs) => {
match &**typs {
[] => primitive_link(f, clean::PrimitiveTuple, "()"),
[ref one] => {
match slice_pat(&&**typs) {
&[] => primitive_link(f, clean::PrimitiveTuple, "()"),
&[ref one] => {
primitive_link(f, clean::PrimitiveTuple, "(")?;
write!(f, "{},", one)?;
primitive_link(f, clean::PrimitiveTuple, ")")
Expand Down