Skip to content

Commit

Permalink
Auto merge of #69148 - estebank:cold-as-ice, r=oli-obk
Browse files Browse the repository at this point in the history
Account for bounds and asociated items when denying `_`

Fix #68801, #69204. Follow up to #67597 and #68071.

Output for the original ICE report:

```
    Checking vinoteca v5.0.0 (/Users/ekuber/workspace/vinoteca)
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
  --> src/producers.rs:43:70
   |
43 | pub fn top<Table: diesel::Table + diesel::query_dsl::InternalJoinDsl<_, diesel::query_source::joins::Inner, _>>(table: Table, limit: usize, connection: DbConn) -> RestResult<Vec<TopWineType>> {
   |                                                                      ^ not allowed in type signatures       ^ not allowed in type signatures

error: aborting due to previous error
```
  • Loading branch information
bors committed Feb 28, 2020
2 parents e2223c9 + c6cfcf9 commit eaa02f5
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 314 deletions.
22 changes: 20 additions & 2 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self_ty: Option<Ty<'tcx>>,
arg_count_correct: bool,
args_for_def_id: impl Fn(DefId) -> (Option<&'b GenericArgs<'b>>, bool),
provided_kind: impl Fn(&GenericParamDef, &GenericArg<'_>) -> subst::GenericArg<'tcx>,
mut provided_kind: impl FnMut(&GenericParamDef, &GenericArg<'_>) -> subst::GenericArg<'tcx>,
mut inferred_kind: impl FnMut(
Option<&[subst::GenericArg<'tcx>]>,
&GenericParamDef,
Expand Down Expand Up @@ -751,6 +751,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
};

let mut missing_type_params = vec![];
let mut inferred_params = vec![];
let substs = Self::create_substs_for_generic_args(
tcx,
def_id,
Expand All @@ -773,7 +774,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.ast_region_to_region(&lt, Some(param)).into()
}
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
self.ast_ty_to_ty(&ty).into()
if let (hir::TyKind::Infer, false) = (&ty.kind, self.allow_ty_infer()) {
inferred_params.push(ty.span);
tcx.types.err.into()
} else {
self.ast_ty_to_ty(&ty).into()
}
}
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
self.ast_const_to_const(&ct.value, tcx.type_of(param.def_id)).into()
Expand Down Expand Up @@ -832,6 +838,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
},
);
if !inferred_params.is_empty() {
// 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.
crate::collect::placeholder_type_error(
tcx,
inferred_params[0],
&[],
inferred_params,
false,
);
}

self.complain_about_missing_type_params(
missing_type_params,
Expand Down
36 changes: 27 additions & 9 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
}

fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
placeholder_type_error(self.tcx(), span, &[], vec![span], false);
self.tcx().sess.delay_span_bug(span, "bad placeholder type");
self.tcx().types.err
}

Expand Down 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
5 changes: 4 additions & 1 deletion src/test/ui/did_you_mean/bad-assoc-ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type D = (u8, u8)::AssocTy;
type E = _::AssocTy;
//~^ ERROR missing angle brackets in associated item path
//~| ERROR the type placeholder `_` is not allowed within types on item signatures
//~| ERROR the type placeholder `_` is not allowed within types on item signatures

type F = &'static (u8)::AssocTy;
//~^ ERROR missing angle brackets in associated item path
Expand Down Expand Up @@ -46,4 +45,8 @@ type I = ty!()::AssocTy;
//~^ ERROR missing angle brackets in associated item path
//~| ERROR ambiguous associated type

trait K<A, B> {}
fn foo<X: K<_, _>>(x: X) {}
//~^ ERROR the type placeholder `_` is not allowed within types on item signatures

fn main() {}
32 changes: 17 additions & 15 deletions src/test/ui/did_you_mean/bad-assoc-ty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ LL | type E = _::AssocTy;
| ^^^^^^^^^^ help: try: `<_>::AssocTy`

error: missing angle brackets in associated item path
--> $DIR/bad-assoc-ty.rs:22:19
--> $DIR/bad-assoc-ty.rs:21:19
|
LL | type F = &'static (u8)::AssocTy;
| ^^^^^^^^^^^^^ help: try: `<(u8)>::AssocTy`

