Skip to content

Commit 4fdcb65

Browse files
Rollup merge of rust-lang#41937 - nikomatsakis:issue-41936-variance-coerce-unsized-cycle, r=eddyb
use equality in the coerce-unsized check This seems both to be a safe, conservative choice, and it sidesteps the cycle in rust-lang#41849. Note that, before I converted variance into proper queries, we were using a hybrid of subtyping and equality, due to the presence of a flag that forced invariance if variance had not yet been computed. (Also, Coerce Unsized is unstable.) Fixes rust-lang#41936. r? @eddyb
2 parents 58d29b1 + 138a4c8 commit 4fdcb65

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/librustc_typeck/coherence/builtin.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,45 @@ pub fn coerce_unsized_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
254254
return err_info;
255255
}
256256

257+
// Here we are considering a case of converting
258+
// `S<P0...Pn>` to S<Q0...Qn>`. As an example, let's imagine a struct `Foo<T, U>`,
259+
// which acts like a pointer to `U`, but carries along some extra data of type `T`:
260+
//
261+
// struct Foo<T, U> {
262+
// extra: T,
263+
// ptr: *mut U,
264+
// }
265+
//
266+
// We might have an impl that allows (e.g.) `Foo<T, [i32; 3]>` to be unsized
267+
// to `Foo<T, [i32]>`. That impl would look like:
268+
//
269+
// impl<T, U: Unsize<V>, V> CoerceUnsized<Foo<T, V>> for Foo<T, U> {}
270+
//
271+
// Here `U = [i32; 3]` and `V = [i32]`. At runtime,
272+
// when this coercion occurs, we would be changing the
273+
// field `ptr` from a thin pointer of type `*mut [i32;
274+
// 3]` to a fat pointer of type `*mut [i32]` (with
275+
// extra data `3`). **The purpose of this check is to
276+
// make sure that we know how to do this conversion.**
277+
//
278+
// To check if this impl is legal, we would walk down
279+
// the fields of `Foo` and consider their types with
280+
// both substitutes. We are looking to find that
281+
// exactly one (non-phantom) field has changed its
282+
// type, which we will expect to be the pointer that
283+
// is becoming fat (we could probably generalize this
284+
// to mutiple thin pointers of the same type becoming
285+
// fat, but we don't). In this case:
286+
//
287+
// - `extra` has type `T` before and type `T` after
288+
// - `ptr` has type `*mut U` before and type `*mut V` after
289+
//
290+
// Since just one field changed, we would then check
291+
// that `*mut U: CoerceUnsized<*mut V>` is implemented
292+
// (in other words, that we know how to do this
293+
// conversion). This will work out because `U:
294+
// Unsize<V>`, and we have a builtin rule that `*mut
295+
// U` can be coerced to `*mut V` if `U: Unsize<V>`.
257296
let fields = &def_a.struct_variant().fields;
258297
let diff_fields = fields.iter()
259298
.enumerate()
@@ -265,8 +304,16 @@ pub fn coerce_unsized_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
265304
return None;
266305
}
267306

268-
// Ignore fields that aren't significantly changed
269-
if let Ok(ok) = infcx.sub_types(false, &cause, b, a) {
307+
// Ignore fields that aren't changed; it may
308+
// be that we could get away with subtyping or
309+
// something more accepting, but we use
310+
// equality because we want to be able to
311+
// perform this check without computing
312+
// variance where possible. (This is because
313+
// we may have to evaluate constraint
314+
// expressions in the course of execution.)
315+
// See e.g. #41936.
316+
if let Ok(ok) = infcx.eq_types(false, &cause, b, a) {
270317
if ok.obligations.is_empty() {
271318
return None;
272319
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #41936. The coerce-unsized trait check in
12+
// coherence was using subtyping, which triggered variance
13+
// computation, which failed because it required type info for fields
14+
// that had not (yet) been computed.
15+
16+
#![feature(unsize)]
17+
#![feature(coerce_unsized)]
18+
19+
use std::{marker,ops};
20+
21+
// Change the array to a non-array, and error disappears
22+
// Adding a new field to the end keeps the error
23+
struct LogDataBuf([u8;8]);
24+
25+
struct Aref<T: ?Sized>
26+
{
27+
// Inner structure triggers the error, removing the inner removes the message.
28+
ptr: Box<ArefInner<T>>,
29+
}
30+
impl<T: ?Sized + marker::Unsize<U>, U: ?Sized> ops::CoerceUnsized<Aref<U>> for Aref<T> {}
31+
32+
struct ArefInner<T: ?Sized>
33+
{
34+
// Even with this field commented out, the error is raised.
35+
data: T,
36+
}
37+
38+
fn main(){}

0 commit comments

Comments
 (0)