Skip to content

Commit 7e9496c

Browse files
Only reachable traits
1 parent 0cd0b21 commit 7e9496c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

compiler/rustc_lint/src/async_fn_in_trait.rs

+6
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,16 @@ impl<'tcx> LateLintPass<'tcx> for AsyncFnInTrait {
9494
if let hir::TraitItemKind::Fn(sig, body) = item.kind
9595
&& let hir::IsAsync::Async(async_span) = sig.header.asyncness
9696
{
97+
// RTN can be used to bound `async fn` in traits in a better way than "always"
9798
if cx.tcx.features().return_type_notation {
9899
return;
99100
}
100101

102+
// Only need to think about library implications of reachable traits
103+
if !cx.tcx.effective_visibilities(()).is_reachable(item.owner_id.def_id) {
104+
return;
105+
}
106+
101107
let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(def, ..), .. }) =
102108
sig.decl.output
103109
else {

tests/ui/async-await/in-trait/warn.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
#![feature(async_fn_in_trait)]
44
#![deny(async_fn_in_trait)]
55

6-
trait Foo {
6+
pub trait Foo {
77
async fn not_send();
8-
//~^ ERROR
8+
//~^ ERROR use of `async fn` in public traits is discouraged
9+
}
10+
11+
mod private {
12+
pub trait FooUnreachable {
13+
async fn not_send();
14+
// No warning
15+
}
16+
}
17+
18+
pub(crate) trait FooCrate {
19+
async fn not_send();
20+
// No warning
921
}
1022

1123
fn main() {}

0 commit comments

Comments
 (0)