Skip to content

Commit 4d05da4

Browse files
Don't emit refining_impl_trait for private items
1 parent 4745d34 commit 4d05da4

File tree

15 files changed

+98
-47
lines changed

15 files changed

+98
-47
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+28
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,22 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
2323
if !tcx.impl_method_has_trait_impl_trait_tys(impl_m.def_id) {
2424
return;
2525
}
26+
// crate-private traits don't have any library guarantees, there's no need to do this check.
27+
if !tcx.visibility(trait_m.container_id(tcx)).is_public() {
28+
return;
29+
}
2630

31+
// If a type in the trait ref is private, then there's also no reason to to do this check.
2732
let impl_def_id = impl_m.container_id(tcx);
33+
for arg in impl_trait_ref.args {
34+
if let Some(ty) = arg.as_type()
35+
&& let Some(self_visibility) = type_visibility(tcx, ty)
36+
&& !self_visibility.is_public()
37+
{
38+
return;
39+
}
40+
}
41+
2842
let impl_m_args = ty::GenericArgs::identity_for_item(tcx, impl_m.def_id);
2943
let trait_m_to_impl_m_args = impl_m_args.rebase_onto(tcx, impl_def_id, impl_trait_ref.args);
3044
let bound_trait_m_sig = tcx.fn_sig(trait_m.def_id).instantiate(tcx, trait_m_to_impl_m_args);
@@ -281,3 +295,17 @@ fn report_mismatched_rpitit_signature<'tcx>(
281295
},
282296
);
283297
}
298+
299+
fn type_visibility<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<ty::Visibility<DefId>> {
300+
match *ty.kind() {
301+
ty::Ref(_, ty, _) => type_visibility(tcx, ty),
302+
ty::Adt(def, args) => {
303+
if def.is_fundamental() {
304+
type_visibility(tcx, args.type_at(0))
305+
} else {
306+
Some(tcx.visibility(def.did()))
307+
}
308+
}
309+
_ => None,
310+
}
311+
}

