review comments

This commit is contained in:
Esteban Küber 2019-12-26 14:01:45 -08:00
parent 3a9c3f92cc
commit e843245716
2 changed files with 49 additions and 50 deletions

View file

@ -2748,11 +2748,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
pub fn ty_of_arg(&self, ty: &hir::Ty<'_>, expected_ty: Option<Ty<'tcx>>) -> Ty<'tcx> {
if crate::collect::is_infer_ty(ty) && expected_ty.is_some() {
self.record_ty(ty.hir_id, expected_ty.unwrap(), ty.span);
expected_ty.unwrap()
} else {
self.ast_ty_to_ty(ty)
match ty.kind {
hir::TyKind::Infer if expected_ty.is_some() => {
self.record_ty(ty.hir_id, expected_ty.unwrap(), ty.span);
expected_ty.unwrap()
}
_ => self.ast_ty_to_ty(ty),
}
}

View file

@ -133,52 +133,50 @@ crate fn placeholder_type_error(
placeholder_types: Vec<Span>,
suggest: bool,
) {
if !placeholder_types.is_empty() {
let possible_names = ["T", "K", "L", "A", "B", "C"];
let used_names = generics
.iter()
.filter_map(|p| match p.name {
hir::ParamName::Plain(ident) => Some(ident.name),
_ => None,
})
.collect::<Vec<_>>();
let mut type_name = "ParamName";
for name in &possible_names {
if !used_names.contains(&Symbol::intern(name)) {
type_name = name;
break;
}
}
let mut sugg: Vec<_> =
placeholder_types.iter().map(|sp| (*sp, type_name.to_string())).collect();
if generics.is_empty() {
sugg.push((ident_span.shrink_to_hi(), format!("<{}>", type_name)));
} else {
sugg.push((
generics.iter().last().unwrap().span.shrink_to_hi(),
format!(", {}", type_name),
));
}
let mut err = struct_span_err!(
tcx.sess,
placeholder_types.clone(),
E0121,
"the type placeholder `_` is not allowed within types on item signatures",
);
for span in &placeholder_types {
err.span_label(*span, "not allowed in type signatures");
}
if suggest {
err.multipart_suggestion(
"use type parameters instead",
sugg,
Applicability::HasPlaceholders,
);
}
err.emit();
if placeholder_types.is_empty() {
return;
}
let possible_names = ["T", "K", "L", "A", "B", "C"];
let used_names = generics
.iter()
.filter_map(|p| match p.name {
hir::ParamName::Plain(ident) => Some(ident.name),
_ => None,
})
.collect::<Vec<_>>();
let type_name = possible_names
.iter()
.find(|n| !used_names.contains(&Symbol::intern(n)))
.unwrap_or(&"ParamName");
let mut sugg: Vec<_> =
placeholder_types.iter().map(|sp| (*sp, type_name.to_string())).collect();
if generics.is_empty() {
sugg.push((ident_span.shrink_to_hi(), format!("<{}>", type_name)));
} else {
sugg.push((
generics.iter().last().unwrap().span.shrink_to_hi(),
format!(", {}", type_name),
));
}
let mut err = struct_span_err!(
tcx.sess,
placeholder_types.clone(),
E0121,
"the type placeholder `_` is not allowed within types on item signatures",
);
for span in &placeholder_types {
err.span_label(*span, "not allowed in type signatures");
}
if suggest {
err.multipart_suggestion(
"use type parameters instead",
sugg,
Applicability::HasPlaceholders,
);
}
err.emit();
}
fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {