Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse and handle where T = U predicate #87471

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,9 +1322,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
generics.span,
);

for predicate in &generics.where_clause.predicates {
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
deny_equality_constraints(self, predicate, generics);
if !self.session.features_untracked().type_equality_constraints {
for predicate in &generics.where_clause.predicates {
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
deny_equality_constraints(self, predicate, generics);
}
}
}
walk_list!(self, visit_generic_param, &generics.params);
Expand Down Expand Up @@ -1546,6 +1548,9 @@ fn deny_equality_constraints(
predicate.span,
"equality constraints are not yet supported in `where` clauses",
);
if this.session.is_nightly_build() {
err.help("add `#![feature(type_equality_constraints)]` to the crate attributes to enable");
}
err.span_label(predicate.span, "not supported");

// Given `<A as Foo>::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ declare_features! (
/// Allows `#[derive(Default)]` and `#[default]` on enums.
(active, derive_default_enum, "1.56.0", Some(86985), None),

/// Allows `where T = U` in where predicates.
(active, type_equality_constraints, "1.56.0", Some(87533), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/outlives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn explicit_outlives_bounds<'tcx>(
| ty::PredicateKind::TypeOutlives(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r_a, r_b)) => {
Some(OutlivesBound::RegionSubRegion(r_b, r_a))
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_infer/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ impl Elaborator<'tcx> {
// Currently, we do not elaborate const-equate
// predicates.
}
ty::PredicateKind::TypeEquate(..) => {
// Currently, we do not elaborate type-equate
// predicates.
}
ty::PredicateKind::RegionOutlives(..) => {
// Nothing to elaborate from `'a: 'b`.
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints {
Subtype(..) |
ConstEvaluatable(..) |
ConstEquate(..) |
TypeEquate(..) |
TypeWellFormedFromEnv(..) => continue,
};
if predicate.is_global() {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ impl FlagComputation {
ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
self.add_ty(ty);
}
ty::PredicateKind::TypeEquate(lhs, rhs) => {
self.add_ty(lhs);
self.add_ty(rhs);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ pub enum PredicateKind<'tcx> {
///
/// Only used for Chalk.
TypeWellFormedFromEnv(Ty<'tcx>),

/// `where T = U`
TypeEquate(Ty<'tcx>, Ty<'tcx>),
}

/// The crate outlives map is computed during typeck and contains the
Expand Down Expand Up @@ -800,6 +803,7 @@ impl<'tcx> Predicate<'tcx> {
| PredicateKind::TypeOutlives(..)
| PredicateKind::ConstEvaluatable(..)
| PredicateKind::ConstEquate(..)
| PredicateKind::TypeEquate(..)
| PredicateKind::TypeWellFormedFromEnv(..) => None,
}
}
Expand All @@ -817,6 +821,7 @@ impl<'tcx> Predicate<'tcx> {
| PredicateKind::ClosureKind(..)
| PredicateKind::ConstEvaluatable(..)
| PredicateKind::ConstEquate(..)
| PredicateKind::TypeEquate(..)
| PredicateKind::TypeWellFormedFromEnv(..) => None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,9 @@ define_print_and_forward_display! {
ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
p!("the type `", print(ty), "` is found in the environment")
}
ty::PredicateKind::TypeEquate(lhs, rhs) => {
p!("the type `", print(lhs), "` equals `", print(rhs), "`")
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ impl fmt::Debug for ty::PredicateKind<'tcx> {
ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
write!(f, "TypeWellFormedFromEnv({:?})", ty)
}
ty::PredicateKind::TypeEquate(lhs, rhs) => {
write!(f, "TypeEquate({:?}, {:?})", lhs, rhs)
}
}
}
}
Expand Down Expand Up @@ -450,6 +453,9 @@ impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
}
ty::PredicateKind::TypeEquate(lhs, rhs) => {
tcx.lift((lhs, rhs)).map(|(lhs, rhs)| ty::PredicateKind::TypeEquate(lhs, rhs))
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir/src/transform/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ impl Checker<'mir, 'tcx> {
| ty::PredicateKind::Projection(_)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => continue,
ty::PredicateKind::ObjectSafe(_) => {
bug!("object safe predicate on function: {:#?}", predicate)
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<'a> Parser<'a> {
}))
// FIXME: Decide what should be used here, `=` or `==`.
// FIXME: We are just dropping the binders in lifetime_defs on the floor here.
} else if self.eat(&token::Eq) || self.eat(&token::EqEq) {
JulianKnodt marked this conversation as resolved.
Show resolved Hide resolved
} else if self.eat(&token::Eq) {
let rhs_ty = self.parse_ty()?;
Ok(ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
span: lo.to(self.prev_token.span),
Expand All @@ -293,7 +293,15 @@ impl<'a> Parser<'a> {
id: ast::DUMMY_NODE_ID,
}))
} else {
self.unexpected()
if self.token == token::EqEq {
let err = self.struct_span_err(
lo.to(self.prev_token.span),
"type equality predicates are `=` not `==`",
);
Err(err)
} else {
self.unexpected()
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ where
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => {
ty.visit_with(self)
}
ty::PredicateKind::TypeEquate(lhs, rhs) => {
lhs.visit_with(self)?;
rhs.visit_with(self)
}
ty::PredicateKind::RegionOutlives(..) => ControlFlow::CONTINUE,
ty::PredicateKind::ConstEvaluatable(defs, substs)
if self.def_id_visitor.tcx().features().const_evaluatable_checked =>
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,7 @@ symbols! {
type_alias_enum_variants,
type_alias_impl_trait,
type_ascription,
type_equality_constraints,
type_id,
type_length_limit,
type_macros,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ crate fn required_region_bounds(
| ty::PredicateKind::RegionOutlives(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ref t, ref r)) => {
// Search for a bound of the form `erased_self_ty
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
)
}

ty::PredicateKind::TypeEquate(..) => {
// Errors for `TypeEquate` predicates show up as
// `SelectionError::FIXME(type_equality_constraints)`,
// not `Unimplemented`.
span_bug!(
span,
"type-equate requirement gave wrong error: `{:?}`",
obligation
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure what this was referencing, I suspect it's fine to leave this?

}

ty::PredicateKind::TypeWellFormedFromEnv(..) => span_bug!(
span,
"TypeWellFormedFromEnv predicate should only exist in the environment"
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
| ty::PredicateKind::ClosureKind(..)
| ty::PredicateKind::Subtype(_)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::ConstEquate(..) => {
let pred = infcx.replace_bound_vars_with_placeholders(binder);
ProcessResult::Changed(mk_pending(vec![
Expand Down Expand Up @@ -491,6 +492,16 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
}
}

ty::PredicateKind::TypeEquate(lhs, rhs) => {
match self.selcx.infcx().can_eq(obligation.param_env, lhs, rhs) {
Ok(()) => ProcessResult::Changed(vec![]),
Err(e) => ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
ExpectedFound::new(false, lhs, rhs),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO create a new fulfillment error code

e,
)),
}
}

ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
match const_evaluatable::is_const_evaluatable(
self.selcx.infcx(),
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_trait_selection/src/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ fn predicate_references_self(
// possible alternatives.
if data.projection_ty.substs[1..].iter().any(has_self_ty) { Some(sp) } else { None }
}
ty::PredicateKind::TypeEquate(lhs, rhs) => {
let has_self = lhs.walk().any(|arg| arg == self_ty.into())
|| rhs.walk().any(|arg| arg == self_ty.into());
if has_self { Some(sp) } else { None }
}
ty::PredicateKind::WellFormed(..)
| ty::PredicateKind::ObjectSafe(..)
| ty::PredicateKind::TypeOutlives(..)
Expand Down Expand Up @@ -343,6 +348,7 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
| ty::PredicateKind::TypeOutlives(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => false,
}
})
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}

ty::PredicateKind::TypeEquate(lhs, rhs) => {
match self.infcx.can_eq(obligation.param_env, lhs, rhs) {
Ok(()) => Ok(EvaluatedToOk),
Err(_) => Ok(EvaluatedToErr),
}
}

ty::PredicateKind::ConstEquate(c1, c2) => {
debug!(?c1, ?c2, "evaluate_predicate_recursively: equating consts");

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ pub fn predicate_obligations<'a, 'tcx>(
wf.compute(c1.into());
wf.compute(c2.into());
}
ty::PredicateKind::TypeEquate(lhs, rhs) => {
wf.compute(lhs.into());
wf.compute(rhs.into());
}
ty::PredicateKind::TypeWellFormedFromEnv(..) => {
bug!("TypeWellFormedFromEnv is only used for Chalk")
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_traits/src/chalk/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment<chalk_ir::Goal<RustInterner<'
| ty::PredicateKind::ClosureKind(..)
| ty::PredicateKind::Subtype(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::ConstEquate(..) => bug!("unexpected predicate {}", predicate),
};
let value = chalk_ir::ProgramClauseImplication {
Expand Down Expand Up @@ -194,6 +195,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData<RustInterner<'tcx>>> for ty::Predi
ty::PredicateKind::ClosureKind(..)
| ty::PredicateKind::Subtype(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::ConstEquate(..) => {
chalk_ir::GoalData::All(chalk_ir::Goals::empty(interner))
}
Expand Down Expand Up @@ -593,6 +595,7 @@ impl<'tcx> LowerInto<'tcx, Option<chalk_ir::QuantifiedWhereClause<RustInterner<'
| ty::PredicateKind::ClosureKind(..)
| ty::PredicateKind::Subtype(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => {
bug!("unexpected predicate {}", &self)
Expand Down Expand Up @@ -721,6 +724,7 @@ impl<'tcx> LowerInto<'tcx, Option<chalk_solve::rust_ir::QuantifiedInlineBound<Ru
| ty::PredicateKind::Subtype(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => {
bug!("unexpected predicate {}", &self)
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_traits/src/implied_outlives_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn compute_implied_outlives_bounds<'tcx>(
| ty::PredicateKind::ObjectSafe(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => vec![],
ty::PredicateKind::WellFormed(arg) => {
wf_args.push(arg);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_traits/src/normalize_erasing_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool {
| ty::PredicateKind::Subtype(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => true,
}
}
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty::PredicateKind::WellFormed(..) => None,
ty::PredicateKind::ObjectSafe(..) => None,
ty::PredicateKind::ConstEvaluatable(..) => None,
ty::PredicateKind::TypeEquate(..) => None,
ty::PredicateKind::ConstEquate(..) => None,
// N.B., this predicate is created by breaking down a
// `ClosureType: FnFoo()` predicate, where
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
| ty::PredicateKind::TypeOutlives(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
}
});
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2294,8 +2294,14 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
}))
}

hir::WherePredicate::EqPredicate(..) => {
// FIXME(#20041)
hir::WherePredicate::EqPredicate(where_eq_pred) => {
let hir::WhereEqPredicate { hir_id: _, span, lhs_ty, rhs_ty } = where_eq_pred;
let lhs = icx.to_ty(&lhs_ty);
let rhs = icx.to_ty(&rhs_ty);
// FIXME(type_equality_constraints): prevent 2 ty::Params here which currently
// doesn't constrain types at all? It's still useful when programming though.
predicates
.insert((ty::PredicateKind::TypeEquate(lhs, rhs).to_predicate(icx.tcx), *span));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ fn trait_predicate_kind<'tcx>(
| ty::PredicateKind::ClosureKind(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
}
}
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/outlives/explicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> {
| ty::PredicateKind::Subtype(..)
| ty::PredicateKind::ConstEvaluatable(..)
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::TypeEquate(..)
| ty::PredicateKind::TypeWellFormedFromEnv(..) => (),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ impl<'a> Clean<Option<WherePredicate>> for ty::Predicate<'a> {
ty::PredicateKind::RegionOutlives(pred) => pred.clean(cx),
ty::PredicateKind::TypeOutlives(pred) => pred.clean(cx),
ty::PredicateKind::Projection(pred) => Some(pred.clean(cx)),
ty::PredicateKind::TypeEquate(..) => todo!(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO not entirely sure whether to make this None, or have something else

ty::PredicateKind::ConstEvaluatable(..) => None,

ty::PredicateKind::Subtype(..)
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
}
clean::WherePredicate::EqPredicate { lhs, rhs } => {
if f.alternate() {
clause.push_str(&format!("{:#} == {:#}", lhs.print(cx), rhs.print(cx),));
clause.push_str(&format!("{:#} = {:#}", lhs.print(cx), rhs.print(cx),));
} else {
clause.push_str(&format!("{} == {}", lhs.print(cx), rhs.print(cx),));
clause.push_str(&format!("{} = {}", lhs.print(cx), rhs.print(cx),));
}
}
}
Expand Down
Loading