Skip to content

Commit a435b49

Browse files
committedJun 28, 2021
Auto merge of #86690 - JohnTitor:rollup-4ukk4yw, r=JohnTitor
Rollup of 6 pull requests Successful merges: - #86206 (Fix type checking of return expressions outside of function bodies) - #86358 (fix pretty print for `loop`) - #86568 (Don't dist miri or rust-analyzer on stable or beta.) - #86683 (:arrow_up: rust-analyzer) - #86687 (Allow anyone to set `perf-regression` label) - #86688 (Add a regression test for issue-65384) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 17ea490 + c5055b7 commit a435b49

19 files changed

+268
-36
lines changed
 

‎compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1954,7 +1954,6 @@ impl<'a> State<'a> {
19541954
self.word_space(":");
19551955
}
19561956
self.head("loop");
1957-
self.s.space();
19581957
self.print_block_with_attrs(blk, attrs);
19591958
}
19601959
ast::ExprKind::Match(ref expr, ref arms) => {

‎compiler/rustc_hir_pretty/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,6 @@ impl<'a> State<'a> {
15391539
self.word_space(":");
15401540
}
15411541
self.head("loop");
1542-
self.s.space();
15431542
self.print_block(&blk);
15441543
}
15451544
hir::ExprKind::Match(ref expr, arms, _) => {

‎compiler/rustc_typeck/src/check/expr.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
675675
expr: &'tcx hir::Expr<'tcx>,
676676
) -> Ty<'tcx> {
677677
if self.ret_coercion.is_none() {
678-
self.tcx.sess.emit_err(ReturnStmtOutsideOfFnBody { span: expr.span });
678+
let mut err = ReturnStmtOutsideOfFnBody {
679+
span: expr.span,
680+
encl_body_span: None,
681+
encl_fn_span: None,
682+
};
683+
684+
let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
685+
let encl_item = self.tcx.hir().expect_item(encl_item_id);
686+
687+
if let hir::ItemKind::Fn(..) = encl_item.kind {
688+
// We are inside a function body, so reporting "return statement
689+
// outside of function body" needs an explanation.
690+
691+
let encl_body_owner_id = self.tcx.hir().enclosing_body_owner(expr.hir_id);
692+
693+
// If this didn't hold, we would not have to report an error in
694+
// the first place.
695+
assert_ne!(encl_item_id, encl_body_owner_id);
696+
697+
let encl_body_id = self.tcx.hir().body_owned_by(encl_body_owner_id);
698+
let encl_body = self.tcx.hir().body(encl_body_id);
699+
700+
err.encl_body_span = Some(encl_body.value.span);
701+
err.encl_fn_span = Some(encl_item.span);
702+
}
703+
704+
self.tcx.sess.emit_err(err);
705+
706+
if let Some(e) = expr_opt {
707+
// We still have to type-check `e` (issue #86188), but calling
708+
// `check_return_expr` only works inside fn bodies.
709+
self.check_expr(e);
710+
}
679711
} else if let Some(e) = expr_opt {
680712
if self.ret_coercion_span.get().is_none() {
681713
self.ret_coercion_span.set(Some(e.span));

‎compiler/rustc_typeck/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ pub struct TypeofReservedKeywordUsed {
147147
pub struct ReturnStmtOutsideOfFnBody {
148148
#[message = "return statement outside of function body"]
149149
pub span: Span,
150+
#[label = "the return is part of this body..."]
151+
pub encl_body_span: Option<Span>,
152+
#[label = "...not the enclosing function body"]
153+
pub encl_fn_span: Option<Span>,
150154
}
151155

152156
#[derive(SessionDiagnostic)]

‎src/bootstrap/dist.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,12 @@ impl Step for RustAnalyzer {
10721072
}
10731073

10741074
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1075+
// This prevents rust-analyzer from being built for "dist" or "install"
1076+
// on the stable/beta channels. It is a nightly-only tool and should
1077+
// not be included.
1078+
if !builder.build.unstable_features() {
1079+
return None;
1080+
}
10751081
let compiler = self.compiler;
10761082
let target = self.target;
10771083
assert!(builder.config.extended);
@@ -1171,6 +1177,12 @@ impl Step for Miri {
11711177
}
11721178

11731179
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1180+
// This prevents miri from being built for "dist" or "install"
1181+
// on the stable/beta channels. It is a nightly-only tool and should
1182+
// not be included.
1183+
if !builder.build.unstable_features() {
1184+
return None;
1185+
}
11741186
let compiler = self.compiler;
11751187
let target = self.target;
11761188
assert!(builder.config.extended);

‎src/test/pretty/ast-stmt-expr-attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn syntax() {
3939
#![attr]
4040
};
4141
let _ =
42-
#[attr] loop {
42+
#[attr] loop {
4343
#![attr]
4444
};
4545
let _ =

‎src/test/pretty/hir-pretty-loop.pp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
// pretty-compare-only
6+
// pretty-mode:hir
7+
// pp-exact:hir-pretty-loop.pp
8+
9+
pub fn foo() { loop { break ; } }

‎src/test/pretty/hir-pretty-loop.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// pretty-compare-only
2+
// pretty-mode:hir
3+
// pp-exact:hir-pretty-loop.pp
4+
5+
pub fn foo(){
6+
loop{
7+
break;
8+
}
9+
}

‎src/test/pretty/stmt_expr_attributes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,8 @@ fn _11() {
166166
#[rustc_dummy] for _ in 0..0 {
167167
#![rustc_dummy]
168168
};
169-
// FIXME: pp bug, two spaces after the loop
170169
let _ =
171-
#[rustc_dummy] loop {
170+
#[rustc_dummy] loop {
172171
#![rustc_dummy]
173172
};
174173
let _ =

‎src/test/ui/issues/issue-51714.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
fn main() {
2+
//~^ NOTE: not the enclosing function body
3+
//~| NOTE: not the enclosing function body
4+
//~| NOTE: not the enclosing function body
5+
//~| NOTE: not the enclosing function body
26
|_: [_; return || {}] | {};
3-
//~^ ERROR return statement outside of function body
7+
//~^ ERROR: return statement outside of function body [E0572]
8+
//~| NOTE: the return is part of this body...
49

510
[(); return || {}];
6-
//~^ ERROR return statement outside of function body
11+
//~^ ERROR: return statement outside of function body [E0572]
12+
//~| NOTE: the return is part of this body...
713

814
[(); return |ice| {}];
9-
//~^ ERROR return statement outside of function body
15+
//~^ ERROR: return statement outside of function body [E0572]
16+
//~| NOTE: the return is part of this body...
1017

1118
[(); return while let Some(n) = Some(0) {}];
12-
//~^ ERROR return statement outside of function body
19+
//~^ ERROR: return statement outside of function body [E0572]
20+
//~| NOTE: the return is part of this body...
1321
}

‎src/test/ui/issues/issue-51714.stderr

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,62 @@
11
error[E0572]: return statement outside of function body
2-
--> $DIR/issue-51714.rs:2:14
2+
--> $DIR/issue-51714.rs:6:14
33
|
4-
LL | |_: [_; return || {}] | {};
5-
| ^^^^^^^^^^^^
4+
LL | / fn main() {
5+
LL | |
6+
LL | |
7+
LL | |
8+
LL | |
9+
LL | | |_: [_; return || {}] | {};
10+
| | ^^^^^^^^^^^^ the return is part of this body...
11+
... |
12+
LL | |
13+
LL | | }
14+
| |_- ...not the enclosing function body
615

716
error[E0572]: return statement outside of function body
8-
--> $DIR/issue-51714.rs:5:10
17+
--> $DIR/issue-51714.rs:10:10
918
|
10-
LL | [(); return || {}];
11-
| ^^^^^^^^^^^^
19+
LL | / fn main() {
20+
LL | |
21+
LL | |
22+
LL | |
23+
... |
24+
LL | | [(); return || {}];
25+
| | ^^^^^^^^^^^^ the return is part of this body...
26+
... |
27+
LL | |
28+
LL | | }
29+
| |_- ...not the enclosing function body
1230

1331
error[E0572]: return statement outside of function body
14-
--> $DIR/issue-51714.rs:8:10
32+
--> $DIR/issue-51714.rs:14:10
1533
|
16-
LL | [(); return |ice| {}];
17-
| ^^^^^^^^^^^^^^^
34+
LL | / fn main() {
35+
LL | |
36+
LL | |
37+
LL | |
38+
... |
39+
LL | | [(); return |ice| {}];
40+
| | ^^^^^^^^^^^^^^^ the return is part of this body...
41+
... |
42+
LL | |
43+
LL | | }
44+
| |_- ...not the enclosing function body
1845

1946
error[E0572]: return statement outside of function body
20-
--> $DIR/issue-51714.rs:11:10
47+
--> $DIR/issue-51714.rs:18:10
2148
|
22-
LL | [(); return while let Some(n) = Some(0) {}];
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49+
LL | / fn main() {
50+
LL | |
51+
LL | |
52+
LL | |
53+
... |
54+
LL | | [(); return while let Some(n) = Some(0) {}];
55+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the return is part of this body...
56+
LL | |
57+
LL | |
58+
LL | | }
59+
| |_- ...not the enclosing function body
2460

2561
error: aborting due to 4 previous errors
2662

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Due to a compiler bug, if a return occurs outside of a function body
2+
// (e.g. in an AnonConst body), the return value expression would not be
3+
// type-checked, leading to an ICE. This test checks that the ICE no
4+
// longer happens, and that an appropriate error message is issued that
5+
// also explains why the return is considered "outside of a function body"
6+
// if it seems to be inside one, as in the main function below.
7+
8+
const C: [(); 42] = {
9+
[(); return || {
10+
//~^ ERROR: return statement outside of function body [E0572]
11+
let tx;
12+
}]
13+
};
14+
15+
fn main() {
16+
//~^ NOTE: ...not the enclosing function body
17+
[(); return || {
18+
//~^ ERROR: return statement outside of function body [E0572]
19+
//~| NOTE: the return is part of this body...
20+
let tx;
21+
}];
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0572]: return statement outside of function body
2+
--> $DIR/issue-86188-return-not-in-fn-body.rs:9:10
3+
|
4+
LL | [(); return || {
5+
| __________^
6+
LL | |
7+
LL | | let tx;
8+
LL | | }]
9+
| |_____^
10+
11+
error[E0572]: return statement outside of function body
12+
--> $DIR/issue-86188-return-not-in-fn-body.rs:17:10
13+
|
14+
LL | / fn main() {
15+
LL | |
16+
LL | | [(); return || {
17+
| |__________^
18+
LL | ||
19+
LL | ||
20+
LL | || let tx;
21+
LL | || }];
22+
| ||_____^ the return is part of this body...
23+
LL | | }
24+
| |_- ...not the enclosing function body
25+
26+
error: aborting due to 2 previous errors
27+
28+
For more information about this error, try `rustc --explain E0572`.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
fn main() {
2+
//~^ NOTE: not the enclosing function body
3+
//~| NOTE: not the enclosing function body
4+
//~| NOTE: not the enclosing function body
25
[(); return match 0 { n => n }];
3-
//~^ ERROR: return statement outside of function body
6+
//~^ ERROR: return statement outside of function body [E0572]
7+
//~| NOTE: the return is part of this body...
48

59
[(); return match 0 { 0 => 0 }];
6-
//~^ ERROR: return statement outside of function body
10+
//~^ ERROR: return statement outside of function body [E0572]
11+
//~| NOTE: the return is part of this body...
712

813
[(); return match () { 'a' => 0, _ => 0 }];
9-
//~^ ERROR: return statement outside of function body
14+
//~^ ERROR: return statement outside of function body [E0572]
15+
//~| NOTE: the return is part of this body...
16+
//~| ERROR: mismatched types [E0308]
17+
//~| NOTE: expected `()`, found `char`
18+
//~| NOTE: this expression has type `()`
1019
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,56 @@
11
error[E0572]: return statement outside of function body
2-
--> $DIR/return-match-array-const.rs:2:10
2+
--> $DIR/return-match-array-const.rs:5:10
33
|
4-
LL | [(); return match 0 { n => n }];
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | / fn main() {
5+
LL | |
6+
LL | |
7+
LL | |
8+
LL | | [(); return match 0 { n => n }];
9+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ the return is part of this body...
10+
... |
11+
LL | |
12+
LL | | }
13+
| |_- ...not the enclosing function body
614

715
error[E0572]: return statement outside of function body
8-
--> $DIR/return-match-array-const.rs:5:10
16+
--> $DIR/return-match-array-const.rs:9:10
917
|
10-
LL | [(); return match 0 { 0 => 0 }];
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
18+
LL | / fn main() {
19+
LL | |
20+
LL | |
21+
LL | |
22+
... |
23+
LL | | [(); return match 0 { 0 => 0 }];
24+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ the return is part of this body...
25+
... |
26+
LL | |
27+
LL | | }
28+
| |_- ...not the enclosing function body
1229

1330
error[E0572]: return statement outside of function body
14-
--> $DIR/return-match-array-const.rs:8:10
31+
--> $DIR/return-match-array-const.rs:13:10
32+
|
33+
LL | / fn main() {
34+
LL | |
35+
LL | |
36+
LL | |
37+
... |
38+
LL | | [(); return match () { 'a' => 0, _ => 0 }];
39+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the return is part of this body...
40+
... |
41+
LL | |
42+
LL | | }
43+
| |_- ...not the enclosing function body
44+
45+
error[E0308]: mismatched types
46+
--> $DIR/return-match-array-const.rs:13:28
1547
|
1648
LL | [(); return match () { 'a' => 0, _ => 0 }];
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49+
| -- ^^^ expected `()`, found `char`
50+
| |
51+
| this expression has type `()`
1852

19-
error: aborting due to 3 previous errors
53+
error: aborting due to 4 previous errors
2054

21-
For more information about this error, try `rustc --explain E0572`.
55+
Some errors have detailed explanations: E0308, E0572.
56+
For more information about an error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(min_type_alias_impl_trait)]
2+
#![feature(type_alias_impl_trait)]
3+
#![allow(incomplete_features)]
4+
5+
trait MyTrait {}
6+
7+
impl MyTrait for () {}
8+
9+
type Bar = impl MyTrait;
10+
11+
impl MyTrait for Bar {}
12+
//~^ ERROR: cannot implement trait on type alias impl trait
13+
14+
fn bazr() -> Bar { }
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot implement trait on type alias impl trait
2+
--> $DIR/issue-65384.rs:11:1
3+
|
4+
LL | impl MyTrait for Bar {}
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: type alias impl trait defined here
8+
--> $DIR/issue-65384.rs:9:12
9+
|
10+
LL | type Bar = impl MyTrait;
11+
| ^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

‎src/tools/rust-analyzer

‎triagebot.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ allow-unauthenticated = [
44
"D-*",
55
"requires-nightly",
66
"regression-*",
7+
"perf-regression",
78
# I-* without I-nominated
89
"I-*", "!I-nominated",
910
"AsyncAwait-OnDeck",

0 commit comments

Comments
 (0)
Please sign in to comment.