Skip to content

Commit 6a3f96d

Browse files
author
Alexander Regueiro
committed
Fixed type inference for tuple struct variants.
1 parent fa07e62 commit 6a3f96d

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
15511551
// Case 2. Reference to a variant constructor.
15521552
Def::Variant(def_id) |
15531553
Def::VariantCtor(def_id, ..) => {
1554-
let adt_def = self_ty.and_then(|t| t.ty_adt_def());
1554+
let adt_def = self_ty.map(|t| t.ty_adt_def().unwrap());
15551555
let (generics_def_id, index) = if let Some(adt_def) = adt_def {
15561556
debug_assert!(adt_def.is_enum());
15571557
(adt_def.did, last)

src/librustc_typeck/check/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5079,7 +5079,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
50795079
let path_segs = AstConv::def_ids_for_path_segments(self, segments, self_ty, def);
50805080

50815081
let mut user_self_ty = None;
5082+
let mut is_alias_variant_ctor = false;
50825083
match def {
5084+
Def::VariantCtor(_, _) => {
5085+
if let Some(self_ty) = self_ty {
5086+
let adt_def = self_ty.ty_adt_def().unwrap();
5087+
user_self_ty = Some(UserSelfTy {
5088+
impl_def_id: adt_def.did,
5089+
self_ty,
5090+
});
5091+
is_alias_variant_ctor = true;
5092+
}
5093+
}
50835094
Def::Method(def_id) |
50845095
Def::AssociatedConst(def_id) => {
50855096
let container = tcx.associated_item(def_id).container;
@@ -5111,12 +5122,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
51115122
// provided (if any) into their appropriate spaces. We'll also report
51125123
// errors if type parameters are provided in an inappropriate place.
51135124

5114-
let is_alias_variant_ctor =
5115-
match def {
5116-
Def::VariantCtor(_, _) if self_ty.is_some() => true,
5117-
_ => false,
5118-
};
5119-
51205125
let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect();
51215126
AstConv::prohibit_generics(self, segments.iter().enumerate().filter_map(|(index, seg)| {
51225127
if !generic_segs.contains(&index) || is_alias_variant_ctor {

src/test/run-pass/enum-variant-generic-args.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![allow(irrefutable_let_patterns)]
55

6+
#[allow(dead_code)]
67
enum Enum<T> { TSVariant(T), SVariant { v: T } }
78
type Alias<T> = Enum<T>;
89
type AliasFixed = Enum<()>;
@@ -16,16 +17,6 @@ macro_rules! is_variant {
1617
);
1718
}
1819

19-
impl<T> Enum<T> {
20-
fn ts_variant() {
21-
is_variant!(TSVariant, Self::TSVariant(()));
22-
}
23-
24-
fn s_variant() {
25-
is_variant!(SVariant, Self::SVariant { v: () });
26-
}
27-
}
28-
2920
fn main() {
3021
// Tuple struct variant
3122

@@ -38,8 +29,6 @@ fn main() {
3829

3930
is_variant!(TSVariant, AliasFixed::TSVariant(()));
4031

41-
Enum::<()>::ts_variant();
42-
4332
// Struct variant
4433

4534
is_variant!(SVariant, Enum::SVariant { v: () });
@@ -50,6 +39,4 @@ fn main() {
5039
is_variant!(SVariant, Alias::<()>::SVariant { v: () });
5140

5241
is_variant!(SVariant, AliasFixed::SVariant { v: () });
53-
54-
Enum::<()>::s_variant();
5542
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type AliasFixed = Enum<()>;
66

77
impl<T> Enum<T> {
88
fn ts_variant() {
9+
Self::TSVariant(());
10+
//~^ ERROR type parameters are not allowed on this name [E0109]
911
Self::TSVariant::<()>(());
1012
//~^ ERROR type arguments are not allowed on this entity [E0109]
1113
Self::<()>::TSVariant(());
@@ -16,6 +18,8 @@ impl<T> Enum<T> {
1618
}
1719

1820
fn s_variant() {
21+
Self::SVariant { v: () };
22+
//~^ ERROR type parameters are not allowed on this name [E0109]
1923
Self::SVariant::<()> { v: () };
2024
//~^ ERROR type arguments are not allowed on this entity [E0109]
2125
//~^^ ERROR mismatched types [E0308]

0 commit comments

Comments
 (0)