Skip to content

Commit f318b1b

Browse files
committed
Split refining_impl_trait lint into _reachable, _internal variants
1 parent fc3800f commit f318b1b

File tree

13 files changed

+187
-29
lines changed

13 files changed

+187
-29
lines changed

compiler/rustc_hir_analysis/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ hir_analysis_rpitit_refined = impl trait in impl method signature does not match
349349
.label = return type from trait method defined here
350350
.unmatched_bound_label = this bound is stronger than that defined on the trait
351351
.note = add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
352+
.feedback_note = we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
352353
353354
hir_analysis_self_in_impl_self =
354355
`Self` is not valid in the self type of an impl block

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

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxIndexSet;
22
use rustc_hir as hir;
33
use rustc_hir::def_id::DefId;
44
use rustc_infer::infer::{outlives::env::OutlivesEnvironment, TyCtxtInferExt};
5-
use rustc_lint_defs::builtin::REFINING_IMPL_TRAIT;
5+
use rustc_lint_defs::builtin::{REFINING_IMPL_TRAIT_INTERNAL, REFINING_IMPL_TRAIT_REACHABLE};
66
use rustc_middle::traits::{ObligationCause, Reveal};
77
use rustc_middle::ty::{
88
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor,
@@ -24,26 +24,23 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
2424
if !tcx.impl_method_has_trait_impl_trait_tys(impl_m.def_id) {
2525
return;
2626
}
27+
2728
// unreachable traits don't have any library guarantees, there's no need to do this check.
28-
if trait_m
29+
let is_internal = trait_m
2930
.container_id(tcx)
3031
.as_local()
3132
.is_some_and(|trait_def_id| !tcx.effective_visibilities(()).is_reachable(trait_def_id))
32-
{
33-
return;
34-
}
33+
// If a type in the trait ref is private, then there's also no reason to do this check.
34+
|| impl_trait_ref.args.iter().any(|arg| {
35+
if let Some(ty) = arg.as_type()
36+
&& let Some(self_visibility) = type_visibility(tcx, ty)
37+
{
38+
return !self_visibility.is_public();
39+
}
40+
false
41+
});
3542

36-
// If a type in the trait ref is private, then there's also no reason to do this check.
3743
let impl_def_id = impl_m.container_id(tcx);
38-
for arg in impl_trait_ref.args {
39-
if let Some(ty) = arg.as_type()
40-
&& let Some(self_visibility) = type_visibility(tcx, ty)
41-
&& !self_visibility.is_public()
42-
{
43-
return;
44-
}
45-
}
46-
4744
let impl_m_args = ty::GenericArgs::identity_for_item(tcx, impl_m.def_id);
4845
let trait_m_to_impl_m_args = impl_m_args.rebase_onto(tcx, impl_def_id, impl_trait_ref.args);
4946
let bound_trait_m_sig = tcx.fn_sig(trait_m.def_id).instantiate(tcx, trait_m_to_impl_m_args);
@@ -86,6 +83,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
8683
trait_m.def_id,
8784
impl_m.def_id,
8885
None,
86+
is_internal,
8987
);
9088
return;
9189
};
@@ -105,6 +103,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
105103
trait_m.def_id,
106104
impl_m.def_id,
107105
None,
106+
is_internal,
108107
);
109108
return;
110109
}
@@ -195,6 +194,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
195194
trait_m.def_id,
196195
impl_m.def_id,
197196
Some(span),
197+
is_internal,
198198
);
199199
return;
200200
}
@@ -235,6 +235,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
235235
trait_m_def_id: DefId,
236236
impl_m_def_id: DefId,
237237
unmatched_bound: Option<Span>,
238+
is_internal: bool,
238239
) {
239240
let mapping = std::iter::zip(
240241
tcx.fn_sig(trait_m_def_id).skip_binder().bound_vars(),
@@ -287,7 +288,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
287288

288289
let span = unmatched_bound.unwrap_or(span);
289290
tcx.emit_node_span_lint(
290-
REFINING_IMPL_TRAIT,
291+
if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
291292
tcx.local_def_id_to_hir_id(impl_m_def_id.expect_local()),
292293
span,
293294
crate::errors::ReturnPositionImplTraitInTraitRefined {

compiler/rustc_hir_analysis/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ pub struct UnusedAssociatedTypeBounds {
10801080
#[derive(LintDiagnostic)]
10811081
#[diag(hir_analysis_rpitit_refined)]
10821082
#[note]
1083+
#[note(hir_analysis_feedback_note)]
10831084
pub(crate) struct ReturnPositionImplTraitInTraitRefined<'tcx> {
10841085
#[suggestion(applicability = "maybe-incorrect", code = "{pre}{return_ty}{post}")]
10851086
pub impl_return_span: Span,

compiler/rustc_lint/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ fn register_builtins(store: &mut LintStore) {
313313
// MACRO_USE_EXTERN_CRATE
314314
);
315315

316+
add_lint_group!(
317+
"refining_impl_trait",
318+
REFINING_IMPL_TRAIT_REACHABLE,
319+
REFINING_IMPL_TRAIT_INTERNAL
320+
);
321+
316322
// Register renamed and removed lints.
317323
store.register_renamed("single_use_lifetime", "single_use_lifetimes");
318324
store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");

compiler/rustc_lint_defs/src/builtin.rs

+56-8
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ declare_lint_pass! {
7777
PROC_MACRO_BACK_COMPAT,
7878
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
7979
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
80-
REFINING_IMPL_TRAIT,
80+
REFINING_IMPL_TRAIT_INTERNAL,
81+
REFINING_IMPL_TRAIT_REACHABLE,
8182
RENAMED_AND_REMOVED_LINTS,
8283
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
8384
RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
@@ -4320,7 +4321,9 @@ declare_lint! {
43204321

43214322
declare_lint! {
43224323
/// The `refining_impl_trait` lint detects usages of return-position impl
4323-
/// traits in trait signatures which are refined by implementations.
4324+
/// traits in trait signatures which are refined by implementations, meaning
4325+
/// the implementation adds information about the return type that is not
4326+
/// present in the trait.
43244327
///
43254328
/// ### Example
43264329
///
@@ -4350,13 +4353,58 @@ declare_lint! {
43504353
///
43514354
/// ### Explanation
43524355
///
4353-
/// Return-position impl trait in traits (RPITITs) desugar to associated types,
4354-
/// and callers of methods for types where the implementation is known are
4356+
/// Callers of methods for types where the implementation is known are
43554357
/// able to observe the types written in the impl signature. This may be
4356-
/// intended behavior, but may also pose a semver hazard for authors of libraries
4357-
/// who do not wish to make stronger guarantees about the types than what is
4358-
/// written in the trait signature.
4359-
pub REFINING_IMPL_TRAIT,
4358+
/// intended behavior, but may also lead to implementation details being
4359+
/// revealed unintentionally. In particular, it may pose a semver hazard
4360+
/// for authors of libraries who do not wish to make stronger guarantees
4361+
/// about the types than what is written in the trait signature.
4362+
pub REFINING_IMPL_TRAIT_REACHABLE,
4363+
Warn,
4364+
"impl trait in impl method signature does not match trait method signature",
4365+
}
4366+
4367+
declare_lint! {
4368+
/// The `refining_impl_trait` lint detects usages of return-position impl
4369+
/// traits in trait signatures which are refined by implementations, meaning
4370+
/// the implementation adds information about the return type that is not
4371+
/// present in the trait.
4372+
///
4373+
/// ### Example
4374+
///
4375+
/// ```rust,compile_fail
4376+
/// #![deny(refining_impl_trait)]
4377+
///
4378+
/// use std::fmt::Display;
4379+
///
4380+
/// pub trait AsDisplay {
4381+
/// fn as_display(&self) -> impl Display;
4382+
/// }
4383+
///
4384+
/// impl<'s> AsDisplay for &'s str {
4385+
/// fn as_display(&self) -> Self {
4386+
/// *self
4387+
/// }
4388+
/// }
4389+
///
4390+
/// fn main() {
4391+
/// // users can observe that the return type of
4392+
/// // `<&str as AsDisplay>::as_display()` is `&str`.
4393+
/// let x: &str = "".as_display();
4394+
/// }
4395+
/// ```
4396+
///
4397+
/// {{produces}}
4398+
///
4399+
/// ### Explanation
4400+
///
4401+
/// Callers of methods for types where the implementation is known are
4402+
/// able to observe the types written in the impl signature. This may be
4403+
/// intended behavior, but may also lead to implementation details being
4404+
/// revealed unintentionally. In particular, it may pose a semver hazard
4405+
/// for authors of libraries who do not wish to make stronger guarantees
4406+
/// about the types than what is written in the trait signature.
4407+
pub REFINING_IMPL_TRAIT_INTERNAL,
43604408
Warn,
43614409
"impl trait in impl method signature does not match trait method signature",
43624410
}

tests/ui/async-await/in-trait/async-example-desugared-boxed.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ LL | fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>> {
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99
|
1010
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
11+
= note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
1112
note: the lint level is defined here
1213
--> $DIR/async-example-desugared-boxed.rs:13:12
1314
|
1415
LL | #[warn(refining_impl_trait)]
1516
| ^^^^^^^^^^^^^^^^^^^
17+
= note: `#[warn(refining_impl_trait_reachable)]` implied by `#[warn(refining_impl_trait)]`
1618
help: replace the return type so that it matches the trait
1719
|
1820
LL | fn foo(&self) -> impl Future<Output = i32> {

tests/ui/async-await/in-trait/async-example-desugared-manual.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ LL | fn foo(&self) -> MyFuture {
88
| ^^^^^^^^
99
|
1010
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
11+
= note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
1112
note: the lint level is defined here
1213
--> $DIR/async-example-desugared-manual.rs:21:12
1314
|
1415
LL | #[warn(refining_impl_trait)]
1516
| ^^^^^^^^^^^^^^^^^^^
17+
= note: `#[warn(refining_impl_trait_reachable)]` implied by `#[warn(refining_impl_trait)]`
1618
help: replace the return type so that it matches the trait
1719
|
1820
LL | fn foo(&self) -> impl Future<Output = i32> {

tests/ui/async-await/return-type-notation/normalizing-self-auto-trait-issue-109924.next.stderr

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,22 @@ LL | #![feature(return_type_notation)]
77
= note: see issue #109417 <https://github.com/rust-lang/rust/issues/109417> for more information
88
= note: `#[warn(incomplete_features)]` on by default
99

10-
warning: 1 warning emitted
10+
warning: impl trait in impl method signature does not match trait method signature
11+
--> $DIR/normalizing-self-auto-trait-issue-109924.rs:15:5
12+
|
13+
LL | async fn bar(&self);
14+
| -------------------- return type from trait method defined here
15+
...
16+
LL | async fn bar(&self) {}
17+
| ^^^^^^^^^^^^^^^^^^^ this bound is stronger than that defined on the trait
18+
|
19+
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
20+
= note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
21+
= note: `#[warn(refining_impl_trait_internal)]` on by default
22+
help: replace the return type so that it matches the trait
23+
|
24+
LL | () {}
25+
| ~~
26+
27+
warning: 2 warnings emitted
1128

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ LL | fn iter(&self) -> impl 'a + Iterator<Item = I::Item<'a>> {
2222
| ^^ this bound is stronger than that defined on the trait
2323
|
2424
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
25-
= note: `#[warn(refining_impl_trait)]` on by default
25+
= note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
26+
= note: `#[warn(refining_impl_trait_reachable)]` on by default
2627
help: replace the return type so that it matches the trait
2728
|
2829
LL | fn iter(&self) -> impl Iterator<Item = <Self as Iterable>::Item<'_>> + '_ {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ impl Foo for Local {
1919

2020
struct LocalIgnoreRefining;
2121
impl Foo for LocalIgnoreRefining {
22-
#[deny(refining_impl_trait)]
22+
#[warn(refining_impl_trait)]
2323
fn bar(self) -> Arc<String> {
24+
//~^ WARN impl method signature does not match trait method signature
2425
Arc::new(String::new())
2526
}
2627
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: impl trait in impl method signature does not match trait method signature
2+
--> $DIR/foreign.rs:23:21
3+
|
4+
LL | fn bar(self) -> Arc<String> {
5+
| ^^^^^^^^^^^
6+
|
7+
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
8+
= note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
9+
note: the lint level is defined here
10+
--> $DIR/foreign.rs:22:12
11+
|
12+
LL | #[warn(refining_impl_trait)]
13+
| ^^^^^^^^^^^^^^^^^^^
14+
= note: `#[warn(refining_impl_trait_internal)]` implied by `#[warn(refining_impl_trait)]`
15+
help: replace the return type so that it matches the trait
16+
|
17+
LL | fn bar(self) -> impl Deref<Target = impl Sized> {
18+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
20+
warning: 1 warning emitted
21+

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

+3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ impl Foo for C {
2525
struct Private;
2626
impl Foo for Private {
2727
fn bar() -> () {}
28+
//~^ ERROR impl method signature does not match trait method signature
2829
}
2930

3031
pub trait Arg<A> {
3132
fn bar() -> impl Sized;
3233
}
3334
impl Arg<Private> for A {
3435
fn bar() -> () {}
36+
//~^ ERROR impl method signature does not match trait method signature
3537
}
3638

3739
pub trait Late {
@@ -52,6 +54,7 @@ mod unreachable {
5254
struct E;
5355
impl UnreachablePub for E {
5456
fn bar() {}
57+
//~^ ERROR impl method signature does not match trait method signature
5558
}
5659
}
5760

0 commit comments

Comments
 (0)