@@ -33,7 +33,7 @@ use super::FnCtxt;
33
33
use crate :: hir:: def_id:: DefId ;
34
34
use crate :: type_error_struct;
35
35
use hir:: def_id:: LOCAL_CRATE ;
36
- use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed } ;
36
+ use rustc_errors:: { struct_span_err, Applicability , DelayDm , DiagnosticBuilder , ErrorGuaranteed } ;
37
37
use rustc_hir as hir;
38
38
use rustc_infer:: traits:: { Obligation , ObligationCause , ObligationCauseCode } ;
39
39
use rustc_middle:: mir:: Mutability ;
@@ -754,19 +754,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
754
754
} else {
755
755
( "" , lint:: builtin:: TRIVIAL_CASTS )
756
756
} ;
757
- fcx. tcx . struct_span_lint_hir ( lint, self . expr . hir_id , self . span , |err| {
758
- err. build ( & format ! (
759
- "trivial {}cast: `{}` as `{}`" ,
760
- adjective,
761
- fcx. ty_to_string( t_expr) ,
762
- fcx. ty_to_string( t_cast)
763
- ) )
764
- . help ( & format ! (
765
- "cast can be replaced by coercion; this might \
766
- require {type_asc_or}a temporary variable"
767
- ) )
768
- . emit ( ) ;
769
- } ) ;
757
+ fcx. tcx . struct_span_lint_hir (
758
+ lint,
759
+ self . expr . hir_id ,
760
+ self . span ,
761
+ DelayDm ( || {
762
+ format ! (
763
+ "trivial {}cast: `{}` as `{}`" ,
764
+ adjective,
765
+ fcx. ty_to_string( t_expr) ,
766
+ fcx. ty_to_string( t_cast)
767
+ )
768
+ } ) ,
769
+ |lint| {
770
+ lint. help ( format ! (
771
+ "cast can be replaced by coercion; this might \
772
+ require {type_asc_or}a temporary variable"
773
+ ) )
774
+ } ,
775
+ ) ;
770
776
}
771
777
772
778
#[ instrument( skip( fcx) , level = "debug" ) ]
@@ -1074,12 +1080,12 @@ impl<'a, 'tcx> CastCheck<'tcx> {
1074
1080
lint:: builtin:: CENUM_IMPL_DROP_CAST ,
1075
1081
self . expr . hir_id ,
1076
1082
self . span ,
1077
- |err| {
1078
- err . build ( & format ! (
1079
- "cannot cast enum `{}` into integer `{}` because it implements `Drop`" ,
1080
- self . expr_ty , self . cast_ty
1081
- ) )
1082
- . emit ( ) ;
1083
+ DelayDm ( || format ! (
1084
+ "cannot cast enum `{}` into integer `{}` because it implements `Drop`" ,
1085
+ self . expr_ty , self . cast_ty
1086
+ ) ) ,
1087
+ |lint| {
1088
+ lint
1083
1089
} ,
1084
1090
) ;
1085
1091
}
@@ -1090,12 +1096,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
1090
1096
lint:: builtin:: LOSSY_PROVENANCE_CASTS ,
1091
1097
self . expr . hir_id ,
1092
1098
self . span ,
1093
- |err| {
1094
- let mut err = err. build ( & format ! (
1099
+ DelayDm ( || format ! (
1095
1100
"under strict provenance it is considered bad style to cast pointer `{}` to integer `{}`" ,
1096
1101
self . expr_ty, self . cast_ty
1097
- ) ) ;
1098
-
1102
+ ) ) ,
1103
+ |lint| {
1099
1104
let msg = "use `.addr()` to obtain the address of a pointer" ;
1100
1105
1101
1106
let expr_prec = self . expr . precedence ( ) . order ( ) ;
@@ -1114,22 +1119,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
1114
1119
( cast_span, format!( ").addr(){scalar_cast}" ) ) ,
1115
1120
] ;
1116
1121
1117
- err . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
1122
+ lint . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
1118
1123
} else {
1119
- err . span_suggestion (
1124
+ lint . span_suggestion (
1120
1125
cast_span,
1121
1126
msg,
1122
1127
format ! ( ".addr(){scalar_cast}" ) ,
1123
1128
Applicability :: MaybeIncorrect ,
1124
1129
) ;
1125
1130
}
1126
1131
1127
- err . help (
1132
+ lint . help (
1128
1133
"if you can't comply with strict provenance and need to expose the pointer \
1129
1134
provenance you can use `.expose_addr()` instead"
1130
1135
) ;
1131
1136
1132
- err . emit ( ) ;
1137
+ lint
1133
1138
} ,
1134
1139
) ;
1135
1140
}
@@ -1139,24 +1144,24 @@ impl<'a, 'tcx> CastCheck<'tcx> {
1139
1144
lint:: builtin:: FUZZY_PROVENANCE_CASTS ,
1140
1145
self . expr . hir_id ,
1141
1146
self . span ,
1142
- |err| {
1143
- let mut err = err . build ( & format ! (
1144
- "strict provenance disallows casting integer `{}` to pointer `{}`" ,
1145
- self . expr_ty , self . cast_ty
1146
- ) ) ;
1147
+ DelayDm ( || format ! (
1148
+ "strict provenance disallows casting integer `{}` to pointer `{}`" ,
1149
+ self . expr_ty , self . cast_ty
1150
+ ) ) ,
1151
+ |lint| {
1147
1152
let msg = "use `.with_addr()` to adjust a valid pointer in the same allocation, to this address" ;
1148
1153
let suggestions = vec ! [
1149
1154
( self . expr_span. shrink_to_lo( ) , String :: from( "(...).with_addr(" ) ) ,
1150
1155
( self . expr_span. shrink_to_hi( ) . to( self . cast_span) , String :: from( ")" ) ) ,
1151
1156
] ;
1152
1157
1153
- err . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
1154
- err . help (
1158
+ lint . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
1159
+ lint . help (
1155
1160
"if you can't comply with strict provenance and don't have a pointer with \
1156
1161
the correct provenance you can use `std::ptr::from_exposed_addr()` instead"
1157
1162
) ;
1158
1163
1159
- err . emit ( ) ;
1164
+ lint
1160
1165
} ,
1161
1166
) ;
1162
1167
}
0 commit comments