Skip to content

Commit 7534efa

Browse files
committed
Reduce error duplication for invalid placeholder types in fn types
1 parent cfeedec commit 7534efa

File tree

4 files changed

+68
-76
lines changed

4 files changed

+68
-76
lines changed

src/librustc_typeck/astconv.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2883,16 +2883,17 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
28832883
let bare_fn_ty =
28842884
ty::Binder::bind(tcx.mk_fn_sig(input_tys, output_ty, decl.c_variadic, unsafety, abi));
28852885

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

src/librustc_typeck/collect.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1503,9 +1503,13 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
15031503
AstConv::ty_of_fn(&icx, header.unsafety, header.abi, decl, &generics, Some(ident.span))
15041504
}
15051505

1506-
ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(ref fn_decl, _, _), .. }) => {
1506+
ForeignItem(&hir::ForeignItem {
1507+
kind: ForeignItemKind::Fn(ref fn_decl, _, _),
1508+
ident,
1509+
..
1510+
}) => {
15071511
let abi = tcx.hir().get_foreign_abi(hir_id);
1508-
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi)
1512+
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi, ident)
15091513
}
15101514

15111515
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => {
@@ -2118,6 +2122,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
21182122
def_id: DefId,
21192123
decl: &'tcx hir::FnDecl<'tcx>,
21202124
abi: abi::Abi,
2125+
ident: Ident,
21212126
) -> ty::PolyFnSig<'tcx> {
21222127
let unsafety = if abi == abi::Abi::RustIntrinsic {
21232128
intrinsic_operation_unsafety(&tcx.item_name(def_id).as_str())
@@ -2130,7 +2135,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
21302135
abi,
21312136
decl,
21322137
&hir::Generics::empty(),
2133-
None,
2138+
Some(ident.span),
21342139
);
21352140

21362141
// Feature gate SIMD types in FFI, since I am not sure that the

src/test/ui/typeck/typeck_type_placeholder_item.rs

-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ fn test7(x: _) { let _x: usize = x; }
3232

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

3736
struct Test9;
3837

@@ -99,7 +98,6 @@ pub fn main() {
9998

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

104102
struct FnTest9;
105103

0 commit comments

Comments
 (0)