Skip to content

Commit 9321d55

Browse files
oli-obkpietroalbini
authored andcommitted
Turn ICE on type arguments on variables into an error
1 parent b87df1c commit 9321d55

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5195,7 +5195,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
51955195
&self,
51965196
res: Res,
51975197
span: Span,
5198-
) -> Result<(DefKind, DefId, Ty<'tcx>), ErrorReported> {
5198+
) -> Result<Res, ErrorReported> {
51995199
let tcx = self.tcx;
52005200
if let Res::SelfCtor(impl_def_id) = res {
52015201
let ty = self.impl_self_ty(span, impl_def_id).ty;
@@ -5205,11 +5205,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
52055205
Some(adt_def) if adt_def.has_ctor() => {
52065206
let variant = adt_def.non_enum_variant();
52075207
let ctor_def_id = variant.ctor_def_id.unwrap();
5208-
Ok((
5209-
DefKind::Ctor(CtorOf::Struct, variant.ctor_kind),
5210-
ctor_def_id,
5211-
tcx.type_of(ctor_def_id),
5212-
))
5208+
Ok(Res::Def(DefKind::Ctor(CtorOf::Struct, variant.ctor_kind), ctor_def_id))
52135209
}
52145210
_ => {
52155211
let mut err = tcx.sess.struct_span_err(span,
@@ -5236,15 +5232,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
52365232
}
52375233
}
52385234
} else {
5239-
match res {
5240-
Res::Def(kind, def_id) => {
5241-
// The things we are substituting into the type should not contain
5242-
// escaping late-bound regions, and nor should the base type scheme.
5243-
let ty = tcx.type_of(def_id);
5244-
Ok((kind, def_id, ty))
5245-
}
5246-
_ => span_bug!(span, "unexpected res in rewrite_self_ctor: {:?}", res),
5247-
}
5235+
Ok(res)
52485236
}
52495237
}
52505238

@@ -5267,27 +5255,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
52675255

52685256
let tcx = self.tcx;
52695257

5270-
match res {
5271-
Res::Local(hid) | Res::Upvar(hid, ..) => {
5272-
let ty = self.local_ty(span, hid).decl_ty;
5273-
let ty = self.normalize_associated_types_in(span, &ty);
5274-
self.write_ty(hir_id, ty);
5275-
return (ty, res);
5276-
}
5277-
_ => {}
5278-
}
5279-
5280-
let (kind, def_id, ty) = match self.rewrite_self_ctor(res, span) {
5281-
Ok(result) => result,
5258+
let res = match self.rewrite_self_ctor(res, span) {
5259+
Ok(res) => res,
52825260
Err(ErrorReported) => return (tcx.types.err, res),
52835261
};
5284-
let path_segs =
5285-
AstConv::def_ids_for_value_path_segments(self, segments, self_ty, kind, def_id);
5262+
let path_segs = match res {
5263+
Res::Local(_) | Res::Upvar(..) => Vec::new(),
5264+
Res::Def(kind, def_id) =>
5265+
AstConv::def_ids_for_value_path_segments(self, segments, self_ty, kind, def_id),
5266+
_ => bug!("instantiate_value_path on {:?}", res),
5267+
};
52865268

52875269
let mut user_self_ty = None;
52885270
let mut is_alias_variant_ctor = false;
5289-
match kind {
5290-
DefKind::Ctor(CtorOf::Variant, _) => {
5271+
match res {
5272+
Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) => {
52915273
if let Some(self_ty) = self_ty {
52925274
let adt_def = self_ty.ty_adt_def().unwrap();
52935275
user_self_ty = Some(UserSelfTy {
@@ -5297,8 +5279,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
52975279
is_alias_variant_ctor = true;
52985280
}
52995281
}
5300-
DefKind::Method
5301-
| DefKind::AssociatedConst => {
5282+
Res::Def(DefKind::Method, def_id)
5283+
| Res::Def(DefKind::AssociatedConst, def_id) => {
53025284
let container = tcx.associated_item(def_id).container;
53035285
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
53045286
match container {
@@ -5338,6 +5320,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
53385320
None
53395321
}
53405322
}));
5323+
5324+
match res {
5325+
Res::Local(hid) | Res::Upvar(hid, ..) => {
5326+
let ty = self.local_ty(span, hid).decl_ty;
5327+
let ty = self.normalize_associated_types_in(span, &ty);
5328+
self.write_ty(hir_id, ty);
5329+
return (ty, res);
5330+
}
5331+
_ => {}
5332+
}
5333+
53415334
if generics_has_err {
53425335
// Don't try to infer type parameters when prohibited generic arguments were given.
53435336
user_self_ty = None;
@@ -5375,6 +5368,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
53755368
tcx.generics_of(*def_id).has_self
53765369
}).unwrap_or(false);
53775370

5371+
let def_id = res.def_id();
5372+
5373+
// The things we are substituting into the type should not contain
5374+
// escaping late-bound regions, and nor should the base type scheme.
5375+
let ty = tcx.type_of(def_id);
5376+
53785377
let substs = AstConv::create_substs_for_generic_args(
53795378
tcx,
53805379
def_id,
@@ -5491,7 +5490,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
54915490
ty_substituted);
54925491
self.write_substs(hir_id, substs);
54935492

5494-
(ty_substituted, Res::Def(kind, def_id))
5493+
(ty_substituted, res)
54955494
}
54965495

54975496
fn check_rustc_args_require_const(&self,

src/test/ui/issues/issue-60989.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct A {}
2+
struct B {}
3+
4+
impl From<A> for B {
5+
fn from(a: A) -> B {
6+
B{}
7+
}
8+
}
9+
10+
fn main() {
11+
let c1 = ();
12+
c1::<()>;
13+
//~^ ERROR type arguments are not allowed for this type
14+
15+
let c1 = A {};
16+
c1::<Into<B>>;
17+
//~^ ERROR type arguments are not allowed for this type
18+
}

src/test/ui/issues/issue-60989.stderr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0109]: type arguments are not allowed for this type
2+
--> $DIR/issue-60989.rs:12:10
3+
|
4+
LL | c1::<()>;
5+
| ^^ type argument not allowed
6+
7+
error[E0109]: type arguments are not allowed for this type
8+
--> $DIR/issue-60989.rs:16:10
9+
|
10+
LL | c1::<Into<B>>;
11+
| ^^^^^^^ type argument not allowed
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0109`.

0 commit comments

Comments
 (0)