Skip to content

Commit

Permalink
Reduce error duplication for invalid placeholder types in fn types
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Mar 25, 2020
1 parent cfeedec commit 7534efa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 76 deletions.
9 changes: 5 additions & 4 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -2883,16 +2883,17 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let bare_fn_ty =
ty::Binder::bind(tcx.mk_fn_sig(input_tys, output_ty, decl.c_variadic, unsafety, abi));

if !self.allow_ty_infer() {
if let (false, Some(ident_span)) = (self.allow_ty_infer(), ident_span) {
// We always collect the spans for placeholder types when evaluating `fn`s, but we
// only want to emit an error complaining about them if infer types (`_`) are not
// allowed. `allow_ty_infer` gates this behavior.
// allowed. `allow_ty_infer` gates this behavior. We check for the presence of
// `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`.
crate::collect::placeholder_type_error(
tcx,
ident_span.map(|sp| sp.shrink_to_hi()).unwrap_or(DUMMY_SP),
ident_span.shrink_to_hi(),
&generics.params[..],
visitor.0,
ident_span.is_some(),
true,
);
}

Expand Down
11 changes: 8 additions & 3 deletions src/librustc_typeck/collect.rs
Expand Up @@ -1503,9 +1503,13 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
AstConv::ty_of_fn(&icx, header.unsafety, header.abi, decl, &generics, Some(ident.span))
}

ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(ref fn_decl, _, _), .. }) => {
ForeignItem(&hir::ForeignItem {
kind: ForeignItemKind::Fn(ref fn_decl, _, _),
ident,
..
}) => {
let abi = tcx.hir().get_foreign_abi(hir_id);
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi)
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi, ident)
}

Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => {
Expand Down Expand Up @@ -2118,6 +2122,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
def_id: DefId,
decl: &'tcx hir::FnDecl<'tcx>,
abi: abi::Abi,
ident: Ident,
) -> ty::PolyFnSig<'tcx> {
let unsafety = if abi == abi::Abi::RustIntrinsic {
intrinsic_operation_unsafety(&tcx.item_name(def_id).as_str())
Expand All @@ -2130,7 +2135,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
abi,
decl,
&hir::Generics::empty(),
None,
Some(ident.span),
);

// Feature gate SIMD types in FFI, since I am not sure that the
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/typeck/typeck_type_placeholder_item.rs
Expand Up @@ -32,7 +32,6 @@ fn test7(x: _) { let _x: usize = x; }

fn test8(_f: fn() -> _) { }
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
//~| ERROR the type placeholder `_` is not allowed within types on item signatures

struct Test9;

Expand Down Expand Up @@ -99,7 +98,6 @@ pub fn main() {

fn fn_test8(_f: fn() -> _) { }
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
//~| ERROR the type placeholder `_` is not allowed within types on item signatures

struct FnTest9;

Expand Down

0 comments on commit 7534efa

Please sign in to comment.