Skip to content

Commit bcf8365

Browse files
committed
Make sure constructors functions are type checked correctly
1 parent 0d75ab2 commit bcf8365

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Unit test for the "user substitutions" that are annotated on each
2+
// node.
3+
4+
struct SomeStruct<T>(T);
5+
6+
fn no_annot() {
7+
let c = 66;
8+
let f = SomeStruct;
9+
f(&c);
10+
}
11+
12+
fn annot_underscore() {
13+
let c = 66;
14+
let f = SomeStruct::<_>;
15+
f(&c);
16+
}
17+
18+
fn annot_reference_any_lifetime() {
19+
let c = 66;
20+
let f = SomeStruct::<&u32>;
21+
f(&c);
22+
}
23+
24+
fn annot_reference_static_lifetime() {
25+
let c = 66;
26+
let f = SomeStruct::<&'static u32>;
27+
f(&c); //~ ERROR
28+
}
29+
30+
fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
31+
let c = 66;
32+
let f = SomeStruct::<&'a u32>;
33+
f(&c); //~ ERROR
34+
}
35+
36+
fn annot_reference_named_lifetime_ok<'a>(c: &'a u32) {
37+
let f = SomeStruct::<&'a u32>;
38+
f(c);
39+
}
40+
41+
fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
42+
let _closure = || {
43+
let c = 66;
44+
let f = SomeStruct::<&'a u32>;
45+
f(&c); //~ ERROR
46+
};
47+
}
48+
49+
fn annot_reference_named_lifetime_across_closure<'a>(_: &'a u32) {
50+
let f = SomeStruct::<&'a u32>;
51+
let _closure = || {
52+
let c = 66;
53+
f(&c); //~ ERROR
54+
};
55+
}
56+
57+
fn annot_reference_named_lifetime_in_closure_ok<'a>(c: &'a u32) {
58+
let _closure = || {
59+
let f = SomeStruct::<&'a u32>;
60+
f(c);
61+
};
62+
}
63+
64+
fn annot_reference_named_lifetime_across_closure_ok<'a>(c: &'a u32) {
65+
let f = SomeStruct::<&'a u32>;
66+
let _closure = || {
67+
f(c);
68+
};
69+
}
70+
71+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error[E0597]: `c` does not live long enough
2+
--> $DIR/adt-tuple-struct-calls.rs:27:7
3+
|
4+
LL | f(&c);
5+
| --^^-
6+
| | |
7+
| | borrowed value does not live long enough
8+
| argument requires that `c` is borrowed for `'static`
9+
LL | }
10+
| - `c` dropped here while still borrowed
11+
12+
error[E0597]: `c` does not live long enough
13+
--> $DIR/adt-tuple-struct-calls.rs:33:7
14+
|
15+
LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
16+
| -- lifetime `'a` defined here
17+
...
18+
LL | f(&c);
19+
| --^^-
20+
| | |
21+
| | borrowed value does not live long enough
22+
| argument requires that `c` is borrowed for `'a`
23+
LL | }
24+
| - `c` dropped here while still borrowed
25+
26+
error[E0597]: `c` does not live long enough
27+
--> $DIR/adt-tuple-struct-calls.rs:45:11
28+
|
29+
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
30+
| -- lifetime `'a` defined here
31+
...
32+
LL | f(&c);
33+
| --^^-
34+
| | |
35+
| | borrowed value does not live long enough
36+
| argument requires that `c` is borrowed for `'a`
37+
LL | };
38+
| - `c` dropped here while still borrowed
39+
40+
error[E0597]: `c` does not live long enough
41+
--> $DIR/adt-tuple-struct-calls.rs:53:11
42+
|
43+
LL | let f = SomeStruct::<&'a u32>;
44+
| - lifetime `'1` appears in the type of `f`
45+
...
46+
LL | f(&c);
47+
| --^^-
48+
| | |
49+
| | borrowed value does not live long enough
50+
| argument requires that `c` is borrowed for `'1`
51+
LL | };
52+
| - `c` dropped here while still borrowed
53+
54+
error: aborting due to 4 previous errors
55+
56+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)