Skip to content

Commit

Permalink
Rollup merge of rust-lang#70369 - estebank:bad-placeholder-in-where, …
Browse files Browse the repository at this point in the history
…r=Centril

Fix smaller issues with invalid placeholder type errors

Follow up to rust-lang#70294.

- Fix placement of suggested generic param when bounds are present.
- Reduce error duplication for invalid placeholder types in `fn` types.

r? @Centril
  • Loading branch information
Centril authored Mar 25, 2020
2 parents 831a267 + 7534efa commit 1059604
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 81 deletions.
9 changes: 5 additions & 4 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
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
15 changes: 11 additions & 4 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ crate fn placeholder_type_error(
// `struct S<T>(T);` instead of `struct S<_, T>(T);`.
sugg.push((arg.span, (*type_name).to_string()));
} else {
let last = generics.iter().last().unwrap();
sugg.push((
generics.iter().last().unwrap().span.shrink_to_hi(),
// Account for bounds, we want `fn foo<T: E, K>(_: K)` not `fn foo<T, K: E>(_: K)`.
last.bounds_span().unwrap_or(last.span).shrink_to_hi(),
format!(", {}", type_name),
));
}
Expand Down Expand Up @@ -1501,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 @@ -2116,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 @@ -2128,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
8 changes: 4 additions & 4 deletions src/test/ui/did_you_mean/bad-assoc-ty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ LL | fn foo<X: K<_, _>>(x: X) {}
|
help: use type parameters instead
|
LL | fn foo<X, T: K<T, T>>(x: X) {}
| ^^^ ^ ^
LL | fn foo<X: K<T, T>, T>(x: X) {}
| ^ ^ ^^^

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/bad-assoc-ty.rs:52:34
Expand All @@ -167,8 +167,8 @@ LL | fn baz<F: Fn() -> _>(_: F) {}
|
help: use type parameters instead
|
LL | fn baz<F, T: Fn() -> T>(_: F) {}
| ^^^ ^
LL | fn baz<F: Fn() -> T, T>(_: F) {}
| ^^^^

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/bad-assoc-ty.rs:58:33
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/typeck/typeck_type_placeholder_item.rs
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 1059604

Please sign in to comment.