-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Allow dropping dyn principal #131857
Merged
+167
−35
Merged
Allow dropping dyn principal #131857
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,4 @@ | ||
before | ||
goodbye | ||
before | ||
goodbye |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,24 +1,12 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/unsized_coercion5.rs:16:32 | ||
| | ||
LL | let y: Box<dyn Send> = x as Box<dyn Trait + Send>; | ||
| ------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `Send`, found trait `Trait + Send` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected struct `Box<dyn Send>` | ||
found struct `Box<dyn Trait + Send>` | ||
|
||
error[E0277]: the size for values of type `impl Trait + ?Sized` cannot be known at compilation time | ||
--> $DIR/unsized_coercion5.rs:16:32 | ||
--> $DIR/unsized_coercion5.rs:17:32 | ||
| | ||
LL | let y: Box<dyn Send> = x as Box<dyn Trait + Send>; | ||
| ^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `impl Trait + ?Sized` | ||
= note: required for the cast from `Box<impl Trait + ?Sized>` to `Box<dyn Trait + Send>` | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to 1 previous error | ||
|
||
Some errors have detailed explanations: E0277, E0308. | ||
For more information about an error, try `rustc --explain E0277`. | ||
For more information about this error, try `rustc --explain E0277`. |
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,68 @@ | ||
//@ run-pass | ||
//@ check-run-results | ||
|
||
use std::{alloc::Layout, any::Any}; | ||
|
||
const fn yeet_principal(x: Box<dyn Any + Send>) -> Box<dyn Send> { | ||
x | ||
} | ||
|
||
trait Bar: Send + Sync {} | ||
|
||
impl<T: Send + Sync> Bar for T {} | ||
|
||
const fn yeet_principal_2(x: Box<dyn Bar>) -> Box<dyn Send> { | ||
x | ||
} | ||
|
||
struct CallMe<F: FnOnce()>(Option<F>); | ||
|
||
impl<F: FnOnce()> CallMe<F> { | ||
fn new(f: F) -> Self { | ||
CallMe(Some(f)) | ||
} | ||
} | ||
|
||
impl<F: FnOnce()> Drop for CallMe<F> { | ||
fn drop(&mut self) { | ||
(self.0.take().unwrap())(); | ||
} | ||
} | ||
|
||
fn goodbye() { | ||
println!("goodbye"); | ||
} | ||
|
||
fn main() { | ||
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Any + Send>; | ||
let x_layout = Layout::for_value(&*x); | ||
let y = yeet_principal(x); | ||
let y_layout = Layout::for_value(&*y); | ||
assert_eq!(x_layout, y_layout); | ||
println!("before"); | ||
drop(y); | ||
|
||
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>; | ||
let x_layout = Layout::for_value(&*x); | ||
let y = yeet_principal_2(x); | ||
let y_layout = Layout::for_value(&*y); | ||
assert_eq!(x_layout, y_layout); | ||
println!("before"); | ||
drop(y); | ||
} | ||
|
||
// Test that upcast works in `const` | ||
|
||
const fn yeet_principal_3(x: &(dyn Any + Send + Sync)) -> &(dyn Send + Sync) { | ||
x | ||
} | ||
|
||
#[used] | ||
pub static FOO: &(dyn Send + Sync) = yeet_principal_3(&false); | ||
|
||
const fn yeet_principal_4(x: &dyn Bar) -> &(dyn Send + Sync) { | ||
x | ||
} | ||
|
||
#[used] | ||
pub static BAR: &(dyn Send + Sync) = yeet_principal_4(&false); |
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,4 @@ | ||
before | ||
goodbye | ||
before | ||
goodbye |
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,12 @@ | ||
#![feature(dyn_star)] | ||
#![allow(incomplete_features)] | ||
|
||
trait Trait {} | ||
impl Trait for usize {} | ||
|
||
fn main() { | ||
// We allow &dyn Trait + Send -> &dyn Send (i.e. dropping principal), | ||
// but we don't (currently?) allow the same for dyn* | ||
let x: dyn* Trait + Send = 1usize; | ||
x as dyn* Send; //~ error: `dyn* Trait + Send` needs to have the same ABI as a pointer | ||
} |
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,11 @@ | ||
error[E0277]: `dyn* Trait + Send` needs to have the same ABI as a pointer | ||
--> $DIR/dyn-star-drop-principal.rs:11:5 | ||
| | ||
LL | x as dyn* Send; | ||
| ^ `dyn* Trait + Send` needs to be a pointer-like type | ||
| | ||
= help: the trait `PointerLike` is not implemented for `dyn* Trait + Send` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yay thanks for testing this one specifically :3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was actually @Jules-Bertholet :D
I only added a
dyn*
test as asked by lcnr.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ty jules :3