Skip to content

Commit ec744d0

Browse files
committed
fix: a partial caret or tilde requirement treats unwritten segments as wildcards
`^0` and `^0.0` bounded their range at 0.0.1, and `~1` and `~0` at 1.1 and 0.1. The caret branch chose the segment to bump through major(), minor() and patch(), which read a segment that was never written as 0, so `^0` fell through to bumping the patch; the tilde branch always bumped the minor. A dependency written `"^0"` against an index with 0.3.0 and 0.4.0 failed with "matches none of", and `"~1"` never selected 1.1.0 or later. The grammar is Cargo's, and Cargo reads an unwritten segment as a wildcard: `^0` is >=0.0.0, <1.0.0; `^0.0` is >=0.0.0, <0.1.0; `~1` is >=1.0.0, <2.0.0. mcpp.xpkg_version already bounds a partial caret this way. The caret bound now bumps the leftmost non-zero segment, looking no further than the patch and stopping at the last segment written; the tilde bound holds the major alone when no minor is written. Every fully written requirement (`^0.0.0`, `^0.0.3`, `^1.2.3`, `~1.2`, the four-segment date versions) keeps its bound. Unit tests cover the partial caret and tilde forms next to the existing caret and tilde cases, and docs/05-dependencies.md (both languages) lists the two forms. Closes #864
1 parent db3b815 commit ec744d0

4 files changed

Lines changed: 73 additions & 8 deletions

File tree

