Skip to content

Commit

Permalink
Only reachable traits
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Sep 30, 2023
1 parent 8e429e0 commit 353683e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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 @@ -92,10 +92,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() {}

0 comments on commit 353683e

Please sign in to comment.