Skip to content

Commit 7059722

Browse files
compiler-errorsKjetil Kjeka
authored andcommitted
Fix spans for bad await in inline const
1 parent a6833b0 commit 7059722

File tree

5 files changed

+26
-64
lines changed

5 files changed

+26
-64
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
7272
let kind = match &e.kind {
7373
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
7474
ExprKind::ConstBlock(c) => {
75-
let c = self.with_new_scopes(|this| hir::ConstBlock {
75+
let c = self.with_new_scopes(c.value.span, |this| hir::ConstBlock {
7676
def_id: this.local_def_id(c.id),
7777
hir_id: this.lower_node_id(c.id),
7878
body: this.lower_const_body(c.value.span, Some(&c.value)),
@@ -189,7 +189,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
189189
None,
190190
e.span,
191191
hir::CoroutineSource::Block,
192-
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
192+
|this| this.with_new_scopes(e.span, |this| this.lower_block_expr(block)),
193193
),
194194
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
195195
ExprKind::Closure(box Closure {
@@ -323,7 +323,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
323323
None,
324324
e.span,
325325
hir::CoroutineSource::Block,
326-
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
326+
|this| this.with_new_scopes(e.span, |this| this.lower_block_expr(block)),
327327
),
328328
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
329329
ExprKind::Err => hir::ExprKind::Err(
@@ -922,9 +922,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
922922
) -> hir::ExprKind<'hir> {
923923
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
924924

925-
let (body_id, coroutine_option) = self.with_new_scopes(move |this| {
926-
let prev = this.current_item;
927-
this.current_item = Some(fn_decl_span);
925+
let (body_id, coroutine_option) = self.with_new_scopes(fn_decl_span, move |this| {
928926
let mut coroutine_kind = None;
929927
let body_id = this.lower_fn_body(decl, |this| {
930928
let e = this.lower_expr_mut(body);
@@ -933,7 +931,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
933931
});
934932
let coroutine_option =
935933
this.coroutine_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
936-
this.current_item = prev;
937934
(body_id, coroutine_option)
938935
});
939936

@@ -1019,7 +1016,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10191016
let outer_decl =
10201017
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
10211018

1022-
let body = self.with_new_scopes(|this| {
1019+
let body = self.with_new_scopes(fn_decl_span, |this| {
10231020
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
10241021
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
10251022
this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
@@ -1041,7 +1038,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10411038
async_ret_ty,
10421039
body.span,
10431040
hir::CoroutineSource::Closure,
1044-
|this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
1041+
|this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
10451042
);
10461043
let hir_id = this.lower_node_id(inner_closure_id);
10471044
this.maybe_forward_track_caller(body.span, closure_hir_id, hir_id);
@@ -1481,7 +1478,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14811478
match self.coroutine_kind {
14821479
Some(hir::CoroutineKind::Gen(_)) => {}
14831480
Some(hir::CoroutineKind::Async(_)) => {
1484-
return hir::ExprKind::Err(self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }));
1481+
return hir::ExprKind::Err(
1482+
self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }),
1483+
);
14851484
}
14861485
Some(hir::CoroutineKind::Coroutine) | None => {
14871486
if !self.tcx.features().coroutines {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
202202
body,
203203
..
204204
}) => {
205-
self.with_new_scopes(|this| {
206-
this.current_item = Some(ident.span);
207-
205+
self.with_new_scopes(ident.span, |this| {
208206
// Note: we don't need to change the return type from `T` to
209207
// `impl Future<Output = T>` here because lower_body
210208
// only cares about the input argument patterns in the function
@@ -837,7 +835,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
837835
},
838836
),
839837
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
840-
self.current_item = Some(i.span);
841838
let asyncness = sig.header.asyncness;
842839
let body_id = self.lower_maybe_async_body(
843840
i.span,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
878878
result
879879
}
880880

881-
fn with_new_scopes<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
881+
fn with_new_scopes<T>(&mut self, scope_span: Span, f: impl FnOnce(&mut Self) -> T) -> T {
882+
let current_item = self.current_item;
883+
self.current_item = Some(scope_span);
884+
882885
let was_in_loop_condition = self.is_in_loop_condition;
883886
self.is_in_loop_condition = false;
884887

@@ -890,6 +893,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
890893

891894
self.is_in_loop_condition = was_in_loop_condition;
892895

896+
self.current_item = current_item;
897+
893898
ret
894899
}
895900

@@ -1239,7 +1244,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12391244
tokens: None,
12401245
};
12411246

1242-
let ct = self.with_new_scopes(|this| hir::AnonConst {
1247+
let ct = self.with_new_scopes(span, |this| hir::AnonConst {
12431248
def_id,
12441249
hir_id: this.lower_node_id(node_id),
12451250
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
@@ -2246,7 +2251,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22462251
}
22472252

22482253
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
2249-
self.with_new_scopes(|this| hir::AnonConst {
2254+
self.with_new_scopes(c.value.span, |this| hir::AnonConst {
22502255
def_id: this.local_def_id(c.id),
22512256
hir_id: this.lower_node_id(c.id),
22522257
body: this.lower_const_body(c.value.span, Some(&c.value)),
Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
11
error[E0728]: `await` is only allowed inside `async` functions and blocks
22
--> $DIR/issue-70594.rs:4:12
33
|
4-
LL | async fn fun() {
5-
| --- this is not `async`
64
LL | [1; ().await];
7-
| ^^^^^ only allowed inside `async` functions and blocks
5+
| ---^^^^^
6+
| | |
7+
| | only allowed inside `async` functions and blocks
8+
| this is not `async`
89

9-
error[E0744]: `.await` is not allowed in a `const`
10-
--> $DIR/issue-70594.rs:4:9
11-
|
12-
LL | [1; ().await];
13-
| ^^^^^^^^
14-
15-
error[E0744]: `.await` is not allowed in a `const`
16-
--> $DIR/issue-70594.rs:4:12
17-
|
18-
LL | [1; ().await];
19-
| ^^^^^
20-
21-
error[E0277]: `()` is not a future
22-
--> $DIR/issue-70594.rs:4:12
23-
|
24-
LL | [1; ().await];
25-
| -^^^^^
26-
| ||
27-
| |`()` is not a future
28-
| help: remove the `.await`
29-
|
30-
= help: the trait `Future` is not implemented for `()`
31-
= note: () must be a future or must implement `IntoFuture` to be awaited
32-
= note: required for `()` to implement `IntoFuture`
33-
34-
error: aborting due to 4 previous errors
10+
error: aborting due to 1 previous error
3511

36-
Some errors have detailed explanations: E0277, E0728, E0744.
37-
For more information about an error, try `rustc --explain E0277`.
12+
For more information about this error, try `rustc --explain E0728`.

tests/ui/async-await/issues/issue-62009-1.stderr

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ LL | fn main() {
2424
LL | (|_| 2333).await;
2525
| ^^^^^ only allowed inside `async` functions and blocks
2626

27-
error[E0277]: `{closure@$DIR/issue-62009-1.rs:12:6: 12:9}` is not a future
28-
--> $DIR/issue-62009-1.rs:12:16
29-
|
30-
LL | (|_| 2333).await;
31-
| -^^^^^
32-
| ||
33-
| |`{closure@$DIR/issue-62009-1.rs:12:6: 12:9}` is not a future
34-
| help: remove the `.await`
35-
|
36-
= help: the trait `Future` is not implemented for closure `{closure@$DIR/issue-62009-1.rs:12:6: 12:9}`
37-
= note: {closure@$DIR/issue-62009-1.rs:12:6: 12:9} must be a future or must implement `IntoFuture` to be awaited
38-
= note: required for `{closure@$DIR/issue-62009-1.rs:12:6: 12:9}` to implement `IntoFuture`
39-
40-
error: aborting due to 4 previous errors
27+
error: aborting due to 3 previous errors
4128

42-
Some errors have detailed explanations: E0277, E0728.
43-
For more information about an error, try `rustc --explain E0277`.
29+
For more information about this error, try `rustc --explain E0728`.

0 commit comments

Comments
 (0)