Skip to content

Commit f1d90a9

Browse files
authored
Rollup merge of rust-lang#57729 - pnkfelix:issue-55748-pat-types-are-constraints-on-bindings-too, r=nikomatsakis
extra testing of how NLL handles wildcard type `_` test that wildcard type `_` is not duplicated by `type Foo<X> = (X, X);` and potentially instantiated at different types when used in type ascriptions in let bindings. (NLL's handling of this for the type ascription *expression form* is currently broken, or at least differs from what AST-borrowck does. I'll file a separate bug about that. Its not something critical to address since that expression is guarded by `#![feature(type_ascription)]`.) cc rust-lang#55748
2 parents 64eff91 + b3690c6 commit f1d90a9

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This test is ensuring that type ascriptions on let bindings
2+
// constrain both:
3+
//
4+
// 1. the input expression on the right-hand side (after any potential
5+
// coercion, and allowing for covariance), *and*
6+
//
7+
// 2. the bindings (if any) nested within the pattern on the left-hand
8+
// side (and here, the type-constraint is *invariant*).
9+
10+
#![feature(nll)]
11+
12+
#![allow(dead_code, unused_mut)]
13+
type PairUncoupled<'a, 'b, T> = (&'a T, &'b T);
14+
type PairCoupledRegions<'a, T> = (&'a T, &'a T);
15+
type PairCoupledTypes<T> = (T, T);
16+
17+
fn uncoupled_lhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
18+
let ((mut y, mut _z),): (PairUncoupled<u32>,) = ((s, &_x),); // ok
19+
// Above compiling does *not* imply below would compile.
20+
// ::std::mem::swap(&mut y, &mut _z);
21+
y
22+
}
23+
24+
fn swap_regions((mut y, mut _z): PairCoupledRegions<u32>) {
25+
::std::mem::swap(&mut y, &mut _z);
26+
}
27+
28+
fn coupled_regions_lhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
29+
let ((y, _z),): (PairCoupledRegions<u32>,) = ((s, &_x),);
30+
// If above line compiled, so should line below ...
31+
32+
// swap_regions((y, _z));
33+
34+
// ... but the ascribed type also invalidates this use of `y`
35+
y //~ ERROR unsatisfied lifetime constraints
36+
}
37+
38+
fn swap_types((mut y, mut _z): PairCoupledTypes<&u32>) {
39+
::std::mem::swap(&mut y, &mut _z);
40+
}
41+
42+
fn coupled_types_lhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
43+
let ((y, _z),): (PairCoupledTypes<&u32>,) = ((s, &_x),);
44+
// If above line compiled, so should line below ...
45+
46+
// swap_types((y, _z));
47+
48+
// ... but the ascribed type also invalidates this use of `y`
49+
y //~ ERROR unsatisfied lifetime constraints
50+
}
51+
52+
fn swap_wilds((mut y, mut _z): PairCoupledTypes<&u32>) {
53+
::std::mem::swap(&mut y, &mut _z);
54+
}
55+
56+
fn coupled_wilds_lhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
57+
let ((y, _z),): (PairCoupledTypes<_>,) = ((s, &_x),);
58+
// If above line compiled, so should line below
59+
// swap_wilds((y, _z));
60+
61+
// ... but the ascribed type also invalidates this use of `y`
62+
y //~ ERROR unsatisfied lifetime constraints
63+
}
64+
65+
fn main() {
66+
uncoupled_lhs(&3, &4);
67+
coupled_regions_lhs(&3, &4);
68+
coupled_types_lhs(&3, &4);
69+
coupled_wilds_lhs(&3, &4);
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: unsatisfied lifetime constraints
2+
--> $DIR/issue-55748-pat-types-constrain-bindings.rs:35:5
3+
|
4+
LL | fn coupled_regions_lhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
5+
| -- lifetime `'a` defined here
6+
...
7+
LL | y //~ ERROR unsatisfied lifetime constraints
8+
| ^ returning this value requires that `'a` must outlive `'static`
9+
10+
error: unsatisfied lifetime constraints
11+
--> $DIR/issue-55748-pat-types-constrain-bindings.rs:49:5
12+
|
13+
LL | fn coupled_types_lhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
14+
| -- lifetime `'a` defined here
15+
...
16+
LL | y //~ ERROR unsatisfied lifetime constraints
17+
| ^ returning this value requires that `'a` must outlive `'static`
18+
19+
error: unsatisfied lifetime constraints
20+
--> $DIR/issue-55748-pat-types-constrain-bindings.rs:62:5
21+
|
22+
LL | fn coupled_wilds_lhs<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
23+
| -- lifetime `'a` defined here
24+
...
25+
LL | y //~ ERROR unsatisfied lifetime constraints
26+
| ^ returning this value requires that `'a` must outlive `'static`
27+
28+
error: aborting due to 3 previous errors
29+

0 commit comments

Comments
 (0)