From 46658b96a3d571e9ef88f0e15b900205b26c0e5b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 31 Dec 2014 21:36:03 -0500 Subject: [PATCH 1/6] typeck: Index[Mut] traits now have *one* input parameter (not two) --- src/librustc_typeck/check/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 19ec85dc61eae..beea3fd4ef2c5 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2524,7 +2524,6 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, } let input_ty = fcx.infcx().next_ty_var(); - let return_ty = fcx.infcx().next_ty_var(); // Try `IndexMut` first, if preferred. let method = match (lvalue_pref, fcx.tcx().lang_items.index_mut_trait()) { @@ -2536,7 +2535,7 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, trait_did, adjustment.clone(), adjusted_ty, - Some(vec![input_ty, return_ty])) + Some(vec![input_ty])) } _ => None, }; @@ -2551,7 +2550,7 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, trait_did, adjustment, adjusted_ty, - Some(vec![input_ty, return_ty])) + Some(vec![input_ty])) } (method, _) => method, }; @@ -2559,9 +2558,9 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, // If some lookup succeeds, write callee into table and extract index/element // type from the method signature. // If some lookup succeeded, install method in table - method.map(|method| { - make_overloaded_lvalue_return_type(fcx, Some(method_call), Some(method)); - (input_ty, return_ty) + method.and_then(|method| { + make_overloaded_lvalue_return_type(fcx, Some(method_call), Some(method)). + map(|ret| (input_ty, ret.ty)) }) } From 862b2d4eca76bb2cb1924855bd0aec7eea1ede99 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 3 Jan 2015 09:46:29 -0500 Subject: [PATCH 2/6] core: use assoc types in `Index[Mut]` --- src/libcore/ops.rs | 44 ++++++++++++++++++++++++++++++++++++++------ src/libcore/slice.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index b51d4d91c2f55..9ba1a5a898034 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -611,6 +611,15 @@ macro_rules! shr_impl { shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 } +// NOTE(stage0) remove trait after a snapshot +#[cfg(stage0)] +#[allow(missing_docs)] +#[lang="index"] +pub trait Index for Sized? { + /// The method for the indexing (`Foo[Bar]`) operation + fn index<'a>(&'a self, index: &Index) -> &'a Result; +} + /// The `Index` trait is used to specify the functionality of indexing operations /// like `arr[idx]` when used in an immutable context. /// @@ -620,12 +629,16 @@ shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// calling `index`, and therefore, `main` prints `Indexing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::Index; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Index for Foo { +/// impl Index for Foo { +/// type Output = Foo; +/// /// fn index<'a>(&'a self, _index: &Foo) -> &'a Foo { /// println!("Indexing!"); /// self @@ -636,10 +649,22 @@ shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// Foo[Foo]; /// } /// ``` +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot #[lang="index"] -pub trait Index for Sized? { +pub trait Index for Sized? { + type Sized? Output; + /// The method for the indexing (`Foo[Bar]`) operation - fn index<'a>(&'a self, index: &Index) -> &'a Result; + fn index<'a>(&'a self, index: &Index) -> &'a Self::Output; +} + +// NOTE(stage0) remove trait after a snapshot +#[cfg(stage0)] +#[allow(missing_docs)] +#[lang="index_mut"] +pub trait IndexMut for Sized? { + /// The method for the indexing (`Foo[Bar]`) operation + fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result; } /// The `IndexMut` trait is used to specify the functionality of indexing @@ -651,12 +676,16 @@ pub trait Index for Sized? { /// calling `index_mut`, and therefore, `main` prints `Indexing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::IndexMut; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl IndexMut for Foo { +/// impl IndexMut for Foo { +/// type Output = Foo; +/// /// fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo { /// println!("Indexing!"); /// self @@ -667,10 +696,13 @@ pub trait Index for Sized? { /// &mut Foo[Foo]; /// } /// ``` +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot #[lang="index_mut"] -pub trait IndexMut for Sized? { +pub trait IndexMut for Sized? { + type Sized? Output; + /// The method for the indexing (`Foo[Bar]`) operation - fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result; + fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output; } /// The `Slice` trait is used to specify the functionality of slicing operations diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 07addf7a56969..c42301eb569d5 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -531,6 +531,8 @@ impl SliceExt for [T] { } } +// NOTE(stage0) remove impl after a snapshot +#[cfg(stage0)] impl ops::Index for [T] { fn index(&self, &index: &uint) -> &T { assert!(index < self.len()); @@ -539,6 +541,19 @@ impl ops::Index for [T] { } } +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot +impl ops::Index for [T] { + type Output = T; + + fn index(&self, &index: &uint) -> &T { + assert!(index < self.len()); + + unsafe { mem::transmute(self.repr().data.offset(index as int)) } + } +} + +// NOTE(stage0) remove impl after a snapshot +#[cfg(stage0)] impl ops::IndexMut for [T] { fn index_mut(&mut self, &index: &uint) -> &mut T { assert!(index < self.len()); @@ -547,6 +562,17 @@ impl ops::IndexMut for [T] { } } +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot +impl ops::IndexMut for [T] { + type Output = T; + + fn index_mut(&mut self, &index: &uint) -> &mut T { + assert!(index < self.len()); + + unsafe { mem::transmute(self.repr().data.offset(index as int)) } + } +} + impl ops::Slice for [T] { #[inline] fn as_slice_<'a>(&'a self) -> &'a [T] { From 706269f6a8e32d82dc3f313be7684f4d06a95303 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 3 Jan 2015 10:40:10 -0500 Subject: [PATCH 3/6] collections: fix fallout --- src/libcollections/bit.rs | 17 +++++++++++++++++ src/libcollections/btree/map.rs | 28 ++++++++++++++++++++++++++++ src/libcollections/ring_buf.rs | 26 ++++++++++++++++++++++++++ src/libcollections/vec.rs | 25 +++++++++++++++++++++++++ src/libcollections/vec_map.rs | 25 +++++++++++++++++++++++++ 5 files changed, 121 insertions(+) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index a5b8c5f3e5718..c48873285ae91 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -164,6 +164,8 @@ pub struct Bitv { nbits: uint } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] // FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing) impl Index for Bitv { #[inline] @@ -176,6 +178,21 @@ impl Index for Bitv { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +// FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing) +impl Index for Bitv { + type Output = bool; + + #[inline] + fn index(&self, i: &uint) -> &bool { + if self.get(*i).expect("index out of bounds") { + &TRUE + } else { + &FALSE + } + } +} + /// Computes how many blocks are needed to store that many bits fn blocks_for_bits(bits: uint) -> uint { // If we want 17 bits, dividing by 32 will produce 0. So we add 1 to make sure we diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index da98c19e888fa..b7aaa39c3af3b 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -898,6 +898,8 @@ impl Show for BTreeMap { } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl Index for BTreeMap where Q: BorrowFrom + Ord @@ -907,6 +909,20 @@ impl Index for BTreeMap } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +#[stable] +impl Index for BTreeMap + where Q: BorrowFrom + Ord +{ + type Output = V; + + fn index(&self, key: &Q) -> &V { + self.get(key).expect("no entry found for key") + } +} + +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl IndexMut for BTreeMap where Q: BorrowFrom + Ord @@ -916,6 +932,18 @@ impl IndexMut for BTreeMap } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +#[stable] +impl IndexMut for BTreeMap + where Q: BorrowFrom + Ord +{ + type Output = V; + + fn index_mut(&mut self, key: &Q) -> &mut V { + self.get_mut(key).expect("no entry found for key") + } +} + /// Genericises over how to get the correct type of iterator from the correct type /// of Node ownership. trait Traverse { diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index e4c9e51a8455b..2045bbeac4cf2 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -1364,6 +1364,8 @@ impl> Hash for RingBuf { } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl Index for RingBuf { #[inline] @@ -1372,6 +1374,19 @@ impl Index for RingBuf { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +#[stable] +impl Index for RingBuf { + type Output = A; + + #[inline] + fn index<'a>(&'a self, i: &uint) -> &'a A { + self.get(*i).expect("Out of bounds access") + } +} + +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl IndexMut for RingBuf { #[inline] @@ -1380,6 +1395,17 @@ impl IndexMut for RingBuf { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +#[stable] +impl IndexMut for RingBuf { + type Output = A; + + #[inline] + fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut A { + self.get_mut(*i).expect("Out of bounds access") + } +} + #[stable] impl FromIterator for RingBuf { fn from_iter>(iterator: T) -> RingBuf { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 7e36792742171..a06cc9a564cc5 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1245,6 +1245,8 @@ impl> Hash for Vec { } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[experimental = "waiting on Index stability"] impl Index for Vec { #[inline] @@ -1253,6 +1255,19 @@ impl Index for Vec { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +#[experimental = "waiting on Index stability"] +impl Index for Vec { + type Output = T; + + #[inline] + fn index<'a>(&'a self, index: &uint) -> &'a T { + &self.as_slice()[*index] + } +} + +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] impl IndexMut for Vec { #[inline] fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T { @@ -1260,6 +1275,16 @@ impl IndexMut for Vec { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl IndexMut for Vec { + type Output = T; + + #[inline] + fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T { + &mut self.as_mut_slice()[*index] + } +} + impl ops::Slice for Vec { #[inline] fn as_slice_<'a>(&'a self) -> &'a [T] { diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 172fd56ed3962..0743eb5709238 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -562,6 +562,8 @@ impl Extend<(uint, V)> for VecMap { } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl Index for VecMap { #[inline] @@ -570,6 +572,18 @@ impl Index for VecMap { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl Index for VecMap { + type Output = V; + + #[inline] + fn index<'a>(&'a self, i: &uint) -> &'a V { + self.get(i).expect("key not present") + } +} + +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl IndexMut for VecMap { #[inline] @@ -578,6 +592,17 @@ impl IndexMut for VecMap { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +#[stable] +impl IndexMut for VecMap { + type Output = V; + + #[inline] + fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V { + self.get_mut(i).expect("key not present") + } +} + macro_rules! iterator { (impl $name:ident -> $elem:ty, $($getter:ident),+) => { #[stable] From d5a6ee3723c88ef5d83cdf6fa2d43893704d8442 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 3 Jan 2015 10:40:19 -0500 Subject: [PATCH 4/6] std: fix fallout --- src/libstd/collections/hash/map.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index d4fc4150fae91..ba30fccaf598b 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1300,6 +1300,8 @@ impl, V, S, H: Hasher + Default> Default for HashMap } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl + Eq, Sized? Q, V, S, H: Hasher> Index for HashMap where Q: BorrowFrom + Hash + Eq @@ -1310,6 +1312,21 @@ impl + Eq, Sized? Q, V, S, H: Hasher> Index for HashMap + Eq, Sized? Q, V, S, H: Hasher> Index for HashMap + where Q: BorrowFrom + Hash + Eq +{ + type Output = V; + + #[inline] + fn index<'a>(&'a self, index: &Q) -> &'a V { + self.get(index).expect("no entry found for key") + } +} + +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] #[stable] impl + Eq, Sized? Q, V, S, H: Hasher> IndexMut for HashMap where Q: BorrowFrom + Hash + Eq @@ -1320,6 +1337,19 @@ impl + Eq, Sized? Q, V, S, H: Hasher> IndexMut for HashMap + Eq, Sized? Q, V, S, H: Hasher> IndexMut for HashMap + where Q: BorrowFrom + Hash + Eq +{ + type Output = V; + + #[inline] + fn index_mut<'a>(&'a mut self, index: &Q) -> &'a mut V { + self.get_mut(index).expect("no entry found for key") + } +} + /// HashMap iterator #[stable] pub struct Iter<'a, K: 'a, V: 'a> { From 647657f31ad2e321d696e108c02e77803f02b0a8 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 3 Jan 2015 10:40:26 -0500 Subject: [PATCH 5/6] serialize: fix fallout --- src/libserialize/json.rs | 25 +++++++++++++++++++++++++ src/libserialize/lib.rs | 1 + 2 files changed, 26 insertions(+) diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index a87044bb3b3ba..c85de54953dc8 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -1123,12 +1123,25 @@ impl Json { } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] impl<'a> ops::Index<&'a str, Json> for Json { fn index(&self, idx: & &str) -> &Json { self.find(*idx).unwrap() } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl<'a> ops::Index<&'a str> for Json { + type Output = Json; + + fn index(&self, idx: & &str) -> &Json { + self.find(*idx).unwrap() + } +} + +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] impl ops::Index for Json { fn index<'a>(&'a self, idx: &uint) -> &'a Json { match self { @@ -1138,6 +1151,18 @@ impl ops::Index for Json { } } +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl ops::Index for Json { + type Output = Json; + + fn index<'a>(&'a self, idx: &uint) -> &'a Json { + match self { + &Json::Array(ref v) => v.index(idx), + _ => panic!("can only index Json with uint if it is an array") + } + } +} + /// The output of the streaming parser. #[deriving(PartialEq, Clone, Show)] pub enum JsonEvent { diff --git a/src/libserialize/lib.rs b/src/libserialize/lib.rs index 4a2bbbeec03a4..1ec6a2af309a7 100644 --- a/src/libserialize/lib.rs +++ b/src/libserialize/lib.rs @@ -25,6 +25,7 @@ Core encoding and decoding interfaces. #![allow(unknown_features)] #![feature(macro_rules, default_type_params, phase, slicing_syntax, globs)] #![feature(unboxed_closures)] +#![feature(associated_types)] // test harness access #[cfg(test)] From 646297e94932c1d898ace45fa065ad325da10635 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 3 Jan 2015 10:40:36 -0500 Subject: [PATCH 6/6] fix rpass/cfail tests --- .../compile-fail/borrowck-overloaded-index-2.rs | 6 +++++- .../borrowck-overloaded-index-autoderef.rs | 10 ++++++++-- .../compile-fail/borrowck-overloaded-index.rs | 14 +++++++++++--- src/test/compile-fail/dst-index.rs | 10 ++++++++-- src/test/run-pass/dst-index.rs | 10 ++++++++-- src/test/run-pass/issue-15734.rs | 15 +++++++++++---- src/test/run-pass/operator-overloading.rs | 5 ++++- src/test/run-pass/overloaded-index-assoc-list.rs | 6 +++++- src/test/run-pass/overloaded-index-autoderef.rs | 10 ++++++++-- src/test/run-pass/overloaded-index-in-field.rs | 6 +++++- src/test/run-pass/overloaded-index.rs | 10 ++++++++-- 11 files changed, 81 insertions(+), 21 deletions(-) diff --git a/src/test/compile-fail/borrowck-overloaded-index-2.rs b/src/test/compile-fail/borrowck-overloaded-index-2.rs index 01afe405d5e12..87e647d16ddf8 100644 --- a/src/test/compile-fail/borrowck-overloaded-index-2.rs +++ b/src/test/compile-fail/borrowck-overloaded-index-2.rs @@ -8,13 +8,17 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(associated_types)] + use std::ops::Index; struct MyVec { data: Vec, } -impl Index for MyVec { +impl Index for MyVec { + type Output = T; + fn index(&self, &i: &uint) -> &T { &self.data[i] } diff --git a/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs b/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs index e8949d4b30bef..e7bd7cdf0b79d 100644 --- a/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs +++ b/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs @@ -11,6 +11,8 @@ // Test that we still see borrowck errors of various kinds when using // indexing and autoderef in combination. +#![feature(associated_types)] + use std::ops::{Index, IndexMut}; struct Foo { @@ -18,7 +20,9 @@ struct Foo { y: int, } -impl Index for Foo { +impl Index for Foo { + type Output = int; + fn index<'a>(&'a self, z: &String) -> &'a int { if z.as_slice() == "x" { &self.x @@ -28,7 +32,9 @@ impl Index for Foo { } } -impl IndexMut for Foo { +impl IndexMut for Foo { + type Output = int; + fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut int { if z.as_slice() == "x" { &mut self.x diff --git a/src/test/compile-fail/borrowck-overloaded-index.rs b/src/test/compile-fail/borrowck-overloaded-index.rs index 933d0f15e4e70..532f32ce770a6 100644 --- a/src/test/compile-fail/borrowck-overloaded-index.rs +++ b/src/test/compile-fail/borrowck-overloaded-index.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(associated_types)] + use std::ops::{Index, IndexMut}; struct Foo { @@ -15,7 +17,9 @@ struct Foo { y: int, } -impl Index for Foo { +impl Index for Foo { + type Output = int; + fn index<'a>(&'a self, z: &String) -> &'a int { if z.as_slice() == "x" { &self.x @@ -25,7 +29,9 @@ impl Index for Foo { } } -impl IndexMut for Foo { +impl IndexMut for Foo { + type Output = int; + fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut int { if z.as_slice() == "x" { &mut self.x @@ -39,7 +45,9 @@ struct Bar { x: int, } -impl Index for Bar { +impl Index for Bar { + type Output = int; + fn index<'a>(&'a self, z: &int) -> &'a int { &self.x } diff --git a/src/test/compile-fail/dst-index.rs b/src/test/compile-fail/dst-index.rs index af97c864dc81a..06d20c3361bc9 100644 --- a/src/test/compile-fail/dst-index.rs +++ b/src/test/compile-fail/dst-index.rs @@ -11,6 +11,8 @@ // Test that overloaded index expressions with DST result types // can't be used as rvalues +#![feature(associated_types)] + use std::ops::Index; use std::fmt::Show; @@ -18,7 +20,9 @@ struct S; impl Copy for S {} -impl Index for S { +impl Index for S { + type Output = str; + fn index<'a>(&'a self, _: &uint) -> &'a str { "hello" } @@ -28,7 +32,9 @@ struct T; impl Copy for T {} -impl Index for T { +impl Index for T { + type Output = Show + 'static; + fn index<'a>(&'a self, idx: &uint) -> &'a (Show + 'static) { static x: uint = 42; &x diff --git a/src/test/run-pass/dst-index.rs b/src/test/run-pass/dst-index.rs index eaf7131e1d878..6a69bfc248f16 100644 --- a/src/test/run-pass/dst-index.rs +++ b/src/test/run-pass/dst-index.rs @@ -11,12 +11,16 @@ // Test that overloaded index expressions with DST result types // work and don't ICE. +#![feature(associated_types)] + use std::ops::Index; use std::fmt::Show; struct S; -impl Index for S { +impl Index for S { + type Output = str; + fn index<'a>(&'a self, _: &uint) -> &'a str { "hello" } @@ -24,7 +28,9 @@ impl Index for S { struct T; -impl Index for T { +impl Index for T { + type Output = Show + 'static; + fn index<'a>(&'a self, idx: &uint) -> &'a (Show + 'static) { static x: uint = 42; &x diff --git a/src/test/run-pass/issue-15734.rs b/src/test/run-pass/issue-15734.rs index e99b1dc5befb0..f261098f53811 100644 --- a/src/test/run-pass/issue-15734.rs +++ b/src/test/run-pass/issue-15734.rs @@ -10,7 +10,8 @@ // If `Index` used an associated type for its output, this test would // work more smoothly. -#![feature(old_orphan_check)] + +#![feature(associated_types, old_orphan_check)] use std::ops::Index; @@ -25,13 +26,17 @@ impl Mat { } } -impl Index<(uint, uint), T> for Mat { +impl Index<(uint, uint)> for Mat { + type Output = T; + fn index<'a>(&'a self, &(row, col): &(uint, uint)) -> &'a T { &self.data[row * self.cols + col] } } -impl<'a, T> Index<(uint, uint), T> for &'a Mat { +impl<'a, T> Index<(uint, uint)> for &'a Mat { + type Output = T; + fn index<'b>(&'b self, index: &(uint, uint)) -> &'b T { (*self).index(index) } @@ -39,7 +44,9 @@ impl<'a, T> Index<(uint, uint), T> for &'a Mat { struct Row { mat: M, row: uint, } -impl> Index for Row { +impl> Index for Row { + type Output = T; + fn index<'a>(&'a self, col: &uint) -> &'a T { &self.mat[(self.row, *col)] } diff --git a/src/test/run-pass/operator-overloading.rs b/src/test/run-pass/operator-overloading.rs index 101b93c1896f1..c0345dbef3a81 100644 --- a/src/test/run-pass/operator-overloading.rs +++ b/src/test/run-pass/operator-overloading.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(associated_types)] use std::cmp; use std::ops; @@ -42,7 +43,9 @@ impl ops::Not for Point { } } -impl ops::Index for Point { +impl ops::Index for Point { + type Output = int; + fn index(&self, x: &bool) -> &int { if *x { &self.x diff --git a/src/test/run-pass/overloaded-index-assoc-list.rs b/src/test/run-pass/overloaded-index-assoc-list.rs index 0347f8a29df28..77bb981cfd9b9 100644 --- a/src/test/run-pass/overloaded-index-assoc-list.rs +++ b/src/test/run-pass/overloaded-index-assoc-list.rs @@ -11,6 +11,8 @@ // Test overloading of the `[]` operator. In particular test that it // takes its argument *by reference*. +#![feature(associated_types)] + use std::ops::Index; struct AssociationList { @@ -28,7 +30,9 @@ impl AssociationList { } } -impl Index for AssociationList { +impl Index for AssociationList { + type Output = V; + fn index<'a>(&'a self, index: &K) -> &'a V { for pair in self.pairs.iter() { if pair.key == *index { diff --git a/src/test/run-pass/overloaded-index-autoderef.rs b/src/test/run-pass/overloaded-index-autoderef.rs index dcb0c40c6088a..d141234287d13 100644 --- a/src/test/run-pass/overloaded-index-autoderef.rs +++ b/src/test/run-pass/overloaded-index-autoderef.rs @@ -10,6 +10,8 @@ // Test overloaded indexing combined with autoderef. +#![feature(associated_types)] + use std::ops::{Index, IndexMut}; struct Foo { @@ -17,7 +19,9 @@ struct Foo { y: int, } -impl Index for Foo { +impl Index for Foo { + type Output = int; + fn index(&self, z: &int) -> &int { if *z == 0 { &self.x @@ -27,7 +31,9 @@ impl Index for Foo { } } -impl IndexMut for Foo { +impl IndexMut for Foo { + type Output = int; + fn index_mut(&mut self, z: &int) -> &mut int { if *z == 0 { &mut self.x diff --git a/src/test/run-pass/overloaded-index-in-field.rs b/src/test/run-pass/overloaded-index-in-field.rs index 1c06ed64fc7b8..9c6afc0912d06 100644 --- a/src/test/run-pass/overloaded-index-in-field.rs +++ b/src/test/run-pass/overloaded-index-in-field.rs @@ -11,6 +11,8 @@ // Test using overloaded indexing when the "map" is stored in a // field. This caused problems at some point. +#![feature(associated_types)] + use std::ops::Index; struct Foo { @@ -22,7 +24,9 @@ struct Bar { foo: Foo } -impl Index for Foo { +impl Index for Foo { + type Output = int; + fn index(&self, z: &int) -> &int { if *z == 0 { &self.x diff --git a/src/test/run-pass/overloaded-index.rs b/src/test/run-pass/overloaded-index.rs index fdf7e7e2cbb13..fe09b47cf0a78 100644 --- a/src/test/run-pass/overloaded-index.rs +++ b/src/test/run-pass/overloaded-index.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(associated_types)] + use std::ops::{Index, IndexMut}; struct Foo { @@ -15,7 +17,9 @@ struct Foo { y: int, } -impl Index for Foo { +impl Index for Foo { + type Output = int; + fn index(&self, z: &int) -> &int { if *z == 0 { &self.x @@ -25,7 +29,9 @@ impl Index for Foo { } } -impl IndexMut for Foo { +impl IndexMut for Foo { + type Output = int; + fn index_mut(&mut self, z: &int) -> &mut int { if *z == 0 { &mut self.x