Skip to content

Commit

Permalink
Allow dropping dyn principal
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors authored and Jules-Bertholet committed Jun 19, 2024
1 parent 8fcd4dd commit e2bb040
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_cranelift/src/unsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub(crate) fn unsized_info<'tcx>(
{
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() {
let b_principal_def_id = data_b.principal_def_id();
if data_a.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
return old_info;
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ pub fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
{
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() {
let b_principal_def_id = data_b.principal_def_id();
if data_a.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
return old_info;
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,8 @@ where
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() {
let b_principal_def_id = b_data.principal_def_id();
if a_data.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
responses.extend(self.consider_builtin_upcast_to_principal(
goal,
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// #2 (region bounds).
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() {
// We may upcast to auto traits that are either explicitly listed in
// the object type's bounds, or implied by the principal trait ref's
// supertraits.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,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()
})
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
.into_iter()
.chain(
Expand Down
46 changes: 46 additions & 0 deletions tests/ui/traits/dyn-drop-principal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//@ run-pass
//@ check-run-results

use std::any::Any;

fn yeet_principal(x: Box<dyn Any + Send>) -> Box<dyn Send> {
x
}

trait Bar: Send {}

impl<T: Send> Bar for T {}

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 y = yeet_principal(x);
println!("before");
drop(y);

let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>;
let y = yeet_principal_2(x);
println!("before");
drop(y);
}
4 changes: 4 additions & 0 deletions tests/ui/traits/dyn-drop-principal.run.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
before
goodbye
before
goodbye

0 comments on commit e2bb040

Please sign in to comment.