Skip to content

Commit 611061b

Browse files
committed
test: Add a test for interesting module template polymorphism
1 parent 979a225 commit 611061b

18 files changed

+196
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = f32;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = f64;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = float;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn plus(x: T, y: T) -> T {
2+
x + y
3+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#[no_core];
2+
3+
4+
#[path = "module-polymorphism-files"]
5+
mod float {
6+
7+
// The type of the float
8+
import inst::T;
9+
10+
// Define T as float
11+
#[path = "inst_float.rs"]
12+
mod inst;
13+
14+
// Add in the implementation from a single source file
15+
#[path = "template.rs"]
16+
mod template;
17+
18+
}
19+
20+
#[path = "module-polymorphism-files"]
21+
mod f64 {
22+
23+
import inst::T;
24+
25+
// Define T as f64
26+
#[path = "inst_f64.rs"]
27+
mod inst;
28+
29+
// Use the implementation for the same source file!
30+
#[path = "template.rs"]
31+
mod template;
32+
33+
}
34+
35+
#[path = "module-polymorphism-files"]
36+
mod f32 {
37+
import inst::T;
38+
39+
#[path = "inst_f32.rs"]
40+
mod inst;
41+
42+
#[path = "template.rs"]
43+
mod template;
44+
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This isn't really xfailed; it's used by the
2+
// module-polymorphism.rc test
3+
// xfail-test
4+
5+
fn main() {
6+
// All of these functions are defined by a single module
7+
// source file but instantiated for different types
8+
assert float::template::plus(1.0f, 2.0f) == 3.0f;
9+
assert f64::template::plus(1.0f64, 2.0f64) == 3.0f64;
10+
assert f32::template::plus(1.0f32, 2.0f32) == 3.0f32;
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = f32;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = f64;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = float;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn plus(x: T, y: T) -> T {
2+
x + y
3+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#[no_core];
2+
3+
4+
#[path = "module-polymorphism2-files"]
5+
mod mystd {
6+
7+
#[path = "float-template"]
8+
mod float {
9+
// The type of the float
10+
import inst::T;
11+
12+
// Unfortunate
13+
import template::*;
14+
export plus;
15+
16+
// Define T as float
17+
#[path = "inst_float.rs"]
18+
mod inst;
19+
20+
// Add in the implementation from a single source file
21+
#[path = "template.rs"]
22+
mod template;
23+
}
24+
25+
26+
#[path = "float-template"]
27+
mod f64 {
28+
29+
import inst::T;
30+
31+
// Unfortunate
32+
import template::*;
33+
export plus;
34+
35+
// Define T as f64
36+
#[path = "inst_f64.rs"]
37+
mod inst;
38+
39+
// Use the implementation for the same source file!
40+
#[path = "template.rs"]
41+
mod template;
42+
43+
}
44+
45+
#[path = "float-template"]
46+
mod f32 {
47+
import inst::T;
48+
49+
// Unfortunate
50+
import template::*;
51+
export plus;
52+
53+
#[path = "inst_f32.rs"]
54+
mod inst;
55+
56+
#[path = "template.rs"]
57+
mod template;
58+
59+
}
60+
61+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This isn't really xfailed; it's used by the
2+
// module-polymorphism.rc test
3+
// xfail-test
4+
5+
fn main() {
6+
// All of these functions are defined by a single module
7+
// source file but instantiated for different types
8+
assert mystd::float::plus(1.0f, 2.0f) == 3.0f;
9+
assert mystd::f64::plus(1.0f64, 2.0f64) == 3.0f64;
10+
assert mystd::f32::plus(1.0f32, 2.0f32) == 3.0f32;
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn plus(x: T, y: T) -> T {
2+
x + y
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = f32;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = f64;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type T = float;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#[no_core];
2+
3+
// Use one template module to specify in a single file the implementation
4+
// of functions for multiple types
5+
6+
#[path = "module-polymorphism3-files"]
7+
mod mystd {
8+
9+
// The template is specified in float-template.rs
10+
#[path = "float-template"]
11+
mod float {
12+
// The type of the float
13+
import inst::T;
14+
15+
// Define T as appropriate for platform
16+
#[path = "inst_float.rs"]
17+
mod inst;
18+
}
19+
20+
// Use the same template
21+
#[path = "float-template"]
22+
mod f64 {
23+
24+
import inst::T;
25+
26+
// Define T as f64
27+
#[path = "inst_f64.rs"]
28+
mod inst;
29+
}
30+
31+
#[path = "float-template"]
32+
mod f32 {
33+
import inst::T;
34+
35+
#[path = "inst_f32.rs"]
36+
mod inst;
37+
}
38+
39+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This isn't really xfailed; it's used by the
2+
// module-polymorphism.rc test
3+
// xfail-test
4+
5+
fn main() {
6+
// All of these functions are defined by a single module
7+
// source file but instantiated for different types
8+
assert mystd::float::plus(1.0f, 2.0f) == 3.0f;
9+
assert mystd::f64::plus(1.0f64, 2.0f64) == 3.0f64;
10+
assert mystd::f32::plus(1.0f32, 2.0f32) == 3.0f32;
11+
}

0 commit comments

Comments
 (0)