Skip to content

Commit 5e50518

Browse files
committed
Add tests cases from review of #132289
1 parent 8247594 commit 5e50518

6 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pubtypeA = &'static[usize;1];
2+
pubtypeB = &'static[usize;100];
3+
4+
pubtraitTrait<P>{
5+
typeAssoc;
6+
}
7+
8+
pubtypeDyn<P> = dynTrait<P,Assoc = A>;
9+
10+
pubtraitLocallyUnimplemented<P>{}
11+
12+
impl<P,T: ?Sized>Trait<P>forT
13+
where
14+
T:LocallyUnimplemented<P>,
15+
{
16+
typeAssoc = B;
17+
}
18+
19+
traitMakeArray<Arr>{
20+
fnmake() -> &'staticArr;
21+
}
22+
impl<constN:usize>MakeArray<[usize;N]>for(){
23+
fnmake() -> &'static[usize;N]{
24+
&[1337;N]
25+
}
26+
}
27+
28+
// it would be sound for this return type to be interpreted as being
29+
// either of A or B, if that's what a soundness fix for overlap of
30+
// dyn Trait's impls would entail
31+
32+
// In this test, we check at the call-site that the interpretation
33+
// is consistent across crates in this specific scenario.
34+
pubfnfunction<P>() -> (<Dyn<P>asTrait<P>>::Assoc,usize){
35+
let val = <()asMakeArray<_>>::make();
36+
(val, val.len())
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::ops::Index;
2+
3+
pubtraitTrait{
4+
fnf(&self)
5+
where
6+
dyn Index<(),Output = ()>:Index<()>;
7+
// rustc (correctly) determines ^^^^^^^^ this bound to be true
8+
}
9+
10+
pubfncall(x:&dynTrait){
11+
x.f();// so we can call `f`
12+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This is a regression test for issues that came up during review of the (closed)
2+
// PR #132289; this single-crate test case is
3+
// the first example from @steffahn during review.
4+
// https://github.com/rust-lang/rust/pull/132289#issuecomment-2564492153
5+
6+
//@ check-pass
7+
8+
typeA = &'static[usize;1];
9+
typeB = &'static[usize;100];
10+
11+
typeDynSomething = dynSomething<Assoc = A>;
12+
13+
traitSuper{
14+
typeAssoc;
15+
}
16+
implSuperforFoo{
17+
typeAssoc = A;
18+
}
19+
20+
traitIsDynSomething{}
21+
implIsDynSomethingforDynSomething{}
22+
23+
impl<T: ?Sized>SuperforT
24+
where
25+
T:IsDynSomething,
26+
{
27+
typeAssoc = B;
28+
}
29+
30+
traitSomething:Super{
31+
fnmethod(&self) -> Self::Assoc;
32+
}
33+
34+
structFoo;
35+
implSomethingforFoo{
36+
fnmethod(&self) -> Self::Assoc{
37+
&[1337]
38+
}
39+
}
40+
41+
fnmain(){
42+
let x = &Foo;
43+
let y:&DynSomething = x;
44+
45+
// no surprises here
46+
let _arr1:A = x.method();
47+
48+
// this (`_arr2`) can't ever become B either, soundly
49+
let _arr2:A = y.method();
50+
// there aren't any other arrays being defined anywhere in this
51+
// test case, besides the length-1 one containing [1337]
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This is a regression test for issues that came up during review of the (closed)
2+
// PR #132289; this 2-crate test case is adapted from
3+
// the second example from @steffahn during review.
4+
// https://github.com/rust-lang/rust/pull/132289#issuecomment-2564587796
5+
6+
//@ run-pass
7+
//@ aux-build: pr_review_132289_2_lib.rs
8+
9+
externcrate pr_review_132289_2_lib;
10+
11+
use pr_review_132289_2_lib::{function,Dyn,LocallyUnimplemented};
12+
13+
structParam;
14+
15+
implLocallyUnimplemented<Param>forDyn<Param>{}
16+
17+
// it would be sound for `function::<Param>`'s return type to be
18+
// either of A or B, if that's what a soundness fix for overlap of
19+
// dyn Trait's impls would entail
20+
21+
// In this test, we check at this call-site that the interpretation
22+
// is consistent with the function definition's body.
23+
fnmain(){
24+
let(arr, len) = function::<Param>();
25+
assert_eq!(arr.len(), len);
26+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This is a regression test for issues that came up during review of the (closed)
2+
// PR #132289; this 3-ish-crate (including std) test case is adapted from
3+
// the third example from @steffahn during review.
4+
// https://github.com/rust-lang/rust/pull/132289#issuecomment-2564599221
5+
6+
//@ run-pass
7+
//@ check-run-results
8+
//@ aux-build: pr_review_132289_3_lib.rs
9+
10+
externcrate pr_review_132289_3_lib;
11+
12+
use std::ops::Index;
13+
14+
use pr_review_132289_3_lib::{call,Trait};
15+
16+
traitSubIndex<I>:Index<I>{}
17+
18+
structParam;
19+
20+
traitProject{
21+
typeTy: ?Sized;
22+
}
23+
implProjectfor(){
24+
typeTy = dynSubIndex<Param,Output = ()>;
25+
}
26+
27+
implIndex<Param>for <()asProject>::Ty{
28+
typeOutput = ();
29+
30+
fnindex(&self, _:Param) -> &(){
31+
&()
32+
}
33+
}
34+
35+
structStruct;
36+
37+
implTraitforStruct{
38+
fnf(&self)
39+
where
40+
// higher-ranked to allow potentially-false bounds
41+
for<'a>dynIndex<(),Output = ()>:Index<()>,
42+
// after #132289 rustc used to believe this bound false
43+
{
44+
println!("hello!");
45+
}
46+
}
47+
48+
fnmain(){
49+
call(&Struct);// <- would segfault if the method `f` wasn't part of the vtable
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello!

0 commit comments

Comments
 (0)