Skip to content

Commit bae9832

Browse files
committed
Add test for issue-40231
1 parent ec45882 commit bae9832

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/test/ui/issues/issue-40231-1.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// check-pass
2+
3+
#![allow(dead_code)]
4+
5+
trait Structure<E>: Sized where E: Encoding {
6+
type RefTarget: ?Sized;
7+
type FfiPtr;
8+
unsafe fn borrow_from_ffi_ptr<'a>(ptr: Self::FfiPtr) -> Option<&'a Self::RefTarget>;
9+
}
10+
11+
enum Slice {}
12+
13+
impl<E> Structure<E> for Slice where E: Encoding {
14+
type RefTarget = [E::Unit];
15+
type FfiPtr = (*const E::FfiUnit, usize);
16+
unsafe fn borrow_from_ffi_ptr<'a>(_ptr: Self::FfiPtr) -> Option<&'a Self::RefTarget> {
17+
panic!()
18+
}
19+
}
20+
21+
trait Encoding {
22+
type Unit: Unit;
23+
type FfiUnit;
24+
}
25+
26+
trait Unit {}
27+
28+
enum Utf16 {}
29+
30+
impl Encoding for Utf16 {
31+
type Unit = Utf16Unit;
32+
type FfiUnit = u16;
33+
}
34+
35+
struct Utf16Unit(pub u16);
36+
37+
impl Unit for Utf16Unit {}
38+
39+
type SUtf16Str = SeStr<Slice, Utf16>;
40+
41+
struct SeStr<S, E> where S: Structure<E>, E: Encoding {
42+
_data: S::RefTarget,
43+
}
44+
45+
impl<S, E> SeStr<S, E> where S: Structure<E>, E: Encoding {
46+
pub unsafe fn from_ptr<'a>(_ptr: S::FfiPtr) -> Option<&'a Self> {
47+
panic!()
48+
}
49+
}
50+
51+
fn main() {
52+
const TEXT_U16: &'static [u16] = &[];
53+
let _ = unsafe { SUtf16Str::from_ptr((TEXT_U16.as_ptr(), TEXT_U16.len())).unwrap() };
54+
}

src/test/ui/issues/issue-40231-2.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// check-pass
2+
3+
#![allow(dead_code)]
4+
5+
trait Structure<E>: Sized where E: Encoding {
6+
type RefTarget: ?Sized;
7+
type FfiPtr;
8+
unsafe fn borrow_from_ffi_ptr<'a>(ptr: Self::FfiPtr) -> Option<&'a Self::RefTarget>;
9+
}
10+
11+
enum Slice {}
12+
13+
impl<E> Structure<E> for Slice where E: Encoding {
14+
type RefTarget = [E::Unit];
15+
type FfiPtr = (*const E::FfiUnit, usize);
16+
unsafe fn borrow_from_ffi_ptr<'a>(_ptr: Self::FfiPtr) -> Option<&'a Self::RefTarget> {
17+
panic!()
18+
}
19+
}
20+
21+
trait Encoding {
22+
type Unit: Unit;
23+
type FfiUnit;
24+
}
25+
26+
trait Unit {}
27+
28+
enum Utf16 {}
29+
30+
impl Encoding for Utf16 {
31+
type Unit = Utf16Unit;
32+
type FfiUnit = u16;
33+
}
34+
35+
struct Utf16Unit(pub u16);
36+
37+
impl Unit for Utf16Unit {}
38+
39+
struct SUtf16Str {
40+
_data: <Slice as Structure<Utf16>>::RefTarget,
41+
}
42+
43+
impl SUtf16Str {
44+
pub unsafe fn from_ptr<'a>(ptr: <Slice as Structure<Utf16>>::FfiPtr)
45+
-> Option<&'a Self> {
46+
std::mem::transmute::<Option<&[<Utf16 as Encoding>::Unit]>, _>(
47+
<Slice as Structure<Utf16>>::borrow_from_ffi_ptr(ptr))
48+
}
49+
}
50+
51+
fn main() {
52+
const TEXT_U16: &'static [u16] = &[];
53+
let _ = unsafe { SUtf16Str::from_ptr((TEXT_U16.as_ptr(), TEXT_U16.len())).unwrap() };
54+
}

0 commit comments

Comments
 (0)