Skip to content

Commit ae42f22

Browse files
make fn() -> _ {} suggestion MachineApplicable
1 parent a719718 commit ae42f22

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

compiler/rustc_typeck/src/collect.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_middle::ty::subst::InternalSubsts;
4141
use rustc_middle::ty::util::Discr;
4242
use rustc_middle::ty::util::IntTypeExt;
4343
use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, Ty, TyCtxt};
44-
use rustc_middle::ty::{ReprOptions, ToPredicate, TypeFoldable};
44+
use rustc_middle::ty::{ReprOptions, ToPredicate};
4545
use rustc_session::lint;
4646
use rustc_session::parse::feature_err;
4747
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -2004,28 +2004,29 @@ fn infer_return_ty_for_fn_sig<'tcx>(
20042004
visitor.visit_ty(ty);
20052005
let mut diag = bad_placeholder(tcx, visitor.0, "return type");
20062006
let ret_ty = fn_sig.skip_binder().output();
2007-
if !ret_ty.references_error() {
2008-
if !ret_ty.is_closure() {
2009-
let ret_ty_str = match ret_ty.kind() {
2010-
// Suggest a function pointer return type instead of a unique function definition
2011-
// (e.g. `fn() -> i32` instead of `fn() -> i32 { f }`, the latter of which is invalid
2012-
// syntax)
2013-
ty::FnDef(..) => ret_ty.fn_sig(tcx).to_string(),
2014-
_ => ret_ty.to_string(),
2015-
};
2007+
if ret_ty.is_suggestable() {
2008+
diag.span_suggestion(
2009+
ty.span,
2010+
"replace with the correct return type",
2011+
ret_ty.to_string(),
2012+
Applicability::MachineApplicable,
2013+
);
2014+
} else if matches!(ret_ty.kind(), ty::FnDef(..)) {
2015+
let fn_sig = ret_ty.fn_sig(tcx);
2016+
if fn_sig.skip_binder().inputs_and_output.iter().all(|t| t.is_suggestable()) {
20162017
diag.span_suggestion(
20172018
ty.span,
20182019
"replace with the correct return type",
2019-
ret_ty_str,
2020-
Applicability::MaybeIncorrect,
2020+
fn_sig.to_string(),
2021+
Applicability::MachineApplicable,
20212022
);
2022-
} else {
2023-
// We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
2024-
// to prevent the user from getting a papercut while trying to use the unique closure
2025-
// syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
2026-
diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
2027-
diag.note("for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html");
20282023
}
2024+
} else if ret_ty.is_closure() {
2025+
// We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
2026+
// to prevent the user from getting a papercut while trying to use the unique closure
2027+
// syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
2028+
diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
2029+
diag.note("for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html");
20292030
}
20302031
diag.emit();
20312032

src/test/ui/type-alias-impl-trait/issue-77179.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
22
--> $DIR/issue-77179.rs:7:22
33
|
44
LL | fn test() -> Pointer<_> {
5-
| --------^-
6-
| | |
7-
| | not allowed in type signatures
8-
| help: replace with the correct return type: `Pointer<i32>`
5+
| ^ not allowed in type signatures
96

107
error: aborting due to previous error
118

0 commit comments

Comments
 (0)