Skip to content

Commit a5fc291

Browse files
Carry latest master through watcher recovery branch
Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
2 parents 10e4171 + f79d575 commit a5fc291

12 files changed

Lines changed: 284 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- Keep watch mode running after full rebuild initialization fails so it can recover after a later valid change. https://github.com/rescript-lang/rescript/pull/8641
2727
- Allow `rescript clean` to remove generated files when the source module graph is invalid. https://github.com/rescript-lang/rescript/pull/8640
2828
- Fix GenType path resolution hanging on Windows when project paths use different short and canonical forms. https://github.com/rescript-lang/rescript/pull/8639
29+
- Fix compiler crashes when wrapping values in `Some` or passing optional arguments whose types come from a transitive dependency. https://github.com/rescript-lang/rescript/pull/8719
2930
- Fix unused-pattern warnings for variant spreads reported without a location. https://github.com/rescript-lang/rescript/pull/8744
3031
- Keep `@warning("-4")` on `if` expressions when formatting; the formatter dropped it. https://github.com/rescript-lang/rescript/pull/8741
3132
- Fix the formatter dropping a `@res.ternary` attribute written in source, e.g. `let b = @res.ternary x`, and print `@JSX` and `@res.ternary` with the same parens as other attributes, e.g. `(@JSX x) + 1`. https://github.com/rescript-lang/rescript/pull/8740
@@ -52,6 +53,7 @@
5253

5354
#### :house: Internal
5455

56+
- Forward-port integer range pattern regression coverage from v12 to v13. https://github.com/rescript-lang/rescript/pull/8718
5557
- Remove the unused `res.iflet` encoding of `if let`, which the parser always rejects with a syntax error suggesting a `switch`. The error is unchanged. https://github.com/rescript-lang/rescript/pull/8741
5658
- Represent variant spread patterns (`...t`) with a dedicated `Ppat_variant_spread` parsetree node instead of `Ppat_type` with a `res.patVariantSpread` attribute, which remains only in the PPX wire representation. https://github.com/rescript-lang/rescript/pull/8742
5759
- Remove the obsolete `res.ternary` and `JSX` attributes from the formatter's internal attribute filters and merge the filters into one. https://github.com/rescript-lang/rescript/pull/8740

