Skip to content

Commit f9e7489

Browse files
TAITs are suggestable
1 parent ae42f22 commit f9e7489

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use crate::ty::subst::{GenericArg, GenericArgKind};
44
use crate::ty::TyKind::*;
55
use crate::ty::{
6-
ConstKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, InferTy,
7-
ProjectionTy, Term, Ty, TyCtxt, TypeAndMut,
6+
ConstKind, DefIdTree, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef,
7+
InferTy, ProjectionTy, Term, Ty, TyCtxt, TypeAndMut,
88
};
99

1010
use rustc_data_structures::fx::FxHashMap;
@@ -74,10 +74,10 @@ impl<'tcx> Ty<'tcx> {
7474
}
7575

7676
/// Whether the type can be safely suggested during error recovery.
77-
pub fn is_suggestable(self) -> bool {
78-
fn generic_arg_is_suggestible(arg: GenericArg<'_>) -> bool {
77+
pub fn is_suggestable(self, tcx: TyCtxt<'tcx>) -> bool {
78+
fn generic_arg_is_suggestible<'tcx>(arg: GenericArg<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
7979
match arg.unpack() {
80-
GenericArgKind::Type(ty) => ty.is_suggestable(),
80+
GenericArgKind::Type(ty) => ty.is_suggestable(tcx),
8181
GenericArgKind::Const(c) => const_is_suggestable(c.val()),
8282
_ => true,
8383
}
@@ -99,36 +99,46 @@ impl<'tcx> Ty<'tcx> {
9999
// temporary, so I'll leave this as a fixme.
100100

101101
match self.kind() {
102-
Opaque(..)
103-
| FnDef(..)
102+
FnDef(..)
104103
| Closure(..)
105104
| Infer(..)
106105
| Generator(..)
107106
| GeneratorWitness(..)
108107
| Bound(_, _)
109108
| Placeholder(_)
110109
| Error(_) => false,
110+
Opaque(did, substs) => {
111+
let parent = tcx.parent(*did).expect("opaque types always have a parent");
112+
if let hir::def::DefKind::TyAlias | hir::def::DefKind::AssocTy = tcx.def_kind(parent)
113+
&& let Opaque(parent_did, _) = tcx.type_of(parent).kind()
114+
&& parent_did == did
115+
{
116+
substs.iter().all(|a| generic_arg_is_suggestible(a, tcx))
117+
} else {
118+
false
119+
}
120+
}
111121
Dynamic(dty, _) => dty.iter().all(|pred| match pred.skip_binder() {
112122
ExistentialPredicate::Trait(ExistentialTraitRef { substs, .. }) => {
113-
substs.iter().all(generic_arg_is_suggestible)
123+
substs.iter().all(|a| generic_arg_is_suggestible(a, tcx))
114124
}
115125
ExistentialPredicate::Projection(ExistentialProjection {
116126
substs, term, ..
117127
}) => {
118128
let term_is_suggestable = match term {
119-
Term::Ty(ty) => ty.is_suggestable(),
129+
Term::Ty(ty) => ty.is_suggestable(tcx),
120130
Term::Const(c) => const_is_suggestable(c.val()),
121131
};
122-
term_is_suggestable && substs.iter().all(generic_arg_is_suggestible)
132+
term_is_suggestable && substs.iter().all(|a| generic_arg_is_suggestible(a, tcx))
123133
}
124134
_ => true,
125135
}),
126136
Projection(ProjectionTy { substs: args, .. }) | Adt(_, args) => {
127-
args.iter().all(generic_arg_is_suggestible)
137+
args.iter().all(|a| generic_arg_is_suggestible(a, tcx))
128138
}
129-
Tuple(args) => args.iter().all(|ty| ty.is_suggestable()),
130-
Slice(ty) | RawPtr(TypeAndMut { ty, .. }) | Ref(_, ty, _) => ty.is_suggestable(),
131-
Array(ty, c) => ty.is_suggestable() && const_is_suggestable(c.val()),
139+
Tuple(args) => args.iter().all(|ty| ty.is_suggestable(tcx)),
140+
Slice(ty) | RawPtr(TypeAndMut { ty, .. }) | Ref(_, ty, _) => ty.is_suggestable(tcx),
141+
Array(ty, c) => ty.is_suggestable(tcx) && const_is_suggestable(c.val()),
132142
_ => true,
133143
}
134144
}

compiler/rustc_typeck/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
8686
let param_type = tcx.infer_ctxt().enter(|infcx| {
8787
infcx.resolve_numeric_literals_with_default(tcx.type_of(param.def_id))
8888
});
89-
if param_type.is_suggestable() {
89+
if param_type.is_suggestable(tcx) {
9090
err.span_suggestion(
9191
tcx.def_span(src_def_id),
9292
"consider changing this type parameter to be a `const` generic",

compiler/rustc_typeck/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24662466
span,
24672467
ty,
24682468
opt_sugg: Some((span, Applicability::MachineApplicable))
2469-
.filter(|_| ty.is_suggestable()),
2469+
.filter(|_| ty.is_suggestable(tcx)),
24702470
});
24712471

24722472
ty

compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
525525
self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
526526
// Only suggest changing the return type for methods that
527527
// haven't set a return type at all (and aren't `fn main()` or an impl).
528-
match (&fn_decl.output, found.is_suggestable(), can_suggest, expected.is_unit()) {
528+
match (&fn_decl.output, found.is_suggestable(self.tcx), can_suggest, expected.is_unit()) {
529529
(&hir::FnRetTy::DefaultReturn(span), true, true, true) => {
530530
err.span_suggestion(
531531
span,

compiler/rustc_typeck/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ 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.is_suggestable() {
2007+
if ret_ty.is_suggestable(tcx) {
20082008
diag.span_suggestion(
20092009
ty.span,
20102010
"replace with the correct return type",
@@ -2013,7 +2013,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
20132013
);
20142014
} else if matches!(ret_ty.kind(), ty::FnDef(..)) {
20152015
let fn_sig = ret_ty.fn_sig(tcx);
2016-
if fn_sig.skip_binder().inputs_and_output.iter().all(|t| t.is_suggestable()) {
2016+
if fn_sig.skip_binder().inputs_and_output.iter().all(|t| t.is_suggestable(tcx)) {
20172017
diag.span_suggestion(
20182018
ty.span,
20192019
"replace with the correct return type",

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ 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-
| ^ not allowed in type signatures
5+
| --------^-
6+
| | |
7+
| | not allowed in type signatures
8+
| help: replace with the correct return type: `Pointer<i32>`
69

710
error: aborting due to previous error
811

0 commit comments

Comments
 (0)