@@ -1294,34 +1294,38 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
1294
1294
& icx,
1295
1295
ast_trait_ref,
1296
1296
selfty,
1297
- match impl_. constness {
1298
- hir:: Constness :: Const => {
1299
- if let Some ( trait_def_id) = ast_trait_ref. trait_def_id ( ) && !tcx. has_attr ( trait_def_id, sym:: const_trait) {
1300
- let trait_name = tcx. item_name ( trait_def_id) ;
1301
- let mut err = tcx. sess . struct_span_err (
1302
- ast_trait_ref. path . span ,
1303
- & format ! ( "const `impl` for trait `{trait_name}` which is not marked with `#[const_trait]`" ) ,
1304
- ) ;
1305
- if trait_def_id. is_local ( ) {
1306
- let sp = tcx. def_span ( trait_def_id) . shrink_to_lo ( ) ;
1307
- err. span_suggestion ( sp, & format ! ( "mark `{trait_name}` as const" ) , "#[const_trait]" , rustc_errors:: Applicability :: MachineApplicable ) ;
1308
- }
1309
- err. note ( "marking a trait with `#[const_trait]` ensures all default method bodies are `const`" ) ;
1310
- err. note ( "adding a non-const method body in the future would be a breaking change" ) ;
1311
- err. emit ( ) ;
1312
- ty:: BoundConstness :: NotConst
1313
- } else {
1314
- ty:: BoundConstness :: ConstIfConst
1315
- }
1316
- } ,
1317
- hir:: Constness :: NotConst => ty:: BoundConstness :: NotConst ,
1318
- } ,
1297
+ check_impl_constness ( tcx, impl_. constness , ast_trait_ref) ,
1319
1298
)
1320
1299
} ) ,
1321
1300
_ => bug ! ( ) ,
1322
1301
}
1323
1302
}
1324
1303
1304
+ fn check_impl_constness (
1305
+ tcx : TyCtxt < ' _ > ,
1306
+ constness : hir:: Constness ,
1307
+ ast_trait_ref : & hir:: TraitRef < ' _ > ,
1308
+ ) -> ty:: BoundConstness {
1309
+ match constness {
1310
+ hir:: Constness :: Const => {
1311
+ if let Some ( trait_def_id) = ast_trait_ref. trait_def_id ( ) && !tcx. has_attr ( trait_def_id, sym:: const_trait) {
1312
+ let trait_name = tcx. item_name ( trait_def_id) . to_string ( ) ;
1313
+ tcx. sess . emit_err ( errors:: ConstImplForNonConstTrait {
1314
+ trait_ref_span : ast_trait_ref. path . span ,
1315
+ trait_name,
1316
+ local_trait_span : trait_def_id. as_local ( ) . map ( |_| tcx. def_span ( trait_def_id) . shrink_to_lo ( ) ) ,
1317
+ marking : ( ) ,
1318
+ adding : ( ) ,
1319
+ } ) ;
1320
+ ty:: BoundConstness :: NotConst
1321
+ } else {
1322
+ ty:: BoundConstness :: ConstIfConst
1323
+ }
1324
+ } ,
1325
+ hir:: Constness :: NotConst => ty:: BoundConstness :: NotConst ,
1326
+ }
1327
+ }
1328
+
1325
1329
fn impl_polarity ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> ty:: ImplPolarity {
1326
1330
let is_rustc_reservation = tcx. has_attr ( def_id, sym:: rustc_reservation_impl) ;
1327
1331
let item = tcx. hir ( ) . expect_item ( def_id. expect_local ( ) ) ;
0 commit comments