Skip to content

Commit 4e0c27b

Browse files
Erase regions from CallArgument, add test + bless
1 parent dce44fa commit 4e0c27b

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16301630

16311631
let op_arg_ty = self.normalize(op_arg_ty, term_location);
16321632
let category = if from_hir_call {
1633-
ConstraintCategory::CallArgument(func_ty)
1633+
ConstraintCategory::CallArgument(self.infcx.tcx.erase_regions(func_ty))
16341634
} else {
16351635
ConstraintCategory::Boring
16361636
};

src/test/ui/associated-types/cache/project-fn-ret-invariant.oneuse.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ LL | let a = bar(f, x);
1515
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
1616

1717
error: lifetime may not live long enough
18-
--> $DIR/project-fn-ret-invariant.rs:40:13
18+
--> $DIR/project-fn-ret-invariant.rs:42:13
1919
|
2020
LL | fn baz<'a, 'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
2121
| -- -- lifetime `'b` defined here
2222
| |
2323
| lifetime `'a` defined here
24-
LL | let f = foo; // <-- No consistent type can be inferred for `f` here.
25-
LL | let a = bar(f, x);
24+
...
25+
LL | let b = bar(f, y);
2626
| ^^^^^^^^^ argument requires that `'b` must outlive `'a`
2727
|
2828
= help: consider adding the following bound: `'b: 'a`
29-
= note: requirement occurs because of a function pointer to `foo`
30-
= note: the function `foo` is invariant over the parameter `'a`
29+
= note: requirement occurs because of the type `Type<'_>`, which makes the generic argument `'_` invariant
30+
= note: the struct `Type<'a>` is invariant over the parameter `'a`
3131
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
3232

3333
help: `'a` and `'b` must be the same: replace one with the other

src/test/ui/associated-types/cache/project-fn-ret-invariant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ fn baz<'a, 'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
3939
let f = foo; // <-- No consistent type can be inferred for `f` here.
4040
let a = bar(f, x);
4141
//[oneuse]~^ ERROR lifetime may not live long enough
42-
//[oneuse]~| ERROR lifetime may not live long enough
4342
let b = bar(f, y);
43+
//[oneuse]~^ ERROR lifetime may not live long enough
4444
(a, b)
4545
}
4646

src/test/ui/borrowck/issue-103624.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// edition:2021
2+
3+
struct StructA {
4+
b: StructB,
5+
}
6+
7+
async fn spawn_blocking<T>(f: impl (Fn() -> T) + Send + Sync + 'static) -> T {
8+
todo!()
9+
}
10+
11+
impl StructA {
12+
async fn foo(&self) {
13+
let bar = self.b.bar().await;
14+
spawn_blocking(move || {
15+
//~^ ERROR borrowed data escapes outside of associated function
16+
self.b;
17+
//~^ ERROR cannot move out of `self.b`, as `self` is a captured variable in an `Fn` closure
18+
})
19+
.await;
20+
}
21+
}
22+
23+
struct StructB {}
24+
25+
impl StructB {
26+
async fn bar(&self) -> Option<u8> {
27+
None
28+
}
29+
}
30+
31+
fn main() {}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0507]: cannot move out of `self.b`, as `self` is a captured variable in an `Fn` closure
2+
--> $DIR/issue-103624.rs:16:13
3+
|
4+
LL | async fn foo(&self) {
5+
| ----- captured outer variable
6+
LL | let bar = self.b.bar().await;
7+
LL | spawn_blocking(move || {
8+
| ------- captured by this `Fn` closure
9+
LL |
10+
LL | self.b;
11+
| ^^^^^^ move occurs because `self.b` has type `StructB`, which does not implement the `Copy` trait
12+
13+
error[E0521]: borrowed data escapes outside of associated function
14+
--> $DIR/issue-103624.rs:14:9
15+
|
16+
LL | async fn foo(&self) {
17+
| -----
18+
| |
19+
| `self` is a reference that is only valid in the associated function body
20+
| let's call the lifetime of this reference `'1`
21+
LL | let bar = self.b.bar().await;
22+
LL | / spawn_blocking(move || {
23+
LL | |
24+
LL | | self.b;
25+
LL | |
26+
LL | | })
27+
| | ^
28+
| | |
29+
| |__________`self` escapes the associated function body here
30+
| argument requires that `'1` must outlive `'static`
31+
32+
error: aborting due to 2 previous errors
33+
34+
Some errors have detailed explanations: E0507, E0521.
35+
For more information about an error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)