Skip to content

Commit ecc1ff2

Browse files
cknittclaude
andcommitted
Accept @notundefined over implementations the compiler cannot see into
Why: The check rejected bindings that are fine: an interface's `@notUndefined type t` over `type t = Dom.element`, an abbreviation of an abstract type, or over a JavaScript object type, which is never undefined. An implementation whose representation the compiler does not see has none that contradicts the interface: an abstract type, also reached through an abbreviation, or a JavaScript object type. A functor parameter's or a locally abstract type stands for any type and does not count as abstract. Diagnostic removed (an abbreviation of an abstract type): Example: // A.resi @notundefined type t // A.res type t = Dom.element Before (diagnostic): Their runtime representations differ: the second declaration is @notundefined, but the first declaration can hold undefined. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 97df09d commit ecc1ff2

7 files changed

Lines changed: 113 additions & 11 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,11 @@ declaration it is an error, "@notUndefined can only be used on abstract
440440
types". `@instanceOf` and `@typeOf` (rule 5) imply it.
441441

442442
When an interface declares a type `@notUndefined`, its implementation must be
443-
a type that cannot hold `undefined` either, or an abstract type, which has no
444-
representation to contradict it. `type t = option<int>` under
443+
a type that cannot hold `undefined` either, or a type whose representation
444+
the compiler does not see, which has none to contradict it: an abstract type,
445+
also through an abbreviation such as `type t = Dom.element`, or a JavaScript
446+
object type. The type of a functor parameter or a locally abstract type
447+
stands for another type and does not count as abstract. `type t = option<int>` under
445448
`@notUndefined type t` is an error: "Their runtime representations
446449
differ: the second declaration is @notUndefined, but the first declaration
447450
can hold undefined". The same holds
@@ -453,7 +456,8 @@ for functor arguments and `with type` constraints.
453456
`typeopt.ml:type_cannot_contain_undefined`;
454457
`includemod.ml:not_undefined_mismatch`.
455458
- Tests: `spec_test` rule 13; fixtures `invalid_attribute_not_undefined.res`,
456-
`not_undefined_functor_argument.res`;
459+
`not_undefined_functor_argument.res`, `not_undefined_functor_parameter.res`;
460+
`tests/tests/src/not_undefined_interface_test.res`;
457461
`tests/build_tests/super_errors_multi/fixtures/Cross_not_undefined_option_implementation`;
458462
`tests/build_tests/not_undefined_attribute`.
459463
- Shaped by #7458, #7464, #8964.

‎compiler/ml/includemod.ml‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,36 @@ let value_descriptions ~loc env cxt subst id vd1 vd2 =
6363
(* Inclusion between type declarations *)
6464

6565
(* [@notUndefined] lets clients unbox [option] of the type, so the
66-
implementation must be a type that cannot hold [undefined] either. An
67-
abstract implementation has no representation to contradict it. *)
66+
implementation must be a type that cannot hold [undefined] either. A type
67+
whose representation the compiler does not see, an abstract type (also
68+
through an abbreviation) or a JavaScript object type, has none that
69+
contradicts it. A functor parameter's or a locally abstract type stands
70+
for any type, so it does not count as abstract. *)
6871
let not_undefined_mismatch env id (decl1 : Types.type_declaration)
6972
(decl2 : Types.type_declaration) =
70-
let is_abstract =
71-
decl1.type_kind = Type_abstract && decl1.type_manifest = None
72-
in
73-
if (not is_abstract) && decl2.type_shape = Never_undefined then
73+
match decl2.type_shape with
74+
| Unstated | Recognized_as _ -> []
75+
| Never_undefined ->
7476
let env = Env.add_type ~check:false id decl1 env in
7577
let ty =
7678
Btype.newgenty (Tconstr (Pident id, decl1.type_params, ref Mnil))
7779
in
78-
if Typeopt.type_cannot_contain_undefined ty env then []
80+
let opaque =
81+
match (Ctype.expand_head_opt env ty).desc with
82+
| Tconstr (path, _, _) -> (
83+
match Env.find_type path env with
84+
| {type_kind = Type_abstract; type_manifest = None; type_newtype_level}
85+
->
86+
Predef.type_is_builtin_path_but_option path = Predef.NA
87+
&& type_newtype_level = None
88+
&& not (Env.is_functor_arg path env)
89+
| _ -> false
90+
| exception Not_found -> false)
91+
| Tobject _ -> true
92+
| _ -> false
93+
in
94+
if opaque || Typeopt.type_cannot_contain_undefined ty env then []
7995
else [Includecore.Not_undefined]
80-
else []
8196

8297
let type_declarations ~loc env ?(old_env = env) cxt subst id decl1 decl2 =
8398
Env.mark_type_used env (Ident.name id) decl1;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/not_undefined_functor_parameter.res:8:7-10:3
4+
5+
6 │ module Checked: {
6+
7 │ @notUndefined type t
7+
8 │ } = {
8+
9 │  type t = X.t
9+
10 │  }
10+
11 │ }
11+
12 │
12+
13+
Signature mismatch:
14+
Modules do not match: {
15+
type t = X.t
16+
} is not included in {
17+
type t
18+
}
19+
Type declarations do not match: type t = X.t is not included in type t
20+
/.../fixtures/not_undefined_functor_parameter.res:7:19-24:
21+
Expected declaration
22+
/.../fixtures/not_undefined_functor_parameter.res:9:5-16:
23+
Actual declaration
24+
Their runtime representations differ:
25+
the second declaration is @notUndefined,
26+
but the first declaration can hold undefined.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Make = (
2+
X: {
3+
type t
4+
},
5+
) => {
6+
module Checked: {
7+
@notUndefined type t
8+
} = {
9+
type t = X.t
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Mocha from "mocha";
4+
import * as Test_utils from "./test_utils.mjs";
5+
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs";
6+
7+
let makeElement = (() => ({}));
8+
9+
function makeRecord() {
10+
return {
11+
name: "a"
12+
};
13+
}
14+
15+
Mocha.describe("Not_undefined_interface_test", () => {
16+
Mocha.test("option of the types is unboxed", () => {
17+
let element = makeElement();
18+
Test_utils.ok("File \"not_undefined_interface_test.res\", line 14, characters 18-25", Primitive_option.some(element) === element);
19+
});
20+
});
21+
22+
export {
23+
makeElement,
24+
makeRecord,
25+
}
26+
/* Not a pure module */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Implementations whose representation the compiler does not see, an
2+
// abstract type through an abbreviation and a JavaScript object type, agree
3+
// with @notUndefined in the interface
4+
type opaque
5+
type element = opaque
6+
type record = {"name": string}
7+
8+
let makeElement: unit => element = %raw(`() => ({})`)
9+
let makeRecord = () => {"name": "a"}
10+
11+
Mocha.describe(__MODULE__, () => {
12+
Mocha.test("option of the types is unboxed", () => {
13+
let element = makeElement()
14+
Test_utils.ok(__LOC__, Obj.magic(Some(element)) === element)
15+
})
16+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@notUndefined type element
2+
@notUndefined type record
3+
let makeElement: unit => element
4+
let makeRecord: unit => record

0 commit comments

Comments
 (0)