Skip to content

Commit 261b606

Browse files
committed
review comments and fix rebase
1 parent 8a3872e commit 261b606

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/librustc_typeck/astconv.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub trait AstConv<'tcx> {
6868
/// Returns the type to use when a type is omitted.
6969
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
7070

71+
/// Returns `true` if `_` is allowed in type signatures in the current context.
7172
fn allow_ty_infer(&self) -> bool;
7273

7374
/// Returns the const to use when a const is omitted.
@@ -2770,8 +2771,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
27702771
let tcx = self.tcx();
27712772

27722773
// We proactively collect all the infered type params to emit a single error per fn def.
2773-
let mut visitor = PlaceholderHirTyCollector::new();
2774-
for ty in &decl.inputs {
2774+
let mut visitor = PlaceholderHirTyCollector::default();
2775+
for ty in decl.inputs {
27752776
visitor.visit_ty(ty);
27762777
}
27772778
let input_tys = decl.inputs.iter().map(|a| self.ty_of_arg(a, None));
@@ -2789,6 +2790,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
27892790
ty::Binder::bind(tcx.mk_fn_sig(input_tys, output_ty, decl.c_variadic, unsafety, abi));
27902791

27912792
if !self.allow_ty_infer() {
2793+
// We always collect the spans for placeholder types when evaluating `fn`s, but we
2794+
// only want to emit an error complaining about them if infer types (`_`) are not
2795+
// allowed. `allow_ty_infer` gates this behavior.
27922796
crate::collect::placeholder_type_error(
27932797
tcx,
27942798
ident_span.unwrap_or(DUMMY_SP),

src/librustc_typeck/collect.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub struct ItemCtxt<'tcx> {
102102

103103
///////////////////////////////////////////////////////////////////////////
104104

105+
#[derive(Default)]
105106
crate struct PlaceholderHirTyCollector(crate Vec<Span>);
106107

107108
impl<'v> Visitor<'v> for PlaceholderHirTyCollector {
@@ -116,16 +117,13 @@ impl<'v> Visitor<'v> for PlaceholderHirTyCollector {
116117
}
117118
}
118119

119-
impl PlaceholderHirTyCollector {
120-
pub fn new() -> PlaceholderHirTyCollector {
121-
PlaceholderHirTyCollector(vec![])
122-
}
123-
}
124-
125120
struct CollectItemTypesVisitor<'tcx> {
126121
tcx: TyCtxt<'tcx>,
127122
}
128123

124+
/// If there are any placeholder types (`_`), emit an error explaining that this is not allowed
125+
/// and suggest adding type parameters in the appropriate place, taking into consideration any and
126+
/// all already existing generic type parameters to avoid suggesting a name that is already in use.
129127
crate fn placeholder_type_error(
130128
tcx: TyCtxt<'tcx>,
131129
ident_span: Span,
@@ -136,6 +134,7 @@ crate fn placeholder_type_error(
136134
if placeholder_types.is_empty() {
137135
return;
138136
}
137+
// This is the whitelist of possible parameter names that we might suggest.
139138
let possible_names = ["T", "K", "L", "A", "B", "C"];
140139
let used_names = generics
141140
.iter()
@@ -181,7 +180,7 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir
181180
_ => return,
182181
};
183182

184-
let mut visitor = PlaceholderHirTyCollector::new();
183+
let mut visitor = PlaceholderHirTyCollector::default();
185184
visitor.visit_item(item);
186185

187186
placeholder_type_error(tcx, item.ident.span, generics, visitor.0, suggest);
@@ -1796,15 +1795,7 @@ fn is_suggestable_infer_ty(ty: &hir::Ty<'_>) -> bool {
17961795
match &ty.kind {
17971796
hir::TyKind::Infer => true,
17981797
hir::TyKind::Slice(ty) | hir::TyKind::Array(ty, _) => is_suggestable_infer_ty(ty),
1799-
hir::TyKind::Tup(tys)
1800-
if !tys.is_empty()
1801-
&& tys.iter().any(|ty| match ty.kind {
1802-
hir::TyKind::Infer => true,
1803-
_ => false,
1804-
}) =>
1805-
{
1806-
true
1807-
}
1798+
hir::TyKind::Tup(tys) => tys.iter().any(|ty| is_suggestable_infer_ty(ty)),
18081799
_ => false,
18091800
}
18101801
}
@@ -1838,7 +1829,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
18381829
match get_infer_ret_ty(&sig.decl.output) {
18391830
Some(ty) => {
18401831
let fn_sig = tcx.typeck_tables_of(def_id).liberated_fn_sigs()[hir_id];
1841-
let mut visitor = PlaceholderHirTyCollector::new();
1832+
let mut visitor = PlaceholderHirTyCollector::default();
18421833
visitor.visit_ty(ty);
18431834
let mut diag = bad_placeholder_type(tcx, visitor.0);
18441835
let ret_ty = fn_sig.output();

0 commit comments

Comments
 (0)