1
1
use super :: errors:: { InvalidAbi , InvalidAbiReason , InvalidAbiSuggestion , MisplacedRelaxTraitBound } ;
2
2
use super :: ResolverAstLoweringExt ;
3
3
use super :: { AstOwner , ImplTraitContext , ImplTraitPosition } ;
4
- use super :: { FnDeclKind , LoweringContext , ParamMode } ;
4
+ use super :: { EffectContext , FnDeclKind , LoweringContext , ParamMode } ;
5
5
6
6
use hir:: definitions:: DefPathData ;
7
7
use rustc_ast:: ptr:: P ;
@@ -90,7 +90,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
90
90
allow_try_trait : Some ( [ sym:: try_trait_v2, sym:: yeet_desugar_details] [ ..] . into ( ) ) ,
91
91
allow_gen_future,
92
92
generics_def_id_map : Default :: default ( ) ,
93
- host_param_id : None ,
93
+ effect_context : None ,
94
94
} ;
95
95
lctx. with_hir_id_owner ( owner, |lctx| f ( lctx) ) ;
96
96
@@ -150,7 +150,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
150
150
lctx. is_in_trait_impl = impl_. of_trait . is_some ( ) ;
151
151
}
152
152
hir:: ItemKind :: Trait ( _, _, generics, _, _) if lctx. tcx . features ( ) . effects => {
153
- lctx. host_param_id = generics
153
+ lctx. effect_context = generics
154
154
. params
155
155
. iter ( )
156
156
. find ( |param| {
@@ -160,7 +160,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
160
160
. iter ( )
161
161
. any ( |attr| attr. has_name ( sym:: rustc_host) )
162
162
} )
163
- . map ( |param| param. def_id ) ;
163
+ . map ( |param| EffectContext :: Parametrized { host_param_id : param. def_id } ) ;
164
164
}
165
165
_ => { }
166
166
}
@@ -256,7 +256,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
256
256
ItemKind :: Const ( box ast:: ConstItem { generics, ty, expr, .. } ) => {
257
257
let ( generics, ( ty, body_id) ) = self . lower_generics (
258
258
generics,
259
- Const :: No ,
259
+ Constness :: Always ,
260
260
id,
261
261
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
262
262
|this| this. lower_const_item ( ty, span, expr. as_deref ( ) ) ,
@@ -286,11 +286,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
286
286
) ;
287
287
288
288
let itctx = ImplTraitContext :: Universal ;
289
- let ( generics, decl) =
290
- this. lower_generics ( generics, header. constness , id, & itctx, |this| {
289
+ let ( generics, decl) = this. lower_generics (
290
+ generics,
291
+ header. constness . into ( ) ,
292
+ id,
293
+ & itctx,
294
+ |this| {
291
295
let ret_id = asyncness. opt_return_id ( ) ;
292
296
this. lower_fn_decl ( & decl, id, * fn_sig_span, FnDeclKind :: Fn , ret_id)
293
- } ) ;
297
+ } ,
298
+ ) ;
294
299
let sig = hir:: FnSig {
295
300
decl,
296
301
header : this. lower_fn_header ( * header) ,
@@ -325,7 +330,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
325
330
add_ty_alias_where_clause ( & mut generics, * where_clauses, true ) ;
326
331
let ( generics, ty) = self . lower_generics (
327
332
& generics,
328
- Const :: No ,
333
+ Constness :: Never ,
329
334
id,
330
335
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
331
336
|this| match ty {
@@ -347,7 +352,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
347
352
ItemKind :: Enum ( enum_definition, generics) => {
348
353
let ( generics, variants) = self . lower_generics (
349
354
generics,
350
- Const :: No ,
355
+ Constness :: Never ,
351
356
id,
352
357
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
353
358
|this| {
@@ -361,7 +366,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
361
366
ItemKind :: Struct ( struct_def, generics) => {
362
367
let ( generics, struct_def) = self . lower_generics (
363
368
generics,
364
- Const :: No ,
369
+ Constness :: Never ,
365
370
id,
366
371
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
367
372
|this| this. lower_variant_data ( hir_id, struct_def) ,
@@ -371,7 +376,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
371
376
ItemKind :: Union ( vdata, generics) => {
372
377
let ( generics, vdata) = self . lower_generics (
373
378
generics,
374
- Const :: No ,
379
+ Constness :: Never ,
375
380
id,
376
381
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
377
382
|this| this. lower_variant_data ( hir_id, vdata) ,
@@ -403,7 +408,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
403
408
// parent lifetime.
404
409
let itctx = ImplTraitContext :: Universal ;
405
410
let ( generics, ( trait_ref, lowered_ty) ) =
406
- self . lower_generics ( ast_generics, * constness, id, & itctx, |this| {
411
+ self . lower_generics ( ast_generics, ( * constness) . into ( ) , id, & itctx, |this| {
407
412
let trait_ref = trait_ref. as_ref ( ) . map ( |trait_ref| {
408
413
this. lower_trait_ref (
409
414
* constness,
@@ -449,7 +454,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
449
454
. unwrap_or ( & [ ] )
450
455
. iter ( )
451
456
. find ( |x| x. has_name ( sym:: const_trait) )
452
- . map_or ( Const :: No , |x| Const :: Yes ( x. span ) ) ;
457
+ . map_or ( Constness :: Never , |x| Constness :: Maybe ( x. span ) ) ;
453
458
let ( generics, ( unsafety, items, bounds) ) = self . lower_generics (
454
459
generics,
455
460
constness,
@@ -472,7 +477,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
472
477
ItemKind :: TraitAlias ( generics, bounds) => {
473
478
let ( generics, bounds) = self . lower_generics (
474
479
generics,
475
- Const :: No ,
480
+ Constness :: Never ,
476
481
id,
477
482
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
478
483
|this| {
@@ -627,7 +632,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
627
632
let fdec = & sig. decl ;
628
633
let itctx = ImplTraitContext :: Universal ;
629
634
let ( generics, ( fn_dec, fn_args) ) =
630
- self . lower_generics ( generics, Const :: No , i. id , & itctx, |this| {
635
+ self . lower_generics ( generics, Constness :: Never , i. id , & itctx, |this| {
631
636
(
632
637
// Disallow `impl Trait` in foreign items.
633
638
this. lower_fn_decl (
@@ -746,7 +751,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
746
751
AssocItemKind :: Const ( box ConstItem { generics, ty, expr, .. } ) => {
747
752
let ( generics, kind) = self . lower_generics (
748
753
& generics,
749
- Const :: No ,
754
+ Constness :: Always ,
750
755
i. id ,
751
756
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
752
757
|this| {
@@ -791,7 +796,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
791
796
add_ty_alias_where_clause ( & mut generics, * where_clauses, false ) ;
792
797
let ( generics, kind) = self . lower_generics (
793
798
& generics,
794
- Const :: No ,
799
+ Constness :: Never ,
795
800
i. id ,
796
801
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
797
802
|this| {
@@ -859,7 +864,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
859
864
let ( generics, kind) = match & i. kind {
860
865
AssocItemKind :: Const ( box ConstItem { generics, ty, expr, .. } ) => self . lower_generics (
861
866
& generics,
862
- Const :: No ,
867
+ Constness :: Always ,
863
868
i. id ,
864
869
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
865
870
|this| {
@@ -895,7 +900,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
895
900
add_ty_alias_where_clause ( & mut generics, * where_clauses, false ) ;
896
901
self . lower_generics (
897
902
& generics,
898
- Const :: No ,
903
+ Constness :: Never ,
899
904
i. id ,
900
905
& ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
901
906
|this| match ty {
@@ -1255,7 +1260,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1255
1260
let header = self . lower_fn_header ( sig. header ) ;
1256
1261
let itctx = ImplTraitContext :: Universal ;
1257
1262
let ( generics, decl) =
1258
- self . lower_generics ( generics, sig. header . constness , id, & itctx, |this| {
1263
+ self . lower_generics ( generics, sig. header . constness . into ( ) , id, & itctx, |this| {
1259
1264
this. lower_fn_decl ( & sig. decl , id, sig. span , kind, is_async)
1260
1265
} ) ;
1261
1266
( generics, hir:: FnSig { header, decl, span : self . lower_span ( sig. span ) } )
@@ -1333,7 +1338,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1333
1338
fn lower_generics < T > (
1334
1339
& mut self ,
1335
1340
generics : & Generics ,
1336
- constness : Const ,
1341
+ constness : Constness ,
1337
1342
parent_node_id : NodeId ,
1338
1343
itctx : & ImplTraitContext ,
1339
1344
f : impl FnOnce ( & mut Self ) -> T ,
@@ -1387,7 +1392,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1387
1392
// Desugar `~const` bound in generics into an additional `const host: bool` param
1388
1393
// if the effects feature is enabled. This needs to be done before we lower where
1389
1394
// clauses since where clauses need to bind to the DefId of the host param
1390
- let host_param_parts = if let Const :: Yes ( span) = constness
1395
+ let host_param_parts = if let Constness :: Maybe ( span) = constness
1391
1396
&& self . tcx . features ( ) . effects
1392
1397
{
1393
1398
if let Some ( param) =
@@ -1396,7 +1401,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
1396
1401
// user has manually specified a `rustc_host` param, in this case, we set
1397
1402
// the param id so that lowering logic can use that. But we don't create
1398
1403
// another host param, so this gives `None`.
1399
- self . host_param_id = Some ( self . local_def_id ( param. id ) ) ;
1404
+ self . effect_context = Some ( EffectContext :: Parametrized {
1405
+ host_param_id : self . local_def_id ( param. id ) ,
1406
+ } ) ;
1400
1407
None
1401
1408
} else {
1402
1409
let param_node_id = self . next_node_id ( ) ;
@@ -1407,13 +1414,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
1407
1414
DefPathData :: TypeNs ( sym:: host) ,
1408
1415
span,
1409
1416
) ;
1410
- self . host_param_id = Some ( def_id) ;
1417
+ self . effect_context = Some ( EffectContext :: Parametrized {
1418
+ host_param_id : def_id,
1419
+ } ) ;
1411
1420
Some ( ( span, hir_id, def_id) )
1412
1421
}
1413
1422
} else {
1414
1423
None
1415
1424
} ;
1416
1425
1426
+ // FIXME(generic_const_items, effects): Instead of interpreting `~const` bounds as `const`
1427
+ // bounds, we should probably introduce `const` bounds into the surface language.
1428
+ if let Constness :: Always = constness && self . tcx . features ( ) . effects {
1429
+ self . effect_context = Some ( EffectContext :: Invariant { value : false } ) ;
1430
+ } ;
1431
+
1417
1432
let mut predicates: SmallVec < [ hir:: WherePredicate < ' hir > ; 4 ] > = SmallVec :: new ( ) ;
1418
1433
predicates. extend ( generics. params . iter ( ) . filter_map ( |param| {
1419
1434
self . lower_generic_bound_predicate (
@@ -1660,3 +1675,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
1660
1675
}
1661
1676
}
1662
1677
}
1678
+
1679
+ #[ derive( Debug ) ]
1680
+ enum Constness {
1681
+ Always ,
1682
+ Maybe ( Span ) ,
1683
+ Never ,
1684
+ }
1685
+
1686
+ impl From < Const > for Constness {
1687
+ fn from ( constness : Const ) -> Self {
1688
+ match constness {
1689
+ Const :: Yes ( span) => Self :: Maybe ( span) ,
1690
+ Const :: No => Self :: Never ,
1691
+ }
1692
+ }
1693
+ }
0 commit comments