Skip to content

Commit e3800a1

Browse files
compiler-errorsWaffleLapkin
authored andcommitted
Allow dropping dyn principal
1 parent 3a85d3f commit e3800a1

File tree

10 files changed

+83
-35
lines changed

10 files changed

+83
-35
lines changed

compiler/rustc_codegen_cranelift/src/unsize.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ pub(crate) fn unsized_info<'tcx>(
3434
{
3535
let old_info =
3636
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
37-
if data_a.principal_def_id() == data_b.principal_def_id() {
37+
let b_principal_def_id = data_b.principal_def_id();
38+
if data_a.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
39+
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
3840
debug_assert!(
3941
validate_trivial_unsize(fx.tcx, data_a, data_b),
4042
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"

compiler/rustc_codegen_ssa/src/base.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub fn validate_trivial_unsize<'tcx>(
147147
infcx.leak_check(universe, None).is_ok()
148148
})
149149
}
150-
(None, None) => true,
150+
(_, None) => true,
151151
_ => false,
152152
}
153153
}
@@ -175,7 +175,8 @@ fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
175175
{
176176
let old_info =
177177
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
178-
if data_a.principal_def_id() == data_b.principal_def_id() {
178+
let b_principal_def_id = data_b.principal_def_id();
179+
if data_a.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
179180
// Codegen takes advantage of the additional assumption, where if the
180181
// principal trait def id of what's being casted doesn't change,
181182
// then we don't need to adjust the vtable at all. This

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ where
785785
let mut responses = vec![];
786786
// If the principal def ids match (or are both none), then we're not doing
787787
// trait upcasting. We're just removing auto traits (or shortening the lifetime).
788-
if a_data.principal_def_id() == b_data.principal_def_id() {
788+
let b_principal_def_id = b_data.principal_def_id();
789+
if a_data.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
789790
responses.extend(self.consider_builtin_upcast_to_principal(
790791
goal,
791792
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10181018
// #2 (region bounds).
10191019
let principal_def_id_a = a_data.principal_def_id();
10201020
let principal_def_id_b = b_data.principal_def_id();
1021-
if principal_def_id_a == principal_def_id_b {
1021+
if principal_def_id_a == principal_def_id_b || principal_def_id_b.is_none() {
10221022
// We may upcast to auto traits that are either explicitly listed in
10231023
// the object type's bounds, or implied by the principal trait ref's
10241024
// supertraits.

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11531153
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
11541154
let iter = data_a
11551155
.principal()
1156+
.filter(|_| {
1157+
// optionally drop the principal, if we're unsizing to no principal
1158+
data_b.principal().is_some()
1159+
})
11561160
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
11571161
.into_iter()
11581162
.chain(

tests/ui/impl-trait/unsized_coercion5.next.stderr

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/unsized_coercion5.rs:16:32
3-
|
4-
LL | let y: Box<dyn Send> = x as Box<dyn Trait + Send>;
5-
| ------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `Send`, found trait `Trait + Send`
6-
| |
7-
| expected due to this
8-
|
9-
= note: expected struct `Box<dyn Send>`
10-
found struct `Box<dyn Trait + Send>`
11-
121
error[E0277]: the size for values of type `impl Trait + ?Sized` cannot be known at compilation time
13-
--> $DIR/unsized_coercion5.rs:16:32
2+
--> $DIR/unsized_coercion5.rs:17:32
143
|
154
LL | let y: Box<dyn Send> = x as Box<dyn Trait + Send>;
165
| ^ doesn't have a size known at compile-time
176
|
187
= help: the trait `Sized` is not implemented for `impl Trait + ?Sized`
198
= note: required for the cast from `Box<impl Trait + ?Sized>` to `Box<dyn Trait + Send>`
209

21-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2211

23-
Some errors have detailed explanations: E0277, E0308.
24-
For more information about an error, try `rustc --explain E0277`.
12+
For more information about this error, try `rustc --explain E0277`.

tests/ui/impl-trait/unsized_coercion5.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
//@ revisions: next old
55
//@[next] compile-flags: -Znext-solver
6+
//@[next] check-pass
67

78
#![feature(trait_upcasting)]
89

@@ -15,7 +16,6 @@ fn hello() -> Box<impl Trait + ?Sized> {
1516
let x = hello();
1617
let y: Box<dyn Send> = x as Box<dyn Trait + Send>;
1718
//[old]~^ ERROR: the size for values of type `impl Trait + ?Sized` cannot be know
18-
//~^^ ERROR: mismatched types
1919
}
2020
Box::new(1u32)
2121
}

tests/ui/traits/dyn-drop-principal.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//@ run-pass
2+
//@ check-run-results
3+
4+
use std::any::Any;
5+
6+
const fn yeet_principal(x: Box<dyn Any + Send>) -> Box<dyn Send> {
7+
x
8+
}
9+
10+
trait Bar: Send + Sync {}
11+
12+
impl<T: Send + Sync> Bar for T {}
13+
14+
const fn yeet_principal_2(x: Box<dyn Bar>) -> Box<dyn Send> {
15+
x
16+
}
17+
18+
struct CallMe<F: FnOnce()>(Option<F>);
19+
20+
impl<F: FnOnce()> CallMe<F> {
21+
fn new(f: F) -> Self {
22+
CallMe(Some(f))
23+
}
24+
}
25+
26+
impl<F: FnOnce()> Drop for CallMe<F> {
27+
fn drop(&mut self) {
28+
(self.0.take().unwrap())();
29+
}
30+
}
31+
32+
fn goodbye() {
33+
println!("goodbye");
34+
}
35+
36+
fn main() {
37+
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Any + Send>;
38+
let y = yeet_principal(x);
39+
println!("before");
40+
drop(y);
41+
42+
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>;
43+
let y = yeet_principal_2(x);
44+
println!("before");
45+
drop(y);
46+
}
47+
48+
// Test that upcast works in `const`
49+
50+
const fn yeet_principal_3(x: &(dyn Any + Send + Sync)) -> &(dyn Send + Sync) {
51+
x
52+
}
53+
54+
#[used]
55+
pub static FOO: &(dyn Send + Sync) = yeet_principal_3(&false);
56+
57+
const fn yeet_principal_4(x: &dyn Bar) -> &(dyn Send + Sync) {
58+
x
59+
}
60+
61+
#[used]
62+
pub static BAR: &(dyn Send + Sync) = yeet_principal_4(&false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
before
2+
goodbye
3+
before
4+
goodbye

0 commit comments

Comments
 (0)