What's the semantics of implicit open when shadowing

under ocaml version 4.10.1
Here is a faked list.ml

let x = 3

I have a file called test.ml sitting in the same directory

List.length [1;2;3]

First, I compile my local list.ml using ocamlc -c list.ml.
Now I compile ocamlc -c test.ml, it comes with an error as expected:

File "test.ml", line 3, characters 0-11:
3 | List.length [1;2;3]
    ^^^^^^^^^^^
Error: This expression has type int
       This is not a function; it cannot be applied.

Now I do ocamlc -open Stdlib -I . -c test.ml and ocamlc -I . -open Stdlib -c test.ml
both compiles, my confusion is that why shadowing does not work here? (My original understanding is that the ocaml compiler did -open Stdlib behind the scene, but it seems more than that)
Thanks

The implicit open Stdlib is done early to make it possible to shadow Stdlib’s submodule with user defined one.

The construction of the initial environment is done in essentially five steps:

  1. start with the predefined environment
  2. add the persistent modules from the standard library
  3. open the Stdlib module
  4. add the persistent modules from all other directories in the path (from the -I option)
  5. open the initially opened modules provided by the -open flag

(see https://github.com/ocaml/ocaml/pull/9345#issuecomment-613463446 for some recent discussion).
which means that all included directory -I dir_k have already been added to the path when the -open M_l flags are handled.

2 Likes