@@ -3957,52 +3957,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3957
3957
Some ( original_span. with_lo ( original_span. hi ( ) - BytePos ( 1 ) ) )
3958
3958
}
3959
3959
3960
- // Rewrite `SelfCtor` to `Ctor`
3961
- pub fn rewrite_self_ctor (
3962
- & self ,
3963
- res : Res ,
3964
- span : Span ,
3965
- ) -> Result < Res , ErrorReported > {
3966
- let tcx = self . tcx ;
3967
- if let Res :: SelfCtor ( impl_def_id) = res {
3968
- let ty = self . impl_self_ty ( span, impl_def_id) . ty ;
3969
- let adt_def = ty. ty_adt_def ( ) ;
3970
-
3971
- match adt_def {
3972
- Some ( adt_def) if adt_def. has_ctor ( ) => {
3973
- let variant = adt_def. non_enum_variant ( ) ;
3974
- let ctor_def_id = variant. ctor_def_id . unwrap ( ) ;
3975
- Ok ( Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , variant. ctor_kind ) , ctor_def_id) )
3976
- }
3977
- _ => {
3978
- let mut err = tcx. sess . struct_span_err ( span,
3979
- "the `Self` constructor can only be used with tuple or unit structs" ) ;
3980
- if let Some ( adt_def) = adt_def {
3981
- match adt_def. adt_kind ( ) {
3982
- AdtKind :: Enum => {
3983
- err. help ( "did you mean to use one of the enum's variants?" ) ;
3984
- } ,
3985
- AdtKind :: Struct |
3986
- AdtKind :: Union => {
3987
- err. span_suggestion (
3988
- span,
3989
- "use curly brackets" ,
3990
- String :: from ( "Self { /* fields */ }" ) ,
3991
- Applicability :: HasPlaceholders ,
3992
- ) ;
3993
- }
3994
- }
3995
- }
3996
- err. emit ( ) ;
3997
-
3998
- Err ( ErrorReported )
3999
- }
4000
- }
4001
- } else {
4002
- Ok ( res)
4003
- }
4004
- }
4005
-
4006
3960
// Instantiates the given path, which must refer to an item with the given
4007
3961
// number of type parameters and type.
4008
3962
pub fn instantiate_value_path ( & self ,
@@ -4022,12 +3976,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4022
3976
4023
3977
let tcx = self . tcx ;
4024
3978
4025
- let res = match self . rewrite_self_ctor ( res, span) {
4026
- Ok ( res) => res,
4027
- Err ( ErrorReported ) => return ( tcx. types . err , res) ,
4028
- } ;
4029
3979
let path_segs = match res {
4030
- Res :: Local ( _) => vec ! [ ] ,
3980
+ Res :: Local ( _) | Res :: SelfCtor ( _ ) => vec ! [ ] ,
4031
3981
Res :: Def ( kind, def_id) =>
4032
3982
AstConv :: def_ids_for_value_path_segments ( self , segments, self_ty, kind, def_id) ,
4033
3983
_ => bug ! ( "instantiate_value_path on {:?}" , res) ,
@@ -4132,13 +4082,53 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4132
4082
tcx. generics_of ( * def_id) . has_self
4133
4083
} ) . unwrap_or ( false ) ;
4134
4084
4085
+ let ( res, self_ctor_substs) = if let Res :: SelfCtor ( impl_def_id) = res {
4086
+ let ty = self . impl_self_ty ( span, impl_def_id) . ty ;
4087
+ let adt_def = ty. ty_adt_def ( ) ;
4088
+
4089
+ match ty. sty {
4090
+ ty:: Adt ( adt_def, substs) if adt_def. has_ctor ( ) => {
4091
+ let variant = adt_def. non_enum_variant ( ) ;
4092
+ let ctor_def_id = variant. ctor_def_id . unwrap ( ) ;
4093
+ (
4094
+ Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , variant. ctor_kind ) , ctor_def_id) ,
4095
+ Some ( substs) ,
4096
+ )
4097
+ }
4098
+ _ => {
4099
+ let mut err = tcx. sess . struct_span_err ( span,
4100
+ "the `Self` constructor can only be used with tuple or unit structs" ) ;
4101
+ if let Some ( adt_def) = adt_def {
4102
+ match adt_def. adt_kind ( ) {
4103
+ AdtKind :: Enum => {
4104
+ err. help ( "did you mean to use one of the enum's variants?" ) ;
4105
+ } ,
4106
+ AdtKind :: Struct |
4107
+ AdtKind :: Union => {
4108
+ err. span_suggestion (
4109
+ span,
4110
+ "use curly brackets" ,
4111
+ String :: from ( "Self { /* fields */ }" ) ,
4112
+ Applicability :: HasPlaceholders ,
4113
+ ) ;
4114
+ }
4115
+ }
4116
+ }
4117
+ err. emit ( ) ;
4118
+
4119
+ return ( tcx. types . err , res)
4120
+ }
4121
+ }
4122
+ } else {
4123
+ ( res, None )
4124
+ } ;
4135
4125
let def_id = res. def_id ( ) ;
4136
4126
4137
4127
// The things we are substituting into the type should not contain
4138
4128
// escaping late-bound regions, and nor should the base type scheme.
4139
4129
let ty = tcx. type_of ( def_id) ;
4140
4130
4141
- let substs = AstConv :: create_substs_for_generic_args (
4131
+ let substs = self_ctor_substs . unwrap_or_else ( || AstConv :: create_substs_for_generic_args (
4142
4132
tcx,
4143
4133
def_id,
4144
4134
& [ ] [ ..] ,
@@ -4208,7 +4198,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4208
4198
}
4209
4199
}
4210
4200
} ,
4211
- ) ;
4201
+ ) ) ;
4212
4202
assert ! ( !substs. has_escaping_bound_vars( ) ) ;
4213
4203
assert ! ( !ty. has_escaping_bound_vars( ) ) ;
4214
4204
0 commit comments