Skip to content

Commit

Permalink
Auto merge of rust-lang#81541 - Aaron1011:early-lint-async-fn, r=petr…
Browse files Browse the repository at this point in the history
…ochenkov

Fix early lints inside an async desugaring

Fixes rust-lang#81531

When we buffer an early lint for a macro invocation,
we need to determine which NodeId to take the lint level from.
Currently, we use the NodeId of the closest def parent. However, if
the macro invocation is inside the desugared closure from an `async fn`
or async closure, that NodeId does not actually exist in the AST.

This commit uses the parent of a desugared closure when computing
`lint_node_id`, which is something that actually exists in the AST (an
`async fn` or async closure).
  • Loading branch information
bors committed Feb 2, 2021
2 parents 3182375 + a74b2fb commit 3682750
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
16 changes: 16 additions & 0 deletions compiler/rustc_lint/src/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
run_early_pass!(self, check_fn, fk, span, id);
self.check_id(id);
ast_visit::walk_fn(self, fk, span);

// Explicitly check for lints associated with 'closure_id', since
// it does not have a corresponding AST node
if let ast_visit::FnKind::Fn(_, _, sig, _, _) = fk {
if let ast::Async::Yes { closure_id, .. } = sig.header.asyncness {
self.check_id(closure_id);
}
}
run_early_pass!(self, check_fn_post, fk, span, id);
}

Expand Down Expand Up @@ -208,6 +216,14 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>

fn visit_expr_post(&mut self, e: &'a ast::Expr) {
run_early_pass!(self, check_expr_post, e);

// Explicitly check for lints associated with 'closure_id', since
// it does not have a corresponding AST node
if let ast::ExprKind::Closure(_, asyncness, ..) = e.kind {
if let ast::Async::Yes { closure_id, .. } = asyncness {
self.check_id(closure_id);
}
}
}

fn visit_generic_arg(&mut self, arg: &'a ast::GenericArg) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// check-pass
// edition:2018
#![warn(semicolon_in_expressions_from_macros)]

#[allow(dead_code)]
Expand All @@ -11,6 +12,11 @@ macro_rules! foo {
}
}

#[allow(semicolon_in_expressions_from_macros)]
async fn bar() {
foo!(first);
}

fn main() {
// This `allow` doesn't work
#[allow(semicolon_in_expressions_from_macros)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: trailing semicolon in macro used in expression position
--> $DIR/semicolon-in-expressions-from-macros.rs:7:13
--> $DIR/semicolon-in-expressions-from-macros.rs:8:13
|
LL | true;
| ^
Expand All @@ -8,7 +8,7 @@ LL | foo!(first)
| ----------- in this macro invocation
|
note: the lint level is defined here
--> $DIR/semicolon-in-expressions-from-macros.rs:2:9
--> $DIR/semicolon-in-expressions-from-macros.rs:3:9
|
LL | #![warn(semicolon_in_expressions_from_macros)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -17,7 +17,7 @@ LL | #![warn(semicolon_in_expressions_from_macros)]
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

warning: trailing semicolon in macro used in expression position
--> $DIR/semicolon-in-expressions-from-macros.rs:7:13
--> $DIR/semicolon-in-expressions-from-macros.rs:8:13
|
LL | true;
| ^
Expand Down

0 comments on commit 3682750

Please sign in to comment.