Fix spans for bad await in inline const

This commit is contained in:
Michael Goulet 2023-11-28 19:06:23 +00:00
parent a76d2e1fd1
commit f2c500bb43
5 changed files with 26 additions and 64 deletions

View File

@ -72,7 +72,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let kind = match &e.kind { let kind = match &e.kind {
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
ExprKind::ConstBlock(c) => { ExprKind::ConstBlock(c) => {
let c = self.with_new_scopes(|this| hir::ConstBlock { let c = self.with_new_scopes(c.value.span, |this| hir::ConstBlock {
def_id: this.local_def_id(c.id), def_id: this.local_def_id(c.id),
hir_id: this.lower_node_id(c.id), hir_id: this.lower_node_id(c.id),
body: this.lower_const_body(c.value.span, Some(&c.value)), body: this.lower_const_body(c.value.span, Some(&c.value)),
@ -189,7 +189,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
None, None,
e.span, e.span,
hir::CoroutineSource::Block, hir::CoroutineSource::Block,
|this| this.with_new_scopes(|this| this.lower_block_expr(block)), |this| this.with_new_scopes(e.span, |this| this.lower_block_expr(block)),
), ),
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr), ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
ExprKind::Closure(box Closure { ExprKind::Closure(box Closure {
@ -323,7 +323,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
None, None,
e.span, e.span,
hir::CoroutineSource::Block, hir::CoroutineSource::Block,
|this| this.with_new_scopes(|this| this.lower_block_expr(block)), |this| this.with_new_scopes(e.span, |this| this.lower_block_expr(block)),
), ),
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()), ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
ExprKind::Err => hir::ExprKind::Err( ExprKind::Err => hir::ExprKind::Err(
@ -941,9 +941,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
) -> hir::ExprKind<'hir> { ) -> hir::ExprKind<'hir> {
let (binder_clause, generic_params) = self.lower_closure_binder(binder); let (binder_clause, generic_params) = self.lower_closure_binder(binder);
let (body_id, coroutine_option) = self.with_new_scopes(move |this| { let (body_id, coroutine_option) = self.with_new_scopes(fn_decl_span, move |this| {
let prev = this.current_item;
this.current_item = Some(fn_decl_span);
let mut coroutine_kind = None; let mut coroutine_kind = None;
let body_id = this.lower_fn_body(decl, |this| { let body_id = this.lower_fn_body(decl, |this| {
let e = this.lower_expr_mut(body); let e = this.lower_expr_mut(body);
@ -952,7 +950,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
}); });
let coroutine_option = let coroutine_option =
this.coroutine_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability); this.coroutine_movability_for_fn(decl, fn_decl_span, coroutine_kind, movability);
this.current_item = prev;
(body_id, coroutine_option) (body_id, coroutine_option)
}); });
@ -1038,7 +1035,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let outer_decl = let outer_decl =
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) }; FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
let body = self.with_new_scopes(|this| { let body = self.with_new_scopes(fn_decl_span, |this| {
// FIXME(cramertj): allow `async` non-`move` closures with arguments. // FIXME(cramertj): allow `async` non-`move` closures with arguments.
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() { if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span }); this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
@ -1060,7 +1057,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
async_ret_ty, async_ret_ty,
body.span, body.span,
hir::CoroutineSource::Closure, hir::CoroutineSource::Closure,
|this| this.with_new_scopes(|this| this.lower_expr_mut(body)), |this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
); );
let hir_id = this.lower_node_id(inner_closure_id); let hir_id = this.lower_node_id(inner_closure_id);
this.maybe_forward_track_caller(body.span, closure_hir_id, hir_id); this.maybe_forward_track_caller(body.span, closure_hir_id, hir_id);
@ -1500,7 +1497,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
match self.coroutine_kind { match self.coroutine_kind {
Some(hir::CoroutineKind::Gen(_)) => {} Some(hir::CoroutineKind::Gen(_)) => {}
Some(hir::CoroutineKind::Async(_)) => { Some(hir::CoroutineKind::Async(_)) => {
return hir::ExprKind::Err(self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span })); return hir::ExprKind::Err(
self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }),
);
} }
Some(hir::CoroutineKind::Coroutine) | None => { Some(hir::CoroutineKind::Coroutine) | None => {
if !self.tcx.features().coroutines { if !self.tcx.features().coroutines {

View File

@ -268,9 +268,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
body, body,
.. ..
}) => { }) => {
self.with_new_scopes(|this| { self.with_new_scopes(ident.span, |this| {
this.current_item = Some(ident.span);
// Note: we don't need to change the return type from `T` to // Note: we don't need to change the return type from `T` to
// `impl Future<Output = T>` here because lower_body // `impl Future<Output = T>` here because lower_body
// only cares about the input argument patterns in the function // only cares about the input argument patterns in the function
@ -867,7 +865,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
}, },
), ),
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => { AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
self.current_item = Some(i.span);
let asyncness = sig.header.asyncness; let asyncness = sig.header.asyncness;
let body_id = self.lower_maybe_async_body( let body_id = self.lower_maybe_async_body(
i.span, i.span,

View File

@ -839,7 +839,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
result result
} }
fn with_new_scopes<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T { fn with_new_scopes<T>(&mut self, scope_span: Span, f: impl FnOnce(&mut Self) -> T) -> T {
let current_item = self.current_item;
self.current_item = Some(scope_span);
let was_in_loop_condition = self.is_in_loop_condition; let was_in_loop_condition = self.is_in_loop_condition;
self.is_in_loop_condition = false; self.is_in_loop_condition = false;
@ -851,6 +854,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.is_in_loop_condition = was_in_loop_condition; self.is_in_loop_condition = was_in_loop_condition;
self.current_item = current_item;
ret ret
} }
@ -1200,7 +1205,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
tokens: None, tokens: None,
}; };
let ct = self.with_new_scopes(|this| hir::AnonConst { let ct = self.with_new_scopes(span, |this| hir::AnonConst {
def_id, def_id,
hir_id: this.lower_node_id(node_id), hir_id: this.lower_node_id(node_id),
body: this.lower_const_body(path_expr.span, Some(&path_expr)), body: this.lower_const_body(path_expr.span, Some(&path_expr)),
@ -2207,7 +2212,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} }
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst { fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
self.with_new_scopes(|this| hir::AnonConst { self.with_new_scopes(c.value.span, |this| hir::AnonConst {
def_id: this.local_def_id(c.id), def_id: this.local_def_id(c.id),
hir_id: this.lower_node_id(c.id), hir_id: this.lower_node_id(c.id),
body: this.lower_const_body(c.value.span, Some(&c.value)), body: this.lower_const_body(c.value.span, Some(&c.value)),

View File

@ -1,37 +1,12 @@
error[E0728]: `await` is only allowed inside `async` functions and blocks error[E0728]: `await` is only allowed inside `async` functions and blocks
--> $DIR/issue-70594.rs:4:12 --> $DIR/issue-70594.rs:4:12
| |
LL | async fn fun() {
| --- this is not `async`
LL | [1; ().await]; LL | [1; ().await];
| ^^^^^ only allowed inside `async` functions and blocks | ---^^^^^
| | |
| | only allowed inside `async` functions and blocks
| this is not `async`
error[E0744]: `.await` is not allowed in a `const` error: aborting due to 1 previous error
--> $DIR/issue-70594.rs:4:9
|
LL | [1; ().await];
| ^^^^^^^^
error[E0744]: `.await` is not allowed in a `const` For more information about this error, try `rustc --explain E0728`.
--> $DIR/issue-70594.rs:4:12
|
LL | [1; ().await];
| ^^^^^
error[E0277]: `()` is not a future
--> $DIR/issue-70594.rs:4:12
|
LL | [1; ().await];
| -^^^^^
| ||
| |`()` is not a future
| help: remove the `.await`
|
= help: the trait `Future` is not implemented for `()`
= note: () must be a future or must implement `IntoFuture` to be awaited
= note: required for `()` to implement `IntoFuture`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0277, E0728, E0744.
For more information about an error, try `rustc --explain E0277`.

View File

@ -24,20 +24,6 @@ LL | fn main() {
LL | (|_| 2333).await; LL | (|_| 2333).await;
| ^^^^^ only allowed inside `async` functions and blocks | ^^^^^ only allowed inside `async` functions and blocks
error[E0277]: `{closure@$DIR/issue-62009-1.rs:12:6: 12:9}` is not a future error: aborting due to 3 previous errors
--> $DIR/issue-62009-1.rs:12:16
|
LL | (|_| 2333).await;
| -^^^^^
| ||
| |`{closure@$DIR/issue-62009-1.rs:12:6: 12:9}` is not a future
| help: remove the `.await`
|
= help: the trait `Future` is not implemented for closure `{closure@$DIR/issue-62009-1.rs:12:6: 12:9}`
= note: {closure@$DIR/issue-62009-1.rs:12:6: 12:9} must be a future or must implement `IntoFuture` to be awaited
= note: required for `{closure@$DIR/issue-62009-1.rs:12:6: 12:9}` to implement `IntoFuture`
error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0728`.
Some errors have detailed explanations: E0277, E0728.
For more information about an error, try `rustc --explain E0277`.