Skip to content

Commit

Permalink
Account for associated items when denying _
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 27, 2020
1 parent a7b727d commit c6cfcf9
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 73 deletions.
34 changes: 26 additions & 8 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,21 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) {
tcx.generics_of(def_id);

match trait_item.kind {
hir::TraitItemKind::Const(..)
| hir::TraitItemKind::Type(_, Some(_))
| hir::TraitItemKind::Method(..) => {
hir::TraitItemKind::Method(..) => {
tcx.type_of(def_id);
if let hir::TraitItemKind::Method(..) = trait_item.kind {
tcx.fn_sig(def_id);
}
tcx.fn_sig(def_id);
}

hir::TraitItemKind::Const(.., Some(_)) => {
tcx.type_of(def_id);
}

hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(_, Some(_)) => {
tcx.type_of(def_id);
// Account for `const C: _;` and `type T = _;`.
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_trait_item(trait_item);
placeholder_type_error(tcx, DUMMY_SP, &[], visitor.0, false);
}

hir::TraitItemKind::Type(_, None) => {}
Expand All @@ -735,8 +743,18 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::HirId) {
tcx.generics_of(def_id);
tcx.type_of(def_id);
tcx.predicates_of(def_id);
if let hir::ImplItemKind::Method(..) = tcx.hir().expect_impl_item(impl_item_id).kind {
tcx.fn_sig(def_id);
let impl_item = tcx.hir().expect_impl_item(impl_item_id);
match impl_item.kind {
hir::ImplItemKind::Method(..) => {
tcx.fn_sig(def_id);
}
hir::ImplItemKind::TyAlias(_) | hir::ImplItemKind::OpaqueTy(_) => {
// Account for `type T = _;`
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_impl_item(impl_item);
placeholder_type_error(tcx, DUMMY_SP, &[], visitor.0, false);
}
hir::ImplItemKind::Const(..) => {}
}
}

Expand Down
25 changes: 24 additions & 1 deletion src/test/ui/typeck/typeck_type_placeholder_item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(type_alias_impl_trait)] // Needed for single test `type Y = impl Trait<_>`
// Needed for `type Y = impl Trait<_>` and `type B = _;`
#![feature(type_alias_impl_trait, associated_type_defaults)]
// This test checks that it is not possible to enable global type
// inference by using the `_` type placeholder.

Expand Down Expand Up @@ -188,3 +189,25 @@ type Y = impl Trait<_>;
fn foo() -> Y {
Struct
}

trait Qux {
type A;
type B = _;
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
const C: _;
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
const D: _ = 42;
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
// type E: _; // FIXME: make the parser propagate the existence of `B`
}
impl Qux for Struct {
type A = _;
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
type B = _;
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
const C: _;
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
//~| ERROR associated constant in `impl` without body
const D: _ = 42;
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
}
Loading

0 comments on commit c6cfcf9

Please sign in to comment.