‎docs/05-dependencies.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ refused offline.
115115
[dependencies]
116116
foo = "^1.2.3" # >= 1.2.3, < 2.0.0 (caret, default)
117117
bar = "~1.2.3" # >= 1.2.3, < 1.3.0 (tilde)
118+
cat = "^0" # >= 0.0.0, < 1.0.0 (a segment not written is a wildcard)
119+
dog = "~1" # >= 1.0.0, < 2.0.0
118120
baz = "=1.2.3" # Exact match
119121
qux = ">=1.0, <2.0" # Range combination
120122
```

‎docs/zh/05-dependencies.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ commit。`git =` 指向本地目录(或 `file://` URL)时,这两件事都
105105
[dependencies]
106106
foo = "^1.2.3" # >= 1.2.3, < 2.0.0 (caret, default)
107107
bar = "~1.2.3" # >= 1.2.3, < 1.3.0 (tilde)
108+
cat = "^0" # >= 0.0.0, < 1.0.0 (a segment not written is a wildcard)
109+
dog = "~1" # >= 1.0.0, < 2.0.0
108110
baz = "=1.2.3" # Exact match
109111
qux = ">=1.0, <2.0" # Range combination
110112
```

‎modules/versioning/src/version_req.cppm‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// "1.2.3" → caret-default: >=1.2.3, <2.0.0
55
// "^1.2.3" → explicit caret
66
// "~1.2.3" → tilde: >=1.2.3, <1.3.0
7+
// "^0", "^0.0" → partial caret: <1.0.0, <0.1.0 (an unwritten segment is a wildcard)
8+
// "~1" → partial tilde: >=1.0.0, <2.0.0
79
// "=1.2.3" → exact: ==1.2.3
810
// ">=1.2, <2.0" → comma-separated comparators (AND)
911
// "*" → any
@@ -372,8 +374,13 @@ bool matches(const Requirement& r, const Version& v) {
372374
case Op::Le: if (!(v <= c.v)) return false; break;
373375
case Op::Caret: {
374376
// ^X.Y.Z = >=X.Y.Z, <(X+1).0.0 (leftmost-nonzero rule)
375-
// For simplicity here: bump major; if major==0 bump minor; if both 0 bump patch.
376-
// Every branch must also zero the segments AFTER the bumped one,
377+
// The bound bumps the leftmost non-zero segment, looking no
378+
// further than the patch. A segment that is not written is a
379+
// wildcard rather than a zero, so the search also stops at the
380+
// last one written: `^0` is <1.0.0 and `^0.0` is <0.1.0, as in
381+
// Cargo, while `^0.0.0` stays <0.0.1. Reading the absent
382+
// segments as zeros made `^0` and `^0.0` admit 0.0.0 alone.
383+
// The bump must also zero the segments AFTER the bumped one,
377384
// or the upper bound inherits the constraint's own tail and
378385
// wrongly excludes releases below it (^2026.7.27.3 would cut off
379386
// 2027.0.0.0..2). The bound is a release, never a pre-release:
@@ -385,19 +392,23 @@ bool matches(const Requirement& r, const Version& v) {
385392
++upper.nums[idx];
386393
upper.nums.resize(idx + 1);
387394
};
388-
if (c.v.major() != 0) bump_at(0);
389-
else if (c.v.minor() != 0) bump_at(1);
390-
else bump_at(2);
395+
const std::size_t written = std::clamp<std::size_t>(c.v.components(), 1, 3);
396+
std::size_t at = written - 1;
397+
for (std::size_t i = 0; i + 1 < written; ++i)
398+
if (c.v.seg(i) != 0) { at = i; break; }
399+
bump_at(at);
391400
if (!(v >= c.v && v < upper)) return false;
392401
break;
393402
}
394403
case Op::Tilde: {
395404
// ~X.Y.Z = >=X.Y.Z, <X.(Y+1).0
405+
// ~X = >=X.0.0, <(X+1).0.0 -- with the minor not written
406+
// there is no minor to hold, so only the major is (Cargo).
396407
Version upper = c.v;
397408
upper.prerelease.clear();
398-
upper.nums.resize(std::max<std::size_t>(upper.nums.size(), 2), 0);
399-
++upper.nums[1];
400-
upper.nums.resize(2);
409+
const std::size_t at = c.v.components() == 1 ? 0 : 1;
410+
upper.nums.resize(at + 1);
411+
++upper.nums[at];
401412
if (!(v >= c.v && v < upper)) return false;
402413
break;
403414
}

‎tests/unit/test_version_req.cpp‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,56 @@ TEST(VersionReq, MatchTilde) {
6464
EXPECT_FALSE(matches(*r, *parse_version("1.2.2")));
6565
}
6666

67+
// A segment that is not written is a wildcard, not a zero (Cargo's partial
68+
// requirements). Reading `^0` as `^0.0.0` made it admit 0.0.0 alone.
69+
TEST(VersionReq, PartialCaretBoundsFollowTheWrittenSegments) {
70+
auto major = parse_req("^0"); ASSERT_TRUE(major);
71+
EXPECT_TRUE (matches(*major, *parse_version("0.0.0")));
72+
EXPECT_TRUE (matches(*major, *parse_version("0.0.5")));
73+
EXPECT_TRUE (matches(*major, *parse_version("0.9.3")));
74+
EXPECT_FALSE(matches(*major, *parse_version("1.0.0")));
75+
76+
auto minor = parse_req("^0.0"); ASSERT_TRUE(minor);
77+
EXPECT_TRUE (matches(*minor, *parse_version("0.0.0")));
78+
EXPECT_TRUE (matches(*minor, *parse_version("0.0.7")));
79+
EXPECT_FALSE(matches(*minor, *parse_version("0.1.0")));
80+
81+
// Every segment written: the leftmost-nonzero rule is unchanged.
82+
auto patch = parse_req("^0.0.0"); ASSERT_TRUE(patch);
83+
EXPECT_TRUE (matches(*patch, *parse_version("0.0.0")));
84+
EXPECT_FALSE(matches(*patch, *parse_version("0.0.1")));
85+
86+
auto exact = parse_req("^0.0.3"); ASSERT_TRUE(exact);
87+
EXPECT_TRUE (matches(*exact, *parse_version("0.0.3")));
88+
EXPECT_FALSE(matches(*exact, *parse_version("0.0.4")));
89+
90+
auto one = parse_req("^1"); ASSERT_TRUE(one);
91+
EXPECT_TRUE (matches(*one, *parse_version("1.9.9")));
92+
EXPECT_FALSE(matches(*one, *parse_version("2.0.0")));
93+
94+
auto zeroFive = parse_req("^0.5"); ASSERT_TRUE(zeroFive);
95+
EXPECT_TRUE (matches(*zeroFive, *parse_version("0.5.9")));
96+
EXPECT_FALSE(matches(*zeroFive, *parse_version("0.6.0")));
97+
}
98+
99+
TEST(VersionReq, PartialTildeWithOnlyAMajorHoldsTheMajor) {
100+
auto r = parse_req("~1"); ASSERT_TRUE(r);
101+
EXPECT_TRUE (matches(*r, *parse_version("1.0.0")));
102+
EXPECT_TRUE (matches(*r, *parse_version("1.1.0")));
103+
EXPECT_TRUE (matches(*r, *parse_version("1.9.9")));
104+
EXPECT_FALSE(matches(*r, *parse_version("2.0.0")));
105+
EXPECT_FALSE(matches(*r, *parse_version("0.9.9")));
106+
107+
auto zero = parse_req("~0"); ASSERT_TRUE(zero);
108+
EXPECT_TRUE (matches(*zero, *parse_version("0.4.0")));
109+
EXPECT_FALSE(matches(*zero, *parse_version("1.0.0")));
110+
111+
// With the minor written, tilde holds it, as before.
112+
auto minor = parse_req("~1.2"); ASSERT_TRUE(minor);
113+
EXPECT_TRUE (matches(*minor, *parse_version("1.2.9")));
114+
EXPECT_FALSE(matches(*minor, *parse_version("1.3.0")));
115+
}
116+
67117
TEST(VersionReq, MatchRangeAnd) {
68118
auto r = parse_req(">=1.2, <2.0"); ASSERT_TRUE(r);
69119
EXPECT_TRUE (matches(*r, *parse_version("1.2.0")));

0 commit comments

Comments
 (0)