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

Allow dropping dyn trait object's principal #114679

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ pub fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
) if src_dyn_kind == target_dyn_kind => {
let old_info =
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
if data_a.principal_def_id() == data_b.principal_def_id() {
if data_a.principal_def_id() == data_b.principal_def_id()
|| data_b.principal().is_none()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjorn3: if this PR ends up being accepted, the change will need to be made to cg-clif as well. should I do that in this PR, or in a separate one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it in the same PR would be nice I think. You can change

if data_a.principal_def_id() == data_b.principal_def_id() {
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
return old_info;
}
the same way as you did here.

{
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
return old_info;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
let mut responses = vec![];
// If the principal def ids match (or are both none), then we're not doing
// trait upcasting. We're just removing auto traits (or shortening the lifetime).
if a_data.principal_def_id() == b_data.principal_def_id() {
if a_data.principal_def_id() == b_data.principal_def_id() || b_data.principal().is_none() {
if let Ok(resp) = self.consider_builtin_upcast_to_principal(
goal,
a_data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
if auto_traits_compatible {
let principal_def_id_a = a_data.principal_def_id();
let principal_def_id_b = b_data.principal_def_id();
if principal_def_id_a == principal_def_id_b {
if principal_def_id_a == principal_def_id_b || principal_def_id_b.is_none() {
// no cyclic
candidates.vec.push(BuiltinUnsizeCandidate);
} else if principal_def_id_a.is_some() && principal_def_id_b.is_some() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
let iter = data_a
.principal()
.filter(|_| {
// optionally drop the principal, if we're unsizing to no principal
data_b.principal().is_some()
})
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
.into_iter()
.chain(
Expand Down
33 changes: 33 additions & 0 deletions tests/ui/traits/dyn-drop-principal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// run-pass
// check-run-results

#![feature(trait_upcasting)]

use std::any::Any;

fn yeet_principal(x: Box<dyn Any + Send>) -> 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 y = yeet_principal(x);
println!("before");
drop(y);
}
2 changes: 2 additions & 0 deletions tests/ui/traits/dyn-drop-principal.run.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
before
goodbye
Loading