Skip to content

Commit c51c90c

Browse files
authored
Rollup merge of #58970 - pnkfelix:issue-58158-size-of-assoc-type-ice, r=petrochenkov
delay_span_bug in wfcheck's ty.lift_to_tcx unwrap Fix #58158
2 parents 378a011 + 533f011 commit c51c90c

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/librustc_typeck/check/wfcheck.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,14 @@ fn check_type_defn<'a, 'tcx, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
251251
let needs_drop_copy = || {
252252
packed && {
253253
let ty = variant.fields.last().unwrap().ty;
254-
let ty = fcx.tcx.erase_regions(&ty).lift_to_tcx(fcx_tcx)
254+
fcx.tcx.erase_regions(&ty).lift_to_tcx(fcx_tcx)
255+
.map(|ty| ty.needs_drop(fcx_tcx, fcx_tcx.param_env(def_id)))
255256
.unwrap_or_else(|| {
256-
span_bug!(item.span, "inference variables in {:?}", ty)
257-
});
258-
ty.needs_drop(fcx_tcx, fcx_tcx.param_env(def_id))
257+
fcx_tcx.sess.delay_span_bug(
258+
item.span, &format!("inference variables in {:?}", ty));
259+
// Just treat unresolved type expression as if it needs drop.
260+
true
261+
})
259262
}
260263
};
261264
let all_sized =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// rust-lang/rust#58158: We have special-case code to deal with case
2+
// when a type is both packed and needs drop glue, (we move the fields
3+
// out of their potentially unaligned locations before dropping them,
4+
// which requires they be Sized; see PR #44884).
5+
//
6+
// So, we need to check if a given type needs drop-glue. That requires
7+
// that we actually know that the concrete type, and we guard against
8+
// the type having unknown parts (i.e. type variables) by ICE'ing in
9+
// that scenario.
10+
//
11+
// But in a case where we have a projection (`Type as Trait::Assoc`)
12+
// where `Type` does not actually implement `Trait`, we of course
13+
// cannot have a concrete type, because there is no impl to look up
14+
// the concrete type for the associated type `Assoc`.
15+
//
16+
// So, this test is just making sure that in such a case that we do
17+
// not immediately ICE, and instead allow the underlying type error to
18+
// surface.
19+
20+
pub struct Matrix<S>(S);
21+
pub struct DefaultAllocator;
22+
23+
pub trait Allocator { type Buffer; }
24+
25+
// impl Allocator for DefaultAllocator { type Buffer = (); }
26+
27+
#[repr(packed)]
28+
struct Foo(Matrix<<DefaultAllocator as Allocator>::Buffer>);
29+
//~^ ERROR the trait bound `DefaultAllocator: Allocator` is not satisfied
30+
31+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0277]: the trait bound `DefaultAllocator: Allocator` is not satisfied
2+
--> $DIR/wf-packed-on-proj-of-type-as-unimpl-trait.rs:28:12
3+
|
4+
LL | struct Foo(Matrix<<DefaultAllocator as Allocator>::Buffer>);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Allocator` is not implemented for `DefaultAllocator`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)