summaryrefslogtreecommitdiff
path: root/compiler/rustc_hir_analysis
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2023-04-25 05:15:50 +0000
committerMichael Goulet <michael@errs.io>2023-05-02 22:36:24 +0000
commit6e01e910cb8ab0109235be7cc7ab7ef465724255 (patch)
treed63e7272022d540f0a93d43f36e359ff31c3f56d /compiler/rustc_hir_analysis
parent98c33e47a495fbd7b22bce9ce32f2815991bc414 (diff)
downloadrust-6e01e910cb8ab0109235be7cc7ab7ef465724255.tar.gz
Implement negative bounds
Diffstat (limited to 'compiler/rustc_hir_analysis')
-rw-r--r--compiler/rustc_hir_analysis/src/astconv/mod.rs26
-rw-r--r--compiler/rustc_hir_analysis/src/bounds.rs8
-rw-r--r--compiler/rustc_hir_analysis/src/lib.rs1
3 files changed, 29 insertions, 6 deletions
diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs
index 6cb008bc5f8..0faf7a4ba45 100644
--- a/compiler/rustc_hir_analysis/src/astconv/mod.rs
+++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs
@@ -665,6 +665,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
span: Span,
binding_span: Option<Span>,
constness: ty::BoundConstness,
+ polarity: ty::ImplPolarity,
bounds: &mut Bounds<'tcx>,
speculative: bool,
trait_ref_span: Span,
@@ -696,10 +697,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ty::Binder::bind_with_vars(tcx.mk_trait_ref(trait_def_id, substs), bound_vars);
debug!(?poly_trait_ref, ?assoc_bindings);
- bounds.push_trait_bound(tcx, poly_trait_ref, span, constness);
+ bounds.push_trait_bound(tcx, poly_trait_ref, span, constness, polarity);
let mut dup_bindings = FxHashMap::default();
for binding in &assoc_bindings {
+ // TODO: negative polarity can't have associated type bindings!
+
// Specify type to assert that error was already reported in `Err` case.
let _: Result<_, ErrorGuaranteed> = self.add_predicates_for_ast_type_binding(
hir_id,
@@ -711,6 +714,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
binding_span.unwrap_or(binding.span),
constness,
only_self_bounds,
+ polarity,
);
// Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
}
@@ -743,6 +747,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
trait_ref: &hir::TraitRef<'_>,
span: Span,
constness: ty::BoundConstness,
+ polarity: ty::ImplPolarity,
self_ty: Ty<'tcx>,
bounds: &mut Bounds<'tcx>,
speculative: bool,
@@ -764,6 +769,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
span,
binding_span,
constness,
+ polarity,
bounds,
speculative,
trait_ref_span,
@@ -799,6 +805,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
span,
binding_span,
constness,
+ ty::ImplPolarity::Positive,
bounds,
speculative,
trait_ref_span,
@@ -961,16 +968,23 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
for ast_bound in ast_bounds {
match ast_bound {
hir::GenericBound::Trait(poly_trait_ref, modifier) => {
- let constness = match modifier {
- hir::TraitBoundModifier::MaybeConst => ty::BoundConstness::ConstIfConst,
- hir::TraitBoundModifier::None => ty::BoundConstness::NotConst,
+ let (constness, polarity) = match modifier {
+ hir::TraitBoundModifier::MaybeConst => {
+ (ty::BoundConstness::ConstIfConst, ty::ImplPolarity::Positive)
+ }
+ hir::TraitBoundModifier::None => {
+ (ty::BoundConstness::NotConst, ty::ImplPolarity::Positive)
+ }
+ hir::TraitBoundModifier::Negative => {
+ (ty::BoundConstness::NotConst, ty::ImplPolarity::Negative)
+ }
hir::TraitBoundModifier::Maybe => continue,
};
-
let _ = self.instantiate_poly_trait_ref(
&poly_trait_ref.trait_ref,
poly_trait_ref.span,
constness,
+ polarity,
param_ty,
bounds,
false,
@@ -1088,6 +1102,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
path_span: Span,
constness: ty::BoundConstness,
only_self_bounds: OnlySelfBounds,
+ polarity: ty::ImplPolarity,
) -> Result<(), ErrorGuaranteed> {
// Given something like `U: SomeTrait<T = X>`, we want to produce a
// predicate like `<U as SomeTrait>::T = X`. This is somewhat
@@ -1438,6 +1453,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
&trait_bound.trait_ref,
trait_bound.span,
ty::BoundConstness::NotConst,
+ ty::ImplPolarity::Positive,
dummy_self,
&mut bounds,
false,
diff --git a/compiler/rustc_hir_analysis/src/bounds.rs b/compiler/rustc_hir_analysis/src/bounds.rs
index 284b099e7bc..7156fea8f89 100644
--- a/compiler/rustc_hir_analysis/src/bounds.rs
+++ b/compiler/rustc_hir_analysis/src/bounds.rs
@@ -42,8 +42,14 @@ impl<'tcx> Bounds<'tcx> {
trait_ref: ty::PolyTraitRef<'tcx>,
span: Span,
constness: ty::BoundConstness,
+ polarity: ty::ImplPolarity,
) {
- self.predicates.push((trait_ref.with_constness(constness).to_predicate(tcx), span));
+ self.predicates.push((
+ trait_ref
+ .map_bound(|trait_ref| ty::TraitPredicate { trait_ref, constness, polarity })
+ .to_predicate(tcx),
+ span,
+ ));
}
pub fn push_projection_bound(
diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs
index 961457b7579..3fe34f23aef 100644
--- a/compiler/rustc_hir_analysis/src/lib.rs
+++ b/compiler/rustc_hir_analysis/src/lib.rs
@@ -528,6 +528,7 @@ pub fn hir_trait_to_predicates<'tcx>(
hir_trait,
DUMMY_SP,
ty::BoundConstness::NotConst,
+ ty::ImplPolarity::Positive,
self_ty,
&mut bounds,
true,