Skip to content

Commit f2c500b

Browse files
Fix spans for bad await in inline const
1 parent a76d2e1 commit f2c500b

File tree

5 files changed

+26
-64
lines changed

5 files changed

+26
-64
lines changed

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+9-10
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(
@@ -941,9 +941,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
941941
) -> hir::ExprKind<'hir> {
942942
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
943943

944-
let (body_id, coroutine_option) = self.with_new_scopes(move |this| {
945-
let prev = this.current_item;
946-
this.current_item = Some(fn_decl_span);
944+
let (body_id, coroutine_option) = self.with_new_scopes(fn_decl_span, move |this| {
947945
let mut coroutine_kind = None;
948946
let body_id = this.lower_fn_body(decl, |this| {
949947
let e = this.lower_expr_mut(body);
@@ -952,7 +950,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
952950
});
953951
let coroutine_option =
954952
this.coroutine_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
955-
this.current_item = prev;
956953
(body_id, coroutine_option)
957954
});
958955

@@ -1038,7 +1035,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10381035
let outer_decl =
10391036
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
10401037

1041-
let body = self.with_new_scopes(|this| {
1038+
let body = self.with_new_scopes(fn_decl_span, |this| {
10421039
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
10431040
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
10441041
this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
@@ -1060,7 +1057,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10601057
async_ret_ty,
10611058
body.span,
10621059
hir::CoroutineSource::Closure,
1063-
|this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
1060+
|this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
10641061
);
10651062
let hir_id = this.lower_node_id(inner_closure_id);
10661063
this.maybe_forward_track_caller(body.span, closure_hir_id, hir_id);
@@ -1500,7 +1497,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
15001497
match self.coroutine_kind {
15011498
Some(hir::CoroutineKind::Gen(_)) => {}
15021499
Some(hir::CoroutineKind::Async(_)) => {
1503-
return hir::ExprKind::Err(self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }));
1500+
return hir::ExprKind::Err(
1501+
self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }),
1502+
);
15041503
}
15051504
Some(hir::CoroutineKind::Coroutine) | None => {
15061505
if !self.tcx.features().coroutines {

Diff for: compiler/rustc_ast_lowering/src/item.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
268268
body,
269269
..
270270
}) => {
271-
self.with_new_scopes(|this| {
272-
this.current_item = Some(ident.span);
273-
271+
self.with_new_scopes(ident.span, |this| {
274272
// Note: we don't need to change the return type from `T` to
275273
// `impl Future<Output = T>` here because lower_body
276274
// only cares about the input argument patterns in the function
@@ -867,7 +865,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
867865
},
868866
),
869867
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
870-
self.current_item = Some(i.span);
871868
let asyncness = sig.header.asyncness;
872869
let body_id = self.lower_maybe_async_body(
873870
i.span,

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
839839
result
840840
}
841841

842-
fn with_new_scopes<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
842+
fn with_new_scopes<T>(&mut self, scope_span: Span, f: impl FnOnce(&mut Self) -> T) -> T {
843+
let current_item = self.current_item;
844+
self.current_item = Some(scope_span);
845+
843846
let was_in_loop_condition = self.is_in_loop_condition;
844847
self.is_in_loop_condition = false;
845848

@@ -851,6 +854,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
851854

852855
self.is_in_loop_condition = was_in_loop_condition;
853856

857+
self.current_item = current_item;
858+
854859
ret
855860
}
856861

@@ -1200,7 +1205,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12001205
tokens: None,
12011206
};
12021207

1203-
let ct = self.with_new_scopes(|this| hir::AnonConst {
1208+
let ct = self.with_new_scopes(span, |this| hir::AnonConst {
12041209
def_id,
12051210
hir_id: this.lower_node_id(node_id),
12061211
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
@@ -2207,7 +2212,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22072212
}
22082213

22092214
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
2210-
self.with_new_scopes(|this| hir::AnonConst {
2215+
self.with_new_scopes(c.value.span, |this| hir::AnonConst {
22112216
def_id: this.local_def_id(c.id),
22122217
hir_id: this.lower_node_id(c.id),
22132218
body: this.lower_const_body(c.value.span, Some(&c.value)),

Diff for: tests/ui/async-await/issue-70594.stderr

+6-31
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`.

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

+2-16
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)