Skip to content

Commit fb2bd79

Browse files
author
enthropy7
committed
fix refining_impl_trait suggestion with return_type_notation
1 parent ebf13cc commit fb2bd79

5 files changed

Lines changed: 49 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ fn report_mismatched_rpitit_signature<'tcx>(
332332
hir::FnRetTy::Return(ty) => ty.span,
333333
});
334334

335+
let return_ty_suggestion = if let Some(trait_ret_span) = trait_return_span
336+
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(trait_ret_span)
337+
{
338+
snippet
339+
} else {
340+
format!("{return_ty}")
341+
};
342+
335343
let span = unmatched_bound.unwrap_or(span);
336344
tcx.emit_node_span_lint(
337345
if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
@@ -342,7 +350,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
342350
trait_return_span,
343351
pre,
344352
post,
345-
return_ty,
353+
return_ty: return_ty_suggestion,
346354
unmatched_bound,
347355
},
348356
);

‎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+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ LL | fn bar(&self) -> impl Copy + '_ {}
103103
help: replace the return type so that it matches the trait
104104
|
105105
LL - fn bar(&self) -> impl Copy + '_ {}
106-
LL + fn bar(&self) -> impl Sized + '_ {}
106+
LL + fn bar(&self) -> impl Sized + 'a {}
107107
|
108108

109109
error: impl trait in impl method signature does not match trait method signature

0 commit comments

Comments
 (0)