@@ -188,6 +188,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
188188 let ( generics, ( ty, body_id) ) = self . lower_generics (
189189 generics,
190190 Const :: No ,
191+ false ,
191192 id,
192193 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
193194 |this| {
@@ -218,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
218219
219220 let itctx = ImplTraitContext :: Universal ;
220221 let ( generics, decl) =
221- this. lower_generics ( generics, header. constness , id, itctx, |this| {
222+ this. lower_generics ( generics, header. constness , false , id, itctx, |this| {
222223 this. lower_fn_decl (
223224 decl,
224225 id,
@@ -262,6 +263,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
262263 let ( generics, ty) = self . lower_generics (
263264 & generics,
264265 Const :: No ,
266+ false ,
265267 id,
266268 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
267269 |this| match ty {
@@ -284,6 +286,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
284286 let ( generics, variants) = self . lower_generics (
285287 generics,
286288 Const :: No ,
289+ false ,
287290 id,
288291 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
289292 |this| {
@@ -298,6 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
298301 let ( generics, struct_def) = self . lower_generics (
299302 generics,
300303 Const :: No ,
304+ false ,
301305 id,
302306 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
303307 |this| this. lower_variant_data ( hir_id, struct_def) ,
@@ -308,6 +312,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
308312 let ( generics, vdata) = self . lower_generics (
309313 generics,
310314 Const :: No ,
315+ false ,
311316 id,
312317 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
313318 |this| this. lower_variant_data ( hir_id, vdata) ,
@@ -339,7 +344,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
339344 // parent lifetime.
340345 let itctx = ImplTraitContext :: Universal ;
341346 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| {
343348 let modifiers = TraitBoundModifiers {
344349 constness : BoundConstness :: Never ,
345350 asyncness : BoundAsyncness :: Normal ,
@@ -391,6 +396,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
391396 let ( generics, ( unsafety, items, bounds) ) = self . lower_generics (
392397 generics,
393398 Const :: No ,
399+ false ,
394400 id,
395401 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
396402 |this| {
@@ -411,6 +417,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
411417 let ( generics, bounds) = self . lower_generics (
412418 generics,
413419 Const :: No ,
420+ false ,
414421 id,
415422 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
416423 |this| {
@@ -645,7 +652,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
645652 let fdec = & sig. decl ;
646653 let itctx = ImplTraitContext :: Universal ;
647654 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| {
649656 (
650657 // Disallow `impl Trait` in foreign items.
651658 this. lower_fn_decl (
@@ -773,6 +780,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
773780 let ( generics, kind) = self . lower_generics (
774781 generics,
775782 Const :: No ,
783+ false ,
776784 i. id ,
777785 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
778786 |this| {
@@ -821,6 +829,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
821829 let ( generics, kind) = self . lower_generics (
822830 & generics,
823831 Const :: No ,
832+ false ,
824833 i. id ,
825834 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
826835 |this| {
@@ -892,7 +901,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
892901 fn lower_impl_item (
893902 & mut self ,
894903 i : & AssocItem ,
895- impl_constness : Const ,
904+ constness_of_trait : Const ,
896905 ) -> & ' hir hir:: ImplItem < ' hir > {
897906 // Since `default impl` is not yet implemented, this is always true in impls.
898907 let has_value = true ;
@@ -904,6 +913,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
904913 AssocItemKind :: Const ( box ConstItem { generics, ty, expr, .. } ) => self . lower_generics (
905914 generics,
906915 Const :: No ,
916+ false ,
907917 i. id ,
908918 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
909919 |this| {
@@ -928,7 +938,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
928938 i. id ,
929939 if self . is_in_trait_impl { FnDeclKind :: Impl } else { FnDeclKind :: Inherent } ,
930940 sig. header . coroutine_kind ,
931- impl_constness ,
941+ constness_of_trait ,
932942 ) ;
933943
934944 ( generics, hir:: ImplItemKind :: Fn ( sig, body_id) )
@@ -939,6 +949,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
939949 self . lower_generics (
940950 & generics,
941951 Const :: No ,
952+ false ,
942953 i. id ,
943954 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
944955 |this| match ty {
@@ -1349,9 +1360,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13491360 let constness =
13501361 if kind == FnDeclKind :: Inherent { sig. header . constness } else { parent_constness } ;
13511362 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+ } ) ;
13551367 ( generics, hir:: FnSig { header, decl, span : self . lower_span ( sig. span ) } )
13561368 }
13571369
@@ -1426,6 +1438,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14261438 & mut self ,
14271439 generics : & Generics ,
14281440 constness : Const ,
1441+ force_append_constness : bool ,
14291442 parent_node_id : NodeId ,
14301443 itctx : ImplTraitContext ,
14311444 f : impl FnOnce ( & mut Self ) -> T ,
@@ -1486,7 +1499,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14861499 // if the effects feature is enabled. This needs to be done before we lower where
14871500 // clauses since where clauses need to bind to the DefId of the host param
14881501 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)
14901505 {
14911506 let span = self . lower_span ( span) ;
14921507 let param_node_id = self . next_node_id ( ) ;
0 commit comments