error: missing angle brackets in associated item path
--> $DIR/bad-assoc-ty.rs:28:10
--> $DIR/bad-assoc-ty.rs:27:10
|
LL | type G = dyn 'static + (Send)::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `<dyn 'static + (Send)>::AssocTy`

error: missing angle brackets in associated item path
--> $DIR/bad-assoc-ty.rs:45:10
--> $DIR/bad-assoc-ty.rs:44:10
|
LL | type I = ty!()::AssocTy;
| ^^^^^^^^^^^^^^ help: try: `<ty!()>::AssocTy`

error: missing angle brackets in associated item path
--> $DIR/bad-assoc-ty.rs:38:19
--> $DIR/bad-assoc-ty.rs:37:19
|
LL | ($ty: ty) => ($ty::AssocTy);
| ^^^^^^^^^^^^ help: try: `<$ty>::AssocTy`
Expand Down Expand Up @@ -87,32 +87,26 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa
LL | type E = _::AssocTy;
| ^ not allowed in type signatures

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/bad-assoc-ty.rs:17:10
|
LL | type E = _::AssocTy;
| ^ not allowed in type signatures

error[E0223]: ambiguous associated type
--> $DIR/bad-assoc-ty.rs:22:19
--> $DIR/bad-assoc-ty.rs:21:19
|
LL | type F = &'static (u8)::AssocTy;
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<u8 as Trait>::AssocTy`

error[E0223]: ambiguous associated type
--> $DIR/bad-assoc-ty.rs:28:10
--> $DIR/bad-assoc-ty.rs:27:10
|
LL | type G = dyn 'static + (Send)::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<(dyn std::marker::Send + 'static) as Trait>::AssocTy`

error[E0223]: ambiguous associated type
--> $DIR/bad-assoc-ty.rs:34:10
--> $DIR/bad-assoc-ty.rs:33:10
|
LL | type H = Fn(u8) -> (u8)::Output;
| ^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<(dyn std::ops::Fn(u8) -> u8 + 'static) as Trait>::Output`

error[E0223]: ambiguous associated type
--> $DIR/bad-assoc-ty.rs:38:19
--> $DIR/bad-assoc-ty.rs:37:19
|
LL | ($ty: ty) => ($ty::AssocTy);
| ^^^^^^^^^^^^ help: use fully-qualified syntax: `<u8 as Trait>::AssocTy`
Expand All @@ -123,11 +117,19 @@ LL | type J = ty!(u8);
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0223]: ambiguous associated type
--> $DIR/bad-assoc-ty.rs:45:10
--> $DIR/bad-assoc-ty.rs:44:10
|
LL | type I = ty!()::AssocTy;
| ^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<u8 as Trait>::AssocTy`

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/bad-assoc-ty.rs:49:13
|
LL | fn foo<X: K<_, _>>(x: X) {}
| ^ ^ not allowed in type signatures
| |
| not allowed in type signatures

error: aborting due to 20 previous errors

Some errors have detailed explanations: E0121, E0223.
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/self/self-infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ struct S;

impl S {
fn f(self: _) {} //~ERROR the type placeholder `_` is not allowed within types on item sig
//~^ ERROR the type placeholder `_` is not allowed within types on item sig
fn g(self: &_) {} //~ERROR the type placeholder `_` is not allowed within types on item sig
//~^ ERROR the type placeholder `_` is not allowed within types on item sig
}

fn main() {}
16 changes: 2 additions & 14 deletions src/test/ui/self/self-infer.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/self-infer.rs:4:16
|
LL | fn f(self: _) {}
| ^ not allowed in type signatures

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/self-infer.rs:4:16
|
Expand All @@ -16,13 +10,7 @@ LL | fn f<T>(self: T) {}
| ^^^ ^

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/self-infer.rs:6:17
|
LL | fn g(self: &_) {}
| ^ not allowed in type signatures

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> $DIR/self-infer.rs:6:17
--> $DIR/self-infer.rs:5:17
|
LL | fn g(self: &_) {}
| ^ not allowed in type signatures
Expand All @@ -32,6 +20,6 @@ help: use type parameters instead
LL | fn g<T>(self: &T) {}
| ^^^ ^

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0121`.
Loading

0 comments on commit eaa02f5

Please sign in to comment.