Add unsized tuple coercions.

This commit is contained in:
Masaki Hara 2017-06-08 14:49:54 +09:00
parent 17fca8b460
commit 23d1521684
No known key found for this signature in database
GPG key ID: 7CA7A85E049A82E8
18 changed files with 549 additions and 18 deletions

View file

@ -0,0 +1,48 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Forbid assignment into a dynamically sized type.
type Fat<T: ?Sized> = (isize, &'static str, T);
//~^ WARNING trait bounds are not (yet) enforced
#[derive(PartialEq,Eq)]
struct Bar;
#[derive(PartialEq,Eq)]
struct Bar1 {
f: isize
}
trait ToBar {
fn to_bar(&self) -> Bar;
fn to_val(&self) -> isize;
}
impl ToBar for Bar1 {
fn to_bar(&self) -> Bar {
Bar
}
fn to_val(&self) -> isize {
self.f
}
}
pub fn main() {
// Assignment.
let f5: &mut Fat<ToBar> = &mut (5, "some str", Bar1 {f :42});
let z: Box<ToBar> = Box::new(Bar1 {f: 36});
f5.2 = Bar1 {f: 36};
//~^ ERROR mismatched types
//~| expected type `ToBar`
//~| found type `Bar1`
//~| expected trait ToBar, found struct `Bar1`
//~| ERROR `ToBar: std::marker::Sized` is not satisfied
}

View file

@ -29,4 +29,16 @@ pub fn main() {
let f2: &Fat<Foo> = &f1;
let f3: &Fat<Bar> = f2;
//~^ ERROR `Foo: Bar` is not satisfied
// Tuple with a vec of isize.
let f1 = ([1, 2, 3],);
let f2: &([isize; 3],) = &f1;
let f3: &([usize],) = f2;
//~^ ERROR mismatched types
// Tuple with a trait.
let f1 = (Foo,);
let f2: &(Foo,) = &f1;
let f3: &(Bar,) = f2;
//~^ ERROR `Foo: Bar` is not satisfied
}

View file

@ -28,4 +28,14 @@ pub fn main() {
let f1 = Fat { ptr: Foo };
let f2: &Fat<Foo> = &f1;
let f3: &mut Fat<Bar> = f2; //~ ERROR mismatched types
// Tuple with a vec of ints.
let f1 = ([1, 2, 3],);
let f2: &([isize; 3],) = &f1;
let f3: &mut ([isize],) = f2; //~ ERROR mismatched types
// Tuple with a trait.
let f1 = (Foo,);
let f2: &(Foo,) = &f1;
let f3: &mut (Bar,) = f2; //~ ERROR mismatched types
}

View file

@ -28,6 +28,16 @@ fn baz<'a>() {
let f1 = Fat { ptr: Foo };
let f2: &Fat<Foo> = &f1; //~ ERROR `f1` does not live long enough
let f3: &'a Fat<Bar> = f2;
// Tuple with a vec of ints.
let f1 = ([1, 2, 3],);
let f2: &([isize; 3],) = &f1; //~ ERROR `f1` does not live long enough
let f3: &'a ([isize],) = f2;
// Tuple with a trait.
let f1 = (Foo,);
let f2: &(Foo,) = &f1; //~ ERROR `f1` does not live long enough
let f3: &'a (Bar,) = f2;
}
pub fn main() {

View file

@ -22,4 +22,12 @@ pub fn main() {
//~| expected type `&Fat<[isize; 3]>`
//~| found type `&Fat<[isize]>`
//~| expected array of 3 elements, found slice
// Tuple with a vec of isizes.
let f1: &([isize],) = &([1, 2, 3],);
let f2: &([isize; 3],) = f1;
//~^ ERROR mismatched types
//~| expected type `&([isize; 3],)`
//~| found type `&([isize],)`
//~| expected array of 3 elements, found slice
}

View file

@ -0,0 +1,21 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Try to initialise a DST struct where the lost information is deeply nested.
// This is an error because it requires an unsized rvalue. This is a problem
// because it would require stack allocation of an unsized temporary (*g in the
// test).
pub fn main() {
let f: ([isize; 3],) = ([5, 6, 7],);
let g: &([isize],) = &f;
let h: &(([isize],),) = &(*g,);
//~^ ERROR `[isize]: std::marker::Sized` is not satisfied
}

View file

@ -28,7 +28,8 @@ struct Fat<T: ?Sized> {
pub fn main() {
{
let _x: Box<Fat<Trait>> = Box::<Fat<Foo>>::new(Fat { f: Foo });
let _x: Box<(i32, Fat<Trait>)> =
Box::<(i32, Fat<Foo>)>::new((42, Fat { f: Foo }));
}
unsafe {
assert!(DROP_RAN);

View file

@ -25,7 +25,8 @@ struct Fat<T: ?Sized> {
pub fn main() {
{
let _x: Box<Fat<[Foo]>> = Box::<Fat<[Foo; 3]>>::new(Fat { f: [Foo, Foo, Foo] });
let _x: Box<(Fat<[Foo]>,)> =
Box::<(Fat<[Foo; 3]>,)>::new((Fat { f: [Foo, Foo, Foo] },));
}
unsafe {
assert_eq!(DROP_RAN, 3);

View file

@ -21,4 +21,14 @@ fn main() {
let slice = &[1,2,3];
let x = Test(&slice);
let Test(&_slice) = x;
let x = (10, [1,2,3]);
let x : &(i32, [i32]) = &x;
let & ref _y = x;
let slice = &[1,2,3];
let x = (10, &slice);
let (_, &_slice) = x;
}

View file

@ -45,6 +45,14 @@ pub fn main() {
};
assert_eq!(r, 42);
// raw DST tuple
let p = (A { f: 42 },);
let o: *const (Trait,) = &p;
let r = unsafe {
(&*o).0.foo()
};
assert_eq!(r, 42);
// raw slice
let a: *const [_] = &[1, 2, 3];
unsafe {
@ -72,6 +80,15 @@ pub fn main() {
assert_eq!(len, 3);
}
// raw DST tuple with slice
let c: *const ([_],) = &([1, 2, 3],);
unsafe {
let b = (&*c).0[0];
assert_eq!(b, 1);
let len = (&*c).0.len();
assert_eq!(len, 3);
}
// all of the above with *mut
let mut x = A { f: 42 };
let z: *mut Trait = &mut x;
@ -87,6 +104,13 @@ pub fn main() {
};
assert_eq!(r, 42);
let mut p = (A { f: 42 },);
let o: *mut (Trait,) = &mut p;
let r = unsafe {
(&*o).0.foo()
};
assert_eq!(r, 42);
let a: *mut [_] = &mut [1, 2, 3];
unsafe {
let b = (*a)[2];
@ -110,4 +134,12 @@ pub fn main() {
let len = (&*c).f.len();
assert_eq!(len, 3);
}
let c: *mut ([_],) = &mut ([1, 2, 3],);
unsafe {
let b = (&*c).0[0];
assert_eq!(b, 1);
let len = (&*c).0.len();
assert_eq!(len, 3);
}
}

View file

@ -0,0 +1,110 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(unused_features)]
#![feature(box_syntax)]
type Fat<T: ?Sized> = (isize, &'static str, T);
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct Bar;
#[derive(Copy, Clone, PartialEq, Eq)]
struct Bar1 {
f: isize
}
trait ToBar {
fn to_bar(&self) -> Bar;
fn to_val(&self) -> isize;
}
impl ToBar for Bar {
fn to_bar(&self) -> Bar {
*self
}
fn to_val(&self) -> isize {
0
}
}
impl ToBar for Bar1 {
fn to_bar(&self) -> Bar {
Bar
}
fn to_val(&self) -> isize {
self.f
}
}
// x is a fat pointer
fn foo(x: &Fat<ToBar>) {
assert_eq!(x.0, 5);
assert_eq!(x.1, "some str");
assert_eq!(x.2.to_bar(), Bar);
assert_eq!(x.2.to_val(), 42);
let y = &x.2;
assert_eq!(y.to_bar(), Bar);
assert_eq!(y.to_val(), 42);
}
fn bar(x: &ToBar) {
assert_eq!(x.to_bar(), Bar);
assert_eq!(x.to_val(), 42);
}
fn baz(x: &Fat<Fat<ToBar>>) {
assert_eq!(x.0, 5);
assert_eq!(x.1, "some str");
assert_eq!((x.2).0, 8);
assert_eq!((x.2).1, "deep str");
assert_eq!((x.2).2.to_bar(), Bar);
assert_eq!((x.2).2.to_val(), 42);
let y = &(x.2).2;
assert_eq!(y.to_bar(), Bar);
assert_eq!(y.to_val(), 42);
}
pub fn main() {
let f1 = (5, "some str", Bar1 {f :42});
foo(&f1);
let f2 = &f1;
foo(f2);
let f3: &Fat<ToBar> = f2;
foo(f3);
let f4: &Fat<ToBar> = &f1;
foo(f4);
let f5: &Fat<ToBar> = &(5, "some str", Bar1 {f :42});
foo(f5);
// Zero size object.
let f6: &Fat<ToBar> = &(5, "some str", Bar);
assert_eq!(f6.2.to_bar(), Bar);
// &*
//
let f7: Box<ToBar> = Box::new(Bar1 {f :42});
bar(&*f7);
// Deep nesting
let f1 = (5, "some str", (8, "deep str", Bar1 {f :42}));
baz(&f1);
let f2 = &f1;
baz(f2);
let f3: &Fat<Fat<ToBar>> = f2;
baz(f3);
let f4: &Fat<Fat<ToBar>> = &f1;
baz(f4);
let f5: &Fat<Fat<ToBar>> = &(5, "some str", (8, "deep str", Bar1 {f :42}));
baz(f5);
}

View file

@ -0,0 +1,83 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// As dst-tuple.rs, but the unsized field is the only field in the tuple.
type Fat<T: ?Sized> = (T,);
// x is a fat pointer
fn foo(x: &Fat<[isize]>) {
let y = &x.0;
assert_eq!(x.0.len(), 3);
assert_eq!(y[0], 1);
assert_eq!(x.0[1], 2);
}
fn foo2<T:ToBar>(x: &Fat<[T]>) {
let y = &x.0;
let bar = Bar;
assert_eq!(x.0.len(), 3);
assert_eq!(y[0].to_bar(), bar);
assert_eq!(x.0[1].to_bar(), bar);
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct Bar;
trait ToBar {
fn to_bar(&self) -> Bar;
}
impl ToBar for Bar {
fn to_bar(&self) -> Bar {
*self
}
}
pub fn main() {
// With a vec of ints.
let f1 = ([1, 2, 3],);
foo(&f1);
let f2 = &f1;
foo(f2);
let f3: &Fat<[isize]> = f2;
foo(f3);
let f4: &Fat<[isize]> = &f1;
foo(f4);
let f5: &Fat<[isize]> = &([1, 2, 3],);
foo(f5);
// With a vec of Bars.
let bar = Bar;
let f1 = ([bar, bar, bar],);
foo2(&f1);
let f2 = &f1;
foo2(f2);
let f3: &Fat<[Bar]> = f2;
foo2(f3);
let f4: &Fat<[Bar]> = &f1;
foo2(f4);
let f5: &Fat<[Bar]> = &([bar, bar, bar],);
foo2(f5);
// Assignment.
let f5: &mut Fat<[isize]> = &mut ([1, 2, 3],);
f5.0[1] = 34;
assert_eq!(f5.0[0], 1);
assert_eq!(f5.0[1], 34);
assert_eq!(f5.0[2], 3);
// Zero size vec.
let f5: &Fat<[isize]> = &([],);
assert!(f5.0.is_empty());
let f5: &Fat<[Bar]> = &([],);
assert!(f5.0.is_empty());
}

View file

@ -0,0 +1,128 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(unknown_features)]
#![feature(box_syntax)]
type Fat<T: ?Sized> = (isize, &'static str, T);
// x is a fat pointer
fn foo(x: &Fat<[isize]>) {
let y = &x.2;
assert_eq!(x.2.len(), 3);
assert_eq!(y[0], 1);
assert_eq!(x.2[1], 2);
assert_eq!(x.0, 5);
assert_eq!(x.1, "some str");
}
fn foo2<T:ToBar>(x: &Fat<[T]>) {
let y = &x.2;
let bar = Bar;
assert_eq!(x.2.len(), 3);
assert_eq!(y[0].to_bar(), bar);
assert_eq!(x.2[1].to_bar(), bar);
assert_eq!(x.0, 5);
assert_eq!(x.1, "some str");
}
fn foo3(x: &Fat<Fat<[isize]>>) {
let y = &(x.2).2;
assert_eq!(x.0, 5);
assert_eq!(x.1, "some str");
assert_eq!((x.2).0, 8);
assert_eq!((x.2).1, "deep str");
assert_eq!((x.2).2.len(), 3);
assert_eq!(y[0], 1);
assert_eq!((x.2).2[1], 2);
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct Bar;
trait ToBar {
fn to_bar(&self) -> Bar;
}
impl ToBar for Bar {
fn to_bar(&self) -> Bar {
*self
}
}
pub fn main() {
// With a vec of ints.
let f1 = (5, "some str", [1, 2, 3]);
foo(&f1);
let f2 = &f1;
foo(f2);
let f3: &Fat<[isize]> = f2;
foo(f3);
let f4: &Fat<[isize]> = &f1;
foo(f4);
let f5: &Fat<[isize]> = &(5, "some str", [1, 2, 3]);
foo(f5);
// With a vec of Bars.
let bar = Bar;
let f1 = (5, "some str", [bar, bar, bar]);
foo2(&f1);
let f2 = &f1;
foo2(f2);
let f3: &Fat<[Bar]> = f2;
foo2(f3);
let f4: &Fat<[Bar]> = &f1;
foo2(f4);
let f5: &Fat<[Bar]> = &(5, "some str", [bar, bar, bar]);
foo2(f5);
// Assignment.
let f5: &mut Fat<[isize]> = &mut (5, "some str", [1, 2, 3]);
f5.2[1] = 34;
assert_eq!(f5.2[0], 1);
assert_eq!(f5.2[1], 34);
assert_eq!(f5.2[2], 3);
// Zero size vec.
let f5: &Fat<[isize]> = &(5, "some str", []);
assert!(f5.2.is_empty());
let f5: &Fat<[Bar]> = &(5, "some str", []);
assert!(f5.2.is_empty());
// Deeply nested.
let f1 = (5, "some str", (8, "deep str", [1, 2, 3]));
foo3(&f1);
let f2 = &f1;
foo3(f2);
let f3: &Fat<Fat<[isize]>> = f2;
foo3(f3);
let f4: &Fat<Fat<[isize]>> = &f1;
foo3(f4);
let f5: &Fat<Fat<[isize]>> = &(5, "some str", (8, "deep str", [1, 2, 3]));
foo3(f5);
// Box.
let f1 = Box::new([1, 2, 3]);
assert_eq!((*f1)[1], 2);
let f2: Box<[isize]> = f1;
assert_eq!((*f2)[1], 2);
// Nested Box.
let f1 : Box<Fat<[isize; 3]>> = box (5, "some str", [1, 2, 3]);
foo(&*f1);
let f2 : Box<Fat<[isize]>> = f1;
foo(&*f2);
let f3 : Box<Fat<[isize]>> =
Box::<Fat<[_; 3]>>::new((5, "some str", [1, 2, 3]));
foo(&*f3);
}