Skip to content

Commit 67a30d2

Browse files
author
Alexander Regueiro
committed
Fixed ICE when type arguments are specified on Self type.
1 parent 6a3f96d commit 67a30d2

File tree

4 files changed

+71
-31
lines changed

4 files changed

+71
-31
lines changed

src/librustc_typeck/astconv.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
14311431
self.normalize_ty(span, tcx.mk_projection(item_def_id, trait_ref.substs))
14321432
}
14331433

1434-
pub fn prohibit_generics<'a, T: IntoIterator<Item = &'a hir::PathSegment>>(&self, segments: T) {
1434+
pub fn prohibit_generics<'a, T: IntoIterator<Item = &'a hir::PathSegment>>(
1435+
&self, segments: T) -> bool {
1436+
let mut has_err = false;
14351437
for segment in segments {
14361438
segment.with_generic_args(|generic_args| {
14371439
let (mut err_for_lt, mut err_for_ty) = (false, false);
@@ -1440,6 +1442,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
14401442
hir::GenericArg::Lifetime(lt) => {
14411443
if err_for_lt { continue }
14421444
err_for_lt = true;
1445+
has_err = true;
14431446
(struct_span_err!(self.tcx().sess, lt.span, E0110,
14441447
"lifetime arguments are not allowed on this entity"),
14451448
lt.span,
@@ -1448,6 +1451,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
14481451
hir::GenericArg::Type(ty) => {
14491452
if err_for_ty { continue }
14501453
err_for_ty = true;
1454+
has_err = true;
14511455
(struct_span_err!(self.tcx().sess, ty.span, E0109,
14521456
"type arguments are not allowed on this entity"),
14531457
ty.span,
@@ -1461,11 +1465,13 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
14611465
}
14621466
}
14631467
for binding in &generic_args.bindings {
1468+
has_err = true;
14641469
Self::prohibit_assoc_ty_binding(self.tcx(), binding.span);
14651470
break;
14661471
}
14671472
})
14681473
}
1474+
has_err
14691475
}
14701476

14711477
pub fn prohibit_assoc_ty_binding(tcx: TyCtxt, span: Span) {

src/librustc_typeck/check/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -5123,13 +5123,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
51235123
// errors if type parameters are provided in an inappropriate place.
51245124

51255125
let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect();
5126-
AstConv::prohibit_generics(self, segments.iter().enumerate().filter_map(|(index, seg)| {
5126+
let generics_has_err = AstConv::prohibit_generics(
5127+
self, segments.iter().enumerate().filter_map(|(index, seg)| {
51275128
if !generic_segs.contains(&index) || is_alias_variant_ctor {
51285129
Some(seg)
51295130
} else {
51305131
None
51315132
}
51325133
}));
5134+
if generics_has_err {
5135+
// Don't try to infer type parameters when prohibited generic arguments were given.
5136+
user_self_ty = None;
5137+
}
51335138

51345139
match def {
51355140
Def::Local(nid) | Def::Upvar(nid, ..) => {
@@ -5301,9 +5306,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
53015306

53025307
if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
53035308
// In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method`
5304-
// is inherent, there is no `Self` parameter, instead, the impl needs
5309+
// is inherent, there is no `Self` parameter; instead, the impl needs
53055310
// type parameters, which we can infer by unifying the provided `Self`
53065311
// with the substituted impl type.
5312+
// This also occurs for an enum variant on a type alias.
53075313
let ty = tcx.type_of(impl_def_id);
53085314

53095315
let impl_ty = self.instantiate_type_scheme(span, &substs, &ty);

src/test/ui/enum-variant-generic-args.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ type AliasFixed = Enum<()>;
77
impl<T> Enum<T> {
88
fn ts_variant() {
99
Self::TSVariant(());
10-
//~^ ERROR type parameters are not allowed on this name [E0109]
10+
//~^ ERROR mismatched types [E0308]
1111
Self::TSVariant::<()>(());
1212
//~^ ERROR type arguments are not allowed on this entity [E0109]
1313
Self::<()>::TSVariant(());
1414
//~^ ERROR type arguments are not allowed on this entity [E0109]
15+
//~^^ ERROR mismatched types [E0308]
1516
Self::<()>::TSVariant::<()>(());
1617
//~^ ERROR type arguments are not allowed on this entity [E0109]
1718
//~^^ ERROR type arguments are not allowed on this entity [E0109]
1819
}
1920

2021
fn s_variant() {
2122
Self::SVariant { v: () };
22-
//~^ ERROR type parameters are not allowed on this name [E0109]
23+
//~^ ERROR mismatched types [E0308]
2324
Self::SVariant::<()> { v: () };
2425
//~^ ERROR type arguments are not allowed on this entity [E0109]
2526
//~^^ ERROR mismatched types [E0308]
+53-26
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,62 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/enum-variant-generic-args.rs:9:25
3+
|
4+
LL | Self::TSVariant(());
5+
| ^^ expected type parameter, found ()
6+
|
7+
= note: expected type `T`
8+
found type `()`
9+
110
error[E0109]: type arguments are not allowed on this entity
2-
--> $DIR/enum-variant-generic-args.rs:9:27
11+
--> $DIR/enum-variant-generic-args.rs:11:27
312
|
413
LL | Self::TSVariant::<()>(());
514
| ^^ type argument not allowed
615

716
error[E0109]: type arguments are not allowed on this entity
8-
--> $DIR/enum-variant-generic-args.rs:11:16
17+
--> $DIR/enum-variant-generic-args.rs:13:16
918
|
1019
LL | Self::<()>::TSVariant(());
1120
| ^^ type argument not allowed
1221

22+
error[E0308]: mismatched types
23+
--> $DIR/enum-variant-generic-args.rs:13:31
24+
|
25+
LL | Self::<()>::TSVariant(());
26+
| ^^ expected type parameter, found ()
27+
|
28+
= note: expected type `T`
29+
found type `()`
30+
1331
error[E0109]: type arguments are not allowed on this entity
14-
--> $DIR/enum-variant-generic-args.rs:13:16
32+
--> $DIR/enum-variant-generic-args.rs:16:16
1533
|
1634
LL | Self::<()>::TSVariant::<()>(());
1735
| ^^ type argument not allowed
1836

1937
error[E0109]: type arguments are not allowed on this entity
20-
--> $DIR/enum-variant-generic-args.rs:13:33
38+
--> $DIR/enum-variant-generic-args.rs:16:33
2139
|
2240
LL | Self::<()>::TSVariant::<()>(());
2341
| ^^ type argument not allowed
2442

43+
error[E0308]: mismatched types
44+
--> $DIR/enum-variant-generic-args.rs:22:29
45+
|
46+
LL | Self::SVariant { v: () };
47+
| ^^ expected type parameter, found ()
48+
|
49+
= note: expected type `T`
50+
found type `()`
51+
2552
error[E0109]: type arguments are not allowed on this entity
26-
--> $DIR/enum-variant-generic-args.rs:19:26
53+
--> $DIR/enum-variant-generic-args.rs:24:26
2754
|
2855
LL | Self::SVariant::<()> { v: () };
2956
| ^^ type argument not allowed
3057

3158
error[E0308]: mismatched types
32-
--> $DIR/enum-variant-generic-args.rs:19:35
59+
--> $DIR/enum-variant-generic-args.rs:24:35
3360
|
3461
LL | Self::SVariant::<()> { v: () };
3562
| ^^ expected type parameter, found ()
@@ -38,13 +65,13 @@ LL | Self::SVariant::<()> { v: () };
3865
found type `()`
3966

4067
error[E0109]: type arguments are not allowed on this entity
41-
--> $DIR/enum-variant-generic-args.rs:22:16
68+
--> $DIR/enum-variant-generic-args.rs:27:16
4269
|
4370
LL | Self::<()>::SVariant { v: () };
4471
| ^^ type argument not allowed
4572

4673
error[E0308]: mismatched types
47-
--> $DIR/enum-variant-generic-args.rs:22:35
74+
--> $DIR/enum-variant-generic-args.rs:27:35
4875
|
4976
LL | Self::<()>::SVariant { v: () };
5077
| ^^ expected type parameter, found ()
@@ -53,19 +80,19 @@ LL | Self::<()>::SVariant { v: () };
5380
found type `()`
5481

5582
error[E0109]: type arguments are not allowed on this entity
56-
--> $DIR/enum-variant-generic-args.rs:25:16
83+
--> $DIR/enum-variant-generic-args.rs:30:16
5784
|
5885
LL | Self::<()>::SVariant::<()> { v: () };
5986
| ^^ type argument not allowed
6087

6188
error[E0109]: type arguments are not allowed on this entity
62-
--> $DIR/enum-variant-generic-args.rs:25:32
89+
--> $DIR/enum-variant-generic-args.rs:30:32
6390
|
6491
LL | Self::<()>::SVariant::<()> { v: () };
6592
| ^^ type argument not allowed
6693

6794
error[E0308]: mismatched types
68-
--> $DIR/enum-variant-generic-args.rs:25:41
95+
--> $DIR/enum-variant-generic-args.rs:30:41
6996
|
7097
LL | Self::<()>::SVariant::<()> { v: () };
7198
| ^^ expected type parameter, found ()
@@ -74,90 +101,90 @@ LL | Self::<()>::SVariant::<()> { v: () };
74101
found type `()`
75102

76103
error[E0109]: type arguments are not allowed on this entity
77-
--> $DIR/enum-variant-generic-args.rs:35:29
104+
--> $DIR/enum-variant-generic-args.rs:40:29
78105
|
79106
LL | Enum::<()>::TSVariant::<()>(());
80107
| ^^ type argument not allowed
81108

82109
error[E0109]: type arguments are not allowed on this entity
83-
--> $DIR/enum-variant-generic-args.rs:38:24
110+
--> $DIR/enum-variant-generic-args.rs:43:24
84111
|
85112
LL | Alias::TSVariant::<()>(());
86113
| ^^ type argument not allowed
87114

88115
error[E0109]: type arguments are not allowed on this entity
89-
--> $DIR/enum-variant-generic-args.rs:40:30
116+
--> $DIR/enum-variant-generic-args.rs:45:30
90117
|
91118
LL | Alias::<()>::TSVariant::<()>(());
92119
| ^^ type argument not allowed
93120

94121
error[E0109]: type arguments are not allowed on this entity
95-
--> $DIR/enum-variant-generic-args.rs:43:29
122+
--> $DIR/enum-variant-generic-args.rs:48:29
96123
|
97124
LL | AliasFixed::TSVariant::<()>(());
98125
| ^^ type argument not allowed
99126

100127
error[E0107]: wrong number of type arguments: expected 0, found 1
101-
--> $DIR/enum-variant-generic-args.rs:45:18
128+
--> $DIR/enum-variant-generic-args.rs:50:18
102129
|
103130
LL | AliasFixed::<()>::TSVariant(());
104131
| ^^ unexpected type argument
105132

106133
error[E0107]: wrong number of type arguments: expected 0, found 1
107-
--> $DIR/enum-variant-generic-args.rs:47:18
134+
--> $DIR/enum-variant-generic-args.rs:52:18
108135
|
109136
LL | AliasFixed::<()>::TSVariant::<()>(());
110137
| ^^ unexpected type argument
111138

112139
error[E0109]: type arguments are not allowed on this entity
113-
--> $DIR/enum-variant-generic-args.rs:47:35
140+
--> $DIR/enum-variant-generic-args.rs:52:35
114141
|
115142
LL | AliasFixed::<()>::TSVariant::<()>(());
116143
| ^^ type argument not allowed
117144

118145
error[E0109]: type arguments are not allowed on this entity
119-
--> $DIR/enum-variant-generic-args.rs:53:28
146+
--> $DIR/enum-variant-generic-args.rs:58:28
120147
|
121148
LL | Enum::<()>::SVariant::<()> { v: () };
122149
| ^^ type argument not allowed
123150

124151
error[E0109]: type arguments are not allowed on this entity
125-
--> $DIR/enum-variant-generic-args.rs:56:23
152+
--> $DIR/enum-variant-generic-args.rs:61:23
126153
|
127154
LL | Alias::SVariant::<()> { v: () };
128155
| ^^ type argument not allowed
129156

130157
error[E0109]: type arguments are not allowed on this entity
131-
--> $DIR/enum-variant-generic-args.rs:58:29
158+
--> $DIR/enum-variant-generic-args.rs:63:29
132159
|
133160
LL | Alias::<()>::SVariant::<()> { v: () };
134161
| ^^ type argument not allowed
135162

136163
error[E0109]: type arguments are not allowed on this entity
137-
--> $DIR/enum-variant-generic-args.rs:61:28
164+
--> $DIR/enum-variant-generic-args.rs:66:28
138165
|
139166
LL | AliasFixed::SVariant::<()> { v: () };
140167
| ^^ type argument not allowed
141168

142169
error[E0107]: wrong number of type arguments: expected 0, found 1
143-
--> $DIR/enum-variant-generic-args.rs:63:18
170+
--> $DIR/enum-variant-generic-args.rs:68:18
144171
|
145172
LL | AliasFixed::<()>::SVariant { v: () };
146173
| ^^ unexpected type argument
147174

148175
error[E0107]: wrong number of type arguments: expected 0, found 1
149-
--> $DIR/enum-variant-generic-args.rs:65:18
176+
--> $DIR/enum-variant-generic-args.rs:70:18
150177
|
151178
LL | AliasFixed::<()>::SVariant::<()> { v: () };
152179
| ^^ unexpected type argument
153180

154181
error[E0109]: type arguments are not allowed on this entity
155-
--> $DIR/enum-variant-generic-args.rs:65:34
182+
--> $DIR/enum-variant-generic-args.rs:70:34
156183
|
157184
LL | AliasFixed::<()>::SVariant::<()> { v: () };
158185
| ^^ type argument not allowed
159186

160-
error: aborting due to 25 previous errors
187+
error: aborting due to 28 previous errors
161188

162189
Some errors occurred: E0107, E0109, E0308.
163190
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)