Skip to content

Commit c6cfcf9

Browse files
committed
Account for associated items when denying _
1 parent a7b727d commit c6cfcf9

File tree

3 files changed

+170
-73
lines changed

3 files changed

+170
-73
lines changed

src/librustc_typeck/collect.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,21 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) {
715715
tcx.generics_of(def_id);
716716

717717
match trait_item.kind {
718-
hir::TraitItemKind::Const(..)
719-
| hir::TraitItemKind::Type(_, Some(_))
720-
| hir::TraitItemKind::Method(..) => {
718+
hir::TraitItemKind::Method(..) => {
721719
tcx.type_of(def_id);
722-
if let hir::TraitItemKind::Method(..) = trait_item.kind {
723-
tcx.fn_sig(def_id);
724-
}
720+
tcx.fn_sig(def_id);
721+
}
722+
723+
hir::TraitItemKind::Const(.., Some(_)) => {
724+
tcx.type_of(def_id);
725+
}
726+
727+
hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(_, Some(_)) => {
728+
tcx.type_of(def_id);
729+
// Account for `const C: _;` and `type T = _;`.
730+
let mut visitor = PlaceholderHirTyCollector::default();
731+
visitor.visit_trait_item(trait_item);
732+
placeholder_type_error(tcx, DUMMY_SP, &[], visitor.0, false);
725733
}
726734

727735
hir::TraitItemKind::Type(_, None) => {}
@@ -735,8 +743,18 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::HirId) {
735743
tcx.generics_of(def_id);
736744
tcx.type_of(def_id);
737745
tcx.predicates_of(def_id);
738-
if let hir::ImplItemKind::Method(..) = tcx.hir().expect_impl_item(impl_item_id).kind {
739-
tcx.fn_sig(def_id);
746+
let impl_item = tcx.hir().expect_impl_item(impl_item_id);
747+
match impl_item.kind {
748+
hir::ImplItemKind::Method(..) => {
749+
tcx.fn_sig(def_id);
750+
}
751+
hir::ImplItemKind::TyAlias(_) | hir::ImplItemKind::OpaqueTy(_) => {
752+
// Account for `type T = _;`
753+
let mut visitor = PlaceholderHirTyCollector::default();
754+
visitor.visit_impl_item(impl_item);
755+
placeholder_type_error(tcx, DUMMY_SP, &[], visitor.0, false);
756+
}
757+
hir::ImplItemKind::Const(..) => {}
740758
}
741759
}
742760

src/test/ui/typeck/typeck_type_placeholder_item.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![feature(type_alias_impl_trait)] // Needed for single test `type Y = impl Trait<_>`
1+
// Needed for `type Y = impl Trait<_>` and `type B = _;`
2+
#![feature(type_alias_impl_trait, associated_type_defaults)]
23
// This test checks that it is not possible to enable global type
34
// inference by using the `_` type placeholder.
45

@@ -188,3 +189,25 @@ type Y = impl Trait<_>;
188189
fn foo() -> Y {
189190
Struct
190191
}
192+
193+
trait Qux {
194+
type A;
195+
type B = _;
196+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
197+
const C: _;
198+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
199+
const D: _ = 42;
200+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
201+
// type E: _; // FIXME: make the parser propagate the existence of `B`
202+
}
203+
impl Qux for Struct {
204+
type A = _;
205+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
206+
type B = _;
207+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
208+
const C: _;
209+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
210+
//~| ERROR associated constant in `impl` without body
211+
const D: _ = 42;
212+
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures
213+
}

0 commit comments

Comments
 (0)