Skip to content

Commit a7458f5

Browse files
committed
fix refining_impl_trait suggestion with return_type_notation
1 parent ebf13cc commit a7458f5

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_infer::infer::TyCtxtInferExt;
66
use rustc_lint_defs::builtin::{REFINING_IMPL_TRAIT_INTERNAL, REFINING_IMPL_TRAIT_REACHABLE};
77
use rustc_middle::span_bug;
88
use rustc_middle::traits::ObligationCause;
9+
use rustc_middle::ty::print::with_types_for_signature;
910
use rustc_middle::ty::{
1011
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable,
1112
TypeVisitableExt, TypeVisitor, TypingMode,
@@ -332,6 +333,10 @@ fn report_mismatched_rpitit_signature<'tcx>(
332333
hir::FnRetTy::Return(ty) => ty.span,
333334
});
334335

336+
// Use ForSignature mode to ensure RPITITs are printed as `impl Trait` rather than
337+
// `impl Trait { T::method(..) }` when RTN is enabled.
338+
let return_ty_suggestion = with_types_for_signature!(format!("{return_ty}"));
339+
335340
let span = unmatched_bound.unwrap_or(span);
336341
tcx.emit_node_span_lint(
337342
if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
@@ -342,7 +347,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
342347
trait_return_span,
343348
pre,
344349
post,
345-
return_ty,
350+
return_ty: return_ty_suggestion,
346351
unmatched_bound,
347352
},
348353
);

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ pub(crate) struct UnusedAssociatedTypeBounds {
10231023
#[diag(hir_analysis_rpitit_refined)]
10241024
#[note]
10251025
#[note(hir_analysis_feedback_note)]
1026-
pub(crate) struct ReturnPositionImplTraitInTraitRefined<'tcx> {
1026+
pub(crate) struct ReturnPositionImplTraitInTraitRefined {
10271027
#[suggestion(applicability = "maybe-incorrect", code = "{pre}{return_ty}{post}")]
10281028
pub impl_return_span: Span,
10291029
#[label]
@@ -1033,7 +1033,7 @@ pub(crate) struct ReturnPositionImplTraitInTraitRefined<'tcx> {
10331033

10341034
pub pre: &'static str,
10351035
pub post: &'static str,
1036-
pub return_ty: Ty<'tcx>,
1036+
pub return_ty: String,
10371037
}
10381038

10391039
#[derive(LintDiagnostic)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(return_type_notation)]
2+
#![deny(refining_impl_trait)]
3+
4+
trait Trait {
5+
fn f() -> impl Sized;
6+
}
7+
8+
impl Trait for () {
9+
fn f() {}
10+
//~^ ERROR impl trait in impl method signature does not match trait method signature
11+
}
12+
13+
fn main() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: impl trait in impl method signature does not match trait method signature
2+
--> $DIR/refine-return-type-notation.rs:9:5
3+
|
4+
LL | fn f() -> impl Sized;
5+
| ---------- return type from trait method defined here
6+
...
7+
LL | fn f() {}
8+
| ^^^^^^
9+
|
10+
= 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
12+
note: the lint level is defined here
13+
--> $DIR/refine-return-type-notation.rs:2:9
14+
|
15+
LL | #![deny(refining_impl_trait)]
16+
| ^^^^^^^^^^^^^^^^^^^
17+
= note: `#[deny(refining_impl_trait_internal)]` implied by `#[deny(refining_impl_trait)]`
18+
help: replace the return type so that it matches the trait
19+
|
20+
LL | fn f()-> impl Sized {}
21+
| +++++++++++++
22+
23+
error: aborting due to 1 previous error
24+

0 commit comments

Comments
 (0)