Skip to content

Commit 5f2e4de

Browse files
cknittclaude
andcommitted
Check @notundefined of an interface against the implementation
Why: Interface inclusion ignored `@notUndefined`. An interface could declare `@notUndefined type t` over `type t = option<int>`, and clients then unboxed `option<t>`, so `Some(M.none)` was `None` (#8964). When the second declaration of an inclusion is `@notUndefined`, the first must be a type that cannot hold `undefined`, by the rule `option` unboxing uses, or an abstract type, which has no representation to contradict it. This covers `.resi` files, functor arguments and `with type` constraints. The check runs in `Includemod`, since `Typeopt` depends on `Includecore` through `Typedecl`. Diagnostic added: Example: // Hidden.resi @notundefined type t // Hidden.res type t = option<int> After (diagnostic): Type declarations do not match: type t = option<int> is not included in type t Their runtime representations differ: the second declaration is @notundefined, but the first declaration can hold undefined. Review: Why accept an abstract implementation without the attribute? The stdlib's `Lazy.res` declares `type t<+'a>` under `@notUndefined` in its interface, and so may bindings; such a type has no representation that could hold `undefined`, so requiring the attribute twice would only break them. Fixes: #8964 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 526b304 commit 5f2e4de

10 files changed

Lines changed: 116 additions & 7 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Reject an implementation that can hold `undefined` for a type its interface declares `@notUndefined`, which made `Some(None)` of the type `None` in clients.

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,24 @@ values are never `undefined`. It affects only rule 12: `option` of the type,
409409
and of an untagged variant with a payload of the type, is unboxed. It does not
410410
make the type a recognized payload kind (rule 4). On any other type
411411
declaration it is an error, "@notUndefined can only be used on abstract
412-
types". Interface inclusion does not compare it. `@instanceOf` and `@typeOf`
413-
(rule 5) imply it.
412+
types". `@instanceOf` and `@typeOf` (rule 5) imply it.
413+
414+
When an interface declares a type `@notUndefined`, its implementation must be
415+
a type that cannot hold `undefined` either, or an abstract type, which has no
416+
representation to contradict it. `type t = option<int>` under
417+
`@notUndefined type t` is an error: "Their runtime representations
418+
differ: the second declaration is @notUndefined, but the first declaration
419+
can hold undefined". The same holds
420+
for functor arguments and `with type` constraints.
414421

415422
- Implementation: `typedecl.ml:transl_declaration`, `is_not_undefined_attr`;
416-
`typeopt.ml:type_cannot_contain_undefined`.
417-
- Tests: `spec_test` rule 13; fixture `invalid_attribute_not_undefined.res`;
423+
`typeopt.ml:type_cannot_contain_undefined`;
424+
`includemod.ml:not_undefined_mismatch`.
425+
- Tests: `spec_test` rule 13; fixtures `invalid_attribute_not_undefined.res`,
426+
`not_undefined_functor_argument.res`;
427+
`tests/build_tests/super_errors_multi/fixtures/Cross_not_undefined_option_implementation`;
418428
`tests/build_tests/not_undefined_attribute`.
419-
- Shaped by #7458, #7464.
429+
- Shaped by #7458, #7464, #8964.
420430

421431
### 14. `@tag` and interfaces
422432

@@ -527,8 +537,8 @@ had to answer, and the answers of rule 5:
527537
4. **The unchecked branch** stays the contract (rule 10).
528538
5. **Realms.** Recognition is local to a realm (rule 10).
529539
6. **Interfaces and functors** compare declared classes and kinds, and the
530-
payload kinds of untagged cases (rule 14). They still do not compare
531-
`@notUndefined`.
540+
payload kinds of untagged cases (rule 14), and `@notUndefined` against
541+
the implementation (rule 13).
532542
7. **Implied `@notUndefined`.** A declared class or kind implies it, and
533543
rule 5 rejects annotations on abbreviations of types that can hold
534544
`undefined`.

‎compiler/ml/includecore.ml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ type type_mismatch =
185185
| Tag_name
186186
| Variant_representation of Ident.t
187187
| Runtime_annotation of string * string
188+
| Not_undefined
188189

189190
let report_type_mismatch0 first second decl ppf err =
190191
let pr fmt = Format.fprintf ppf fmt in
@@ -217,6 +218,11 @@ let report_type_mismatch0 first second decl ppf err =
217218
| Tag_name -> pr "Their @tag annotations differ"
218219
| Variant_representation s ->
219220
pr "The internal representations for case %s are not equal" (Ident.name s)
221+
| Not_undefined ->
222+
pr
223+
"Their runtime representations differ:@ %s %s is @notUndefined,@ but %s \
224+
%s can hold undefined"
225+
second decl first decl
220226
| Runtime_annotation (declared1, declared2) ->
221227
pr
222228
"Their runtime representations differ:@ %s %s states %s,@ %s %s states %s"

‎compiler/ml/includecore.mli‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type type_mismatch =
3939
| Tag_name
4040
| Variant_representation of Ident.t
4141
| Runtime_annotation of string * string
42+
| Not_undefined
4243

4344
val value_descriptions :
4445
loc:Location.t ->

‎compiler/ml/includemod.ml‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,35 @@ let value_descriptions ~loc env cxt subst id vd1 vd2 =
6262

6363
(* Inclusion between type declarations *)
6464

65+
(* [@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. *)
68+
let not_undefined_mismatch env id (decl1 : Types.type_declaration)
69+
(decl2 : Types.type_declaration) =
70+
let is_abstract =
71+
decl1.type_kind = Type_abstract && decl1.type_manifest = None
72+
in
73+
if
74+
(not is_abstract)
75+
&& List.exists Typedecl.is_not_undefined_attr decl2.type_attributes
76+
then
77+
let env = Env.add_type ~check:false id decl1 env in
78+
let ty =
79+
Btype.newgenty (Tconstr (Pident id, decl1.type_params, ref Mnil))
80+
in
81+
if Typeopt.type_cannot_contain_undefined ty env then []
82+
else [Includecore.Not_undefined]
83+
else []
84+
6585
let type_declarations ~loc env ?(old_env = env) cxt subst id decl1 decl2 =
6686
Env.mark_type_used env (Ident.name id) decl1;
6787
let decl2 = Subst.type_declaration subst decl2 in
6888
let err =
6989
Includecore.type_declarations ~loc env (Ident.name id) decl1 id decl2
7090
in
91+
let err =
92+
if err <> [] then err else not_undefined_mismatch env id decl1 decl2
93+
in
7194
if err <> [] then
7295
raise (Error [(cxt, old_env, Type_declarations (id, decl1, decl2, err))])
7396

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/not_undefined_functor_argument.res:9:23-11:1
4+
5+
7 │ }
6+
8 │
7+
9 │ module Applied = Make({
8+
10 │  type t = option<int>
9+
11 │ })
10+
12 │
11+
12+
Signature mismatch:
13+
Modules do not match:
14+
{
15+
type t = option<int>
16+
}
17+
is not included in
18+
{
19+
type t
20+
}
21+
Type declarations do not match:
22+
type t = option<int>
23+
is not included in
24+
type t
25+
/.../fixtures/not_undefined_functor_argument.res:3:19-24:
26+
Expected declaration
27+
/.../fixtures/not_undefined_functor_argument.res:10:3-22:
28+
Actual declaration
29+
Their runtime representations differ:
30+
the second declaration is @notUndefined,
31+
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+
@notUndefined type t
4+
},
5+
) => {
6+
type u = X.t
7+
}
8+
9+
module Applied = Make({
10+
type t = option<int>
11+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
===== Hidden.res =====
2+
3+
We've found a bug for you!
4+
/.../fixtures/Cross_not_undefined_option_implementation/Hidden.res:1:1-20
5+
6+
1 │ type t = option<int>
7+
2 │ let none: t = None
8+
3 │
9+
10+
The implementation /.../fixtures/Cross_not_undefined_option_implementation/Hidden.res
11+
does not match the interface /.../fixtures/Cross_not_undefined_option_implementation/hidden.cmi:
12+
Type declarations do not match:
13+
type t = option<int>
14+
is not included in
15+
type t
16+
/.../fixtures/Cross_not_undefined_option_implementation/Hidden.resi:1:15-20:
17+
Expected declaration
18+
/.../fixtures/Cross_not_undefined_option_implementation/Hidden.res:1:1-20:
19+
Actual declaration
20+
Their runtime representations differ:
21+
the second declaration is @notUndefined,
22+
but the first declaration can hold undefined.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type t = option<int>
2+
let none: t = None
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@notUndefined type t
2+
let none: t

0 commit comments

Comments
 (0)