@@ -5300,52 +5300,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5300
5300
Some ( original_span. with_lo ( original_span. hi ( ) - BytePos ( 1 ) ) )
5301
5301
}
5302
5302
5303
- // Rewrite `SelfCtor` to `Ctor`
5304
- pub fn rewrite_self_ctor (
5305
- & self ,
5306
- res : Res ,
5307
- span : Span ,
5308
- ) -> Result < Res , ErrorReported > {
5309
- let tcx = self . tcx ;
5310
- if let Res :: SelfCtor ( impl_def_id) = res {
5311
- let ty = self . impl_self_ty ( span, impl_def_id) . ty ;
5312
- let adt_def = ty. ty_adt_def ( ) ;
5313
-
5314
- match adt_def {
5315
- Some ( adt_def) if adt_def. has_ctor ( ) => {
5316
- let variant = adt_def. non_enum_variant ( ) ;
5317
- let ctor_def_id = variant. ctor_def_id . unwrap ( ) ;
5318
- Ok ( Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , variant. ctor_kind ) , ctor_def_id) )
5319
- }
5320
- _ => {
5321
- let mut err = tcx. sess . struct_span_err ( span,
5322
- "the `Self` constructor can only be used with tuple or unit structs" ) ;
5323
- if let Some ( adt_def) = adt_def {
5324
- match adt_def. adt_kind ( ) {
5325
- AdtKind :: Enum => {
5326
- err. help ( "did you mean to use one of the enum's variants?" ) ;
5327
- } ,
5328
- AdtKind :: Struct |
5329
- AdtKind :: Union => {
5330
- err. span_suggestion (
5331
- span,
5332
- "use curly brackets" ,
5333
- String :: from ( "Self { /* fields */ }" ) ,
5334
- Applicability :: HasPlaceholders ,
5335
- ) ;
5336
- }
5337
- }
5338
- }
5339
- err. emit ( ) ;
5340
-
5341
- Err ( ErrorReported )
5342
- }
5343
- }
5344
- } else {
5345
- Ok ( res)
5346
- }
5347
- }
5348
-
5349
5303
// Instantiates the given path, which must refer to an item with the given
5350
5304
// number of type parameters and type.
5351
5305
pub fn instantiate_value_path ( & self ,
@@ -5365,12 +5319,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5365
5319
5366
5320
let tcx = self . tcx ;
5367
5321
5368
- let res = match self . rewrite_self_ctor ( res, span) {
5369
- Ok ( res) => res,
5370
- Err ( ErrorReported ) => return ( tcx. types . err , res) ,
5371
- } ;
5372
5322
let path_segs = match res {
5373
- Res :: Local ( _) => vec ! [ ] ,
5323
+ Res :: Local ( _) | Res :: SelfCtor ( _ ) => vec ! [ ] ,
5374
5324
Res :: Def ( kind, def_id) =>
5375
5325
AstConv :: def_ids_for_value_path_segments ( self , segments, self_ty, kind, def_id) ,
5376
5326
_ => bug ! ( "instantiate_value_path on {:?}" , res) ,
@@ -5475,13 +5425,53 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5475
5425
tcx. generics_of ( * def_id) . has_self
5476
5426
} ) . unwrap_or ( false ) ;
5477
5427
5428
+ let ( res, self_ctor_substs) = if let Res :: SelfCtor ( impl_def_id) = res {
5429
+ let ty = self . impl_self_ty ( span, impl_def_id) . ty ;
5430
+ let adt_def = ty. ty_adt_def ( ) ;
5431
+
5432
+ match ty. sty {
5433
+ ty:: Adt ( adt_def, substs) if adt_def. has_ctor ( ) => {
5434
+ let variant = adt_def. non_enum_variant ( ) ;
5435
+ let ctor_def_id = variant. ctor_def_id . unwrap ( ) ;
5436
+ (
5437
+ Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , variant. ctor_kind ) , ctor_def_id) ,
5438
+ Some ( substs) ,
5439
+ )
5440
+ }
5441
+ _ => {
5442
+ let mut err = tcx. sess . struct_span_err ( span,
5443
+ "the `Self` constructor can only be used with tuple or unit structs" ) ;
5444
+ if let Some ( adt_def) = adt_def {
5445
+ match adt_def. adt_kind ( ) {
5446
+ AdtKind :: Enum => {
5447
+ err. help ( "did you mean to use one of the enum's variants?" ) ;
5448
+ } ,
5449
+ AdtKind :: Struct |
5450
+ AdtKind :: Union => {
5451
+ err. span_suggestion (
5452
+ span,
5453
+ "use curly brackets" ,
5454
+ String :: from ( "Self { /* fields */ }" ) ,
5455
+ Applicability :: HasPlaceholders ,
5456
+ ) ;
5457
+ }
5458
+ }
5459
+ }
5460
+ err. emit ( ) ;
5461
+
5462
+ return ( tcx. types . err , res)
5463
+ }
5464
+ }
5465
+ } else {
5466
+ ( res, None )
5467
+ } ;
5478
5468
let def_id = res. def_id ( ) ;
5479
5469
5480
5470
// The things we are substituting into the type should not contain
5481
5471
// escaping late-bound regions, and nor should the base type scheme.
5482
5472
let ty = tcx. type_of ( def_id) ;
5483
5473
5484
- let substs = AstConv :: create_substs_for_generic_args (
5474
+ let substs = self_ctor_substs . unwrap_or_else ( || AstConv :: create_substs_for_generic_args (
5485
5475
tcx,
5486
5476
def_id,
5487
5477
& [ ] [ ..] ,
@@ -5551,7 +5541,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5551
5541
}
5552
5542
}
5553
5543
} ,
5554
- ) ;
5544
+ ) ) ;
5555
5545
assert ! ( !substs. has_escaping_bound_vars( ) ) ;
5556
5546
assert ! ( !ty. has_escaping_bound_vars( ) ) ;
5557
5547
0 commit comments