@@ -5190,52 +5190,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5190
5190
Some ( original_span. with_lo ( original_span. hi ( ) - BytePos ( 1 ) ) )
5191
5191
}
5192
5192
5193
- // Rewrite `SelfCtor` to `Ctor`
5194
- pub fn rewrite_self_ctor (
5195
- & self ,
5196
- res : Res ,
5197
- span : Span ,
5198
- ) -> Result < Res , ErrorReported > {
5199
- let tcx = self . tcx ;
5200
- if let Res :: SelfCtor ( impl_def_id) = res {
5201
- let ty = self . impl_self_ty ( span, impl_def_id) . ty ;
5202
- let adt_def = ty. ty_adt_def ( ) ;
5203
-
5204
- match adt_def {
5205
- Some ( adt_def) if adt_def. has_ctor ( ) => {
5206
- let variant = adt_def. non_enum_variant ( ) ;
5207
- let ctor_def_id = variant. ctor_def_id . unwrap ( ) ;
5208
- Ok ( Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , variant. ctor_kind ) , ctor_def_id) )
5209
- }
5210
- _ => {
5211
- let mut err = tcx. sess . struct_span_err ( span,
5212
- "the `Self` constructor can only be used with tuple or unit structs" ) ;
5213
- if let Some ( adt_def) = adt_def {
5214
- match adt_def. adt_kind ( ) {
5215
- AdtKind :: Enum => {
5216
- err. help ( "did you mean to use one of the enum's variants?" ) ;
5217
- } ,
5218
- AdtKind :: Struct |
5219
- AdtKind :: Union => {
5220
- err. span_suggestion (
5221
- span,
5222
- "use curly brackets" ,
5223
- String :: from ( "Self { /* fields */ }" ) ,
5224
- Applicability :: HasPlaceholders ,
5225
- ) ;
5226
- }
5227
- }
5228
- }
5229
- err. emit ( ) ;
5230
-
5231
- Err ( ErrorReported )
5232
- }
5233
- }
5234
- } else {
5235
- Ok ( res)
5236
- }
5237
- }
5238
-
5239
5193
// Instantiates the given path, which must refer to an item with the given
5240
5194
// number of type parameters and type.
5241
5195
pub fn instantiate_value_path ( & self ,
@@ -5255,12 +5209,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5255
5209
5256
5210
let tcx = self . tcx ;
5257
5211
5258
- let res = match self . rewrite_self_ctor ( res, span) {
5259
- Ok ( res) => res,
5260
- Err ( ErrorReported ) => return ( tcx. types . err , res) ,
5261
- } ;
5262
5212
let path_segs = match res {
5263
- Res :: Local ( _) | Res :: Upvar ( ..) => Vec :: new ( ) ,
5213
+ Res :: Local ( _) | Res :: Upvar ( ..) | Res :: SelfCtor ( _ ) => vec ! [ ] ,
5264
5214
Res :: Def ( kind, def_id) =>
5265
5215
AstConv :: def_ids_for_value_path_segments ( self , segments, self_ty, kind, def_id) ,
5266
5216
_ => bug ! ( "instantiate_value_path on {:?}" , res) ,
@@ -5368,13 +5318,53 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5368
5318
tcx. generics_of ( * def_id) . has_self
5369
5319
} ) . unwrap_or ( false ) ;
5370
5320
5321
+ let ( res, self_ctor_substs) = if let Res :: SelfCtor ( impl_def_id) = res {
5322
+ let ty = self . impl_self_ty ( span, impl_def_id) . ty ;
5323
+ let adt_def = ty. ty_adt_def ( ) ;
5324
+
5325
+ match ty. sty {
5326
+ ty:: Adt ( adt_def, substs) if adt_def. has_ctor ( ) => {
5327
+ let variant = adt_def. non_enum_variant ( ) ;
5328
+ let ctor_def_id = variant. ctor_def_id . unwrap ( ) ;
5329
+ (
5330
+ Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , variant. ctor_kind ) , ctor_def_id) ,
5331
+ Some ( substs) ,
5332
+ )
5333
+ }
5334
+ _ => {
5335
+ let mut err = tcx. sess . struct_span_err ( span,
5336
+ "the `Self` constructor can only be used with tuple or unit structs" ) ;
5337
+ if let Some ( adt_def) = adt_def {
5338
+ match adt_def. adt_kind ( ) {
5339
+ AdtKind :: Enum => {
5340
+ err. help ( "did you mean to use one of the enum's variants?" ) ;
5341
+ } ,
5342
+ AdtKind :: Struct |
5343
+ AdtKind :: Union => {
5344
+ err. span_suggestion (
5345
+ span,
5346
+ "use curly brackets" ,
5347
+ String :: from ( "Self { /* fields */ }" ) ,
5348
+ Applicability :: HasPlaceholders ,
5349
+ ) ;
5350
+ }
5351
+ }
5352
+ }
5353
+ err. emit ( ) ;
5354
+
5355
+ return ( tcx. types . err , res)
5356
+ }
5357
+ }
5358
+ } else {
5359
+ ( res, None )
5360
+ } ;
5371
5361
let def_id = res. def_id ( ) ;
5372
5362
5373
5363
// The things we are substituting into the type should not contain
5374
5364
// escaping late-bound regions, and nor should the base type scheme.
5375
5365
let ty = tcx. type_of ( def_id) ;
5376
5366
5377
- let substs = AstConv :: create_substs_for_generic_args (
5367
+ let substs = self_ctor_substs . unwrap_or_else ( || AstConv :: create_substs_for_generic_args (
5378
5368
tcx,
5379
5369
def_id,
5380
5370
& [ ] [ ..] ,
@@ -5444,7 +5434,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5444
5434
}
5445
5435
}
5446
5436
} ,
5447
- ) ;
5437
+ ) ) ;
5448
5438
assert ! ( !substs. has_escaping_bound_vars( ) ) ;
5449
5439
assert ! ( !ty. has_escaping_bound_vars( ) ) ;
5450
5440
0 commit comments