Skip to content

Commit 736ef39

Browse files
committed
Auto merge of rust-lang#107254 - chenyukang:yukang/fix-107113-wrong-sugg-in-macro, r=estebank
Avoid wrong code suggesting for attribute macro Fixes rust-lang#107113 r? `@estebank`
2 parents 8131b97 + ac25636 commit 736ef39

File tree

9 files changed

+85
-5
lines changed

9 files changed

+85
-5
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+3
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
880880
expected: Ty<'tcx>,
881881
expr_ty: Ty<'tcx>,
882882
) -> bool {
883+
if in_external_macro(self.tcx.sess, expr.span) {
884+
return false;
885+
}
883886
if let ty::Adt(expected_adt, args) = expected.kind() {
884887
if let hir::ExprKind::Field(base, ident) = expr.kind {
885888
let base_ty = self.typeck_results.borrow().expr_ty(base);

compiler/rustc_middle/src/lint.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
449449
match expn_data.kind {
450450
ExpnKind::Root
451451
| ExpnKind::Desugaring(
452-
DesugaringKind::ForLoop | DesugaringKind::WhileLoop | DesugaringKind::OpaqueTy,
452+
DesugaringKind::ForLoop
453+
| DesugaringKind::WhileLoop
454+
| DesugaringKind::OpaqueTy
455+
| DesugaringKind::Async
456+
| DesugaringKind::Await,
453457
) => false,
454458
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
455459
ExpnKind::Macro(MacroKind::Bang, _) => {
@@ -459,3 +463,12 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
459463
ExpnKind::Macro { .. } => true, // definitely a plugin
460464
}
461465
}
466+
467+
/// Return whether `span` is generated by `async` or `await`.
468+
pub fn is_from_async_await(span: Span) -> bool {
469+
let expn_data = span.ctxt().outer_expn_data();
470+
match expn_data.kind {
471+
ExpnKind::Desugaring(DesugaringKind::Async | DesugaringKind::Await) => true,
472+
_ => false,
473+
}
474+
}

src/tools/clippy/clippy_lints/src/redundant_locals.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::is_from_proc_macro;
33
use clippy_utils::ty::needs_ordered_drop;
44
use rustc_hir::def::Res;
5-
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
5+
use rustc_hir::{
6+
BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath,
7+
};
68
use rustc_lint::{LateContext, LateLintPass, LintContext};
7-
use rustc_middle::lint::in_external_macro;
9+
use rustc_middle::lint::{in_external_macro, is_from_async_await};
810
use rustc_session::{declare_lint_pass, declare_tool_lint};
911
use rustc_span::symbol::Ident;
1012

@@ -65,6 +67,9 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
6567
// the local is user-controlled
6668
if !in_external_macro(cx.sess(), local.span);
6769
if !is_from_proc_macro(cx, expr);
70+
// Async function parameters are lowered into the closure body, so we can't lint them.
71+
// see `lower_maybe_async_body` in `rust_ast_lowering`
72+
if !is_from_async_await(local.span);
6873
then {
6974
span_lint_and_help(
7075
cx,
@@ -93,7 +98,12 @@ fn find_binding(pat: &Pat<'_>, name: Ident) -> Option<BindingAnnotation> {
9398
}
9499

95100
/// Check if a rebinding of a local affects the code's drop behavior.
96-
fn affects_drop_behavior<'tcx>(cx: &LateContext<'tcx>, bind: HirId, rebind: HirId, rebind_expr: &Expr<'tcx>) -> bool {
101+
fn affects_drop_behavior<'tcx>(
102+
cx: &LateContext<'tcx>,
103+
bind: HirId,
104+
rebind: HirId,
105+
rebind_expr: &Expr<'tcx>,
106+
) -> bool {
97107
let hir = cx.tcx.hir();
98108

99109
// the rebinding is in a different scope than the original binding
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
// edition:2018
3+
fn _consume_reference<T: ?Sized>(_: &T) {}
4+
5+
async fn _foo() {
6+
_consume_reference::<i32>(&Box::new(7_i32));
7+
_consume_reference::<i32>(&*async { Box::new(7_i32) }.await);
8+
//~^ ERROR mismatched types
9+
_consume_reference::<[i32]>(&vec![7_i32]);
10+
_consume_reference::<[i32]>(&async { vec![7_i32] }.await);
11+
}
12+
13+
fn main() { }

tests/ui/coercion/coerce-block-tail-83783.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// check-fail
1+
// run-rustfix
22
// edition:2018
33
fn _consume_reference<T: ?Sized>(_: &T) {}
44

tests/ui/coercion/coerce-block-tail-83783.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ LL | _consume_reference::<i32>(&async { Box::new(7_i32) }.await);
66
|
77
= note: expected type `i32`
88
found struct `Box<i32>`
9+
help: consider unboxing the value
10+
|
11+
LL | _consume_reference::<i32>(&*async { Box::new(7_i32) }.await);
12+
| +
913

1014
error: aborting due to previous error
1115

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
8+
use proc_macro::TokenStream;
9+
10+
#[proc_macro_attribute]
11+
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
12+
"fn main() -> std::io::Result<()> { () } ".parse().unwrap()
13+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// edition:2021
2+
// aux-build:issue-107113.rs
3+
4+
#[macro_use]
5+
extern crate issue_107113;
6+
7+
#[issue_107113::main] //~ ERROR mismatched types [E0308]
8+
async fn main() -> std::io::Result<()> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-107113-wrap.rs:7:1
3+
|
4+
LL | #[issue_107113::main]
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| expected `Result<(), Error>`, found `()`
8+
| expected `Result<(), std::io::Error>` because of return type
9+
|
10+
= note: expected enum `Result<(), std::io::Error>`
11+
found unit type `()`
12+
= note: this error originates in the attribute macro `issue_107113::main` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)