diff options
| author | León Orell Valerian Liehr <me@fmease.dev> | 2023-12-20 15:22:06 +0100 |
|---|---|---|
| committer | León Orell Valerian Liehr <me@fmease.dev> | 2023-12-20 19:39:46 +0100 |
| commit | 5e4f12b41a13d6adf883cfeae8a17724ea457faf (patch) | |
| tree | dc503e9554557b08835ac3127af07a47c29018e2 /compiler/rustc_ast_lowering | |
| parent | bf9229a2e366b4c311f059014a4aa08af16de5d8 (diff) | |
| download | rust-5e4f12b41a13d6adf883cfeae8a17724ea457faf.tar.gz rust-5e4f12b41a13d6adf883cfeae8a17724ea457faf.zip | |
Refactor AST trait bound modifiers
Diffstat (limited to 'compiler/rustc_ast_lowering')
| -rw-r--r-- | compiler/rustc_ast_lowering/src/item.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_ast_lowering/src/lib.rs | 53 |
2 files changed, 34 insertions, 27 deletions
diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 83452c22280..c138ce459a8 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1371,7 +1371,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // need to compute this at all unless there is a Maybe bound. let mut is_param: Option<bool> = None; for bound in &bound_pred.bounds { - if !matches!(*bound, GenericBound::Trait(_, TraitBoundModifier::Maybe)) { + if !matches!( + *bound, + GenericBound::Trait( + _, + TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. } + ) + ) { continue; } let is_param = *is_param.get_or_insert_with(compute_is_param); diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 96ed3eee02e..f5d7d85b622 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1425,19 +1425,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound { GenericBound::Trait( ty, - modifier @ (TraitBoundModifier::None - | TraitBoundModifier::MaybeConst(_) - | TraitBoundModifier::Negative), - ) => { - Some(this.lower_poly_trait_ref(ty, itctx, modifier.to_constness())) - } - // `~const ?Bound` will cause an error during AST validation - // anyways, so treat it like `?Bound` as compilation proceeds. + TraitBoundModifiers { + polarity: BoundPolarity::Positive | BoundPolarity::Negative(_), + constness, + }, + ) => Some(this.lower_poly_trait_ref(ty, itctx, (*constness).into())), + // We can safely ignore constness here, since AST validation + // will take care of invalid modifier combinations. GenericBound::Trait( _, - TraitBoundModifier::Maybe - | TraitBoundModifier::MaybeConstMaybe - | TraitBoundModifier::MaybeConstNegative, + TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. }, ) => None, GenericBound::Outlives(lifetime) => { if lifetime_bound.is_none() { @@ -2032,9 +2029,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { itctx: &ImplTraitContext, ) -> hir::GenericBound<'hir> { match tpb { - GenericBound::Trait(p, modifier) => hir::GenericBound::Trait( - self.lower_poly_trait_ref(p, itctx, modifier.to_constness()), - self.lower_trait_bound_modifier(*modifier), + GenericBound::Trait(p, modifiers) => hir::GenericBound::Trait( + self.lower_poly_trait_ref(p, itctx, modifiers.constness.into()), + self.lower_trait_bound_modifiers(*modifiers), ), GenericBound::Outlives(lifetime) => { hir::GenericBound::Outlives(self.lower_lifetime(lifetime)) @@ -2320,25 +2317,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } } - fn lower_trait_bound_modifier(&mut self, f: TraitBoundModifier) -> hir::TraitBoundModifier { - match f { - TraitBoundModifier::None => hir::TraitBoundModifier::None, - TraitBoundModifier::MaybeConst(_) => hir::TraitBoundModifier::MaybeConst, - - TraitBoundModifier::Negative => { + fn lower_trait_bound_modifiers( + &mut self, + modifiers: TraitBoundModifiers, + ) -> hir::TraitBoundModifier { + match (modifiers.constness, modifiers.polarity) { + (BoundConstness::Never, BoundPolarity::Positive) => hir::TraitBoundModifier::None, + (BoundConstness::Never, BoundPolarity::Maybe(_)) => hir::TraitBoundModifier::Maybe, + (BoundConstness::Never, BoundPolarity::Negative(_)) => { if self.tcx.features().negative_bounds { hir::TraitBoundModifier::Negative } else { hir::TraitBoundModifier::None } } - - // `MaybeConstMaybe` will cause an error during AST validation, but we need to pick a - // placeholder for compilation to proceed. - TraitBoundModifier::MaybeConstMaybe | TraitBoundModifier::Maybe => { - hir::TraitBoundModifier::Maybe + (BoundConstness::Maybe(_), BoundPolarity::Positive) => { + hir::TraitBoundModifier::MaybeConst + } + // Invalid modifier combinations will cause an error during AST validation. + // Arbitrarily pick a placeholder for compilation to proceed. + (BoundConstness::Maybe(_), BoundPolarity::Maybe(_)) => hir::TraitBoundModifier::Maybe, + (BoundConstness::Maybe(_), BoundPolarity::Negative(_)) => { + hir::TraitBoundModifier::MaybeConst } - TraitBoundModifier::MaybeConstNegative => hir::TraitBoundModifier::MaybeConst, } } |