‎compiler/ml/typeopt.ml‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,31 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
4848
| For_sure_yes -> true
4949
| For_sure_no -> false
5050
| NA -> (
51-
let decl = Env.find_type p env in
52-
match decl.type_kind with
53-
| exception _ -> false
54-
| Type_abstract ->
55-
List.exists Typedecl.is_not_undefined_attr decl.type_attributes
56-
| Type_open -> false
57-
| Type_record _ -> true
58-
| Type_variant
59-
( ( [
60-
{cd_id = {name = "None"}; cd_args = Cstr_tuple []};
61-
{cd_id = {name = "Some"}; cd_args = Cstr_tuple [_]};
62-
]
63-
| [
64-
{cd_id = {name = "Some"}; cd_args = Cstr_tuple [_]};
65-
{cd_id = {name = "None"}; cd_args = Cstr_tuple []};
66-
]
67-
| [{cd_id = {name = "()"}; cd_args = Cstr_tuple []}] ),
68-
_ ) ->
51+
match Env.find_type p env with
52+
| exception Not_found ->
53+
(* A transitive dependency's interface may not be on the search path.
54+
Keep option wrapping when its runtime representation is unknown. *)
55+
false
56+
| {type_kind = Type_abstract; type_attributes} ->
57+
List.exists Typedecl.is_not_undefined_attr type_attributes
58+
| {type_kind = Type_open} -> false
59+
| {type_kind = Type_record _} -> true
60+
| {
61+
type_kind =
62+
Type_variant
63+
( ( [
64+
{cd_id = {name = "None"}; cd_args = Cstr_tuple []};
65+
{cd_id = {name = "Some"}; cd_args = Cstr_tuple [_]};
66+
]
67+
| [
68+
{cd_id = {name = "Some"}; cd_args = Cstr_tuple [_]};
69+
{cd_id = {name = "None"}; cd_args = Cstr_tuple []};
70+
]
71+
| [{cd_id = {name = "()"}; cd_args = Cstr_tuple []}] ),
72+
_ );
73+
} ->
6974
false (* conservative *)
70-
| Type_variant (cdecls, layout_ref) ->
75+
| {type_kind = Type_variant (cdecls, layout_ref)} ->
7176
let layout = Variant_runtime.get_layout layout_ref in
7277
let rec all_cases_cannot_contain_undefined position = function
7378
| [] -> true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
let variant = Some(Facade.variant)
2+
let record = Some(Facade.record)
3+
let alias = Some(Facade.alias)
4+
let abstract = Some(Facade.abstract)
5+
let unboxed = Some(Facade.unboxed)
6+
7+
let abstractIsSome = Option.isSome(abstract)
8+
let unboxedIsSome = Option.isSome(unboxed)
9+
10+
let abstractRoundTrip = switch abstract {
11+
| Some(value) => Facade.abstractIsUndefined(value)
12+
| None => false
13+
}
14+
15+
let unboxedRoundTrip = switch unboxed {
16+
| Some(value) => Facade.unboxedIsUndefined(value)
17+
| None => false
18+
}
19+
20+
let optionalAbstract = Facade.optionalAbstract(~value=Facade.abstract)
21+
let optionalUnboxed = Facade.optionalUnboxed(~value=Facade.unboxed)
22+
let omittedAbstract = Facade.optionalAbstract()
23+
let omittedUnboxed = Facade.optionalUnboxed()
24+
25+
let optionalAbstractIsSome = Option.isSome(optionalAbstract)
26+
let optionalUnboxedIsSome = Option.isSome(optionalUnboxed)
27+
28+
let optionalAbstractRoundTrip = switch optionalAbstract {
29+
| Some(value) => Facade.abstractIsUndefined(value)
30+
| None => false
31+
}
32+
33+
let optionalUnboxedRoundTrip = switch optionalUnboxed {
34+
| Some(value) => Facade.unboxedIsUndefined(value)
35+
| None => false
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Variant = {
2+
type t = X
3+
let value = X
4+
}
5+
6+
module Record = {
7+
type t = {value: int}
8+
let value = {value: 42}
9+
}
10+
11+
module Alias = {
12+
type t = int
13+
let value: t = 42
14+
}
15+
16+
module Abstract = {
17+
type t = option<int>
18+
let value: t = None
19+
let isUndefined = value => Option.isNone(value)
20+
}
21+
22+
module Unboxed = {
23+
@unboxed
24+
type t = Value(option<int>)
25+
let value = Value(None)
26+
let isUndefined = value => {
27+
let Value(inner) = value
28+
Option.isNone(inner)
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Variant: {
2+
type t = X
3+
let value: t
4+
}
5+
6+
module Record: {
7+
type t = {value: int}
8+
let value: t
9+
}
10+
11+
module Alias: {
12+
type t = int
13+
let value: t
14+
}
15+
16+
module Abstract: {
17+
type t
18+
let value: t
19+
let isUndefined: t => bool
20+
}
21+
22+
module Unboxed: {
23+
@unboxed
24+
type t = Value(option<int>)
25+
let value: t
26+
let isUndefined: t => bool
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let variant = A.Variant.value
2+
let record = A.Record.value
3+
let alias = A.Alias.value
4+
let abstract = A.Abstract.value
5+
let unboxed = A.Unboxed.value
6+
7+
let abstractIsUndefined = A.Abstract.isUndefined
8+
let unboxedIsUndefined = A.Unboxed.isUndefined
9+
10+
let optionalAbstract = (~value: option<A.Abstract.t>=?) => value
11+
let optionalUnboxed = (~value: option<A.Unboxed.t>=?) => value
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @ts-check
2+
3+
import * as assert from "node:assert/strict";
4+
import * as fs from "node:fs/promises";
5+
import * as path from "node:path";
6+
import { pathToFileURL } from "node:url";
7+
import { setup } from "#dev/process";
8+
9+
const root = import.meta.dirname;
10+
const { bsc } = setup(root);
11+
const outputDir = path.join(root, "lib");
12+
13+
/**
14+
* @param {string} directory
15+
* @param {string} source
16+
* @param {string[]} includes
17+
* @param {string[]} [extraFlags]
18+
*/
19+
async function compile(directory, source, includes, extraFlags = []) {
20+
const output = path.join(outputDir, directory);
21+
const stem = path.basename(source, path.extname(source));
22+
await fs.mkdir(output, { recursive: true });
23+
const result = await bsc([
24+
"-bs-project-root",
25+
root,
26+
"-bs-package-name",
27+
"option_transitive_dependency",
28+
"-bs-package-output",
29+
`esmodule:lib/${directory}:.mjs`,
30+
...includes.flatMap(include => ["-I", path.join(outputDir, include)]),
31+
...extraFlags,
32+
"-o",
33+
path.join(output, `${stem}.${source.endsWith(".resi") ? "cmi" : "cmj"}`),
34+
path.join(root, directory, source),
35+
]);
36+
assert.equal(result.status, 0, `${source}: ${result.stdout}${result.stderr}`);
37+
}
38+
39+
await fs.rm(outputDir, { recursive: true, force: true });
40+
41+
try {
42+
await compile("dep", "A.resi", ["dep"]);
43+
await compile("dep", "A.res", ["dep"], ["-bs-read-cmi"]);
44+
await compile("facade", "Facade.res", ["dep", "facade"]);
45+
// The consumer can load Facade, but not the dependency declaring its types.
46+
await compile("app", "Main.res", ["facade", "app"]);
47+
48+
const main = await import(
49+
pathToFileURL(path.join(outputDir, "app", "Main.mjs")).href
50+
);
51+
const facade = await import(
52+
pathToFileURL(path.join(outputDir, "facade", "Facade.mjs")).href
53+
);
54+
assert.equal(main.variant, facade.variant);
55+
assert.deepEqual(main.record, { value: 42 });
56+
assert.equal(main.alias, 42);
57+
58+
// Some(undefined) must remain distinct from None, including optional args.
59+
for (const field of [
60+
"abstractIsSome",
61+
"unboxedIsSome",
62+
"abstractRoundTrip",
63+
"unboxedRoundTrip",
64+
"optionalAbstractIsSome",
65+
"optionalUnboxedIsSome",
66+
"optionalAbstractRoundTrip",
67+
"optionalUnboxedRoundTrip",
68+
]) {
69+
assert.equal(main[field], true, field);
70+
}
71+
assert.equal(main.omittedAbstract, undefined);
72+
assert.equal(main.omittedUnboxed, undefined);
73+
} finally {
74+
await fs.rm(outputDir, { recursive: true, force: true });
75+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "option_transitive_dependency",
3+
"sources": []
4+
}

‎tests/ounit_tests/ounit_tests_main.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let suites =
3535
Ounit_analysis_config_tests.suites;
3636
Ounit_analysis_references_tests.suites;
3737
Ounit_ffi_inclusion_tests.suites;
38+
Ounit_typeopt_tests.suites;
3839
Ounit_gentype_tests.suites;
3940
]
4041

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: ))
2+
3+
let suites =
4+
__FILE__
5+
>::: [
6+
( "missing type declaration keeps option wrapping" >:: fun _ ->
7+
let typ =
8+
Btype.newgenty
9+
(Types.Tconstr
10+
(Path.Pident (Ident.create "missing"), [], ref Types.Mnil))
11+
in
12+
OUnit.assert_equal false
13+
(Typeopt.type_cannot_contain_undefined typ Env.empty) );
14+
( "builtin integers still omit option wrapping" >:: fun _ ->
15+
OUnit.assert_equal true
16+
(Typeopt.type_cannot_contain_undefined Predef.type_int Env.empty)
17+
);
18+
]

0 commit comments

Comments
 (0)