Skip to content

Commit f728836

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 667b281 commit f728836

10 files changed

Lines changed: 113 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
@@ -430,16 +430,26 @@ values are never `undefined`. It affects only rule 12: `option` of the type,
430430
and of an untagged variant with a payload of the type, is unboxed. It does not
431431
make the type a recognized payload kind (rule 4). On any other type
432432
declaration it is an error, "@notUndefined can only be used on abstract
433-
types". Interface inclusion does not compare it. `@instanceOf` and `@typeOf`
434-
(rule 5) imply it.
433+
types". `@instanceOf` and `@typeOf` (rule 5) imply it.
434+
435+
When an interface declares a type `@notUndefined`, its implementation must be
436+
a type that cannot hold `undefined` either, or an abstract type, which has no
437+
representation to contradict it. `type t = option<int>` under
438+
`@notUndefined type t` is an error: "Their runtime representations
439+
differ: the second declaration is @notUndefined, but the first declaration
440+
can hold undefined". The same holds
441+
for functor arguments and `with type` constraints.
435442

436443
- Implementation: `typedecl.ml:transl_declaration`;
437444
`ast_untagged_variants.ml:has_not_undefined`;
438445
`variant_layout.ml:declaration_shape`;
439-
`typeopt.ml:type_cannot_contain_undefined`.
440-
- Tests: `spec_test` rule 13; fixture `invalid_attribute_not_undefined.res`;
446+
`typeopt.ml:type_cannot_contain_undefined`;
447+
`includemod.ml:not_undefined_mismatch`.
448+
- Tests: `spec_test` rule 13; fixtures `invalid_attribute_not_undefined.res`,
449+
`not_undefined_functor_argument.res`;
450+
`tests/build_tests/super_errors_multi/fixtures/Cross_not_undefined_option_implementation`;
441451
`tests/build_tests/not_undefined_attribute`.
442-
- Shaped by #7458, #7464.
452+
- Shaped by #7458, #7464, #8964.
443453

444454
### 14. `@tag` and interfaces
445455

@@ -538,8 +548,8 @@ had to answer, and the answers of rule 5:
538548
4. **The unchecked branch** stays the contract (rule 10).
539549
5. **Realms.** Recognition is local to a realm (rule 10).
540550
6. **Interfaces and functors** compare declared classes and kinds, and the
541-
payload kinds of untagged cases (rule 14). They still do not compare
542-
`@notUndefined`.
551+
payload kinds of untagged cases (rule 14), and `@notUndefined` against
552+
the implementation (rule 13).
543553
7. **Implied `@notUndefined`.** A declared class or kind implies it, and
544554
rule 5 rejects annotations on abbreviations of types that can hold
545555
`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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,32 @@ 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 (not is_abstract) && decl2.type_shape = Never_undefined then
74+
let env = Env.add_type ~check:false id decl1 env in
75+
let ty =
76+
Btype.newgenty (Tconstr (Pident id, decl1.type_params, ref Mnil))
77+
in
78+
if Typeopt.type_cannot_contain_undefined ty env then []
79+
else [Includecore.Not_undefined]
80+
else []
81+
6582
let type_declarations ~loc env ?(old_env = env) cxt subst id decl1 decl2 =
6683
Env.mark_type_used env (Ident.name id) decl1;
6784
let decl2 = Subst.type_declaration subst decl2 in
6885
let err =
6986
Includecore.type_declarations ~loc env (Ident.name id) decl1 id decl2
7087
in
88+
let err =
89+
if err <> [] then err else not_undefined_mismatch env id decl1 decl2
90+
in
7191
if err <> [] then
7292
raise (Error [(cxt, old_env, Type_declarations (id, decl1, decl2, err))])
7393

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)