Skip to content

Commit 80cf576

Browse files
authored
Rollup merge of #127110 - surechen:fix_125488_06, r=compiler-errors
Fix a error suggestion for E0121 when using placeholder _ as return types on function signature. Recommit after refactoring based on comment: #126017 (comment) But when changing return type's lifetime to `ReError` will affect the subsequent borrow check process and cause test11 in typeck_type_placeholder_item.rs to lost E0515 message. ```rust fn test11(x: &usize) -> &_ { //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types &x //~ ERROR cannot return reference to function parameter(this E0515 msg will disappear) } ``` fixes #125488 r? ``@pnkfelix``
2 parents 7715295 + 50edb32 commit 80cf576

6 files changed

+127
-11
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1459,8 +1459,25 @@ fn infer_return_ty_for_fn_sig<'tcx>(
14591459
Some(ty) => {
14601460
let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
14611461
// Typeck doesn't expect erased regions to be returned from `type_of`.
1462+
// This is a heuristic approach. If the scope has region paramters,
1463+
// we should change fn_sig's lifetime from `ReErased` to `ReError`,
1464+
// otherwise to `ReStatic`.
1465+
let has_region_params = generics.params.iter().any(|param| match param.kind {
1466+
GenericParamKind::Lifetime { .. } => true,
1467+
_ => false,
1468+
});
14621469
let fn_sig = tcx.fold_regions(fn_sig, |r, _| match *r {
1463-
ty::ReErased => tcx.lifetimes.re_static,
1470+
ty::ReErased => {
1471+
if has_region_params {
1472+
ty::Region::new_error_with_message(
1473+
tcx,
1474+
DUMMY_SP,
1475+
"erased region is not allowed here in return type",
1476+
)
1477+
} else {
1478+
tcx.lifetimes.re_static
1479+
}
1480+
}
14641481
_ => r,
14651482
});
14661483

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ run-rustfix
2+
3+
#[allow(dead_code)]
4+
5+
fn main() {
6+
struct S<'a>(&'a ());
7+
8+
fn f1(s: S<'_>) -> S<'_> {
9+
//~^ ERROR the placeholder `_` is not allowed
10+
s
11+
}
12+
13+
fn f2(s: S<'_>) -> S<'_> {
14+
//~^ ERROR the placeholder `_` is not allowed
15+
let x = true;
16+
if x {
17+
s
18+
} else {
19+
s
20+
}
21+
}
22+
23+
fn f3(s: S<'_>) -> S<'_> {
24+
//~^ ERROR the placeholder `_` is not allowed
25+
return s;
26+
}
27+
28+
fn f4(s: S<'_>) -> S<'_> {
29+
//~^ ERROR the placeholder `_` is not allowed
30+
let _x = 1;
31+
return s;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ run-rustfix
2+
3+
#[allow(dead_code)]
4+
5+
fn main() {
6+
struct S<'a>(&'a ());
7+
8+
fn f1(s: S<'_>) -> _ {
9+
//~^ ERROR the placeholder `_` is not allowed
10+
s
11+
}
12+
13+
fn f2(s: S<'_>) -> _ {
14+
//~^ ERROR the placeholder `_` is not allowed
15+
let x = true;
16+
if x {
17+
s
18+
} else {
19+
s
20+
}
21+
}
22+
23+
fn f3(s: S<'_>) -> _ {
24+
//~^ ERROR the placeholder `_` is not allowed
25+
return s;
26+
}
27+
28+
fn f4(s: S<'_>) -> _ {
29+
//~^ ERROR the placeholder `_` is not allowed
30+
let _x = 1;
31+
return s;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
2+
--> $DIR/infer-return-ty-for-fn-sig-issue-125488.rs:8:24
3+
|
4+
LL | fn f1(s: S<'_>) -> _ {
5+
| ^
6+
| |
7+
| not allowed in type signatures
8+
| help: replace with the correct return type: `S<'_>`
9+
10+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
11+
--> $DIR/infer-return-ty-for-fn-sig-issue-125488.rs:13:24
12+
|
13+
LL | fn f2(s: S<'_>) -> _ {
14+
| ^
15+
| |
16+
| not allowed in type signatures
17+
| help: replace with the correct return type: `S<'_>`
18+
19+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
20+
--> $DIR/infer-return-ty-for-fn-sig-issue-125488.rs:23:24
21+
|
22+
LL | fn f3(s: S<'_>) -> _ {
23+
| ^
24+
| |
25+
| not allowed in type signatures
26+
| help: replace with the correct return type: `S<'_>`
27+
28+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
29+
--> $DIR/infer-return-ty-for-fn-sig-issue-125488.rs:28:24
30+
|
31+
LL | fn f4(s: S<'_>) -> _ {
32+
| ^
33+
| |
34+
| not allowed in type signatures
35+
| help: replace with the correct return type: `S<'_>`
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0121`.

tests/ui/typeck/typeck_type_placeholder_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Test9 {
4747

4848
fn test11(x: &usize) -> &_ {
4949
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
50-
&x //~ ERROR cannot return reference to function parameter
50+
&x
5151
}
5252

5353
unsafe fn test12(x: *const usize) -> *const *const _ {

tests/ui/typeck/typeck_type_placeholder_item.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ LL | fn test11(x: &usize) -> &_ {
158158
| -^
159159
| ||
160160
| |not allowed in type signatures
161-
| help: replace with the correct return type: `&'static &'static usize`
161+
| help: replace with the correct return type: `&&usize`
162162

163163
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
164164
--> $DIR/typeck_type_placeholder_item.rs:53:52
@@ -687,13 +687,7 @@ help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
687687
LL + #![feature(const_trait_impl)]
688688
|
689689

690-
error[E0515]: cannot return reference to function parameter `x`
691-
--> $DIR/typeck_type_placeholder_item.rs:50:5
692-
|
693-
LL | &x
694-
| ^^ returns a reference to data owned by the current function
695-
696-
error: aborting due to 75 previous errors
690+
error: aborting due to 74 previous errors
697691

698-
Some errors have detailed explanations: E0015, E0046, E0121, E0282, E0403, E0515.
692+
Some errors have detailed explanations: E0015, E0046, E0121, E0282, E0403.
699693
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)