-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #42527 - qnighy:unsized-tuple-coercions, r=arielb1
Unsized tuple coercions Part of #18469. Fixes #32702. #37685 and #34451 might also be related. This PR does the following: - Introduce explicit `Sized` constraints on tuple initializers, similar to that of record-struct initializers. Not much relevant to the main contribution but I noticed this when making tests for unsized tuple coercions. - Implement `(.., T): Unsize<(.., U)>` where `T: Unsize<U>`. - Assume `(.., T)` is MaybeUnsizedUnivariant. - Modify `src/librustc/ty/util.rs` and `src/librustc_trans/glue.rs` so that tuples and structs are uniformly traversed when translating.
- Loading branch information
Showing
26 changed files
with
707 additions
and
19 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/doc/unstable-book/src/language-features/unsized-tuple-coercion.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# `unsized_tuple_coercion` | ||
|
||
The tracking issue for this feature is: [#42877] | ||
|
||
[#42877]: https://github.com/rust-lang/rust/issues/42877 | ||
|
||
------------------------ | ||
|
||
This is a part of [RFC0401]. According to the RFC, there should be an implementation like this: | ||
|
||
```rust | ||
impl<..., T, U: ?Sized> Unsized<(..., U)> for (..., T) where T: Unsized<U> {} | ||
``` | ||
|
||
This implementation is currently gated behind `#[feature(unsized_tuple_coercion)]` to avoid insta-stability. Therefore you can use it like this: | ||
|
||
```rust | ||
#![feature(unsized_tuple_coercion)] | ||
|
||
fn main() { | ||
let x : ([i32; 3], [i32; 3]) = ([1, 2, 3], [4, 5, 6]); | ||
let y : &([i32; 3], [i32]) = &x; | ||
assert_eq!(y.1[0], 4); | ||
} | ||
``` | ||
|
||
[RFC0401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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. | ||
|
||
#![feature(unsized_tuple_coercion)] | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.