@@ -188,6 +188,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
188
188
let ( generics, ( ty, body_id) ) = self . lower_generics (
189
189
generics,
190
190
Const :: No ,
191
+ false ,
191
192
id,
192
193
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
193
194
|this| {
@@ -218,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
218
219
219
220
let itctx = ImplTraitContext :: Universal ;
220
221
let ( generics, decl) =
221
- this. lower_generics ( generics, header. constness , id, itctx, |this| {
222
+ this. lower_generics ( generics, header. constness , false , id, itctx, |this| {
222
223
this. lower_fn_decl (
223
224
decl,
224
225
id,
@@ -262,6 +263,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
262
263
let ( generics, ty) = self . lower_generics (
263
264
& generics,
264
265
Const :: No ,
266
+ false ,
265
267
id,
266
268
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
267
269
|this| match ty {
@@ -284,6 +286,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
284
286
let ( generics, variants) = self . lower_generics (
285
287
generics,
286
288
Const :: No ,
289
+ false ,
287
290
id,
288
291
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
289
292
|this| {
@@ -298,6 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
298
301
let ( generics, struct_def) = self . lower_generics (
299
302
generics,
300
303
Const :: No ,
304
+ false ,
301
305
id,
302
306
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
303
307
|this| this. lower_variant_data ( hir_id, struct_def) ,
@@ -308,6 +312,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
308
312
let ( generics, vdata) = self . lower_generics (
309
313
generics,
310
314
Const :: No ,
315
+ false ,
311
316
id,
312
317
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
313
318
|this| this. lower_variant_data ( hir_id, vdata) ,
@@ -339,7 +344,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
339
344
// parent lifetime.
340
345
let itctx = ImplTraitContext :: Universal ;
341
346
let ( generics, ( trait_ref, lowered_ty) ) =
342
- self . lower_generics ( ast_generics, Const :: No , id, itctx, |this| {
347
+ self . lower_generics ( ast_generics, Const :: No , false , id, itctx, |this| {
343
348
let modifiers = TraitBoundModifiers {
344
349
constness : BoundConstness :: Never ,
345
350
asyncness : BoundAsyncness :: Normal ,
@@ -391,6 +396,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
391
396
let ( generics, ( unsafety, items, bounds) ) = self . lower_generics (
392
397
generics,
393
398
Const :: No ,
399
+ false ,
394
400
id,
395
401
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
396
402
|this| {
@@ -411,6 +417,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
411
417
let ( generics, bounds) = self . lower_generics (
412
418
generics,
413
419
Const :: No ,
420
+ false ,
414
421
id,
415
422
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
416
423
|this| {
@@ -645,7 +652,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
645
652
let fdec = & sig. decl ;
646
653
let itctx = ImplTraitContext :: Universal ;
647
654
let ( generics, ( fn_dec, fn_args) ) =
648
- self . lower_generics ( generics, Const :: No , i. id , itctx, |this| {
655
+ self . lower_generics ( generics, Const :: No , false , i. id , itctx, |this| {
649
656
(
650
657
// Disallow `impl Trait` in foreign items.
651
658
this. lower_fn_decl (
@@ -773,6 +780,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
773
780
let ( generics, kind) = self . lower_generics (
774
781
generics,
775
782
Const :: No ,
783
+ false ,
776
784
i. id ,
777
785
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
778
786
|this| {
@@ -821,6 +829,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
821
829
let ( generics, kind) = self . lower_generics (
822
830
& generics,
823
831
Const :: No ,
832
+ false ,
824
833
i. id ,
825
834
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
826
835
|this| {
@@ -892,7 +901,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
892
901
fn lower_impl_item (
893
902
& mut self ,
894
903
i : & AssocItem ,
895
- impl_constness : Const ,
904
+ constness_of_trait : Const ,
896
905
) -> & ' hir hir:: ImplItem < ' hir > {
897
906
// Since `default impl` is not yet implemented, this is always true in impls.
898
907
let has_value = true ;
@@ -904,6 +913,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
904
913
AssocItemKind :: Const ( box ConstItem { generics, ty, expr, .. } ) => self . lower_generics (
905
914
generics,
906
915
Const :: No ,
916
+ false ,
907
917
i. id ,
908
918
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
909
919
|this| {
@@ -928,7 +938,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
928
938
i. id ,
929
939
if self . is_in_trait_impl { FnDeclKind :: Impl } else { FnDeclKind :: Inherent } ,
930
940
sig. header . coroutine_kind ,
931
- impl_constness ,
941
+ constness_of_trait ,
932
942
) ;
933
943
934
944
( generics, hir:: ImplItemKind :: Fn ( sig, body_id) )
@@ -939,6 +949,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
939
949
self . lower_generics (
940
950
& generics,
941
951
Const :: No ,
952
+ false ,
942
953
i. id ,
943
954
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
944
955
|this| match ty {
@@ -1349,9 +1360,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
1349
1360
let constness =
1350
1361
if kind == FnDeclKind :: Inherent { sig. header . constness } else { parent_constness } ;
1351
1362
let itctx = ImplTraitContext :: Universal ;
1352
- let ( generics, decl) = self . lower_generics ( generics, constness, id, itctx, |this| {
1353
- this. lower_fn_decl ( & sig. decl , id, sig. span , kind, coroutine_kind)
1354
- } ) ;
1363
+ let ( generics, decl) =
1364
+ self . lower_generics ( generics, constness, kind == FnDeclKind :: Impl , id, itctx, |this| {
1365
+ this. lower_fn_decl ( & sig. decl , id, sig. span , kind, coroutine_kind)
1366
+ } ) ;
1355
1367
( generics, hir:: FnSig { header, decl, span : self . lower_span ( sig. span ) } )
1356
1368
}
1357
1369
@@ -1426,6 +1438,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1426
1438
& mut self ,
1427
1439
generics : & Generics ,
1428
1440
constness : Const ,
1441
+ force_append_constness : bool ,
1429
1442
parent_node_id : NodeId ,
1430
1443
itctx : ImplTraitContext ,
1431
1444
f : impl FnOnce ( & mut Self ) -> T ,
@@ -1486,7 +1499,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
1486
1499
// if the effects feature is enabled. This needs to be done before we lower where
1487
1500
// clauses since where clauses need to bind to the DefId of the host param
1488
1501
let host_param_parts = if let Const :: Yes ( span) = constness
1489
- && self . tcx . features ( ) . effects
1502
+ // if this comes from implementing a `const` trait, we must force constness to be appended
1503
+ // to the impl item, no matter whether effects is enabled.
1504
+ && ( self . tcx . features ( ) . effects || force_append_constness)
1490
1505
{
1491
1506
let span = self . lower_span ( span) ;
1492
1507
let param_node_id = self . next_node_id ( ) ;
0 commit comments