@@ -264,34 +264,31 @@ pub fn ast_path_substs_for_ty<'tcx>(
264
264
265
265
let ( regions, types, assoc_bindings) = match path. segments . last ( ) . unwrap ( ) . parameters {
266
266
ast:: AngleBracketedParameters ( ref data) => {
267
- convert_angle_bracketed_parameters ( this, rscope, data)
267
+ convert_angle_bracketed_parameters ( this, rscope, path . span , decl_generics , data)
268
268
}
269
269
ast:: ParenthesizedParameters ( ref data) => {
270
270
span_err ! ( tcx. sess, path. span, E0214 ,
271
271
"parenthesized parameters may only be used with a trait" ) ;
272
- convert_parenthesized_parameters ( this, data)
272
+ convert_parenthesized_parameters ( this, rscope , path . span , decl_generics , data)
273
273
}
274
274
} ;
275
275
276
276
prohibit_projections ( this. tcx ( ) , & assoc_bindings) ;
277
277
278
278
create_substs_for_ast_path ( this,
279
- rscope,
280
279
path. span ,
281
280
decl_generics,
282
281
None ,
283
282
types,
284
283
regions)
285
284
}
286
285
287
- fn create_substs_for_ast_path < ' tcx > (
286
+ fn create_region_substs < ' tcx > (
288
287
this : & AstConv < ' tcx > ,
289
288
rscope : & RegionScope ,
290
289
span : Span ,
291
290
decl_generics : & ty:: Generics < ' tcx > ,
292
- self_ty : Option < Ty < ' tcx > > ,
293
- types : Vec < Ty < ' tcx > > ,
294
- regions : Vec < ty:: Region > )
291
+ regions_provided : Vec < ty:: Region > )
295
292
-> Substs < ' tcx >
296
293
{
297
294
let tcx = this. tcx ( ) ;
@@ -300,9 +297,9 @@ fn create_substs_for_ast_path<'tcx>(
300
297
// region with the current anon region binding (in other words,
301
298
// whatever & would get replaced with).
302
299
let expected_num_region_params = decl_generics. regions . len ( TypeSpace ) ;
303
- let supplied_num_region_params = regions . len ( ) ;
300
+ let supplied_num_region_params = regions_provided . len ( ) ;
304
301
let regions = if expected_num_region_params == supplied_num_region_params {
305
- regions
302
+ regions_provided
306
303
} else {
307
304
let anon_regions =
308
305
rscope. anon_regions ( span, expected_num_region_params) ;
@@ -314,51 +311,82 @@ fn create_substs_for_ast_path<'tcx>(
314
311
}
315
312
316
313
match anon_regions {
317
- Ok ( v) => v. into_iter ( ) . collect ( ) ,
318
- Err ( _) => ( 0 ..expected_num_region_params)
319
- . map ( |_| ty:: ReStatic ) . collect ( ) // hokey
314
+ Ok ( anon_regions) => anon_regions,
315
+ Err ( _) => ( 0 ..expected_num_region_params) . map ( |_| ty:: ReStatic ) . collect ( )
320
316
}
321
317
} ;
318
+ Substs :: new_type ( vec ! [ ] , regions)
319
+ }
320
+
321
+ /// Given the type/region arguments provided to some path (along with
322
+ /// an implicit Self, if this is a trait reference) returns the complete
323
+ /// set of substitutions. This may involve applying defaulted type parameters.
324
+ ///
325
+ /// Note that the type listing given here is *exactly* what the user provided.
326
+ ///
327
+ /// The `region_substs` should be the result of `create_region_substs`
328
+ /// -- that is, a substitution with no types but the correct number of
329
+ /// regions.
330
+ fn create_substs_for_ast_path < ' tcx > (
331
+ this : & AstConv < ' tcx > ,
332
+ span : Span ,
333
+ decl_generics : & ty:: Generics < ' tcx > ,
334
+ self_ty : Option < Ty < ' tcx > > ,
335
+ types_provided : Vec < Ty < ' tcx > > ,
336
+ region_substs : Substs < ' tcx > )
337
+ -> Substs < ' tcx >
338
+ {
339
+ let tcx = this. tcx ( ) ;
340
+
341
+ debug ! ( "create_substs_for_ast_path(decl_generics={}, self_ty={}, \
342
+ types_provided={}, region_substs={}",
343
+ decl_generics. repr( tcx) , self_ty. repr( tcx) , types_provided. repr( tcx) ,
344
+ region_substs. repr( tcx) ) ;
345
+
346
+ assert_eq ! ( region_substs. regions( ) . len( TypeSpace ) , decl_generics. regions. len( TypeSpace ) ) ;
347
+ assert ! ( region_substs. types. is_empty( ) ) ;
322
348
323
349
// Convert the type parameters supplied by the user.
324
350
let ty_param_defs = decl_generics. types . get_slice ( TypeSpace ) ;
325
- let supplied_ty_param_count = types. len ( ) ;
326
- let formal_ty_param_count =
327
- ty_param_defs. iter ( )
328
- . take_while ( |x| !ty:: is_associated_type ( tcx, x. def_id ) )
329
- . count ( ) ;
330
- let required_ty_param_count =
331
- ty_param_defs. iter ( )
332
- . take_while ( |x| {
333
- x. default . is_none ( ) &&
334
- !ty:: is_associated_type ( tcx, x. def_id )
335
- } )
336
- . count ( ) ;
351
+ let supplied_ty_param_count = types_provided. len ( ) ;
352
+ let formal_ty_param_count = ty_param_defs. len ( ) ;
353
+ let required_ty_param_count = ty_param_defs. iter ( )
354
+ . take_while ( |x| x. default . is_none ( ) )
355
+ . count ( ) ;
356
+
357
+ let mut type_substs = types_provided;
337
358
if supplied_ty_param_count < required_ty_param_count {
338
359
let expected = if required_ty_param_count < formal_ty_param_count {
339
360
"expected at least"
340
361
} else {
341
362
"expected"
342
363
} ;
343
- span_fatal ! ( this. tcx( ) . sess, span, E0243 ,
344
- "wrong number of type arguments: {} {}, found {}" ,
345
- expected,
346
- required_ty_param_count,
347
- supplied_ty_param_count) ;
364
+ span_err ! ( this. tcx( ) . sess, span, E0243 ,
365
+ "wrong number of type arguments: {} {}, found {}" ,
366
+ expected,
367
+ required_ty_param_count,
368
+ supplied_ty_param_count) ;
369
+ while type_substs. len ( ) < required_ty_param_count {
370
+ type_substs. push ( tcx. types . err ) ;
371
+ }
348
372
} else if supplied_ty_param_count > formal_ty_param_count {
349
373
let expected = if required_ty_param_count < formal_ty_param_count {
350
374
"expected at most"
351
375
} else {
352
376
"expected"
353
377
} ;
354
- span_fatal ! ( this. tcx( ) . sess, span, E0244 ,
355
- "wrong number of type arguments: {} {}, found {}" ,
356
- expected,
357
- formal_ty_param_count,
358
- supplied_ty_param_count) ;
378
+ span_err ! ( this. tcx( ) . sess, span, E0244 ,
379
+ "wrong number of type arguments: {} {}, found {}" ,
380
+ expected,
381
+ formal_ty_param_count,
382
+ supplied_ty_param_count) ;
383
+ type_substs. truncate ( formal_ty_param_count) ;
359
384
}
385
+ assert ! ( type_substs. len( ) >= required_ty_param_count &&
386
+ type_substs. len( ) <= formal_ty_param_count) ;
360
387
361
- let mut substs = Substs :: new_type ( types, regions) ;
388
+ let mut substs = region_substs;
389
+ substs. types . extend ( TypeSpace , type_substs. into_iter ( ) ) ;
362
390
363
391
match self_ty {
364
392
None => {
@@ -374,7 +402,8 @@ fn create_substs_for_ast_path<'tcx>(
374
402
}
375
403
}
376
404
377
- for param in & ty_param_defs[ supplied_ty_param_count..] {
405
+ let actual_supplied_ty_param_count = substs. types . len ( TypeSpace ) ;
406
+ for param in & ty_param_defs[ actual_supplied_ty_param_count..] {
378
407
match param. default {
379
408
Some ( default) => {
380
409
// This is a default type parameter.
@@ -400,8 +429,10 @@ struct ConvertedBinding<'tcx> {
400
429
401
430
fn convert_angle_bracketed_parameters < ' tcx > ( this : & AstConv < ' tcx > ,
402
431
rscope : & RegionScope ,
432
+ span : Span ,
433
+ decl_generics : & ty:: Generics < ' tcx > ,
403
434
data : & ast:: AngleBracketedParameterData )
404
- -> ( Vec < ty :: Region > ,
435
+ -> ( Substs < ' tcx > ,
405
436
Vec < Ty < ' tcx > > ,
406
437
Vec < ConvertedBinding < ' tcx > > )
407
438
{
@@ -410,6 +441,9 @@ fn convert_angle_bracketed_parameters<'tcx>(this: &AstConv<'tcx>,
410
441
. map ( |l| ast_region_to_region ( this. tcx ( ) , l) )
411
442
. collect ( ) ;
412
443
444
+ let region_substs =
445
+ create_region_substs ( this, rscope, span, decl_generics, regions) ;
446
+
413
447
let types: Vec < _ > =
414
448
data. types . iter ( )
415
449
. map ( |t| ast_ty_to_ty ( this, rscope, & * * t) )
@@ -422,7 +456,7 @@ fn convert_angle_bracketed_parameters<'tcx>(this: &AstConv<'tcx>,
422
456
span : b. span } )
423
457
. collect ( ) ;
424
458
425
- ( regions , types, assoc_bindings)
459
+ ( region_substs , types, assoc_bindings)
426
460
}
427
461
428
462
/// Returns the appropriate lifetime to use for any output lifetimes
@@ -479,11 +513,17 @@ fn convert_ty_with_lifetime_elision<'tcx>(this: &AstConv<'tcx>,
479
513
}
480
514
481
515
fn convert_parenthesized_parameters < ' tcx > ( this : & AstConv < ' tcx > ,
516
+ rscope : & RegionScope ,
517
+ span : Span ,
518
+ decl_generics : & ty:: Generics < ' tcx > ,
482
519
data : & ast:: ParenthesizedParameterData )
483
- -> ( Vec < ty :: Region > ,
520
+ -> ( Substs < ' tcx > ,
484
521
Vec < Ty < ' tcx > > ,
485
522
Vec < ConvertedBinding < ' tcx > > )
486
523
{
524
+ let region_substs =
525
+ create_region_substs ( this, rscope, span, decl_generics, Vec :: new ( ) ) ;
526
+
487
527
let binding_rscope = BindingRscope :: new ( ) ;
488
528
let inputs = data. inputs . iter ( )
489
529
. map ( |a_t| ast_ty_to_ty ( this, & binding_rscope, & * * a_t) )
@@ -514,7 +554,7 @@ fn convert_parenthesized_parameters<'tcx>(this: &AstConv<'tcx>,
514
554
span : output_span
515
555
} ;
516
556
517
- ( vec ! [ ] , vec ! [ input_ty] , vec ! [ output_binding] )
557
+ ( region_substs , vec ! [ input_ty] , vec ! [ output_binding] )
518
558
}
519
559
520
560
pub fn instantiate_poly_trait_ref < ' tcx > (
@@ -626,7 +666,7 @@ fn ast_path_to_trait_ref<'a,'tcx>(
626
666
the crate attributes to enable") ;
627
667
}
628
668
629
- convert_angle_bracketed_parameters ( this, rscope, data)
669
+ convert_angle_bracketed_parameters ( this, rscope, path . span , & trait_def . generics , data)
630
670
}
631
671
ast:: ParenthesizedParameters ( ref data) => {
632
672
// For now, require that parenthetical notation be used
@@ -640,12 +680,11 @@ fn ast_path_to_trait_ref<'a,'tcx>(
640
680
the crate attributes to enable") ;
641
681
}
642
682
643
- convert_parenthesized_parameters ( this, data)
683
+ convert_parenthesized_parameters ( this, rscope , path . span , & trait_def . generics , data)
644
684
}
645
685
} ;
646
686
647
687
let substs = create_substs_for_ast_path ( this,
648
- rscope,
649
688
path. span ,
650
689
& trait_def. generics ,
651
690
self_ty,
0 commit comments