@@ -36,7 +36,7 @@ use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, BARE_TRAIT_OBJECT
36
36
use rustc_span:: edition:: Edition ;
37
37
use rustc_span:: lev_distance:: find_best_match_for_name;
38
38
use rustc_span:: symbol:: { kw, Ident , Symbol } ;
39
- use rustc_span:: Span ;
39
+ use rustc_span:: { sym , Span } ;
40
40
use rustc_target:: spec:: abi;
41
41
use rustc_trait_selection:: traits;
42
42
use rustc_trait_selection:: traits:: astconv_object_safety_violations;
@@ -275,6 +275,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
275
275
item_segment. args ( ) ,
276
276
item_segment. infer_args ,
277
277
None ,
278
+ None ,
278
279
) ;
279
280
if let Some ( b) = item_segment. args ( ) . bindings . first ( ) {
280
281
Self :: prohibit_assoc_ty_binding ( self . tcx ( ) , b. span ) ;
@@ -324,6 +325,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
324
325
generic_args : & ' a hir:: GenericArgs < ' _ > ,
325
326
infer_args : bool ,
326
327
self_ty : Option < Ty < ' tcx > > ,
328
+ constness : Option < ty:: BoundConstness > ,
327
329
) -> ( SubstsRef < ' tcx > , GenericArgCountResult ) {
328
330
// If the type is parameterized by this region, then replace this
329
331
// region with the current anon region binding (in other words,
@@ -534,6 +536,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
534
536
& mut substs_ctx,
535
537
) ;
536
538
539
+ if let Some ( ty:: BoundConstness :: ConstIfConst ) = constness
540
+ && generics. has_self && !tcx. has_attr ( def_id, sym:: const_trait)
541
+ {
542
+ tcx. sess . emit_err ( crate :: errors:: ConstBoundForNonConstTrait { span } ) ;
543
+ }
544
+
537
545
( substs, arg_count)
538
546
}
539
547
@@ -601,6 +609,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
601
609
item_segment. args ( ) ,
602
610
item_segment. infer_args ,
603
611
None ,
612
+ None ,
604
613
) ;
605
614
606
615
if let Some ( b) = item_segment. args ( ) . bindings . first ( ) {
@@ -620,6 +629,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
620
629
& self ,
621
630
trait_ref : & hir:: TraitRef < ' _ > ,
622
631
self_ty : Ty < ' tcx > ,
632
+ constness : ty:: BoundConstness ,
623
633
) -> ty:: TraitRef < ' tcx > {
624
634
self . prohibit_generics ( trait_ref. path . segments . split_last ( ) . unwrap ( ) . 1 . iter ( ) , |_| { } ) ;
625
635
@@ -629,6 +639,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
629
639
self_ty,
630
640
trait_ref. path . segments . last ( ) . unwrap ( ) ,
631
641
true ,
642
+ Some ( constness) ,
632
643
)
633
644
}
634
645
@@ -655,6 +666,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
655
666
args,
656
667
infer_args,
657
668
Some ( self_ty) ,
669
+ Some ( constness) ,
658
670
) ;
659
671
660
672
let tcx = self . tcx ( ) ;
@@ -680,6 +692,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
680
692
speculative,
681
693
& mut dup_bindings,
682
694
binding_span. unwrap_or ( binding. span ) ,
695
+ constness,
683
696
) ;
684
697
// Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
685
698
}
@@ -783,13 +796,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
783
796
self_ty : Ty < ' tcx > ,
784
797
trait_segment : & hir:: PathSegment < ' _ > ,
785
798
is_impl : bool ,
799
+ constness : Option < ty:: BoundConstness > ,
786
800
) -> ty:: TraitRef < ' tcx > {
787
801
let ( substs, _) = self . create_substs_for_ast_trait_ref (
788
802
span,
789
803
trait_def_id,
790
804
self_ty,
791
805
trait_segment,
792
806
is_impl,
807
+ constness,
793
808
) ;
794
809
if let Some ( b) = trait_segment. args ( ) . bindings . first ( ) {
795
810
Self :: prohibit_assoc_ty_binding ( self . tcx ( ) , b. span ) ;
@@ -805,6 +820,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
805
820
self_ty : Ty < ' tcx > ,
806
821
trait_segment : & ' a hir:: PathSegment < ' a > ,
807
822
is_impl : bool ,
823
+ constness : Option < ty:: BoundConstness > ,
808
824
) -> ( SubstsRef < ' tcx > , GenericArgCountResult ) {
809
825
self . complain_about_internal_fn_trait ( span, trait_def_id, trait_segment, is_impl) ;
810
826
@@ -816,6 +832,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
816
832
trait_segment. args ( ) ,
817
833
trait_segment. infer_args ,
818
834
Some ( self_ty) ,
835
+ constness,
819
836
)
820
837
}
821
838
@@ -1027,6 +1044,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1027
1044
speculative : bool ,
1028
1045
dup_bindings : & mut FxHashMap < DefId , Span > ,
1029
1046
path_span : Span ,
1047
+ constness : ty:: BoundConstness ,
1030
1048
) -> Result < ( ) , ErrorGuaranteed > {
1031
1049
// Given something like `U: SomeTrait<T = X>`, we want to produce a
1032
1050
// predicate like `<U as SomeTrait>::T = X`. This is somewhat
@@ -1122,10 +1140,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1122
1140
trait_ref. substs ,
1123
1141
) ;
1124
1142
1125
- debug ! (
1126
- "add_predicates_for_ast_type_binding: substs for trait-ref and assoc_item: {:?}" ,
1127
- substs_trait_ref_and_assoc_item
1128
- ) ;
1143
+ debug ! ( ?substs_trait_ref_and_assoc_item) ;
1129
1144
1130
1145
ty:: ProjectionTy {
1131
1146
item_def_id : assoc_item. def_id ,
@@ -1146,8 +1161,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1146
1161
tcx. collect_constrained_late_bound_regions ( & projection_ty) ;
1147
1162
let late_bound_in_ty =
1148
1163
tcx. collect_referenced_late_bound_regions ( & trait_ref. rebind ( ty) ) ;
1149
- debug ! ( "late_bound_in_trait_ref = {:?}" , late_bound_in_trait_ref) ;
1150
- debug ! ( "late_bound_in_ty = {:?}" , late_bound_in_ty) ;
1164
+ debug ! ( ? late_bound_in_trait_ref) ;
1165
+ debug ! ( ? late_bound_in_ty) ;
1151
1166
1152
1167
// FIXME: point at the type params that don't have appropriate lifetimes:
1153
1168
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
@@ -1648,6 +1663,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1648
1663
1649
1664
// Checks that `bounds` contains exactly one element and reports appropriate
1650
1665
// errors otherwise.
1666
+ #[ instrument( level = "debug" , skip( self , all_candidates, ty_param_name, is_equality) , ret) ]
1651
1667
fn one_bound_for_assoc_type < I > (
1652
1668
& self ,
1653
1669
all_candidates : impl Fn ( ) -> I ,
@@ -1677,10 +1693,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1677
1693
return Err ( reported) ;
1678
1694
}
1679
1695
} ;
1680
- debug ! ( "one_bound_for_assoc_type: bound = {:?}" , bound) ;
1696
+ debug ! ( ? bound) ;
1681
1697
1682
1698
if let Some ( bound2) = next_cand {
1683
- debug ! ( "one_bound_for_assoc_type: bound2 = {:?}" , bound2) ;
1699
+ debug ! ( ? bound2) ;
1684
1700
1685
1701
let is_equality = is_equality ( ) ;
1686
1702
let bounds = IntoIterator :: into_iter ( [ bound, bound2] ) . chain ( matching_candidates) ;
@@ -1776,6 +1792,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1776
1792
// parameter or `Self`.
1777
1793
// NOTE: When this function starts resolving `Trait::AssocTy` successfully
1778
1794
// it should also start reporting the `BARE_TRAIT_OBJECTS` lint.
1795
+ #[ instrument( level = "debug" , skip( self , hir_ref_id, span, qself, assoc_segment) , fields( assoc_ident=?assoc_segment. ident) , ret) ]
1779
1796
pub fn associated_path_to_ty (
1780
1797
& self ,
1781
1798
hir_ref_id : hir:: HirId ,
@@ -1793,8 +1810,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1793
1810
Res :: Err
1794
1811
} ;
1795
1812
1796
- debug ! ( "associated_path_to_ty: {:?}::{}" , qself_ty, assoc_ident) ;
1797
-
1798
1813
// Check if we have an enum variant.
1799
1814
let mut variant_resolution = None ;
1800
1815
if let ty:: Adt ( adt_def, _) = qself_ty. kind ( ) {
@@ -2050,6 +2065,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2050
2065
item_def_id : DefId ,
2051
2066
trait_segment : & hir:: PathSegment < ' _ > ,
2052
2067
item_segment : & hir:: PathSegment < ' _ > ,
2068
+ constness : ty:: BoundConstness ,
2053
2069
) -> Ty < ' tcx > {
2054
2070
let tcx = self . tcx ( ) ;
2055
2071
@@ -2094,8 +2110,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2094
2110
2095
2111
debug ! ( "qpath_to_ty: self_type={:?}" , self_ty) ;
2096
2112
2097
- let trait_ref =
2098
- self . ast_path_to_mono_trait_ref ( span, trait_def_id, self_ty, trait_segment, false ) ;
2113
+ let trait_ref = self . ast_path_to_mono_trait_ref (
2114
+ span,
2115
+ trait_def_id,
2116
+ self_ty,
2117
+ trait_segment,
2118
+ false ,
2119
+ Some ( constness) ,
2120
+ ) ;
2099
2121
2100
2122
let item_substs = self . create_substs_for_associated_item (
2101
2123
span,
@@ -2534,12 +2556,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2534
2556
Res :: Def ( DefKind :: AssocTy , def_id) => {
2535
2557
debug_assert ! ( path. segments. len( ) >= 2 ) ;
2536
2558
self . prohibit_generics ( path. segments [ ..path. segments . len ( ) - 2 ] . iter ( ) , |_| { } ) ;
2559
+ // HACK: until we support `<Type as ~const Trait>`, assume all of them are.
2560
+ let constness = if tcx. has_attr ( tcx. parent ( def_id) , sym:: const_trait) {
2561
+ ty:: BoundConstness :: ConstIfConst
2562
+ } else {
2563
+ ty:: BoundConstness :: NotConst
2564
+ } ;
2537
2565
self . qpath_to_ty (
2538
2566
span,
2539
2567
opt_self_ty,
2540
2568
def_id,
2541
2569
& path. segments [ path. segments . len ( ) - 2 ] ,
2542
2570
path. segments . last ( ) . unwrap ( ) ,
2571
+ constness,
2543
2572
)
2544
2573
}
2545
2574
Res :: PrimTy ( prim_ty) => {
@@ -2658,6 +2687,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2658
2687
& GenericArgs :: none ( ) ,
2659
2688
true ,
2660
2689
None ,
2690
+ None ,
2661
2691
) ;
2662
2692
EarlyBinder ( self . normalize_ty ( span, tcx. at ( span) . type_of ( def_id) ) )
2663
2693
. subst ( tcx, substs)
@@ -2766,6 +2796,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2766
2796
}
2767
2797
}
2768
2798
2799
+ #[ instrument( level = "debug" , skip( self , hir_id, unsafety, abi, decl, generics, hir_ty) , ret) ]
2769
2800
pub fn ty_of_fn (
2770
2801
& self ,
2771
2802
hir_id : hir:: HirId ,
@@ -2775,8 +2806,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2775
2806
generics : Option < & hir:: Generics < ' _ > > ,
2776
2807
hir_ty : Option < & hir:: Ty < ' _ > > ,
2777
2808
) -> ty:: PolyFnSig < ' tcx > {
2778
- debug ! ( "ty_of_fn" ) ;
2779
-
2780
2809
let tcx = self . tcx ( ) ;
2781
2810
let bound_vars = tcx. late_bound_vars ( hir_id) ;
2782
2811
debug ! ( ?bound_vars) ;
@@ -2826,7 +2855,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2826
2855
hir:: FnRetTy :: DefaultReturn ( ..) => tcx. mk_unit ( ) ,
2827
2856
} ;
2828
2857
2829
- debug ! ( "ty_of_fn: output_ty={:?}" , output_ty) ;
2858
+ debug ! ( ? output_ty) ;
2830
2859
2831
2860
let fn_ty = tcx. mk_fn_sig ( input_tys. into_iter ( ) , output_ty, decl. c_variadic , unsafety, abi) ;
2832
2861
let bare_fn_ty = ty:: Binder :: bind_with_vars ( fn_ty, bound_vars) ;
@@ -2903,8 +2932,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2903
2932
let hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Impl ( i) , .. } ) =
2904
2933
hir. get ( hir. get_parent_node ( fn_hir_id) ) else { bug ! ( "ImplItem should have Impl parent" ) } ;
2905
2934
2906
- let trait_ref =
2907
- self . instantiate_mono_trait_ref ( i. of_trait . as_ref ( ) ?, self . ast_ty_to_ty ( i. self_ty ) ) ;
2935
+ let trait_ref = self . instantiate_mono_trait_ref (
2936
+ i. of_trait . as_ref ( ) ?,
2937
+ self . ast_ty_to_ty ( i. self_ty ) ,
2938
+ ty:: BoundConstness :: NotConst ,
2939
+ ) ;
2908
2940
2909
2941
let assoc = tcx. associated_items ( trait_ref. def_id ) . find_by_name_and_kind (
2910
2942
tcx,
0 commit comments