@@ -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,15 @@ 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 . span_err (
543
+ span,
544
+ "~const can only be applied to `#[const_trait]` traits" ,
545
+ ) ;
546
+ }
547
+
537
548
( substs, arg_count)
538
549
}
539
550
@@ -601,6 +612,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
601
612
item_segment. args ( ) ,
602
613
item_segment. infer_args ,
603
614
None ,
615
+ None ,
604
616
) ;
605
617
606
618
if let Some ( b) = item_segment. args ( ) . bindings . first ( ) {
@@ -620,6 +632,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
620
632
& self ,
621
633
trait_ref : & hir:: TraitRef < ' _ > ,
622
634
self_ty : Ty < ' tcx > ,
635
+ constness : ty:: BoundConstness ,
623
636
) -> ty:: TraitRef < ' tcx > {
624
637
self . prohibit_generics ( trait_ref. path . segments . split_last ( ) . unwrap ( ) . 1 . iter ( ) , |_| { } ) ;
625
638
@@ -629,6 +642,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
629
642
self_ty,
630
643
trait_ref. path . segments . last ( ) . unwrap ( ) ,
631
644
true ,
645
+ Some ( constness) ,
632
646
)
633
647
}
634
648
@@ -655,6 +669,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
655
669
args,
656
670
infer_args,
657
671
Some ( self_ty) ,
672
+ Some ( constness) ,
658
673
) ;
659
674
660
675
let tcx = self . tcx ( ) ;
@@ -680,6 +695,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
680
695
speculative,
681
696
& mut dup_bindings,
682
697
binding_span. unwrap_or ( binding. span ) ,
698
+ constness,
683
699
) ;
684
700
// Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
685
701
}
@@ -783,13 +799,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
783
799
self_ty : Ty < ' tcx > ,
784
800
trait_segment : & hir:: PathSegment < ' _ > ,
785
801
is_impl : bool ,
802
+ constness : Option < ty:: BoundConstness > ,
786
803
) -> ty:: TraitRef < ' tcx > {
787
804
let ( substs, _) = self . create_substs_for_ast_trait_ref (
788
805
span,
789
806
trait_def_id,
790
807
self_ty,
791
808
trait_segment,
792
809
is_impl,
810
+ constness,
793
811
) ;
794
812
if let Some ( b) = trait_segment. args ( ) . bindings . first ( ) {
795
813
Self :: prohibit_assoc_ty_binding ( self . tcx ( ) , b. span ) ;
@@ -805,6 +823,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
805
823
self_ty : Ty < ' tcx > ,
806
824
trait_segment : & ' a hir:: PathSegment < ' a > ,
807
825
is_impl : bool ,
826
+ constness : Option < ty:: BoundConstness > ,
808
827
) -> ( SubstsRef < ' tcx > , GenericArgCountResult ) {
809
828
self . complain_about_internal_fn_trait ( span, trait_def_id, trait_segment, is_impl) ;
810
829
@@ -816,6 +835,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
816
835
trait_segment. args ( ) ,
817
836
trait_segment. infer_args ,
818
837
Some ( self_ty) ,
838
+ constness,
819
839
)
820
840
}
821
841
@@ -1027,6 +1047,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1027
1047
speculative : bool ,
1028
1048
dup_bindings : & mut FxHashMap < DefId , Span > ,
1029
1049
path_span : Span ,
1050
+ constness : ty:: BoundConstness ,
1030
1051
) -> Result < ( ) , ErrorGuaranteed > {
1031
1052
// Given something like `U: SomeTrait<T = X>`, we want to produce a
1032
1053
// predicate like `<U as SomeTrait>::T = X`. This is somewhat
@@ -1122,10 +1143,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1122
1143
trait_ref. substs ,
1123
1144
) ;
1124
1145
1125
- debug ! (
1126
- "add_predicates_for_ast_type_binding: substs for trait-ref and assoc_item: {:?}" ,
1127
- substs_trait_ref_and_assoc_item
1128
- ) ;
1146
+ debug ! ( ?substs_trait_ref_and_assoc_item) ;
1129
1147
1130
1148
ty:: ProjectionTy {
1131
1149
item_def_id : assoc_item. def_id ,
@@ -1146,8 +1164,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1146
1164
tcx. collect_constrained_late_bound_regions ( & projection_ty) ;
1147
1165
let late_bound_in_ty =
1148
1166
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) ;
1167
+ debug ! ( ? late_bound_in_trait_ref) ;
1168
+ debug ! ( ? late_bound_in_ty) ;
1151
1169
1152
1170
// FIXME: point at the type params that don't have appropriate lifetimes:
1153
1171
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
@@ -1648,6 +1666,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1648
1666
1649
1667
// Checks that `bounds` contains exactly one element and reports appropriate
1650
1668
// errors otherwise.
1669
+ #[ instrument( level = "debug" , skip( self , all_candidates, ty_param_name, is_equality) , ret) ]
1651
1670
fn one_bound_for_assoc_type < I > (
1652
1671
& self ,
1653
1672
all_candidates : impl Fn ( ) -> I ,
@@ -1677,10 +1696,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1677
1696
return Err ( reported) ;
1678
1697
}
1679
1698
} ;
1680
- debug ! ( "one_bound_for_assoc_type: bound = {:?}" , bound) ;
1699
+ debug ! ( ? bound) ;
1681
1700
1682
1701
if let Some ( bound2) = next_cand {
1683
- debug ! ( "one_bound_for_assoc_type: bound2 = {:?}" , bound2) ;
1702
+ debug ! ( ? bound2) ;
1684
1703
1685
1704
let is_equality = is_equality ( ) ;
1686
1705
let bounds = IntoIterator :: into_iter ( [ bound, bound2] ) . chain ( matching_candidates) ;
@@ -1776,6 +1795,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1776
1795
// parameter or `Self`.
1777
1796
// NOTE: When this function starts resolving `Trait::AssocTy` successfully
1778
1797
// it should also start reporting the `BARE_TRAIT_OBJECTS` lint.
1798
+ #[ instrument( level = "debug" , skip( self , hir_ref_id, span, qself, assoc_segment) , fields( assoc_ident=?assoc_segment. ident) , ret) ]
1779
1799
pub fn associated_path_to_ty (
1780
1800
& self ,
1781
1801
hir_ref_id : hir:: HirId ,
@@ -1793,8 +1813,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1793
1813
Res :: Err
1794
1814
} ;
1795
1815
1796
- debug ! ( "associated_path_to_ty: {:?}::{}" , qself_ty, assoc_ident) ;
1797
-
1798
1816
// Check if we have an enum variant.
1799
1817
let mut variant_resolution = None ;
1800
1818
if let ty:: Adt ( adt_def, _) = qself_ty. kind ( ) {
@@ -2050,6 +2068,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2050
2068
item_def_id : DefId ,
2051
2069
trait_segment : & hir:: PathSegment < ' _ > ,
2052
2070
item_segment : & hir:: PathSegment < ' _ > ,
2071
+ constness : ty:: BoundConstness ,
2053
2072
) -> Ty < ' tcx > {
2054
2073
let tcx = self . tcx ( ) ;
2055
2074
@@ -2094,8 +2113,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2094
2113
2095
2114
debug ! ( "qpath_to_ty: self_type={:?}" , self_ty) ;
2096
2115
2097
- let trait_ref =
2098
- self . ast_path_to_mono_trait_ref ( span, trait_def_id, self_ty, trait_segment, false ) ;
2116
+ let trait_ref = self . ast_path_to_mono_trait_ref (
2117
+ span,
2118
+ trait_def_id,
2119
+ self_ty,
2120
+ trait_segment,
2121
+ false ,
2122
+ Some ( constness) ,
2123
+ ) ;
2099
2124
2100
2125
let item_substs = self . create_substs_for_associated_item (
2101
2126
span,
@@ -2534,12 +2559,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2534
2559
Res :: Def ( DefKind :: AssocTy , def_id) => {
2535
2560
debug_assert ! ( path. segments. len( ) >= 2 ) ;
2536
2561
self . prohibit_generics ( path. segments [ ..path. segments . len ( ) - 2 ] . iter ( ) , |_| { } ) ;
2562
+ // HACK: until we support `<Type as ~const Trait>`, assume all of them are.
2563
+ let constness = if tcx. has_attr ( tcx. parent ( def_id) , sym:: const_trait) {
2564
+ ty:: BoundConstness :: ConstIfConst
2565
+ } else {
2566
+ ty:: BoundConstness :: NotConst
2567
+ } ;
2537
2568
self . qpath_to_ty (
2538
2569
span,
2539
2570
opt_self_ty,
2540
2571
def_id,
2541
2572
& path. segments [ path. segments . len ( ) - 2 ] ,
2542
2573
path. segments . last ( ) . unwrap ( ) ,
2574
+ constness,
2543
2575
)
2544
2576
}
2545
2577
Res :: PrimTy ( prim_ty) => {
@@ -2658,6 +2690,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2658
2690
& GenericArgs :: none ( ) ,
2659
2691
true ,
2660
2692
None ,
2693
+ None ,
2661
2694
) ;
2662
2695
EarlyBinder ( self . normalize_ty ( span, tcx. at ( span) . type_of ( def_id) ) )
2663
2696
. subst ( tcx, substs)
@@ -2766,6 +2799,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2766
2799
}
2767
2800
}
2768
2801
2802
+ #[ instrument( level = "debug" , skip( self , hir_id, unsafety, abi, decl, generics, hir_ty) , ret) ]
2769
2803
pub fn ty_of_fn (
2770
2804
& self ,
2771
2805
hir_id : hir:: HirId ,
@@ -2775,8 +2809,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2775
2809
generics : Option < & hir:: Generics < ' _ > > ,
2776
2810
hir_ty : Option < & hir:: Ty < ' _ > > ,
2777
2811
) -> ty:: PolyFnSig < ' tcx > {
2778
- debug ! ( "ty_of_fn" ) ;
2779
-
2780
2812
let tcx = self . tcx ( ) ;
2781
2813
let bound_vars = tcx. late_bound_vars ( hir_id) ;
2782
2814
debug ! ( ?bound_vars) ;
@@ -2826,7 +2858,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2826
2858
hir:: FnRetTy :: DefaultReturn ( ..) => tcx. mk_unit ( ) ,
2827
2859
} ;
2828
2860
2829
- debug ! ( "ty_of_fn: output_ty={:?}" , output_ty) ;
2861
+ debug ! ( ? output_ty) ;
2830
2862
2831
2863
let fn_ty = tcx. mk_fn_sig ( input_tys. into_iter ( ) , output_ty, decl. c_variadic , unsafety, abi) ;
2832
2864
let bare_fn_ty = ty:: Binder :: bind_with_vars ( fn_ty, bound_vars) ;
@@ -2903,8 +2935,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2903
2935
let hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Impl ( i) , .. } ) =
2904
2936
hir. get ( hir. get_parent_node ( fn_hir_id) ) else { bug ! ( "ImplItem should have Impl parent" ) } ;
2905
2937
2906
- let trait_ref =
2907
- self . instantiate_mono_trait_ref ( i. of_trait . as_ref ( ) ?, self . ast_ty_to_ty ( i. self_ty ) ) ;
2938
+ let trait_ref = self . instantiate_mono_trait_ref (
2939
+ i. of_trait . as_ref ( ) ?,
2940
+ self . ast_ty_to_ty ( i. self_ty ) ,
2941
+ ty:: BoundConstness :: NotConst ,
2942
+ ) ;
2908
2943
2909
2944
let assoc = tcx. associated_items ( trait_ref. def_id ) . find_by_name_and_kind (
2910
2945
tcx,
0 commit comments