Skip to content

Commit 2f7841e

Browse files
committed
restructure a bit
1 parent f3672f8 commit 2f7841e

File tree

1 file changed

+23
-45
lines changed

1 file changed

+23
-45
lines changed

crates/ty_python_semantic/src/types/constraints.rs

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,19 +1267,6 @@ impl<'db> InteriorNode<'db> {
12671267
let left_typevar = left_constraint.typevar(db);
12681268
let right_typevar = right_constraint.typevar(db);
12691269
if !left_typevar.is_same_typevar_as(db, right_typevar) {
1270-
let mut add_implication = |node: Node<'db>| {
1271-
let positive_left_node =
1272-
Node::new_satisfied_constraint(db, left_constraint.when_true());
1273-
let positive_right_node =
1274-
Node::new_satisfied_constraint(db, right_constraint.when_true());
1275-
let lhs = positive_left_node.and(db, positive_right_node);
1276-
let implication = lhs.implies(db, node);
1277-
domain = domain.and(db, implication);
1278-
1279-
let intersection = node.ite(db, lhs, Node::AlwaysFalse);
1280-
simplified = simplified.and(db, intersection);
1281-
};
1282-
12831270
// We've structured our constraints so that a typevar's upper/lower bound can only
12841271
// be another typevar if the bound is "later" in our arbitrary ordering. That means
12851272
// we only have to check this pair of constraints in one direction — though we do
@@ -1303,9 +1290,10 @@ impl<'db> InteriorNode<'db> {
13031290
};
13041291

13051292
// We then look for cases where the "constrained" typevar's upper and/or lower
1306-
// bound matches the "bound" typevar. If so, we add an implication to the
1307-
// constraint set.
1308-
match (
1293+
// bound matches the "bound" typevar. If so, we're going to add an implication to
1294+
// the constraint set that replaces the upper/lower bound that matched the bound
1295+
// typevar with the bound constraint's corresponding bound.
1296+
let (new_lower, new_upper) = match (
13091297
constrained_constraint.lower(db),
13101298
constrained_constraint.upper(db),
13111299
) {
@@ -1314,50 +1302,40 @@ impl<'db> InteriorNode<'db> {
13141302
if constrained_lower.is_same_typevar_as(db, bound_typevar)
13151303
&& constrained_upper.is_same_typevar_as(db, bound_typevar) =>
13161304
{
1317-
add_implication(Node::new_constraint(
1318-
db,
1319-
ConstrainedTypeVar::new(
1320-
db,
1321-
constrained_typevar,
1322-
bound_constraint.lower(db),
1323-
bound_constraint.upper(db),
1324-
),
1325-
));
1305+
(bound_constraint.lower(db), bound_constraint.upper(db))
13261306
}
13271307

13281308
// (CL ≤ C ≤ B) ∧ (BL ≤ B ≤ BU) → (CL ≤ C ≤ BU)
13291309
(constrained_lower, Type::TypeVar(constrained_upper))
13301310
if constrained_upper.is_same_typevar_as(db, bound_typevar) =>
13311311
{
1332-
add_implication(Node::new_constraint(
1333-
db,
1334-
ConstrainedTypeVar::new(
1335-
db,
1336-
constrained_typevar,
1337-
constrained_lower,
1338-
bound_constraint.upper(db),
1339-
),
1340-
));
1312+
(constrained_lower, bound_constraint.upper(db))
13411313
}
13421314

13431315
// (B ≤ C ≤ CU) ∧ (BL ≤ B ≤ BU) → (BL ≤ C ≤ CU)
13441316
(Type::TypeVar(constrained_lower), constrained_upper)
13451317
if constrained_lower.is_same_typevar_as(db, bound_typevar) =>
13461318
{
1347-
add_implication(Node::new_constraint(
1348-
db,
1349-
ConstrainedTypeVar::new(
1350-
db,
1351-
constrained_typevar,
1352-
bound_constraint.lower(db),
1353-
constrained_upper,
1354-
),
1355-
));
1319+
(bound_constraint.lower(db), constrained_upper)
13561320
}
13571321

1358-
_ => {}
1359-
}
1322+
_ => continue,
1323+
};
1324+
1325+
let new_node = Node::new_constraint(
1326+
db,
1327+
ConstrainedTypeVar::new(db, constrained_typevar, new_lower, new_upper),
1328+
);
1329+
let positive_left_node =
1330+
Node::new_satisfied_constraint(db, left_constraint.when_true());
1331+
let positive_right_node =
1332+
Node::new_satisfied_constraint(db, right_constraint.when_true());
1333+
let lhs = positive_left_node.and(db, positive_right_node);
1334+
let implication = lhs.implies(db, new_node);
1335+
domain = domain.and(db, implication);
13601336

1337+
let intersection = new_node.ite(db, lhs, Node::AlwaysFalse);
1338+
simplified = simplified.and(db, intersection);
13611339
continue;
13621340
}
13631341

0 commit comments

Comments
 (0)