Skip to content

Commit e0bb309

Browse files
committed
fix(opam): cleanly reject argumentless (and)/(or)
Otherwise they would raise a code error later. Signed-off-by: Etienne Millon <[email protected]>
1 parent c51f6c2 commit e0bb309

File tree

4 files changed

+116
-29
lines changed

4 files changed

+116
-29
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Unreleased
4141

4242
- Attach melange rules to the default alias (#7926, @haochenx)
4343

44+
- In opam constraints, reject `(and)` and `(or)` with no arguments at parse
45+
time (#7730, @emillon)
46+
4447
3.8.1 (2023-06-05)
4548
------------------
4649

src/dune_rules/package.ml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ module Dependency = struct
157157
| And conjuncts -> list sexp (string "and" :: List.map ~f:encode conjuncts)
158158
| Or disjuncts -> list sexp (string "or" :: List.map ~f:encode disjuncts)
159159

160+
let logical_op t =
161+
let open Dune_lang.Decoder in
162+
let+ x = repeat t
163+
and+ version = Syntax.get_exn Stanza.syntax
164+
and+ loc = loc in
165+
let empty_list_rejected_since = (3, 9) in
166+
if List.is_empty x && version >= empty_list_rejected_since then
167+
Syntax.Error.deleted_in loc Stanza.syntax empty_list_rejected_since
168+
~what:"Logical operators with no arguments";
169+
x
170+
160171
let decode =
161172
let open Dune_lang.Decoder in
162173
let ops =
@@ -183,10 +194,10 @@ module Dependency = struct
183194
fix (fun t ->
184195
let logops =
185196
[ ( "and"
186-
, let+ x = repeat t in
197+
, let+ x = logical_op t in
187198
And x )
188199
; ( "or"
189-
, let+ x = repeat t in
200+
, let+ x = logical_op t in
190201
Or x )
191202
]
192203
in
@@ -262,7 +273,9 @@ module Dependency = struct
262273
| Or [ c ] -> opam_constraint c
263274
| Or (c :: cs) ->
264275
nopos (Logop (nopos `Or, opam_constraint c, opam_constraint (And cs)))
265-
| And [] | Or [] -> Code_error.raise "opam_constraint" []
276+
| And [] | Or [] ->
277+
User_error.raise
278+
[ Pp.textf "logical operations with no arguments are not supported" ]
266279

267280
let opam_depend { name; constraint_ } =
268281
let constraint_ = Option.map ~f:opam_constraint constraint_ in
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
`(and)`/`(or)` with no argument should get rejected.
2+
3+
$ cat > dune-project << EOF
4+
> (lang dune 3.9)
5+
> (generate_opam_files)
6+
> (package
7+
> (name p)
8+
> (depends
9+
> (p (and))))
10+
> EOF
11+
12+
$ dune build
13+
File "dune-project", line 6, characters 5-10:
14+
6 | (p (and))))
15+
^^^^^
16+
Error: Logical operators with no arguments was deleted in version 3.9 of the
17+
dune language.
18+
[1]
19+
20+
$ cat > dune-project << EOF
21+
> (lang dune 3.9)
22+
> (generate_opam_files)
23+
> (package
24+
> (name p)
25+
> (depends
26+
> (p (or))))
27+
> EOF
28+
29+
$ dune build
30+
File "dune-project", line 6, characters 5-9:
31+
6 | (p (or))))
32+
^^^^
33+
Error: Logical operators with no arguments was deleted in version 3.9 of the
34+
dune language.
35+
[1]
36+
37+
In < 3.9 they are accepted, assuming they are not used to generate opam files.
38+
39+
$ cat > dune-project << EOF
40+
> (lang dune 3.8)
41+
> (package
42+
> (name p)
43+
> (allow_empty)
44+
> (depends
45+
> (p (and))))
46+
> EOF
47+
48+
$ dune build
49+
50+
$ cat > dune-project << EOF
51+
> (lang dune 3.8)
52+
> (package
53+
> (name p)
54+
> (allow_empty)
55+
> (depends
56+
> (p (or))))
57+
> EOF
58+
59+
$ dune build
60+
61+
Generating opam files should trigger a (nice) error:
62+
63+
$ cat > dune-project << EOF
64+
> (lang dune 3.8)
65+
> (generate_opam_files)
66+
> (package
67+
> (name p)
68+
> (depends
69+
> (p (and))))
70+
> EOF
71+
72+
$ dune build
73+
Error: logical operations with no arguments are not supported
74+
-> required by _build/default/p.opam
75+
-> required by _build/install/default/lib/p/opam
76+
-> required by _build/default/p.install
77+
-> required by alias all
78+
-> required by alias default
79+
[1]
80+
81+
$ cat > dune-project << EOF
82+
> (lang dune 3.8)
83+
> (generate_opam_files)
84+
> (package
85+
> (name p)
86+
> (depends
87+
> (p (or))))
88+
> EOF
89+
90+
$ dune build
91+
Error: logical operations with no arguments are not supported
92+
-> required by _build/default/p.opam
93+
-> required by _build/install/default/lib/p/opam
94+
-> required by _build/default/p.install
95+
-> required by alias all
96+
-> required by alias default
97+
[1]

test/blackbox-tests/test-cases/opam-constraints.t

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,3 @@ constraints.
6767
"@doc" {with-doc}
6868
]
6969
]
70-
71-
`(and)`/`(or)` with no argument should get rejected.
72-
73-
$ cat > dune-project << EOF
74-
> (lang dune 2.1)
75-
> (generate_opam_files)
76-
> (package
77-
> (name p)
78-
> (depends
79-
> (p (and))))
80-
> EOF
81-
82-
$ dune build 2>&1 | grep 'Internal error'
83-
Internal error, please report upstream including the contents of _build/log.
84-
85-
$ cat > dune-project << EOF
86-
> (lang dune 2.1)
87-
> (generate_opam_files)
88-
> (package
89-
> (name p)
90-
> (depends
91-
> (p (or))))
92-
> EOF
93-
94-
$ dune build 2>&1 | grep 'Internal error'
95-
Internal error, please report upstream including the contents of _build/log.

0 commit comments

Comments
 (0)