Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async_fn_in_trait lint #116184

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only reachable traits
  • Loading branch information
compiler-errors committed Oct 3, 2023
commit 90dfa24415d4f29e1970bf215458ccefec60f85f
6 changes: 6 additions & 0 deletions compiler/rustc_lint/src/async_fn_in_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ impl<'tcx> LateLintPass<'tcx> for AsyncFnInTrait {
if let hir::TraitItemKind::Fn(sig, body) = item.kind
&& let hir::IsAsync::Async(async_span) = sig.header.asyncness
{
// RTN can be used to bound `async fn` in traits in a better way than "always"
if cx.tcx.features().return_type_notation {
return;
}

// Only need to think about library implications of reachable traits
if !cx.tcx.effective_visibilities(()).is_reachable(item.owner_id.def_id) {
return;
}

let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(def, ..), .. }) =
sig.decl.output
else {
Expand Down
16 changes: 14 additions & 2 deletions tests/ui/async-await/in-trait/warn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
#![feature(async_fn_in_trait)]
#![deny(async_fn_in_trait)]

trait Foo {
pub trait Foo {
async fn not_send();
//~^ ERROR
//~^ ERROR use of `async fn` in public traits is discouraged
}

mod private {
pub trait FooUnreachable {
async fn not_send();
// No warning
}
}

pub(crate) trait FooCrate {
async fn not_send();
// No warning
}

fn main() {}