Skip to content

Commit 89faafc

Browse files
committed
Auto merge of #28381 - ebfull:aint-fraid-a-no-ghosts-redux, r=me
#27483 redux at Gankro's request. Fixes #26905, Closes #28239 r? @nrc
2 parents d3fc6e1 + b096403 commit 89faafc

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

src/librustc/middle/ty/sty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,14 @@ impl<'tcx> TyS<'tcx> {
899899
}
900900
}
901901

902+
pub fn is_phantom_data(&self) -> bool {
903+
if let TyStruct(def, _) = self.sty {
904+
def.is_phantom_data()
905+
} else {
906+
false
907+
}
908+
}
909+
902910
pub fn is_bool(&self) -> bool { self.sty == TyBool }
903911

904912
pub fn is_param(&self, space: subst::ParamSpace, index: u32) -> bool {

src/librustc_trans/trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ fn coerce_unsized<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
533533
Rvalue::new(ByRef)));
534534
} else {
535535
// Otherwise, simply copy the data from the source.
536-
assert_eq!(src_ty, target_ty);
536+
assert!(src_ty.is_phantom_data() || src_ty == target_ty);
537537
memcpy_ty(bcx, ll_target, ll_source, src_ty);
538538
}
539539
}

src/librustc_typeck/coherence/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,16 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
480480
let fields = &def_a.struct_variant().fields;
481481
let diff_fields = fields.iter().enumerate().filter_map(|(i, f)| {
482482
let (a, b) = (f.ty(tcx, substs_a), f.ty(tcx, substs_b));
483-
if infcx.sub_types(false, origin, b, a).is_ok() {
483+
484+
if f.unsubst_ty().is_phantom_data() {
485+
// Ignore PhantomData fields
486+
None
487+
} else if infcx.sub_types(false, origin, b, a).is_ok() {
488+
// Ignore fields that aren't significantly changed
484489
None
485490
} else {
491+
// Collect up all fields that were significantly changed
492+
// i.e. those that contain T in coerce_unsized T -> U
486493
Some((i, a, b))
487494
}
488495
}).collect::<Vec<_>>();

src/test/compile-fail/issue-26905.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2015 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+
#![feature(unsize, coerce_unsized)]
12+
13+
// Verfies that non-PhantomData ZSTs still cause coercions to fail.
14+
// They might have additional semantics that we don't want to bulldoze.
15+
16+
use std::marker::{Unsize, PhantomData};
17+
use std::ops::CoerceUnsized;
18+
19+
struct NotPhantomData<T: ?Sized>(PhantomData<T>);
20+
21+
struct MyRc<T: ?Sized> {
22+
_ptr: *const T,
23+
_boo: NotPhantomData<T>,
24+
}
25+
26+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<MyRc<U>> for MyRc<T>{ } //~ERROR
27+
28+
fn main() {
29+
let data = [1, 2, 3];
30+
let iter = data.iter();
31+
let x = MyRc { _ptr: &iter, _boo: NotPhantomData(PhantomData) };
32+
let _y: MyRc<Iterator<Item=&u32>> = x;
33+
}
34+

src/test/run-pass/issue-26905.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015 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+
#![feature(unsize, coerce_unsized)]
12+
13+
// Verfies that PhantomData is ignored for DST coercions
14+
15+
use std::marker::{Unsize, PhantomData};
16+
use std::ops::CoerceUnsized;
17+
18+
struct MyRc<T: ?Sized> {
19+
_ptr: *const T,
20+
_boo: PhantomData<T>,
21+
}
22+
23+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<MyRc<U>> for MyRc<T>{ }
24+
25+
fn main() {
26+
let data = [1, 2, 3];
27+
let iter = data.iter();
28+
let x = MyRc { _ptr: &iter, _boo: PhantomData };
29+
let _y: MyRc<Iterator<Item=&u32>> = x;
30+
}
31+

0 commit comments

Comments
 (0)