@@ -2579,16 +2579,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2579
2579
2580
2580
/// As `instantiate_type_scheme`, but for the bounds found in a
2581
2581
/// generic type scheme.
2582
- fn instantiate_bounds ( & self , span : Span , def_id : DefId , substs : SubstsRef < ' tcx > )
2583
- -> ty:: InstantiatedPredicates < ' tcx > {
2582
+ fn instantiate_bounds (
2583
+ & self ,
2584
+ span : Span ,
2585
+ def_id : DefId ,
2586
+ substs : SubstsRef < ' tcx > ,
2587
+ ) -> ( ty:: InstantiatedPredicates < ' tcx > , Vec < Span > ) {
2584
2588
let bounds = self . tcx . predicates_of ( def_id) ;
2589
+ let spans: Vec < Span > = bounds. predicates . iter ( ) . map ( |( _, span) | * span) . collect ( ) ;
2585
2590
let result = bounds. instantiate ( self . tcx , substs) ;
2586
2591
let result = self . normalize_associated_types_in ( span, & result) ;
2587
- debug ! ( "instantiate_bounds(bounds={:?}, substs={:?}) = {:?}" ,
2592
+ debug ! (
2593
+ "instantiate_bounds(bounds={:?}, substs={:?}) = {:?}, {:?}" ,
2588
2594
bounds,
2589
2595
substs,
2590
- result) ;
2591
- result
2596
+ result,
2597
+ spans,
2598
+ ) ;
2599
+ ( result, spans)
2592
2600
}
2593
2601
2594
2602
/// Replaces the opaque types from the given value with type variables,
@@ -3151,8 +3159,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3151
3159
3152
3160
// All the input types from the fn signature must outlive the call
3153
3161
// so as to validate implied bounds.
3154
- for & fn_input_ty in fn_inputs {
3155
- self . register_wf_obligation ( fn_input_ty, sp , traits:: MiscObligation ) ;
3162
+ for ( fn_input_ty, arg_expr ) in fn_inputs. iter ( ) . zip ( args . iter ( ) ) {
3163
+ self . register_wf_obligation ( fn_input_ty, arg_expr . span , traits:: MiscObligation ) ;
3156
3164
}
3157
3165
3158
3166
let expected_arg_count = fn_inputs. len ( ) ;
@@ -3513,7 +3521,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3513
3521
self . write_user_type_annotation_from_substs ( hir_id, did, substs, None ) ;
3514
3522
3515
3523
// Check bounds on type arguments used in the path.
3516
- let bounds = self . instantiate_bounds ( path_span, did, substs) ;
3524
+ let ( bounds, _ ) = self . instantiate_bounds ( path_span, did, substs) ;
3517
3525
let cause = traits:: ObligationCause :: new ( path_span, self . body_id ,
3518
3526
traits:: ItemObligation ( did) ) ;
3519
3527
self . add_obligations_for_parameters ( cause, & bounds) ;
@@ -4634,12 +4642,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4634
4642
// First, store the "user substs" for later.
4635
4643
self . write_user_type_annotation_from_substs ( hir_id, def_id, substs, user_self_ty) ;
4636
4644
4637
- // Add all the obligations that are required, substituting and
4638
- // normalized appropriately.
4639
- let bounds = self . instantiate_bounds ( span, def_id, & substs) ;
4640
- self . add_obligations_for_parameters (
4641
- traits:: ObligationCause :: new ( span, self . body_id , traits:: ItemObligation ( def_id) ) ,
4642
- & bounds) ;
4645
+ self . add_required_obligations ( span, def_id, & substs) ;
4643
4646
4644
4647
// Substitute the values for the type parameters into the type of
4645
4648
// the referenced item.
@@ -4676,6 +4679,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4676
4679
( ty_substituted, res)
4677
4680
}
4678
4681
4682
+ /// Add all the obligations that are required, substituting and normalized appropriately.
4683
+ fn add_required_obligations ( & self , span : Span , def_id : DefId , substs : & SubstsRef < ' tcx > ) {
4684
+ let ( bounds, spans) = self . instantiate_bounds ( span, def_id, & substs) ;
4685
+
4686
+ for ( i, mut obligation) in traits:: predicates_for_generics (
4687
+ traits:: ObligationCause :: new (
4688
+ span,
4689
+ self . body_id ,
4690
+ traits:: ItemObligation ( def_id) ,
4691
+ ) ,
4692
+ self . param_env ,
4693
+ & bounds,
4694
+ ) . into_iter ( ) . enumerate ( ) {
4695
+ // This makes the error point at the bound, but we want to point at the argument
4696
+ if let Some ( span) = spans. get ( i) {
4697
+ obligation. cause . code = traits:: BindingObligation ( def_id, * span) ;
4698
+ }
4699
+ self . register_predicate ( obligation) ;
4700
+ }
4701
+ }
4702
+
4679
4703
fn check_rustc_args_require_const ( & self ,
4680
4704
def_id : DefId ,
4681
4705
hir_id : hir:: HirId ,
0 commit comments