compiler/rustc_lint_defs/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4496,7 +4496,7 @@ declare_lint! {
44964496
///
44974497
/// use std::fmt::Display;
44984498
///
4499-
/// trait AsDisplay {
4499+
/// pub trait AsDisplay {
45004500
/// fn as_display(&self) -> impl Display;
45014501
/// }
45024502
///

tests/ui/async-await/in-trait/async-example-desugared-extra.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// edition: 2021
33

44
#![feature(async_fn_in_trait)]
5-
#![feature(return_position_impl_trait_in_trait)]
5+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
66
#![allow(incomplete_features)]
77

88
use std::future::Future;
99
use std::pin::Pin;
1010
use std::task::Poll;
1111

12-
trait MyTrait {
12+
pub trait MyTrait {
1313
async fn foo(&self) -> i32;
1414
}
1515

@@ -27,7 +27,7 @@ impl Future for MyFuture {
2727
}
2828

2929
impl MyTrait for i32 {
30-
#[allow(refining_impl_trait)]
30+
#[expect(refining_impl_trait)]
3131
fn foo(&self) -> impl Future<Output = i32> + Clone {
3232
MyFuture(*self)
3333
}

tests/ui/impl-trait/in-trait/auxiliary/rpitit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(return_position_impl_trait_in_trait)]
1+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
22

33
use std::ops::Deref;
44

@@ -8,7 +8,7 @@ pub trait Foo {
88

99
pub struct Foreign;
1010
impl Foo for Foreign {
11-
#[allow(refining_impl_trait)]
11+
#[expect(refining_impl_trait)]
1212
fn bar(self) -> &'static () {
1313
&()
1414
}

tests/ui/impl-trait/in-trait/bad-item-bound-within-rpitit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![feature(return_position_impl_trait_in_trait)]
44

5-
trait Iterable {
5+
pub trait Iterable {
66
type Item<'a>
77
where
88
Self: 'a;

tests/ui/impl-trait/in-trait/deep-match-works.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// check-pass
22

3-
#![feature(return_position_impl_trait_in_trait)]
3+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
44
#![allow(incomplete_features)]
55

6-
struct Wrapper<T>(T);
6+
pub struct Wrapper<T>(T);
77

8-
trait Foo {
8+
pub trait Foo {
99
fn bar() -> Wrapper<impl Sized>;
1010
}
1111

1212
impl Foo for () {
13-
#[allow(refining_impl_trait)]
13+
#[expect(refining_impl_trait)]
1414
fn bar() -> Wrapper<i32> {
1515
Wrapper(0)
1616
}

tests/ui/impl-trait/in-trait/foreign.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
// check-pass
22
// aux-build: rpitit.rs
33

4+
#![feature(lint_reasons)]
5+
46
extern crate rpitit;
57

68
use rpitit::{Foo, Foreign};
79
use std::sync::Arc;
810

911
// Implement an RPITIT from another crate.
10-
struct Local;
12+
pub struct Local;
1113
impl Foo for Local {
12-
#[allow(refining_impl_trait)]
14+
#[expect(refining_impl_trait)]
15+
fn bar(self) -> Arc<String> {
16+
Arc::new(String::new())
17+
}
18+
}
19+
20+
struct LocalIgnoreRefining;
21+
impl Foo for LocalIgnoreRefining {
22+
#[deny(refining_impl_trait)]
1323
fn bar(self) -> Arc<String> {
1424
Arc::new(String::new())
1525
}
@@ -24,4 +34,5 @@ fn main() {
2434
let &() = Foreign.bar();
2535

2636
let x: Arc<String> = Local.bar();
37+
let x: Arc<String> = LocalIgnoreRefining.bar();
2738
}

tests/ui/impl-trait/in-trait/nested-rpitit.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
// check-pass
22

3-
#![feature(return_position_impl_trait_in_trait)]
3+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
44
#![allow(incomplete_features)]
55

66
use std::fmt::Display;
77
use std::ops::Deref;
88

9-
trait Foo {
9+
pub trait Foo {
1010
fn bar(self) -> impl Deref<Target = impl Display + ?Sized>;
1111
}
1212

13-
struct A;
13+
pub struct A;
1414

1515
impl Foo for A {
16-
#[allow(refining_impl_trait)]
16+
#[expect(refining_impl_trait)]
1717
fn bar(self) -> &'static str {
1818
"Hello, world"
1919
}
2020
}
2121

22-
struct B;
22+
pub struct B;
2323

2424
impl Foo for B {
25-
#[allow(refining_impl_trait)]
25+
#[expect(refining_impl_trait)]
2626
fn bar(self) -> Box<i32> {
2727
Box::new(42)
2828
}

tests/ui/impl-trait/in-trait/refine.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
#![feature(return_position_impl_trait_in_trait, async_fn_in_trait)]
22
#![deny(refining_impl_trait)]
33

4-
trait Foo {
4+
pub trait Foo {
55
fn bar() -> impl Sized;
66
}
77

8-
struct A;
8+
pub struct A;
99
impl Foo for A {
1010
fn bar() -> impl Copy {}
1111
//~^ ERROR impl method signature does not match trait method signature
1212
}
1313

14-
struct B;
14+
pub struct B;
1515
impl Foo for B {
1616
fn bar() {}
1717
//~^ ERROR impl method signature does not match trait method signature
1818
}
1919

20-
struct C;
20+
pub struct C;
2121
impl Foo for C {
2222
fn bar() -> () {}
2323
//~^ ERROR impl method signature does not match trait method signature
2424
}
2525

26-
trait Late {
26+
struct Private;
27+
impl Foo for Private {
28+
fn bar() -> () {}
29+
}
30+
31+
pub trait Arg<A> {
32+
fn bar() -> impl Sized;
33+
}
34+
impl Arg<Private> for A {
35+
fn bar() -> () {}
36+
}
37+
38+
pub trait Late {
2739
fn bar<'a>(&'a self) -> impl Sized + 'a;
2840
}
2941

30-
struct D;
42+
pub struct D;
3143
impl Late for D {
3244
fn bar(&self) -> impl Copy + '_ {}
3345
//~^ ERROR impl method signature does not match trait method signature

tests/ui/impl-trait/in-trait/refine.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ LL | fn bar() -> impl Sized {}
4646
| ~~~~~~~~~~
4747

4848
error: impl trait in impl method signature does not match trait method signature
49-
--> $DIR/refine.rs:32:27
49+
--> $DIR/refine.rs:44:27
5050
|
5151
LL | fn bar<'a>(&'a self) -> impl Sized + 'a;
5252
| --------------- return type from trait method defined here

tests/ui/impl-trait/in-trait/reveal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// check-pass
22

3-
#![feature(return_position_impl_trait_in_trait)]
3+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
44
#![allow(incomplete_features)]
55

6-
trait Foo {
6+
pub trait Foo {
77
fn f() -> Box<impl Sized>;
88
}
99

1010
impl Foo for () {
11-
#[allow(refining_impl_trait)]
11+
#[expect(refining_impl_trait)]
1212
fn f() -> Box<String> {
1313
Box::new(String::new())
1414
}

tests/ui/impl-trait/in-trait/rpitit-shadowed-by-missing-adt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// issue: 113903
22

3-
#![feature(return_position_impl_trait_in_trait)]
3+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
44

55
use std::ops::Deref;
66

@@ -10,7 +10,7 @@ pub trait Tr {
1010
}
1111

1212
impl Tr for () {
13-
#[allow(refining_impl_trait)]
13+
#[expect(refining_impl_trait)]
1414
fn w() -> &'static () {
1515
&()
1616
}

tests/ui/impl-trait/in-trait/signature-mismatch.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// revisions: success failure
33
//[success] check-pass
44

5-
#![feature(return_position_impl_trait_in_trait)]
5+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
66

77
use std::future::Future;
88

9-
trait Captures<'a> {}
9+
pub trait Captures<'a> {}
1010
impl<T> Captures<'_> for T {}
1111

12-
trait Captures2<'a, 'b> {}
12+
pub trait Captures2<'a, 'b> {}
1313
impl<T> Captures2<'_, '_> for T {}
1414

15-
trait AsyncTrait {
15+
pub trait AsyncTrait {
1616
#[cfg(success)]
1717
fn async_fn(&self, buff: &[u8]) -> impl Future<Output = Vec<u8>>;
1818

@@ -44,23 +44,23 @@ impl AsyncTrait for Struct {
4444
// Does not capture more lifetimes that trait def'n, since trait def'n
4545
// implicitly captures all in-scope lifetimes.
4646
#[cfg(success)]
47-
#[allow(refining_impl_trait)]
47+
#[expect(refining_impl_trait)]
4848
fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + 'a {
4949
async move { buff.to_vec() }
5050
}
5151

5252
// Does not capture more lifetimes that trait def'n, since trait def'n
5353
// implicitly captures all in-scope lifetimes.
5454
#[cfg(success)]
55-
#[allow(refining_impl_trait)]
55+
#[expect(refining_impl_trait)]
5656
fn async_fn_early<'a: 'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + 'a {
5757
async move { buff.to_vec() }
5858
}
5959

6060
// Does not capture more lifetimes that trait def'n, since trait def'n
6161
// implicitly captures all in-scope lifetimes.
6262
#[cfg(success)]
63-
#[allow(refining_impl_trait)]
63+
#[expect(refining_impl_trait)]
6464
fn async_fn_multiple<'a, 'b>(
6565
&'a self,
6666
buff: &'b [u8],

tests/ui/impl-trait/in-trait/specialization-substs-remap.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// check-pass
22

33
#![feature(specialization)]
4-
#![feature(return_position_impl_trait_in_trait)]
4+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
55
#![allow(incomplete_features)]
66

7-
trait Foo {
7+
pub trait Foo {
88
fn bar(&self) -> impl Sized;
99
}
1010

1111
impl<U> Foo for U
1212
where
1313
U: Copy,
1414
{
15-
#[allow(refining_impl_trait)]
15+
#[expect(refining_impl_trait)]
1616
fn bar(&self) -> U {
1717
*self
1818
}

tests/ui/impl-trait/in-trait/success.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
// check-pass
22

3-
#![feature(return_position_impl_trait_in_trait)]
3+
#![feature(return_position_impl_trait_in_trait, lint_reasons)]
44
#![allow(incomplete_features)]
55

66
use std::fmt::Display;
77

8-
trait Foo {
8+
pub trait Foo {
99
fn bar(&self) -> impl Display;
1010
}
1111

1212
impl Foo for i32 {
13-
#[allow(refining_impl_trait)]
13+
#[expect(refining_impl_trait)]
1414
fn bar(&self) -> i32 {
1515
*self
1616
}
1717
}
1818

1919
impl Foo for &'static str {
20-
#[allow(refining_impl_trait)]
20+
#[expect(refining_impl_trait)]
2121
fn bar(&self) -> &'static str {
2222
*self
2323
}
2424
}
2525

26-
struct Yay;
26+
pub struct Yay;
2727

2828
impl Foo for Yay {
29-
#[allow(refining_impl_trait)]
29+
#[expect(refining_impl_trait)]
3030
fn bar(&self) -> String {
3131
String::from(":^)")
3232
}

0 commit comments

Comments
 (0)